OpenGL Crashes Computer

Started by
6 comments, last by Red Ghost 14 years, 10 months ago
Well , I'm just learning how to use opengl (and the w32 api) (I used SDL before this). I know this is kind of messy , but can anyone help me with this. I'm reading a book called "OpenGL Game Programming" , and after some hacking , I got this beast. It crashes my computer whenever I run it. I think it's to do with the pixel format descriptor , or/and the message loop. Please Helppp! NOTE: I posted in this forum and not in the OpenGL forum , as I believe that whatever the problem is , it's a newbie thing ... so ... WARNING! Please do not try to run the following code , as it will probably crash your computer too. You have been warned... I hold no ...

#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include "resource.h"

LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam);
void DrawFrame();

int WinMain(HINSTANCE hInst,HINSTANCE hPrev,
            LPSTR sCmdLine,int iShow)
{
  WNDCLASSEX wc;
  memset(&wc,0,sizeof(wc)); // Fill all unneeded params with zeros
  wc.cbSize=sizeof(wc);
  wc.style=CS_VREDRAW | CS_HREDRAW;
  wc.cbClsExtra=0;
  wc.cbWndExtra=0;
  wc.lpfnWndProc=WndProc;
  wc.hInstance=hInst;
  wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
  wc.hCursor=LoadCursor(NULL,IDC_ARROW);
  wc.hbrBackground= (HBRUSH)COLOR_APPWORKSPACE;
  wc.lpszClassname="UltraVM";

  RegisterClassEx(&wc);

  HWND win=CreateWindowEx(WS_EX_APPWINDOW,"UltraVM","UltraVM 1.0",
                          WS_OVERLAPPEDWINDOW | WS_VISIBLE |
WS_SYSMENU | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
                          CW_USEDEFAULT,
                          CW_USEDEFAULT,320,240,NULL,LoadMenu(hInst,MAKEINTRESOURCE(IDR_APPMENU)),
                          hInst,
                          NULL);

  ShowWindow(win,iShow);
  UpdateWindow(win);

  int done=0;
  MSG msg;
  while (!done)
    {
      PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);    // pending message?

      if (msg.message == WM_QUIT)      // If a quit message has been received
        {
          // you want to quit the application
          done = TRUE;
        }
      //else {

      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glColor3f(1,0,0);
      glBegin(GL_QUADS);
        glVertex2d(0,0);
        glVertex2d(20,0);
        glVertex2d(20,40);
        glVertex2d(0,40);
      glEnd();

      // do stuff
      TranslateMessage(&msg);     // translate the message
      DispatchMessage(&msg);      // dispatch the message to Windows
      //}

    }
  // return termination code
  return msg.wParam;
}

void ShowAbout(HWND hWnd)
{
  MessageBox(hWnd,"UltraVM W32 R1 SR10 Base -- Developed by Aviraldg","About",
             MB_ICONINFORMATION);
}

void SetPFD(HDC hDC)
{
  static PIXELFORMATDESCRIPTOR pfd =
  {
    sizeof(PIXELFORMATDESCRIPTOR),          // size of the structure
    1,                                      // version, always set to 1
    PFD_DRAW_TO_WINDOW |                    // support window
    PFD_SUPPORT_OPENGL |                    // support OpenGL
    PFD_DOUBLEBUFFER,                       // support double buffering
    PFD_TYPE_RGBA,                          // RGBA color mode
    32,                                     // go for 32 bit color mode
    0, 0, 0, 0, 0, 0,                       // ignore color bits, not used
    0,                                      // no alpha buffer
    0,                                      // ignore shift bit
    0,                                      // no accumulation buffer
    0, 0, 0, 0,                             // ignore accumulation bits
    16,                                     // 16 bit z-buffer size
    0,                                      // no stencil buffer
    0,                                      // no auxiliary buffer
    PFD_MAIN_PLANE,                         // main drawing plane
    0,                                      // reserved
    0, 0, 0
  };                              // layer masks ignored
// choose best matching pixel format, return index
  int nPixelFormat = ChoosePixelFormat(hDC, &pfd);
// set pixel format to device context
  SetPixelFormat(hDC, nPixelFormat, &pfd);
}

LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
  static HGLRC hRC;
  static HDC hDC;

  switch (uMsg)
    {
    case WM_CREATE:                // window creation
      hDC = GetDC(hWnd);                // get device context for window
      SetPFD(hDC);
      hRC = wglCreateContext(hDC);      // create rendering context
      wglMakeCurrent(hDC, hRC);
      return 0;                // return to message loop
      break;

    case WM_CLOSE:                // close message

      PostQuitMessage(0);      // send quit message
      return 0;
      break;

    case WM_COMMAND:
      switch (wParam)
        {
        case ID_FILE_EXIT:
          PostQuitMessage(0);
          break;
        case ID_HELP_ABOUT:
          ShowAbout(hWnd);
          break;
        }
      break;

    case WM_SIZE:
      int h,w;
      h=HIWORD(lParam);
      w=LOWORD(lParam);
      glViewport(0,0,w,h); //wparam - width , lparam - height
      glMatrixMode(GL_PROJECTION);
      glPushMatrix();
      glLoadIdentity();



      glMatrixMode(GL_MODELVIEW);
      glPushMatrix();

      glLoadIdentity();

      glOrtho(0,0,320,240,-1,1);
      break;
      
    default:
      break;
    }
  // pass all unhandled messages to DefWindowProc
  return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
Regards,
Aviral
Advertisement
User code should not crash your computer, not matter how bad the code. The most likely suspect is an old graphics card driver. See about updating your graphics card driver. It may also be that you have a very old operating system, but that is less likely.

I am not familiar with Win32 programming so I will refrain from any comment on the code itself, but the above stands regardless.
Could also be overheating if that program hogs all the resources. Do you get a blue screen and some kind of error message?
Nopes, I have a properly updated driver ... besides , about every other OpenGL app works fine on my computer...

EDIT: I suppose I haven't described the problem well enough...
When I run the application , it starts and shows the window ... OK
Then , Windows locks up! No cursor movement , nothing... And just some time
after that , it reboots w/o BSODding ... (Btw. I have the BSOD option on) Also , I checked out SpeedFan , and it's definitely not an overheating problem.
Regards,
Aviral
You could always try to change the message loop.

//To go through all the messages while( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) ){   TranslateMessage(&msg);        DispatchMessage(&msg);      }//If quit message has been recievedif (msg.message == WM_QUIT)     {   done = TRUE;}//And only after messages have been processed we'll try rendering the screenglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glColor3f(1,0,0);glBegin(GL_QUADS); glVertex2d(0,0); glVertex2d(20,0); glVertex2d(20,40); glVertex2d(0,40);glEnd();
Quote:Original post by Maldion
You could always try to change the message loop.


Yep, Maldion hit it right on the spot: You need to process all of the window messages before you render a frame, otherwise the message-queue becomes so backed up, it crashes! [smile]
No , I tried Maldion's code , but to no avail... I tried modifying the PIXELFORMATDESCRIPTOR , the message loop ; nothing works...
Regards,
Aviral
Hi,

In your message procedure, in WM_CREATE case, you should remove return 0.
You do not need to put GL initiation in the Windos Message pump.

It is simple to do:
- set your window class structure and register it.
- create your window and store its handle
- if the handle is not null, get the device context.
- create a pixel format descriptor and associate it with your device context (besides, it is better in your window creation to set CS_OWNDC)
- create an openGL context and make it current.
None of these steps require the message pump and can be done before your UpdateWindow method.

Hope that helps.
Ghostly yours,
Red.
Ghostly yours,Red.

This topic is closed to new replies.

Advertisement