PDA

View Full Version : bugger :)



paneb
03-24-2003, 08:16 AM
hi guys i just wrote the first nehe tutorial, and when i run it i have my window except i cant do anything with it..i cant move it , i cant minimize it, i cant resize..no nothing..
i wrote it myself reading off the web, so i thought maybe i got something wrong so i dled the actual zip file from nehe and compared each line, but i still cant find what is causing that..
so this is really the topic that should be put at the very end of this forum, and if anyone has time to waste (and they dont have something already planned http://www.opengl.org/discussion_boards/ubb/smile.gif ), then could you just look at the code? thanks a lot




//~~~~~~~~~~~~~~~~ gl_window ~~~~~~~~~~~~~~~~~~~~~~~
//What can it do: initialize an OpenGL window
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//include files needed
#include <windows.h> //WinAPI
//next 3 lines are for OpenGL
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>

HGLRC g_hRC = NULL; //rendering contest - links OpenGL calls to DC (device context)
HDC g_hDC = NULL; //device context - allows drawing to a window
HWND g_hWnd = NULL; //window handle
HINSTANCE g_hInstance; //instance of the program

BOOL g_bKey[256]; //used for key presses
BOOL g_bActive = TRUE; //active flag - default is TRUE
BOOL g_bFullScn = FALSE; //fullscreen flag - default to TRUE

//window procedure for interacting with message
LRESULT CALLBACK WndProc (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);

//helper functions for nice code - see individual function headers for more info ~~~~~~~~~~~~~~~~~~~~~
unsigned short InitGL (void);
unsigned short CreateGLWindow (const TCHAR * title, UINT widtht, UINT height, unsigned short bits, BOOL fullscn);
void ResizeGLScene (UINT width, UINT height);
unsigned short DrawGLScene (void);
void KillGLWindow (void);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//ResizeGLScene~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~
//what it does: resize the window with given dimensions
void ResizeGLScene (UINT width, UINT height)
{
glViewport (0, 0, width, height); //reset the current viewport

glMatrixMode (GL_PROJECTION); //enter projection matrix mode
glLoadIdentity (); //set projection matrix to identity matrix

//set a perspective projection matrix (calculate aspect ratio)
if (height == 0) height = 1; //dont want divisio by zero
gluPerspective (45.0f, (float)width/(float)height, 0.1f, 100.0f);

glMatrixMode (GL_MODELVIEW); //enter modelview matrix mode
glLoadIdentity (); //set modelview matrix to identity matrix
}//ResizeGLScene

//InitGL~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~
//what it does: initialize our OpenGL setup
unsigned short InitGL (void)
{
glShadeModel (GL_SMOOTH); //use smooth shading
glClearColor (0.0f, 0.0f, 0.0f, 0.5f); //clear color for the window - black in this case (alpha is not used here
//set up the depth buffer
glClearDepth (1.0f); //depth buffer setup
glEnable (GL_DEPTH_TEST); //enable depth testing
glDepthFunc (GL_LEQUAL); //MSDN: Passes if the incoming z value is less than or equal to the stored z value

//next line tells OpenGL we want to use the nicest perspective view possible
glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

return TRUE; //no problems
}//InitGL

//DrawGLScene~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//what it does: place to put any drawing code
unsigned short DrawGLScene (void)
{

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear screenand depth buffers
glLoadIdentity (); //set modelview matrix to identity matrix (remeber we are still in GL_MODELVIEW matrix mode
return TRUE;
}//DrawGLScene

//KillGLWindow ()~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//what it does: clean up everything
void KillGLWindow (void)
{

if (g_bFullScn) { //we are in fullscreen
ChangeDisplaySettings (NULL, 0); //switch to desktop
ShowCursor (TRUE); //show cursor
}

//destroy any RC
if (g_hRC) {
if (!wglMakeCurrent (NULL, NULL)) {
MessageBox (NULL, "Failed to release DC and RC", "GL window: fatal error", MB_OK | MB_ICONEXCLAMATION);
}


//release RC
if (!wglDeleteContext (g_hRC)) {
MessageBox (NULL, "Release of RC failed", "GL window: fatal error", MB_OK | MB_ICONEXCLAMATION);
}

g_hRC = NULL; //make null
}

//destroy any DC
if (g_hDC &amp;&amp; !ReleaseDC (g_hWnd, g_hDC)) {
MessageBox (NULL, "Release of DC failed", "GL window: fatal error", MB_OK | MB_ICONEXCLAMATION);
g_hDC = NULL; //make null
}

//destroy any window handle
if (g_hWnd &amp;&amp; !DestroyWindow (g_hWnd)) {
MessageBox (NULL, "Failed to release window handle", "GL window: fatal error", MB_OK | MB_ICONEXCLAMATION);
g_hWnd = NULL;
}

//unregister our window class
if (!UnregisterClass ("gl_window", g_hInstance)) {
MessageBox (NULL, "Failed to unregister window class", "GL window: fatal error", MB_OK | MB_ICONEXCLAMATION);
g_hInstance = NULL; //set to null
}
}//KillGLWindow ()

//CreateGLWindow~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//what it does: create the window using passed values
unsigned short CreateGLWindow (const TCHAR * title, UINT width, UINT height, unsigned short bits, BOOL fullscn)
{
unsigned int p_nPixelFormat; //id # of the mode corresponding to our needs
WNDCLASS p_wc; //window class struct
DWORD p_dwStyle, p_dwExStyle; //style bits for our window
RECT p_rWndRect; //hold window dimensions

p_rWndRect.bottom = (long) height;
p_rWndRect.left = 0;
p_rWndRect.right = (long) width;
p_rWndRect.top = 0;

g_bFullScn = fullscn;

g_hInstance = GetModuleHandle (NULL); //get instance
//initialize wndclass struct
p_wc.cbClsExtra = 0;
p_wc.cbWndExtra = 0;
p_wc.hbrBackground = NULL;
p_wc.hCursor = LoadCursor (NULL, IDC_ARROW);
p_wc.hIcon = LoadIcon (NULL, IDI_WINLOGO);
p_wc.hInstance = g_hInstance;
p_wc.lpfnWndProc = WndProc;
p_wc.lpszClassName = "gl_window";
p_wc.lpszMenuName = NULL;
p_wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;

//try to register the class
if (!RegisterClass (&amp;p_wc)) {
MessageBox (NULL, "Failed to register class!", "GL window: fatal error", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}


//fullscreen mode
if (g_bFullScn) {
//create and initialize a device mode
DEVMODE p_dmScreenSettings;
memset (&amp;p_dmScreenSettings, 0, sizeof (p_dmScreenSettings));
p_dmScreenSettings.dmSize = sizeof (p_dmScreenSettings);
p_dmScreenSettings.dmPelsHeight = height;
p_dmScreenSettings.dmPelsWidth = width;
p_dmScreenSettings.dmBitsPerPel = bits;
p_dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSHEIGHT | DM_PELSWIDTH;

//try switch to requested mode
if (ChangeDisplaySettings(&amp;p_dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) {
if (MessageBox (NULL, "The requested fullscreen mode is not available on your system! Switch to windowed mode?", "GL window: Error", MB_YESNO | MB_ICONEXCLAMATION) == IDYES)
g_bFullScn = FALSE;
else {
MessageBox (NULL, "Closing", "GL window", MB_OK | MB_ICONINFORMATION);
return FALSE;
}
}
}

//check that we are still in fullscreen mode
if (g_bFullScn) {
p_dwExStyle = WS_EX_APPWINDOW;
p_dwStyle = WS_POPUP;
ShowCursor (FALSE); //hide cursor
}
//not fullscreen mode
else {
p_dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
p_dwStyle = WS_OVERLAPPEDWINDOW;
}

//make sure that any borders do not overlapped our OpenGL scene
AdjustWindowRectEx (&amp;p_rWndRect, p_dwStyle, FALSE, p_dwExStyle);

//now create the window and check for success
if (!(g_hWnd = CreateWindowEx (p_dwExStyle,
"gl_window",
"GL window",
p_dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
0,0,
p_rWndRect.right - p_rWndRect.left,
p_rWndRect.bottom - p_rWndRect.top,
NULL, NULL,
g_hInstance,
NULL))) {
//destroy the window
KillGLWindow ();
MessageBox (NULL, "Failed to create the window", "GL window: fatal error", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}

//create a suitable pixel format for our gl window
static PIXELFORMATDESCRIPTOR p_pfd =
{ sizeof (PIXELFORMATDESCRIPTOR), //size of pfd
1, //vers. #
//must support the following
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA, //want RGBA format
bits, //color depth
0,0,0,0,0,0, //ignore color bits
0, //no alpha buffer
0, //no shift bit
0, //no accumulation buffer
0,0,0,0, //no accumulation bits
16, //16-bit Z-buffer
0, //no stencil buffer
0, //no auxiliary buffer
PFD_MAIN_PLANE, //main drawing layer
0, //reserved
0,0,0 //nolayer masks
};

//request device context
if (!(g_hDC = GetDC (g_hWnd))) {
KillGLWindow ();
MessageBox (NULL, "Failed to create a GL device context", "GL window: fatal error", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}

//request pixel format
if (!(p_nPixelFormat = ChoosePixelFormat (g_hDC, &amp;p_pfd))) {
KillGLWindow ();
MessageBox (NULL, "Could not find matching pixel format", "GL window: fatal error", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}

//we got our pixel format, try to set it
if (!SetPixelFormat (g_hDC, p_nPixelFormat, &amp;p_pfd)) {
KillGLWindow ();
MessageBox (NULL, "Cannot use pixel format", "GL window: fatal error", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}

//now, try to create a GL rendering context
if (! (g_hRC = wglCreateContext (g_hDC))) {
KillGLWindow ();
MessageBox (NULL, "Failed to create GL rendering context", "GL window: fatal error", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}

//attempt to make rendering context active
if (!wglMakeCurrent (g_hDC, g_hRC)) {
KillGLWindow ();
MessageBox (NULL, "Could not make rendering context active", "GL window: fatal error", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}


//everything succeeded, so show the window
ShowWindow (g_hWnd, SW_SHOW);
SetForegroundWindow (g_hWnd);
SetFocus (g_hWnd);
ResizeGLScene (height, width);

//call InitGL () to initialize the window
if (!InitGL ()) {
KillGLWindow ();
MessageBox (NULL, "Failed to initialize window", "GL window: fatal error", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}

return TRUE;
}//CreateGLWindow

//WndProc~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//what it does: handle all messages
LRESULT CALLBACK WndProc (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch (Msg) {
case WM_ACTIVATE:
{
if (!HIWORD(wParam))
g_bActive = TRUE;
else
g_bActive = 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_KEYUP: //get key being released
{
g_bKey[wParam] = FALSE;
return 0;
}
case WM_KEYDOWN:
{
g_bKey[wParam] = TRUE;
return 0;
}
case WM_SIZE:
{
ResizeGLScene (LOWORD(wParam), HIWORD(wParam)); //LoWord=Width, HiWord=Height
return 0;
}
default:
return DefWindowProc (hWnd, Msg, wParam, lParam);

}
}//WndProc

//WinMain~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//what it does: entry point for the program
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE HPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG p_msg; //message structure for our window
BOOL done = FALSE; //tell when to exit the loop

//ask for fullscreen or windowed mode
if (MessageBox (NULL, "Start in fullscreen mode?", "GL window: mode", MB_YESNO | MB_ICONINFORMATION) == IDYES)
g_bFullScn = TRUE;

//now we can create our window
if (!CreateGLWindow ("GL window", 640, 480, 16, g_bFullScn))
return 0; //exit if failed

//here is the message loop - it stops if done is true
while (!done) {
if (PeekMessage (&amp;p_msg, NULL, 0,0, PM_REMOVE)) {
if (p_msg.message == WM_QUIT)
done = TRUE;
else {
TranslateMessage (&amp;p_msg);
DispatchMessage (&amp;p_msg);
}
}
else { //we do not have a message waiting to be processed at the moment
if (g_bActive) {
if (g_bKey[VK_ESCAPE]) //ESC was pressed
done = TRUE;
else {
DrawGLScene (); //draw the scene
SwapBuffers (g_hDC); //swap the buffers since we are using dlb buffering
}
}

if (g_bKey[VK_F1]) { //F1 was pressed?
//if so, then start to shutdown
g_bKey[VK_F1] = FALSE;
KillGLWindow ();
g_bFullScn = !g_bFullScn;
//make a new window
if (!CreateGLWindow ("GL window", 640, 480, 16, g_bFullScn))
return 0; //exit if failed
}
}
}

KillGLWindow (); //destroy window
return p_msg.wParam; //exit program
}//WinMain


this isn't a priority


when i run it, the prog uses 100% of the cpu..and i did some step checking, and it looks like the program is stuck in DrawGLScene ()..i used a messagebox in a few place to see what happened..and once it got to DrawGLScene(0. it kept on popping that same message..it musht be something with my message loop..


[This message has been edited by paneb (edited 03-24-2003).]

nexusone
03-24-2003, 08:33 AM
I have not had any problems with the nehe code, check to see if you have installed the openGL video drivers for your card.
The drivers are done by the MFG of the card, so check there site for the drivers.

Also you could be running in OpenGL software mode, for when there are not hardware drivers.

also download the code in zip format and try the exe that comes with it.

paneb
03-24-2003, 08:48 AM
yea, the executable i dled from nehe does work..its just something in my prog that must be wrong..but i cant find it..i really dont see anything wrong..the code from nehe does work..so that means mine should work too since its basically the same prog

jleffelm
03-25-2003, 12:30 PM
when i run it, the prog uses 100% of the cpu..and i did some step checking, and it looks like the program is stuck in DrawGLScene ()..i used a messagebox in a few place to see what happened..and once it got to DrawGLScene(0. it kept on popping that same message..it musht be something with my message loop..


Um, your program should have been popping up the message box over and over again that you had in your DrawGLScene. DrawGLScene is inside a while loop that will be repainting the scene over and over again. If the message is popping up again and again, then your program isn't getting stuck in the DrawGLScene function.

-Drew