//~~~~~~~~~~~~~~~~ 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 && !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 && !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 (&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 (&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(&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 (&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, &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, &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 (&p_msg, NULL, 0,0, PM_REMOVE)) {
if (p_msg.message == WM_QUIT)
done = TRUE;
else {
TranslateMessage (&p_msg);
DispatchMessage (&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
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..
i dled the code from nehe to see it that would work, and it does..i really dont see whats wrong with my code..
thanks
annoying bug
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 ), then could you just look at the code? thanks a lot
Did you try compiling the copy you downloaded?
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
That is because of the WM_ parameters to createwindowex()
You must specify that its WM_POPUP or something like that.
You must specify that its WM_POPUP or something like that.
--What are you nutz?I have nothing to say to your unevolved little brain. The more I say gives you more weapons to ask stupid questions.
so i fixed a few typos taht i found..but regarding the WS_POPUP it is set already if the window goes fullscreen..(am i wrong??)..
smart_idiot (<- lol ) : yea i did compile the copy straight from nehe and it works..i compared them line by line and (besides the small stuff i changed to my liking, it does the same thing...i musta missed something....everything else works nicely, but i cant do anything with the window itself(no moving, resizing...all i can get is the menu when i right click on the titlebar, but all the functions from the menu dontdo anything)
smart_idiot (<- lol ) : yea i did compile the copy straight from nehe and it works..i compared them line by line and (besides the small stuff i changed to my liking, it does the same thing...i musta missed something....everything else works nicely, but i cant do anything with the window itself(no moving, resizing...all i can get is the menu when i right click on the titlebar, but all the functions from the menu dontdo anything)
i just checked again, and i did a line by line comparison..i dont get it..i guess i am letting something stupid go by..i know i said this wasn''t important, but i mean someone has to have time to waste
thanks
thanks
what the...??? alright i found the (weird!) problem:
in my wndproc, instead of having:
i alwasy thought i could have the default part..i guess not.. ohh well
in my wndproc, instead of having:
default: return DefWindowProc (hWnd, Msg, wParam, lParam);//i have to omit the default part and do this:return DefWind...
i alwasy thought i could have the default part..i guess not.. ohh well
just a note, you should check out WinMerge, it''s a handy app that makes compairing two files (in this case, the nehe one and yours) line by line. It will hilight any differences. WinMerge is a godsend!
Cheers,
D
Cheers,
D
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement