#pragma comment(lib, "OpenGL32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "glaux.lib")
#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glut.h>
#include <gl\glaux.h>
HGLRC hRC = NULL;
HDC hDC = NULL;
HWND hWnd = NULL;
HINSTANCE hInstance;
bool keys[256];
bool active = TRUE;
bool fullscreen = TRUE;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
GLvoid ReSizeGLScene(GLsizei width, GLsizei height)
{
if(height==0)
{
height = 1;
}
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int InitGL(GLvoid)
{
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
return TRUE;
}
int DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
return TRUE;
}
GLvoid KillGLWindow(GLvoid)
{
if(fullscreen)
{
ChangeDisplaySettings(NULL, 0);
ShowCursor(TRUE);
}
if(hRC)
{
if(!wglMakeCurrent(NULL, NULL));
{
MessageBox(NULL,"Release of DC and RC failed", "Shutdown Error", MB_OK | MB_ICONINFORMATION);
}
if(!wglDeleteContext(hRC))
{
MessageBox(NULL,"Release of Rendering Context failed","Shutdown Error", MB_OK | MB_ICONINFORMATION);
}
hRC = NULL;
}
if(hDC && !ReleaseDC(hWnd,hDC))
{
MessageBox(NULL,"Release of Device Context failed", "Shutdown Error", MB_OK | MB_ICONINFORMATION);
hDC = NULL;
}
if(hWnd && !DestroyWindow(hWnd))
{
MessageBox(NULL,"Release of WindowHandler failed", "Shutdown Error", MB_OK | MB_ICONINFORMATION);
hWnd = NULL;
}
if(!UnregisterClass("OpenGL", hInstance))
{
MessageBox(NULL,"Could not Unregister Class", "Shutdown Error", MB_OK | MB_ICONINFORMATION);
hInstance = NULL;
}
}
BOOL CreateGLWindow(char* title, int height, int width, int bits, bool fullscreenflag)
{
GLuint PixelFormat;
WNDCLASS wc;
DWORD dwExstyle;
DWORD dwstyle;
RECT WindowRect;
WindowRect.left=(long)0;
WindowRect.right=(long)width;
WindowRect.top=(long)0;
WindowRect.bottom=(long)height;
fullscreen = fullscreenflag;
hInstance = GetModuleHandle(NULL);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = "OpenGL";
if(!RegisterClass(&wc))
{
MessageBox(NULL,"Failed to register window class", "Error", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
if(fullscreen)
{
DEVMODE dmScreenSettings;
memset (&dmScreenSettings,0, sizeof(dmScreenSettings));
dmScreenSettings.dmSize=sizeof(dmScreenSettings);
dmScreenSettings.dmPelsWidth = width;
dmScreenSettings.dmPelsHeight = height;
dmScreenSettings.dmBitsPerPel = bits;
dmScreenSettings.dmFields= DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
if(ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) !=DISP_CHANGE_SUCCESSFUL)
{
if(MessageBox(NULL,"The requested fullscreen Mode is not supported by \n your Video card. use windowed mode instead?", "Error", MB_YESNO | MB_ICONEXCLAMATION) == IDYES)
{
fullscreen = FALSE;
}
else
{
MessageBox(NULL,"Program will now close","Erorr", MB_OK | MB_ICONSTOP);
return FALSE;
}
}
}
if(fullscreen)
{
dwExstyle = WS_EX_APPWINDOW;
dwstyle = WS_POPUP;
ShowCursor(FALSE);
}
else
{
dwExstyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
dwstyle = WS_OVERLAPPEDWINDOW;
}
AdjustWindowRectEx(&WindowRect, dwExstyle, FALSE, dwstyle);
if(!(hWnd = CreateWindowEx( dwExstyle,
"OpenGL",
title,
WS_CLIPSIBLINGS |
WS_CLIPCHILDREN |
dwstyle,
0, 0,
WindowRect.right- WindowRect.left,
WindowRect.bottom- WindowRect.top,
NULL,
NULL,
hInstance,
NULL)))
{
KillGLWindow();
MessageBox(NULL, "Window Creation Error", "Error", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
bits,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
16,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};
if(!(hDC=GetDC(hWnd)))
{
KillGLWindow();
MessageBox(NULL, "Can't Create a GL device context", "Error", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
if(!(PixelFormat = ChoosePixelFormat(hDC, &pfd)))
{
KillGLWindow();
MessageBox(NULL, "Can't find suitable PixelFormat", "Error", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
if(!SetPixelFormat(hDC, PixelFormat, &pfd))
{
KillGLWindow();
MessageBox(NULL, "Can't set the PixelFormat", "Error", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
if(!(hRC = wglCreateContext(hDC)))
{
KillGLWindow();
MessageBox(NULL, "Can't Create a rendereing context", "Error", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
if(!wglMakeCurrent(hDC, hRC))
{
KillGLWindow();
MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
ShowWindow(hWnd, SW_SHOW);
SetForegroundWindow(hWnd);
SetFocus(hWnd);
ReSizeGLScene(width, height);
if(!InitGL())
{
KillGLWindow();
MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
switch (uMsg)
{
case WM_ACTIVATE:
{
if(!HIWORD(wParam))
{
active = TRUE;
}
else
{
active = 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_KEYDOWN:
{
keys[wParam] = TRUE;
return 0;
}
case WM_KEYUP:
{
keys[wParam] = FALSE;
return 0;
}
case WM_SIZE:
{
ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));
return 0;
}
}
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
BOOL done = FALSE;
if(MessageBox(NULL, "Would you Like to run fullscreen mode?", "Start Fullscreen?", MB_YESNO | MB_ICONQUESTION) == IDNO)
{
fullscreen = FALSE;
}
if(!CreateGLWindow("Pete's", 640,480,16,fullscreen))
{
return 0;
}
while(!done)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if(msg.message==WM_QUIT)
{
done = TRUE;
}
else
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
else
{
if(active)
{
if(keys[VK_ESCAPE])
{
done = TRUE;
}
else
{
DrawGLScene();
SwapBuffers(hDC);
}
}
if(keys[VK_F1])
{
keys[VK_F1] = FALSE;
KillGLWindow();
fullscreen=!fullscreen;
if(!CreateGLWindow("Pete's", 640,480,16,fullscreen))
{
return 0;
}
}
}
}
KillGLWindow();
return(msg.wParam);
}
Hi, i get alot of Errors (
Hi I am very new to OpenGL and i just started at lesson 1
and basicly writing the content into the compiler as I am reading.
(http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=01)
Anyway when i compile I get up would you like to run in Windowed mode? yes/no, and when i click yes The Error box i made pops up "The requested fullscreen mode is not supported by your graphic card, use windowed mode instead?" So i Press yes and it transform into windowedmode. Then if i want to quit by pressing the x in the top right corner of my window I get another error box, "Release of DC and RC failed."
Here is my code:
Im not overly familiar with Win32 code, so I can't comment on the code.
However I assume the code works well for everyone else who uses the tutorial.
The only thing I can think of is to update your graphics card drivers.
However I assume the code works well for everyone else who uses the tutorial.
The only thing I can think of is to update your graphics card drivers.
Well, thats just the weird thing, because when i change the settings and/or I run the code when i just copy paste All works fine
I doubt the video card isn't able to render 640*480*16 even with Windows basic drivers. The line who is causing error here is:
if(ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) !=DISP_CHANGE_SUCCESSFUL)
What you should do is put a breakpoint there in visual studio (or any ide your using) and check if dmScreenSettings is filled up correctly using the watch window. If it is, you should check what the function return doing somethign like that:
long test = 0;
test = ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN);
and check the value of test. Once you got it, check this page
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/devcons_7gz7.asp
and in the return value section of this page you see every possible values test can take, and what it mean.
if(ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) !=DISP_CHANGE_SUCCESSFUL)
What you should do is put a breakpoint there in visual studio (or any ide your using) and check if dmScreenSettings is filled up correctly using the watch window. If it is, you should check what the function return doing somethign like that:
long test = 0;
test = ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN);
and check the value of test. Once you got it, check this page
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/devcons_7gz7.asp
and in the return value section of this page you see every possible values test can take, and what it mean.
Thank you, but as I said in the first post I am really new to OpenGL, so I'm not quite sure what you mean by all that, could you be so kind and break it into steps for me, what I shall do first, second etc, etc^^:D?
Thanks in Advance
Thanks in Advance
Well, this is something to do with basic C debugging and using help, not OpenGL.
You can know this is the line who gone wrong because the message that pop-up is the one in the if brackets, saying that the ChangeDisplaySettings didn't return DISP_CHANGE_SUCCESSFUL as you see in this part of the code:
So assuming you are using Visual Studio, the way to put a breakpoint is really simple, just click on the left part of the line (or select it and press F9) and a red circle will appear next to the line. Now next time you run the program (F5 or play button), when it will step on this line the program will stop his execution at this line.
Now at this point in the code, the dmScreenSettings struct is suposed to be filled with parameters (size, resolution, bpp, flags). Usually when visual studio break somewhere, you have a windows at the bottom of the IDE called "watch". In this windows, you can type the name of the variable you want to see the value (in this case, dmScreenSettings) and you should see all variables inside the struct and what are the values, and check if it's something who make sense, if it doesn't the problem is in the lines before that.
For the rest (checking what the function return since it's obviously something else than DISP_CHANGE_SUCCESSFUL), I think i'm pretty clear in my previous post. Just do the same thing with the "test" variable than what you already did with "dmScreenSettings" (put the breakpoint AFTER the ChangeDisplaySettings line so it get executed).
If you can't understand that, I suggest you go take C courses before entering OpenGL world :)
You can know this is the line who gone wrong because the message that pop-up is the one in the if brackets, saying that the ChangeDisplaySettings didn't return DISP_CHANGE_SUCCESSFUL as you see in this part of the code:
if(ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) !=DISP_CHANGE_SUCCESSFUL){[...] MessageBox(NULL,"The requested fullscreen Mode is not supported by \n your Video card. use windowed mode instead?", "Error", MB_YESNO | MB_ICONEXCLAMATION);[...]}
So assuming you are using Visual Studio, the way to put a breakpoint is really simple, just click on the left part of the line (or select it and press F9) and a red circle will appear next to the line. Now next time you run the program (F5 or play button), when it will step on this line the program will stop his execution at this line.
Now at this point in the code, the dmScreenSettings struct is suposed to be filled with parameters (size, resolution, bpp, flags). Usually when visual studio break somewhere, you have a windows at the bottom of the IDE called "watch". In this windows, you can type the name of the variable you want to see the value (in this case, dmScreenSettings) and you should see all variables inside the struct and what are the values, and check if it's something who make sense, if it doesn't the problem is in the lines before that.
For the rest (checking what the function return since it's obviously something else than DISP_CHANGE_SUCCESSFUL), I think i'm pretty clear in my previous post. Just do the same thing with the "test" variable than what you already did with "dmScreenSettings" (put the breakpoint AFTER the ChangeDisplaySettings line so it get executed).
If you can't understand that, I suggest you go take C courses before entering OpenGL world :)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement