Forums - Open Redstone Engineers
C++/SDL/OpenGL Pong :D - Printable Version

+- Forums - Open Redstone Engineers (https://forum.openredstone.org)
+-- Forum: Off-Topic (https://forum.openredstone.org/forum-4.html)
+--- Forum: Programming (https://forum.openredstone.org/forum-8.html)
+--- Thread: C++/SDL/OpenGL Pong :D (/thread-401.html)



C++/SDL/OpenGL Pong :D - Greymouth - 06-18-2013

Code:
#include <iostream>
#include "SDL.h"
#include "SDL_opengl.h"
#include "SDL_image.h"
#include <string>

using namespace std;

//Load Textures
GLuint loadTexture(const string &fileName) {
    SDL_Surface *image = IMG_Load(fileName.c_str());

    SDL_DisplayFormatAlpha(image);

    unsigned object(0);

    glGenTextures(1, &object);

    glBindTexture(GL_TEXTURE_2D, object);

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image -> w, image -> h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image -> pixels);

    SDL_FreeSurface(image);

    return object;
}

//Collision Checker
bool checkCollision(float Ax, float Ay, float Aw, float Ah, float Bx, float By, float Bw, float Bh) {
    if (Ay+Ah < By) return false;
    else if (Ay > By+Bh) return false;
    else if (Ax+Aw < Bx) return false;
    else if (Ax > Bx+Bw) return false;

    return true;
}

//Main
int main(int argc, char** argv) {

    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    SDL_WM_SetCaption("Game", NULL);

    SDL_SetVideoMode(600, 400, 32, SDL_OPENGL);

    glClearColor(1, 1, 1, 1);
    glViewport(0, 0, 600, 400);

    glShadeModel(GL_SMOOTH);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glDisable(GL_DEPTH_TEST);

    //Enable GL_BLEND
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    cout << "OpenGL Loaded" << endl;

    //isRunning
    bool isRunning = true;

    SDL_Event event;

    //Pad
    float PadX = 300;
    float PadY = 370;
    float Width = 100;
    float Height = 10;

    //Pad 2
    float Pad2X = 300;
    float Pad2Y = 20;
    float Width2 = 100;
    float Height2 = 10;

    //Pad Movement
    bool Left = false, Right = false;

    //Pad 2 Movement
    bool Left2 = false, Right2 = false;

    //Ball
    float BallX = 50;
    float BallY = 50;
    float BallWH = 15;

    //Ball Movement
    float VelX = 4;
    float VelY = 4;

    //Pad Texture
    unsigned int pad_texture = 0;
    unsigned int pad2_texture = 0;
    unsigned int ball_texture = 0;
    unsigned int backround_texture = 0;
    pad_texture = loadTexture("Pad.png");
    pad2_texture = loadTexture("Pad2.png");
    ball_texture = loadTexture("Ball.png");
    backround_texture = loadTexture("Backround.png");

    //Game Loop
    while (isRunning) {
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                isRunning = false;
            }
            if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE) {
                isRunning = false;
            }

            //Pad Movement
            if (event.type == SDL_KEYDOWN) {

                //Pad
                if (event.key.keysym.sym == SDLK_LEFT) {
                    Left = true;
                }
                if (event.key.keysym.sym == SDLK_RIGHT) {
                    Right = true;
                }

                //Pad 2
                if (event.key.keysym.sym == SDLK_a) {
                    Left2 = true;
                }
                if (event.key.keysym.sym == SDLK_d) {
                    Right2 = true;
                }
            }

            else if (event.type == SDL_KEYUP) {
                //Pad
                if (event.key.keysym.sym == SDLK_LEFT) {
                    Left = false;
                }
                if (event.key.keysym.sym == SDLK_RIGHT) {
                    Right = false;
                }

                //Pad 2
                if (event.key.keysym.sym == SDLK_a) {
                    Left2 = false;
                }
                if (event.key.keysym.sym == SDLK_d) {
                    Right2 = false;
                }
            }
        }
        //Game Logic

            //Movement
            if (Left == true) {
                PadX -= 8;
            }
            if (Right == true) {
                PadX += 8;
            }

            //Movement 2
            if (Left2 == true) {
                Pad2X -= 8;
            }
            if (Right2 == true) {
                Pad2X += 8;
            }

            //Pad Wall Collision
            if (PadX < 0) {
                PadX = 0;
            }
            if (PadX+Width > 600) {
                PadX = 600-Width;
            }

            //Pad 2 Wall Collision
            if (Pad2X < 0) {
                Pad2X = 0;
            }
            if (Pad2X+Width2 > 600) {
                Pad2X = 600-Width2;
            }

            //Ball Movement
            BallX += VelX;
            BallY += VelY;

            //Ball
            if (BallX < 0) {
                VelX = -VelX;
            }
            else if (BallX+BallWH > 600) {
                VelX = -VelX;
            }
            if (BallY < 0) {
                isRunning = false;
            }
            else if (BallY+BallWH > 400) {
                isRunning = false;
            }

            //Ball Pad Collision
            if (checkCollision(BallX, BallY, BallWH, BallWH, PadX, PadY, Width, Height) == true) {
                VelY = -VelY;

                float temp = BallX + BallWH/2;
                temp = temp - PadX;
                temp = temp - 50;
                temp = temp / 7;
                VelX = temp;
            }

            //Ball Pad2 Collision
            if (checkCollision(BallX, BallY, BallWH, BallWH, Pad2X, Pad2Y, Width2, Height2) == true) {
                VelY = -VelY;

                float temp = BallX + BallWH/2;
                temp = temp - Pad2X;
                temp = temp - 50;
                temp = temp / 7;
                VelX = temp;
            }

        //Rendering
        glClear(GL_COLOR_BUFFER_BIT);

        glPushMatrix();

        glOrtho(0, 600, 400, 0, -1, 1);

        glColor4ub(255, 255, 255, 255);

        glEnable(GL_TEXTURE_2D);

        //Backround
        glBindTexture(GL_TEXTURE_2D, backround_texture);
        
        glBegin(GL_QUADS);

        glTexCoord2d(0, 0); glVertex2f(0, 0);
        glTexCoord2d(1, 0); glVertex2f(600, 0);
        glTexCoord2d(1, 1); glVertex2f(600, 400);
        glTexCoord2d(0, 1); glVertex2f(0, 400);

        glEnd();

        //Pad
        glBindTexture(GL_TEXTURE_2D, pad_texture);

        glBegin(GL_QUADS);

        glTexCoord2d(0, 0); glVertex2f(PadX, PadY);
        glTexCoord2d(1, 0); glVertex2f(PadX+Width, PadY);
        glTexCoord2d(1, 1); glVertex2f(PadX+Width, PadY+Height);
        glTexCoord2d(0, 1); glVertex2f(PadX, PadY+Height);

        glEnd();

        //Pad 2
        glBindTexture(GL_TEXTURE_2D, pad2_texture);

        glBegin(GL_QUADS);

        glTexCoord2d(0, 0); glVertex2f(Pad2X, Pad2Y);
        glTexCoord2d(1, 0); glVertex2f(Pad2X+Width2, Pad2Y);
        glTexCoord2d(1, 1); glVertex2f(Pad2X+Width2, Pad2Y+Height2);
        glTexCoord2d(0, 1); glVertex2f(Pad2X, Pad2Y+Height2);

        glEnd();

        //Ball
        glBindTexture(GL_TEXTURE_2D, ball_texture);

        glBegin(GL_QUADS);

        glTexCoord2d(0, 0); glVertex2f(BallX, BallY);
        glTexCoord2d(1, 0); glVertex2f(BallX+BallWH, BallY);
        glTexCoord2d(1, 1); glVertex2f(BallX+BallWH, BallY+BallWH);
        glTexCoord2d(0, 1); glVertex2f(BallX, BallY+BallWH);

        glEnd();

        glDisable(GL_TEXTURE_2D);

        glPopMatrix();

        SDL_GL_SwapBuffers();

        SDL_Delay(1);
    }

    SDL_Quit();

    return 0;
}

This was a test and I'll be recreating it with better more correct game logic and format everthing correctly using separate header files and cpp files.


RE: C++/SDL/OpenGL Pong :D - Dcentrics - 03-21-2014

Code:
#include <iostream>
#include "SDL.h"
#include "SDL_opengl.h"
#include "SDL_image.h"
#include <string>

using namespace std;

//Load Textures
GLuint loadTexture(const string &fileName) {
    SDL_Surface *image = IMG_Load(fileName.c_str());

    SDL_DisplayFormatAlpha(image);

    unsigned object(0);

    glGenTextures(1, &object);

    glBindTexture(GL_TEXTURE_2D, object);

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image -> w, image -> h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image -> pixels);

    SDL_FreeSurface(image);

    return object;
}

//Collision Checker
bool checkCollision(float Ax, float Ay, float Aw, float Ah, float Bx, float By, float Bw, float Bh) {
    if (Ay+Ah < By) return false;
    else if (Ay > By+Bh) return false;
    else if (Ax+Aw < Bx) return false;
    else if (Ax > Bx+Bw) return false;

    return true;
}

//Main
int main(int argc, char** argv) {

    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    SDL_WM_SetCaption("Game", NULL);

    SDL_SetVideoMode(600, 400, 32, SDL_OPENGL);

    glClearColor(1, 1, 1, 1);
    glViewport(0, 0, 600, 400);

    glShadeModel(GL_SMOOTH);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glDisable(GL_DEPTH_TEST);

    //Enable GL_BLEND
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    cout << "OpenGL Loaded" << endl;

    //isRunning
    bool isRunning = true;

    SDL_Event event;

    //Pad
    float PadX = 300;
    float PadY = 370;
    float Width = 100;
    float Height = 10;

    //Pad 2
    float Pad2X = 300;
    float Pad2Y = 20;
    float Width2 = 100;
    float Height2 = 10;

    //Pad Movement
    bool Left = false, Right = false;

    //Pad 2 Movement
    bool Left2 = false, Right2 = false;

    //Ball
    float BallX = 50;
    float BallY = 50;
    float BallWH = 15;

    //Ball Movement
    float VelX = 4;
    float VelY = 4;

    //Pad Texture
    unsigned int pad_texture = 0;
    unsigned int pad2_texture = 0;
    unsigned int ball_texture = 0;
    unsigned int backround_texture = 0;
    pad_texture = loadTexture("Pad.png");
    pad2_texture = loadTexture("Pad2.png");
    ball_texture = loadTexture("Ball.png");
    backround_texture = loadTexture("Backround.png");

    //Game Loop
    while (isRunning) {
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                isRunning = false;
            }
            if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE) {
                isRunning = false;
            }

            //Pad Movement
            if (event.type == SDL_KEYDOWN) {

                //Pad
                if (event.key.keysym.sym == SDLK_LEFT) {
                    Left = true;
                }
                if (event.key.keysym.sym == SDLK_RIGHT) {
                    Right = true;
                }

                //Pad 2
                if (event.key.keysym.sym == SDLK_a) {
                    Left2 = true;
                }
                if (event.key.keysym.sym == SDLK_d) {
                    Right2 = true;
                }
            }

            else if (event.type == SDL_KEYUP) {
                //Pad
                if (event.key.keysym.sym == SDLK_LEFT) {
                    Left = false;
                }
                if (event.key.keysym.sym == SDLK_RIGHT) {
                    Right = false;
                }

                //Pad 2
                if (event.key.keysym.sym == SDLK_a) {
                    Left2 = false;
                }
                if (event.key.keysym.sym == SDLK_d) {
                    Right2 = false;
                }
            }
        }
        //Game Logic

            //Movement
            if (Left == true) {
                PadX -= 8;
            }
            if (Right == true) {
                PadX += 8;
            }

            //Movement 2
            if (Left2 == true) {
                Pad2X -= 8;
            }
            if (Right2 == true) {
                Pad2X += 8;
            }

            //Pad Wall Collision
            if (PadX < 0) {
                PadX = 0;
            }
            if (PadX+Width > 600) {
                PadX = 600-Width;
            }

            //Pad 2 Wall Collision
            if (Pad2X < 0) {
                Pad2X = 0;
            }
            if (Pad2X+Width2 > 600) {
                Pad2X = 600-Width2;
            }

            //Ball Movement
            BallX += VelX;
            BallY += VelY;

            //Ball
            if (BallX < 0) {
                VelX = -VelX;
            }
            else if (BallX+BallWH > 600) {
                VelX = -VelX;
            }
            if (BallY < 0) {
                isRunning = false;
            }
            else if (BallY+BallWH > 400) {
                isRunning = false;
            }

            //Ball Pad Collision
            if (checkCollision(BallX, BallY, BallWH, BallWH, PadX, PadY, Width, Height) == true) {
                VelY = -VelY;

                float temp = BallX + BallWH/2;
                temp = temp - PadX;
                temp = temp - 50;
                temp = temp / 7;
                VelX = temp;
            }

            //Ball Pad2 Collision
            if (checkCollision(BallX, BallY, BallWH, BallWH, Pad2X, Pad2Y, Width2, Height2) == true) {
                VelY = -VelY;

                float temp = BallX + BallWH/2;
                temp = temp - Pad2X;
                temp = temp - 50;
                temp = temp / 7;
                VelX = temp;
            }

        //Rendering
        glClear(GL_COLOR_BUFFER_BIT);

        glPushMatrix();

        glOrtho(0, 600, 400, 0, -1, 1);

        glColor4ub(255, 255, 255, 255);

        glEnable(GL_TEXTURE_2D);

        //Backround
        glBindTexture(GL_TEXTURE_2D, backround_texture);
        
        glBegin(GL_QUADS);

        glTexCoord2d(0, 0); glVertex2f(0, 0);
        glTexCoord2d(1, 0); glVertex2f(600, 0);
        glTexCoord2d(1, 1); glVertex2f(600, 400);
        glTexCoord2d(0, 1); glVertex2f(0, 400);

        glEnd();

        //Pad
        glBindTexture(GL_TEXTURE_2D, pad_texture);

        glBegin(GL_QUADS);

        glTexCoord2d(0, 0); glVertex2f(PadX, PadY);
        glTexCoord2d(1, 0); glVertex2f(PadX+Width, PadY);
        glTexCoord2d(1, 1); glVertex2f(PadX+Width, PadY+Height);
        glTexCoord2d(0, 1); glVertex2f(PadX, PadY+Height);

        glEnd();

        //Pad 2
        glBindTexture(GL_TEXTURE_2D, pad2_texture);

        glBegin(GL_QUADS);

        glTexCoord2d(0, 0); glVertex2f(Pad2X, Pad2Y);
        glTexCoord2d(1, 0); glVertex2f(Pad2X+Width2, Pad2Y);
        glTexCoord2d(1, 1); glVertex2f(Pad2X+Width2, Pad2Y+Height2);
        glTexCoord2d(0, 1); glVertex2f(Pad2X, Pad2Y+Height2);

        glEnd();

        //Ball
        glBindTexture(GL_TEXTURE_2D, ball_texture);

        glBegin(GL_QUADS);

        glTexCoord2d(0, 0); glVertex2f(BallX, BallY);
        glTexCoord2d(1, 0); glVertex2f(BallX+BallWH, BallY);
        glTexCoord2d(1, 1); glVertex2f(BallX+BallWH, BallY+BallWH);
        glTexCoord2d(0, 1); glVertex2f(BallX, BallY+BallWH);

        glEnd();

        glDisable(GL_TEXTURE_2D);

        glPopMatrix();

        SDL_GL_SwapBuffers();

        SDL_Delay(1);
    }

    SDL_Quit();

    return 0;
}
makes me hurl[/quote]


RE: C++/SDL/OpenGL Pong :D - Iceglade - 03-21-2014

Bbbbbb"bbbbbbbnbbbbbbbuuuuuuuuummmmmmmmmmppppppp


RE: C++/SDL/OpenGL Pong :D - redstonewarrior - 03-21-2014

No function should be that long.
Ever.


RE: C++/SDL/OpenGL Pong :D - Dcentrics - 03-22-2014

(03-21-2014, 09:25 PM)redstonewarrior Wrote: No function should be that long.
Ever.



RE: C++/SDL/OpenGL Pong :D - Iceglade - 03-25-2014

That really is quality code, what are you talking about?