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 && (*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) && (*t != NULL) )
{
if( (*t)->name != NULL )
free((*t)->name);
free(*t);
*t = NULL;
}
}boolean initGL(context * nglc)
{
if(createWindow(nglc) && 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(&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(&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(&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(&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(&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,&pfd),&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(&nglc,768,1024,32,“default”);
initGL(nglc);
while(loop)
{
if( PeekMessage(&msg,nglc->hWnd,0,0,PM_REMOVE) )
DispatchMessage(&msg);
}
killGL(nglc);
deleteContext(&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