Jump to content
  • Advertisement
Sign in to follow this  
Niddles

OpenGL Buffering? (New to OpenGL)

This topic is 4418 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone, I was starting to make a version of pong with openGL, and I'm working on the paddle movement right now and when I move the paddle left and right it leaves a trail, I think this has to do with buffering, but I'm not sure. Can someone tell me how to lose the trail? I tried one way, which was creating a quad behind it that was the same color as the background, but it didn't work. Here's the code.
#include <windows.h>
#include <iostream>
#include <gl/gl.h>
#define pwidth 0.16f
#define pheight 0.06f



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);
void gameInit();
void gameRun(HDC hDC);
void movePaddle(int dir);
typedef struct 
{
    float x, y, x2, y2;
}paddle;

paddle PlayerPaddle;

int WINAPI WinMain (HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpCmdLine,
                    int iCmdShow)
{
    WNDCLASS wc;
    HWND hWnd;
    HDC hDC;
    HGLRC hRC;        
    MSG msg;
    BOOL bQuit = FALSE;
    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 = "PongAPP";
    RegisterClass (&wc);

    hWnd = CreateWindow (
      "PongAPP", "Pong",
      WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
      0, 0, 800, 800,
      NULL, NULL, hInstance, NULL);
    if(hWnd == NULL)
    {
        MessageBox(NULL, "Hwnd is null", "WinMain", MB_ICONEXCLAMATION);
        return 0;
    }

    EnableOpenGL (hWnd, &hDC, &hRC);
    gameInit();

    while (!bQuit)
    {
        if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
        {
            if (msg.message == WM_QUIT)
            {
                bQuit = TRUE;
            }
            else
            {
                TranslateMessage (&msg);
                DispatchMessage (&msg);
            }
        }
        else
        {
            gameRun(hDC);
        }
    }
    DisableOpenGL (hWnd, hDC, hRC);

    DestroyWindow (hWnd);

    return msg.wParam;
}


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:
        switch (wParam)
        {
        case VK_ESCAPE:
            PostQuitMessage(0);
            return 0;
        case VK_LEFT:
            movePaddle(1);
            return 0;
        case VK_RIGHT:
            movePaddle(-1);
            return 0;
        }
        return 0;

    default:
        return DefWindowProc (hWnd, message, wParam, lParam);
    }
}



void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
    PIXELFORMATDESCRIPTOR pfd;
    int iFormat;
    *hDC = GetDC (hWnd);
    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);
    *hRC = wglCreateContext( *hDC );
    wglMakeCurrent( *hDC, *hRC );

}



void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
    wglMakeCurrent (NULL, NULL);
    wglDeleteContext (hRC);
    ReleaseDC (hWnd, hDC);
}
void movePaddle(int dir)
{
    switch(dir)
    {
        case 1:
            PlayerPaddle.x -=0.02f;
            glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
            glClear(GL_COLOR_BUFFER_BIT);
            break;
        case -1:
            PlayerPaddle.x +=0.02f;
            glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
            glClear(GL_COLOR_BUFFER_BIT);
            break;
    }
}
void gameInit()
{
    PlayerPaddle.x = -0.13f;
    PlayerPaddle.y = -0.95f;
    PlayerPaddle.x2 = PlayerPaddle.x + pwidth;
    PlayerPaddle.y2 = PlayerPaddle.y + pheight;
}
void gameRun(HDC hDC)
{
    static float oldx, oldx2, oldy, oldy2;
    
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
    glBegin(GL_QUADS);
        glColor3f(0.0f, 0.0f, 0.0f);
        glVertex2f(oldx, oldy);
        glVertex2f(oldx, oldy2);
        glVertex2f(oldx2, oldy2);
        glVertex2f(oldx2, oldy);
        glColor3f(1.0f, 1.0f, 1.0f);
        glVertex2f(PlayerPaddle.x, PlayerPaddle.y);
        glVertex2f(PlayerPaddle.x, PlayerPaddle.y2);
        glVertex2f(PlayerPaddle.x2, PlayerPaddle.y2);
        glVertex2f(PlayerPaddle.x2, PlayerPaddle.y);
    glEnd();
    glPopMatrix();
    SwapBuffers(hDC);
    oldx = PlayerPaddle.x;
    oldx2 = PlayerPaddle.x2;
    oldy = PlayerPaddle.y;
    oldy2 = PlayerPaddle.y2;
}

Share this post


Link to post
Share on other sites
Advertisement
I fixed it!:

#include <windows.h>
#include <iostream>
#include <gl/gl.h>
#define pwidth 0.16f
#define pheight 0.06f

#pragma comment(lib,"opengl32.lib")

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);
void gameInit();
void gameRun(HDC hDC);
void movePaddle(int dir);
typedef struct
{
float x, y, x2, y2;
}paddle;

paddle PlayerPaddle;

int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int iCmdShow)
{
WNDCLASS wc;
HWND hWnd;
HDC hDC;
HGLRC hRC;
MSG msg;
BOOL bQuit = FALSE;
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 = L"PongAPP";
RegisterClass (&wc);

hWnd = CreateWindow (
L"PongAPP", L"Pong",
WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
0, 0, 800, 800,
NULL, NULL, hInstance, NULL);
if(hWnd == NULL)
{
MessageBox(NULL, L"Hwnd is null", L"WinMain", MB_ICONEXCLAMATION);
return 0;
}

EnableOpenGL (hWnd, &hDC, &hRC);
gameInit();

while (!bQuit)
{
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
bQuit = TRUE;
}
else
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
else
{
gameRun(hDC);
}
}
DisableOpenGL (hWnd, hDC, hRC);

DestroyWindow (hWnd);

return msg.wParam;
}


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:
switch (wParam)
{
case VK_ESCAPE:
PostQuitMessage(0);
return 0;
case VK_LEFT:
movePaddle(1);
return 0;
case VK_RIGHT:
movePaddle(-1);
return 0;
}
return 0;

default:
return DefWindowProc (hWnd, message, wParam, lParam);
}
}



void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;
*hDC = GetDC (hWnd);
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);
*hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC );

}



void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
wglMakeCurrent (NULL, NULL);
wglDeleteContext (hRC);
ReleaseDC (hWnd, hDC);
}
void movePaddle(int dir)
{
switch(dir)
{
case 1:
PlayerPaddle.x -=0.02f;
PlayerPaddle.x2 -=0.02f;
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
break;
case -1:
PlayerPaddle.x +=0.02f;
PlayerPaddle.x2 +=0.02f;
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
break;
}
}
void gameInit()
{
PlayerPaddle.x = -0.13f;
PlayerPaddle.y = -0.95f;
PlayerPaddle.x2 = PlayerPaddle.x + pwidth;
PlayerPaddle.y2 = PlayerPaddle.y + pheight;
}
void gameRun(HDC hDC)
{
static float oldx, oldx2, oldy, oldy2;

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glBegin(GL_QUADS);

glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f(PlayerPaddle.x, PlayerPaddle.y);
glVertex2f(PlayerPaddle.x, PlayerPaddle.y2);
glVertex2f(PlayerPaddle.x2, PlayerPaddle.y2);
glVertex2f(PlayerPaddle.x2, PlayerPaddle.y);
glEnd();
glPopMatrix();
SwapBuffers(hDC);
oldx = PlayerPaddle.x;
oldx2 = PlayerPaddle.x2;
oldy = PlayerPaddle.y;
oldy2 = PlayerPaddle.y2;
}





void movePaddle(int dir)
{
switch(dir)
{
case 1:
PlayerPaddle.x -=0.02f;
PlayerPaddle.x2 -=0.02f;
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
break;
case -1:
PlayerPaddle.x +=0.02f;
PlayerPaddle.x2 +=0.02f;
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
break;
}
}
...
glBegin(GL_QUADS);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f(PlayerPaddle.x, PlayerPaddle.y);
glVertex2f(PlayerPaddle.x, PlayerPaddle.y2);
glVertex2f(PlayerPaddle.x2, PlayerPaddle.y2);
glVertex2f(PlayerPaddle.x2, PlayerPaddle.y);
glEnd();
...
Why don't you write the movePaddle function like this:

void movePaddle(int dir)
{
PlayerPaddle.x -=dir*0.02;
PlayerPaddle.x2 -=dir*0.02;
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!