PDA

View Full Version : SetPixelFormat freezes



Cardinal
12-25-2002, 03:18 PM
running the following initialization code, the program will cause my computer to freeze when executing SetPixelFormat(...). I have not tested it on other system, and if anyone else could, that would be awesome. Here is .c file contents:




#include <stdarg.h>
#include "mgl.h"

/***** Static Prototypes ******/
static boolean createWindow(context * nglc);
static void destroyWindow(context * nglc);
static boolean createGL(context * nglc);
static void destroyGL(context * nglc);

void createContext(context **t,int height,int width,int bits,char *name)
{
if( t!=NULL &amp;&amp; (*t = (context *)malloc(sizeof(context))) != NULL )
{
(*t)->height = height;
(*t)->width = width;
(*t)->bits = bits;
(*t)->hInst = NULL;
(*t)->hWnd = NULL;
(*t)->hDC = NULL;
(*t)->hRC = NULL;
(*t)->name = (name!=NULL?strdup(name):strdup(""));
}
}

void deleteContext(context **t)
{
if( (t != NULL) &amp;&amp; (*t != NULL) )
{
if( (*t)->name != NULL )
free((*t)->name);
free(*t);
*t = NULL;
}
}

boolean initGL(context * nglc)
{
if(createWindow(nglc) &amp;&amp; createGL(nglc))
return true;
killGL(nglc);
return false;
}

void killGL(context * nglc)
{
destroyGL(nglc);
destroyWindow(nglc);
}

/* Creates a window for use by initGL */
static boolean createWindow(context *nglc)
{
WNDCLASSEX wcx;
DEVMODE devmode;

memset(&amp;wcx,0,sizeof(WNDCLASSEX));
wcx.cbSize = sizeof(WNDCLASSEX);
wcx.style = CS_OWNDC;
wcx.lpfnWndProc = msgHandler;
if( (wcx.hInstance = (nglc->hInst = GetModuleHandle(NULL))) == NULL) return false;
wcx.lpszClassName = "mglClass";

memset(&amp;devmode,0,sizeof(DEVMODE));
devmode.dmSize = sizeof(devmode);
devmode.dmPelsWidth = nglc->width;
devmode.dmPelsHeight = nglc->height;
devmode.dmBitsPerPel = nglc->bits;
devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;

if( RegisterClassEx(&amp;wcx) == 0 ) return false;

if( (nglc->hWnd = CreateWindowEx(WS_EX_APPWINDOW,"mglClass",nglc->name,CS_OWNDC,0,0,nglc->width,nglc->height,NULL,NULL,nglc->hInst,NULL)) == NULL ) return false;

if( ChangeDisplaySettings(&amp;devmode,0) != DISP_CHANGE_SUCCESSFUL ) return false;

ShowWindow(nglc->hWnd,SW_SHOWNORMAL);
ShowCursor(false);
SetCapture(nglc->hWnd);

return true;
}

/* destroys a initGL window */
static void destroyWindow(context * nglc)
{
ReleaseCapture();
ShowCursor(true);
ChangeDisplaySettings(NULL,0);
if( nglc->hWnd )
{
DestroyWindow(nglc->hWnd);
nglc->hWnd = NULL;
}
if( nglc->hInst )
{
UnregisterClass("mglClass",nglc->hInst);
nglc->hInst = NULL;
}
}

static boolean createGL(context * nglc)
{
PIXELFORMATDESCRIPTOR pfd;

memset(&amp;pfd,0,sizeof(PIXELFORMATDESCRIPTOR));
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = nglc->bits;
/* pfd.cAccumBits = 0; */
pfd.cDepthBits = nglc->bits;
/* pfd.cStencilBits = 0; */
pfd.iLayerType = PFD_MAIN_PLANE;

if( !(nglc->hDC = GetDC(nglc->hWnd)) ) return false;
if( !SetPixelFormat(nglc->hDC,ChoosePixelFormat(nglc->hDC,&amp;pfd),&amp;pfd) ) return false;
if( !(nglc->hRC = wglCreateContext(nglc->hDC)) ) return false;
if( !wglMakeCurrent(nglc->hDC,nglc->hRC) ) return false;

return true;
}

static void destroyGL(context * nglc)
{
wglMakeCurrent(NULL,NULL);
if( nglc->hRC )
{
wglDeleteContext(nglc->hRC);
nglc->hRC = NULL;
}
if( nglc->hDC )
{
ReleaseDC(nglc->hWnd,nglc->hDC);
nglc->hDC = NULL;
}
}


and the accompaning header




#ifndef MGLH
#define MGLH

/***** include files ******/
#include<windows.h>
#include<stdio.h>
#include<gl/gl.h>
#include<gl/glu.h>
#include<gl/glaux.h>

/***** structures ******/
typedef struct
{
int height;
int width;
int bits;
HINSTANCE hInst;
HWND hWnd;
HGLRC hRC;
HDC hDC;
char *name;
} context;

#ifndef BOOLEAN
#define BOOLEAN
typedef enum{false,true} boolean;
#endif
/***** gl prototypes ******/

/* window message handler-MUST BE DEFINED BY USER */
LRESULT msgHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam );

/* creates a new pointer at t, with preset values: height, width, bits, and name */
void createContext(context **t,int height,int width,int bits,char *name);

/* deletes a existing context, and frees all associated memory */
void deleteContext(context **t);

/* PRECONDITION: nglc is not NULL
* initializes and starts a rendering context */
boolean initGL(context *nglc);

/* PRECONDITION: nglc is not NULL
* destroys and resets nglc */
void killGL(context *nglc);

#endif


This is the main .c file:




/***** global includes ******/
#include <stdio.h>

/***** local includes ******/
#include "mgl.h"

/***** typedefs ******/
#ifndef BOOLEAN
#define BOOLEAN
typedef enum{false,true} boolean;
#endif

/***** global static variables ******/
static boolean loop = true;

/***** code ******/
int main(int argc,char *argv[])
{
context *nglc;
MSG msg;

createContext(&amp;nglc,768,1024,32,"default");
initGL(nglc);
while(loop)
{
if( PeekMessage(&amp;msg,nglc->hWnd,0,0,PM_REMOVE) )
DispatchMessage(&amp;msg);
}
killGL(nglc);
deleteContext(&amp;nglc);
return 0;
}

LRESULT msgHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch(uMsg)
{
case(WM_KEYDOWN):
loop = false;
default:
break;
}
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}


Give it a try, and see if it has the same results. Be aware the freeze requires a reboot.

Thanks,
Cardinal

mikael_aronsson
12-26-2002, 12:29 AM
Hi !

A good start would be to check what ChoosePixelFormat() returns, can you run other OpenGL apps without problems ?, just so you don't have a wacky driver.

I can't test it, don't have mgl installed.

Mikael

Jan
12-26-2002, 11:26 AM
The first thing you should do, is to go to nvidias site and download their pixelformat-demo program. If it crashes too, you have the wrong driver.

Some time ago nVidia released a driver, which had a bug in the pixelformat extension. If you still have this driver, you should download the latest one, the bug is fixed there.

Jan.

Cardinal
12-26-2002, 06:58 PM
I downloaded a random lesson from nehe.gamedev.net and it ran just fine; i've also been running opengl games, and other programs without problems before-so I am fairly certain its a code/language dependant problem.

I am compiling the code with lcc-win32, and this is first program I have done outside of Borland Builder.

In the first version, which suffered from the same problem, i had a



if( ChoosePixelFormat(...) <= 0 ) return false;

but always returned valid formats, and even an invalid format should only make SetPixelFormat return false.

[This message has been edited by Cardinal (edited 12-26-2002).]

Cardinal
01-09-2003, 01:13 AM
I am not exactly sure why SetPixelFormat triggered the freezes, but after correctly declaring:



LRESULT CALLBACK msgHandler(...)

the code worked gracefully. G'luck to any of you with similarly strange and painstaking bugs.