Skip to main content
gamedev.net
🔒 Locked

Runtime error when minimizing my program.

Started by KING_KONG_coder Jul 3, 2004 at 7:07 PM 6 replies 700+ views
Original Post
KING_KONG_coder
KING_KONG_coder
I have this simple OpenGL program that has a bouncing square. The source code was based on OpenGL Super Bible. Here is the code #include <windows.h> // Header File For Windows #include <gl\gl.h> // Header File For The OpenGL32 Library #include <gl\glu.h> // Initial square position and size GLfloat x1 = 100.0f; GLfloat y1 = 150.0f; GLsizei rsize = 50; // Step size in x and y directions // (number of pixels to move each time) GLfloat xstep = 1.0f; GLfloat ystep = 1.0f; // Keep track of window’s changing width and height GLfloat windowWidth; GLfloat windowHeight; const char g_szClassName[] = "myWindowClass"; // Step 4: the Window Procedure void SetDCPixelFormat(HDC hDC) { int nPixelFormat; static PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), // Size of this structure 1, // Version of thisstructure PFD_DRAW_TO_WINDOW | // Draw to window (not bitmap) PFD_SUPPORT_OPENGL | // Support OpenGL calls PFD_DOUBLEBUFFER, // Double-buffered mode PFD_TYPE_RGBA, // RGBA Color mode 24, // Want 24bit color 0,0,0,0,0,0, // Not used to select mode 0,0, // Not used to select mode 0,0,0,0,0, // Not used to select mode 32, // Size of depth buffer 0, // Not used to select mode 0, // Not used to select mode PFD_MAIN_PLANE, // Draw in main plane 0, // Not used to select mode 0,0,0 }; // Not used to select mode // Choose a pixel format that best matches that described in pfd nPixelFormat = ChoosePixelFormat(hDC, &pfd); // Set the pixel format for the device context SetPixelFormat(hDC, nPixelFormat, &pfd); } void CALLBACK ChangeSize(GLsizei w, GLsizei h) { // Prevent a divide by zero, when window is too short // (you can’t 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 glOrtho(0.0f, windowWidth, 0.0f, windowHeight, 1.0f, -1.0f); } // Called by AUX library to update window void CALLBACK RenderScene(void) { // 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); // 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); //glFlush(); } // Called by AUX library when idle (window not being // resized or moved) void CALLBACK IdleFunction(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 in case 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; // Redraw the scene with new coordinates //RenderScene(); } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static HGLRC hRC; // Permanent Rendering context static HDC hDC; // Private GDI Device context switch(msg) { case WM_CREATE: { // Store the device context hDC = GetDC(hwnd); // Select the pixel format SetDCPixelFormat(hDC); // Create the rendering contextand make it current hRC = wglCreateContext(hDC); wglMakeCurrent(hDC, hRC); // Create a timer that fires every millisecond SetTimer(hwnd,101,1,NULL); } break; case WM_KEYDOWN : switch (wParam) { case VK_HOME : MessageBox(NULL, "Home Pressed", "Error!", MB_ICONEXCLAMATION | MB_OK); break ; default : break ; } break; // Window is resized. case WM_SIZE: { // Call our function which modifies the clipping // volume and viewport ChangeSize(LOWORD(lParam), HIWORD(lParam)); } break; // Timer, moves and bounces the rectangle, simply calls // our previous OnIdle function, then invalidates the // window so it will be redrawn. case WM_TIMER: { IdleFunction(); InvalidateRect(hwnd,NULL,FALSE); } break; case WM_PAINT: { // Call OpenGL drawing code RenderScene(); // Call function to swap the buffers SwapBuffers(hDC); // Validate the newly painted client area ValidateRect(hwnd,NULL); } break; case WM_CLOSE: DestroyWindow(hwnd); break; case WM_DESTROY: { // Kill the timer that we created KillTimer(hwnd,101); // Deselect the current renderingcontext and delete it wglMakeCurrent(hDC,NULL); wglDeleteContext(hRC); // Tell the application to terminateafter the window // is gone. PostQuitMessage(0); } break; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wc; HWND hwnd; MSG Msg; //Step 1: Registering the Window Class wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_HREDRAW | CS_VREDRAW; 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 = NULL; wc.lpszMenuName = NULL; wc.lpszClassName = g_szClassName; wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if(!RegisterClassEx(&wc)) { MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } // Step 2: Creating the Window hwnd = CreateWindowEx( WS_EX_CLIENTEDGE, g_szClassName, "The title of my window", WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 100, 100, 250, 250, NULL, NULL, hInstance, NULL); if(hwnd == NULL) { MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); // Step 3: The Message Loop while(GetMessage(&Msg, NULL, 0, 0) > 0) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; } When I minimize the program, I get an error. Why??
tHiSiSbOb
tHiSiSbOb
I haven't read your code, but can offer you some tips. If your compiler has a debugger that you can use, run your code through it and see what line of code is causing the program to flip out. Actually, I might be able to do it for you...Gimme a few minutes.
----------------------------------------------------"Plant a tree. Remove a Bush" -A bumper sticker I saw.
tHiSiSbOb
tHiSiSbOb
Ok....Done. I compiled it without a problem. When I minimize the window, I get no errors. The only thing I can suggest is for you to update your graphics drivers...
----------------------------------------------------"Plant a tree. Remove a Bush" -A bumper sticker I saw.
KING_KONG_coder
KING_KONG_coder
You got no errors?? I'm using Win98. Must be the OS.

Here's one of the errors,

SIMPLE_WINDOW caused an exception 10H in module SIMPLE_WINDOW.EXE at 0197:004011b0.
Registers:
EAX=00000002 CS=0197 EIP=004011b0 EFLGS=00010202
EBX=00000001 SS=019f ESP=0064f308 EBP=0064f318
ECX=0044f648 DS=019f ESI=00000000 FS=4307
EDX=69073f40 ES=019f EDI=00000000 GS=0000
Bytes at CS:EIP:
d9 1d a4 b4 40 00 c7 05 a0 b4 40 00 00 00 7a 43
Stack dump:
00520ae8 00520ad0 00000000 00000001 0064f328 0040145d 00000000 00000000 0064f354 7abdbd39 00000b68 00000005 00000001 00000000 0064f35c 000083cc
KING_KONG_coder
KING_KONG_coder
When I compiled lesson 7 from the nehe tutorials it works fine when I minimize the program, it can't be my the display drivers.
Kuladus
Kuladus
comment out the call to ur render function and see if u get the same exception, and in the same way comment out other parts so you can hopefully narrow down the problem.
KING_KONG_coder
KING_KONG_coder
I think I figured out what's the problem. It's the compiler. I previously used the Borland commandline compiler. I tried compiling the same code in Visual C++ 6.0 and made a release version, I got no errors when I minimized the program. What the heck????
KING_KONG_coder
KING_KONG_coder
Visual C++ 6.0 probably have a better optimizer/compiler than Borland. I found out the real problem. Minimizing the window calls the WM_SIZE functions which calls the changesize function creating some messes. I solved it. WOOOO, I love debugging.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.