Thread: first opengl game, problems.

  1. #1
    irregularly symmetrical n3v's Avatar
    Join Date
    Mar 2006
    Location
    Finland
    Posts
    67

    first opengl game, problems.

    alright, i'm trying to just code a very simple pong game in opengl, to get the hang of it, but i'm running into a lot of problems. the main problem with my code is, when i press the arrow keys, instead of just moving the player's pong paddle, it moves all the objects on the screen (two pong paddles and a ball.)

    problem number two is that i don't know why a segment of my code works. every frame, i'm doing gltranslate() to move things, and i thought in gltranslate(x,y,z) those numbers would represent how far from zero the objects will move, but as long as the values are nonzero, they continue to move indefinitely at a certain speed, depending on the numbers inputted into the function.

    problem three is that i don't understand glOrtho(). despite my best efforts, whenever i use it, i can't see any objects in my window.

    So here's the code:

    Code:
    /**************************
     * Includes
     *
     **************************/
    
    #include <windows.h>
    #include <gl/gl.h>
    bool keys[256] = {false};
    float xme = 0.0;
    float xcom = 0.0;
    float xball, yball;
    
    class pong {
          public:
          pong();
          ~pong();
          float move(int i, float v);
          float coords[8];
          float xmove, ymove;
          void gldisplay(float c[8], float x, float y);
          };
    /**************************
     * Function Declarations
     *
     **************************/
    LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
    WPARAM wParam, LPARAM lParam);
    void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
    void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);
    
    
    /**************************
     * WinMain
     *
     **************************/
    
    int WINAPI WinMain (HINSTANCE hInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpCmdLine,
                        int iCmdShow)
    {
                        pong player, cpu, ball;
                        player.xmove = 0.0;
                        cpu.xmove = 0.0;
                        ball.xmove = 0.0;
                        ball.ymove = 0.0;
                        
                        glVertex2f (0.05, -0.05);
                        glVertex2f (0.05, 0.05);
                        glVertex2f (-0.05, 0.05);
                        glVertex2f (-0.05, -0.05);
                        
                        ball.coords[0] = 0.05;
                        ball.coords[1] = -0.05;
                        ball.coords[2] = 0.05;
                        ball.coords[3] = 0.05;
                        ball.coords[4] = -0.05;
                        ball.coords[5] = 0.05;
                        ball.coords[6] = -0.05;
                        ball.coords[7] = -0.05;
                        
                        player.coords[0] = -0.25;
                        player.coords[1] = -0.9;
                        player.coords[2] = -0.25;
                        player.coords[3] = -0.8;
                        player.coords[4] = 0.25;
                        player.coords[5] = -0.8;
                        player.coords[6] = 0.25;
                        player.coords[7] = -0.9;
                        
                        cpu.coords[0] = 0.25;
                        cpu.coords[1] = 0.9;
                        cpu.coords[2] = 0.25;
                        cpu.coords[3] = 0.8;
                        cpu.coords[4] = -0.25;
                        cpu.coords[5] = 0.8;
                        cpu.coords[6] = -0.25;
                        cpu.coords[7] = 0.9;
                        
        WNDCLASS wc;
        HWND hWnd;
        HDC hDC;
        HGLRC hRC;        
        MSG msg;
        BOOL bQuit = FALSE;
        float theta = 0.0f;
    
        /* register window class */
        wc.style = CS_OWNDC;
        wc.lpfnWndProc = WndProc;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.hInstance = hInstance;
        wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wc.hCursor = LoadCursor (NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
        wc.lpszMenuName = NULL;
        wc.lpszClassName = "GLSample";
        RegisterClass (&wc);
    
        /* create main window */
        hWnd = CreateWindow (
          "GLSample", "OpenGL Sample", 
          WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
          0, 0, 256, 256,
          NULL, NULL, hInstance, NULL);
    
        /* enable OpenGL for the window */
        EnableOpenGL (hWnd, &hDC, &hRC);
        /* program main loop */
        while (!bQuit)
        {
            /* check for messages */
            if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
            {
                /* handle or dispatch messages */
                if (msg.message == WM_QUIT)
                {
                    bQuit = TRUE;
                }
                else
                {
                    TranslateMessage (&msg);
                    DispatchMessage (&msg);
                }
            }
            else
            {
                /* OpenGL animation code goes here */
    
                glPushMatrix();
                glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
                glClear (GL_COLOR_BUFFER_BIT);
                glColor3f (1.0f, 1.0f, 1.0f);
                //glOrtho (0.0, 1.0, 0.0, 1.0, -1.0, 1.0); // could someone explain how to
                // set this code correctly? every time i use it, i can't see anything
               
                        
                player.gldisplay(player.coords, player.xmove, 0.0);
                cpu.gldisplay(cpu.coords, 0.0, 0.0);
                ball.gldisplay(ball.coords, 0.0,0.0);
                
                
                if (keys[VK_RIGHT] == true && player.xmove < 0.7) {
                                            player.xmove = 0.015;
                                            }
                if (keys[VK_LEFT] == true && player.xmove > -0.7) {
                                            player.xmove = -0.015;  
                                            //^^^^^^^^^^^^^^^^^^
                                            // why does this work? shouldn't
                                            // this number have to get bigger for the gltranslate() to work?
                                            
                                            }
                if (keys[VK_RIGHT] == false && keys[VK_LEFT] == false){
                     player.xmove = 0.0;
                     }
                     
                SwapBuffers (hDC);
    
                theta += 1.0f;
                Sleep (5);
            }
        }
    
        /* shutdown OpenGL */
        DisableOpenGL (hWnd, hDC, hRC);
    
        /* destroy the window explicitly */
        DestroyWindow (hWnd);
    
        return msg.wParam;
    }
    
    
    /********************
     * Window Procedure
     *
     ********************/
    
    LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
                              WPARAM wParam, LPARAM lParam)
    {
    
        switch (message)
        {
        case WM_CREATE:
            return 0;
        case WM_CLOSE:
            PostQuitMessage (0);
            return 0;
    
        case WM_DESTROY:
            return 0;
    
        case WM_KEYDOWN:
            keys[wParam] = true;
                 return 0;
        case WM_KEYUP:
             keys[wParam] = false;
             return 0;
        default:
            return DefWindowProc (hWnd, message, wParam, lParam);
        }
    }
    
    
    /*******************
     * Enable OpenGL
     *
     *******************/
    
    void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
    {
        PIXELFORMATDESCRIPTOR pfd;
        int iFormat;
    
        /* get the device context (DC) */
        *hDC = GetDC (hWnd);
    
        /* set the pixel format for the DC */
        ZeroMemory (&pfd, sizeof (pfd));
        pfd.nSize = sizeof (pfd);
        pfd.nVersion = 1;
        pfd.dwFlags = PFD_DRAW_TO_WINDOW | 
          PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
        pfd.iPixelType = PFD_TYPE_RGBA;
        pfd.cColorBits = 24;
        pfd.cDepthBits = 16;
        pfd.iLayerType = PFD_MAIN_PLANE;
        iFormat = ChoosePixelFormat (*hDC, &pfd);
        SetPixelFormat (*hDC, iFormat, &pfd);
    
        /* create and enable the render context (RC) */
        *hRC = wglCreateContext( *hDC );
        wglMakeCurrent( *hDC, *hRC );
    
    }
    
    
    /******************
     * Disable OpenGL
     *
     ******************/
    
    void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
    {
        wglMakeCurrent (NULL, NULL);
        wglDeleteContext (hRC);
        ReleaseDC (hWnd, hDC);
    }
    
    float pong::move(int i, float v) {
         if (i == 2) {
         return  v + 0.0002;
             }
         else if (i == 1) {
         return  v - 0.0002;
             }
             }
             
    void pong::gldisplay(float c[9], float x, float y)
    {
                   glTranslatef(x , y, 0.0);
                glBegin (GL_QUADS);
                        glVertex2f (c[0], c[1]);
                        glVertex2f (c[2], c[3]);
                        glVertex2f (c[4], c[5]);
                        glVertex2f (c[6], c[7]);
                glEnd ();
                
     }
     
     pong::pong(){}
     pong::~pong(){}
    If you make a man a fire,
    he will be warm for a day.
    If you set a man on fire,
    he will be warm for the rest of his life.

  2. #2
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    Whoosh! There's lot of code there. After reading your problem, i came up with some conclusions here. (sorry, i don't have time to read the whole thing, don't have that much patient)

    1) Everything move: make sure you use glLoadIdentity(); for every object on the screen. If not, the object will take the coordinate of the object's translation before it.

    2) About the number going => infinite , let just say you have this:
    Code:
    float xMove;
    
    if(key[VK_LEFT]){ xMove -= 1; } //this is assume that you're using ortho
    
    //now put the xMove in the translatef
    
    glTranslatef(xMove, 0, 0);
    //draw objects down here
    You have to add value into it, not set it.

    3) Ortho problem. Hm, remember when you're using ortho, the screen height and width pixel will equal to what you set. So if you set for 1 pixel, you'll see a very tiny dot on the screen.

    Here is the definition of glOrtho:
    glOrtho ( left , right , bottom , top , znear, zfar);

    so let say you want your play screen is 800x600, use this:

    Code:
    glOrtho (0, 800, 600, 0, 1, -1);
    Don't worry too much about the znear and zfar since you're not using 3d stuff. I suggest using glPerspective for 3D.
    Last edited by hdragon; 07-11-2006 at 08:24 PM.
    Hello, testing testing. Everthing is running perfectly...for now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Engine Link Prob
    By swgh in forum Game Programming
    Replies: 2
    Last Post: 01-26-2006, 12:14 AM
  2. 2D OpenGL Strategy game
    By Shakti in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 12-15-2005, 12:16 PM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. Guessing Game Problems
    By Undeadenemy in forum C Programming
    Replies: 7
    Last Post: 05-30-2003, 11:47 PM
  5. My Memory Game
    By jazy921 in forum C Programming
    Replies: 0
    Last Post: 05-05-2003, 05:13 PM