Sign in to follow this  

2D Animation with NeheBaseCode Problem

Recommended Posts

gp343    101
Hi there, i've been trying to reproduce a 2d animation of a bouncing rectangle using nehe base code. However, the rectangle is not showing properly and the animation is distorted when the window is resize. Can some body please run the below code and assist me in correct what i've done wrong. Thanks James #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <stdio.h> #include <gl\gl.h> #include <gl\glu.h> #include <gl\glaux.h> HGLRC hRC = NULL; HDC hDC = NULL; HINSTANCE hInstance; HWND hWnd = NULL; bool keys[256]; bool active = true; bool fullScreen = true; GLfloat x1 = 0.0f; GLfloat y1 = 0.0f; GLfloat rsize = 25.0f; GLfloat xstep = 1.0f; GLfloat ystep = 1.0f; GLfloat windowWidth; GLfloat windowHeight; LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); //void TimerFunction(void); GLvoid ResizeGLScene(GLsizei w, GLsizei h){ // Prevent a divide by zero, when window is too short // (you cant make a window of zero width). if(h == 0) h = 1; // Set the viewport to be the entire window glViewport(0, 0, w, h); // Reset the coordinate system before modifying glLoadIdentity(); // Keep the square square, this time, save calculated // width and height for later use if (w <= h) { windowHeight = 250.0f*h/w; windowWidth = 250.0f; } else { windowWidth = 250.0f*w/h; windowHeight = 250.0f; } // Set the clipping volume gluOrtho2D(0.0f, windowWidth, 0.0f, windowHeight); } int InitGL(GLvoid) { glShadeModel(GL_SMOOTH); glClearColor(0.0f,0.0f,0.0f,0.0f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); return true; } void TimerFunction(void) { // Reverse direction when you reach left or right edge if(x1 > windowWidth-rsize || x1 < 0) xstep = -xstep; // Reverse direction when you reach top or bottom edge if(y1 > windowHeight-rsize || y1 < 0) ystep = -ystep; // Check bounds. This is incase the window is made // smaller and the rectangle is outside the new // clipping volume if(x1 > windowWidth-rsize) x1 = windowWidth-rsize-1; if(y1 > windowHeight-rsize) y1 = windowHeight-rsize-1; // Actually move the square x1 += xstep; y1 += ystep; } int DrawGLScene(GLvoid) { // Set background clearing color to blue glClearColor(0.0f, 0.0f, 1.0f, 1.0f); // Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT); TimerFunction(); // Set drawing color to Red, and draw rectangle at // current position. glColor3f(1.0f, 0.0f, 0.0f); glRectf(x1, y1, x1+rsize, y1+rsize); return true; } GLvoid KillGLWindow(GLvoid){ if(fullScreen){ ChangeDisplaySettings(NULL,0); ShowCursor(true); } if(hRC){ if(!wglMakeCurrent(NULL, NULL)){ MessageBox(NULL, "Release of DC and RC Failed", "SHUT_DOWN_ERROR", MB_OK | MB_ICONINFORMATION); } if(!wglDeleteContext(hRC)){ MessageBox(NULL, "Release of Rendering Context Failed", "SHUT_DOWN_ERROR", MB_OK | MB_ICONINFORMATION); } hRC = NULL; } if(hDC && !ReleaseDC(hWnd, hDC)){ MessageBox(NULL, "Release of Device Context Failed", "SHUT_DOWN_ERROR", MB_OK | MB_ICONINFORMATION); hDC = NULL; } if(hWnd && !DestroyWindow(hWnd)){ MessageBox(NULL, "Could Not Release Hwnd", "SHUT_DOWN_ERROR", MB_OK | MB_ICONINFORMATION); hWnd = NULL; } if(!UnregisterClass("OpenGL",hInstance)){ MessageBox(NULL, "Could Not Unregister Class", "SHUT_DOWN_ERROR", MB_OK | MB_ICONINFORMATION); hInstance = NULL; } } bool CreateGLWindow(char* title, int width, int height, int bits, bool fullScreenFlag){ GLuint PixelFormat; WNDCLASS wc; DWORD dwExstyle; DWORD dwstyle; RECT windowRect; windowRect.left = (long)0; windowRect.right = (long)width; = (long)0; windowRect.bottom = (long)height; fullScreen = fullScreenFlag; hInstance = GetModuleHandle(NULL); = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = (WNDPROC)WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = NULL; wc.lpszMenuName = NULL; wc.lpszClassName = "OpenGL"; if(!RegisterClass(&wc)){ MessageBox(NULL, "Failed to Register the Window Class","ERROR", MB_OK | MB_ICONEXCLAMATION); return false; } if(fullScreen){ DEVMODE dmScreenSettings; memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); dmScreenSettings.dmSize = sizeof(dmScreenSettings); dmScreenSettings.dmPelsWidth = width; dmScreenSettings.dmPelsHeight = height; dmScreenSettings.dmBitsPerPel = bits; dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT; if(ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL){ if(MessageBox(NULL, "The requested FullScreen Mode is not supported by\nYour Video Card. Use Window Mode Instead?","Arlef OpenGL", MB_YESNO | MB_ICONEXCLAMATION) == IDYES){ fullScreen = true; }else{ MessageBox(NULL, "Program Will Now Close", "Arlef OpenGL", MB_OK | MB_ICONSTOP); return false; } } } if(fullScreen){ dwExstyle = WS_EX_APPWINDOW; dwstyle = WS_POPUP; ShowCursor(false); }else{ dwExstyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; dwstyle = WS_OVERLAPPEDWINDOW; } AdjustWindowRectEx(&windowRect, dwstyle, false, dwExstyle); if(!(hWnd = CreateWindowEx(dwExstyle, "OpenGL", title, WS_CLIPSIBLINGS | WS_CLIPCHILDREN | dwstyle, 0,0, windowRect.right - windowRect.left, windowRect.bottom -, NULL, NULL, hInstance, NULL))){ KillGLWindow(); MessageBox(NULL, "Window Creation Error", "ERROR", MB_OK | MB_ICONEXCLAMATION); return false; } static PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_TYPE_RGBA, bits, 0,0,0,0, 0, 0, 0, 0,0,0,0, 16, 0, 0, PFD_MAIN_PLANE, 0, 0,0,0 }; if(!(hDC = GetDC(hWnd))){ KillGLWindow(); MessageBox(NULL, "Can't Create a GL Device Context", "ERROR", MB_OK | MB_ICONEXCLAMATION); return false; } if(!(PixelFormat = ChoosePixelFormat(hDC, &pfd))){ KillGLWindow(); MessageBox(NULL, "Can't Find a Suitable PixelFormat", "ERROR", MB_OK | MB_ICONEXCLAMATION); return false; } if(!(SetPixelFormat(hDC, PixelFormat, &pfd))){ KillGLWindow(); MessageBox(NULL, "Can't Set the Pixel Format", "ERROR", MB_OK | MB_ICONEXCLAMATION); return false; } if(!(hRC = wglCreateContext(hDC))){ KillGLWindow(); MessageBox(NULL, "Can't Create a GL Rendering Context", "ERROR", MB_OK | MB_ICONEXCLAMATION); return false; } if(!(wglMakeCurrent(hDC, hRC))){ KillGLWindow(); MessageBox(NULL, "Can't Activate the GL Rendering Context", "ERROR", MB_OK | MB_ICONEXCLAMATION); return false; } ShowWindow(hWnd, SW_SHOW); SetForegroundWindow(hWnd); SetFocus(hWnd); ResizeGLScene(width, height); if(!(InitGL())){ KillGLWindow(); MessageBox(NULL, "Initialization Failed", "ERROR", MB_OK | MB_ICONEXCLAMATION); return false; } return true; }//End of Create GL Window LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){ switch(uMsg){ case WM_ACTIVATE: { if(!HIWORD(wParam)){ active = true; }else{ active = false; } return 0; } case WM_SYSCOMMAND: { switch(wParam){ case SC_SCREENSAVE: case SC_MONITORPOWER: return 0; } break; } case WM_CLOSE: { PostQuitMessage(0); return 0; } case WM_KEYDOWN: { keys[wParam] = true; return 0; } case WM_SIZE: { ResizeGLScene(LOWORD(lParam), HIWORD(lParam)); return 0; } }//End of switch return DefWindowProc(hWnd, uMsg, wParam, lParam); }//End of Wnd Proc int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){ MSG msg; bool done = false; if(MessageBox(NULL, "Would you like to run in full screen mode?", "Start FullScreen?", MB_YESNO | MB_ICONQUESTION) == IDNO){ fullScreen = false; } if(!CreateGLWindow("Arlef OpenGL FrameWork",640,480,16,fullScreen)){ return 0; } while(!done){ if(PeekMessage(&msg, NULL, 0,0, PM_REMOVE)){ if(msg.message == WM_QUIT){ done = true; }else{ TranslateMessage(&msg); DispatchMessage(&msg); } }else{ if(active){ if(keys[VK_ESCAPE]){ done = true; }else{ DrawGLScene(); SwapBuffers(hDC); } } if(keys[VK_F1]){ keys[VK_F1] =false; KillGLWindow(); fullScreen = !fullScreen; if(!CreateGLWindow("Arlef OpenGL FrameWorkd", 640, 480, 16, fullScreen)){ return 0; } } } }//While Loop KillGLWindow(); return (msg.wParam); }//End of Winmain

Share this post

Link to post
Share on other sites
Thunder_Hawk    314
You need to clear the depth buffer (not that this program really needs one in the first place [wink]). This is due to the way you set up your depth buffer.



On a side note, you should move the glClearColor call to your init function.

Oh yeah, in the future, use [source] [/source] tags to make the code you post much easier to read, especially if you post this much.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this