death_oclock 103 Posting Whiz

I am trying to write an openGL application from a tutorial I found and tried to copy it as accurately as possible without just copy+pasting it, so I can actually learn the code. The source file from the tutorial compiles and runs perfectly, but my code fails at a call of CreateWindowEx().
Here is just that code:

hWnd = CreateWindowEx(dwExStyle,
			lpszClassName,
			title,
			dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
			0,
			0,
			WindowRect.right - WindowRect.left,			WindowRect.bottom - WindowRect.top,
			NULL,
			NULL,
			hInstance,
			NULL);
	
if(!hWnd)
{
	KillGLWindow();
	MessageBox(NULL, "Failed to create window.", "Error!", MB_ICONEXCLAMATION | MB_OK);
	return FALSE;
}

It always call the message box.

Here is the entire code:

#include <windows.h>
#include <GL/glu.h>
#include <GL/gl.h>

HDC hDC = NULL;
HGLRC hRC = NULL;
HWND hWnd = NULL;
HINSTANCE hInstance;
LPSTR lpszClassName = "WndClass";

bool keys[256];
bool active = true;
bool fullscreen = true;

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

GLvoid ResizeGLScene(GLsizei width, GLsizei height)
{
	if(height == 0)
	{
		height = 1;
	}
	
	glViewport(0, 0, width, height);
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	
	// Calculate aspect ratio
	gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);
	
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

int InitGL()
{
	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); // 2nd arg: (GL_NICEST | GL_FASTEST)
	
	return TRUE;
}

int DrawGLScene()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	return TRUE;
}

GLvoid KillGLWindow()
{
	if(fullscreen)
	{
		ChangeDisplaySettings(NULL, 0);
		ShowCursor(true);
	}
	
	if(hRC)
	{
		if(!wglMakeCurrent(NULL, NULL))
		{
			MessageBox(NULL, "Failed to release DC and RC.", "Error!", MB_ICONEXCLAMATION | MB_OK);
		}
		
		if(!wglDeleteContext(hRC))
		{
			MessageBox(NULL, "Failed to delete RC.", "Error!", MB_ICONEXCLAMATION | MB_OK);
		}
		
		hRC = NULL;
	}
	
	if(hDC && !ReleaseDC(hWnd, hDC))
	{
		MessageBox(NULL, "Failed to release DC.", "Error!", MB_ICONEXCLAMATION | MB_OK);
		hDC = NULL;
	}
	
	if(hWnd && !DestroyWindow(hWnd))
	{
		MessageBox(NULL, "Failed to destroy window.", "Error!", MB_ICONEXCLAMATION | MB_OK);
		hWnd = NULL;
	}
	
	if(!UnregisterClass(lpszClassName, hInstance))
	{
		MessageBox(NULL, "Failed to unregister window class.", "Error!", MB_ICONEXCLAMATION | MB_OK);
		hInstance = NULL;
	}
}

bool CreateGLWindow(char *title, int width, int height, 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);
	if(!hInstance)
	{
		MessageBox(NULL, "Failed to get module handle.", "Error!", MB_ICONEXCLAMATION | MB_OK);
		return FALSE;
	}
	
	wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra =  0;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = NULL;
	wc.lpszMenuName = NULL;
	wc.lpszClassName = lpszClassName;
	
	if(!RegisterClass(&wc))
	{
		MessageBox(NULL, "Failed to register window class.", "Error!", MB_ICONEXCLAMATION | MB_OK);
		return FALSE;
	}
	
	if(fullscreen)
	{
		DEVMODE dmode;
		memset(&dmode, 0, sizeof(dmode));
		dmode.dmSize = sizeof(dmode);
		dmode.dmPelsWidth = width;
		dmode.dmPelsHeight = height;
		dmode.dmBitsPerPel = bits;
		dmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;
		
		if(ChangeDisplaySettings(&dmode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
		{
			if(MessageBox(NULL, "The requested fullscreen mode is not supported by\nyour video card. Use windowed mode instead?", title, MB_ICONEXCLAMATION | MB_YESNO) == IDYES)
			{
				fullscreen = false;
			}
			else
			{
				MessageBox(NULL, "Program will now close.", title, MB_ICONEXCLAMATION | MB_OK);
				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, dwStyle, FALSE, dwExStyle);
	
	hWnd = CreateWindowEx(dwExStyle,
						  lpszClassName,
						  title,
						  dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
						  0,
						  0,
						  WindowRect.right - WindowRect.left,
						  WindowRect.bottom - WindowRect.top,
						  NULL,
						  NULL,
						  hInstance,
						  NULL);
	
	if(!hWnd)
	{
		KillGLWindow();
		MessageBox(NULL, "Failed to create window.", "Error!", MB_ICONEXCLAMATION | MB_OK);
		return FALSE;
	}
	
	static PIXELFORMATDESCRIPTOR pfd;
	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 = bits;
	pfd.cDepthBits = 16;
	pfd.iLayerType = PFD_MAIN_PLANE;
	
	hDC = GetDC(hWnd);
	if(!hDC)
	{
		KillGLWindow();
		MessageBox(NULL, "Failed to get device context.", "Error!", MB_ICONEXCLAMATION | MB_OK);
		return FALSE;
	}
	
	PixelFormat = ChoosePixelFormat(hDC, &pfd);
	if(!PixelFormat)
	{
		KillGLWindow();
		MessageBox(NULL, "Failed to find pixel format.", "Error!", MB_ICONEXCLAMATION | MB_OK);
		return FALSE;
	}
	
	if(!SetPixelFormat(hDC, PixelFormat, &pfd))
	{
		KillGLWindow();
		MessageBox(NULL, "Failed to set pixel format.", "Error!", MB_ICONEXCLAMATION | MB_OK);
		return FALSE;
	}
	
	hRC = wglCreateContext(hDC);
	if(!hRC)
	{
		KillGLWindow();
		MessageBox(NULL, "Failed to create rendering context.", "Error!", MB_ICONEXCLAMATION | MB_OK);
		return FALSE;
	}
	
	if(!wglMakeCurrent(hDC, hRC))
	{
		KillGLWindow();
		MessageBox(NULL, "Failed to activate rendering context.", "Error!", MB_ICONEXCLAMATION | MB_OK);
		return FALSE;
	}
	
	ShowWindow(hWnd, SW_SHOW);
	SetForegroundWindow(hWnd);
	SetFocus(hWnd);
	ResizeGLScene(width, height);
	
	if(!InitGL())
	{
		KillGLWindow();
		MessageBox(NULL, "Failed to inititalize GL window", "Error!", MB_ICONEXCLAMATION | MB_OK);
		return FALSE;
	}
}

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 quit = FALSE;
    /*
	if(MessageBox(NULL, "Would you like to run in fullscreen mode?", "Start FullScreen?", MB_YESNO | MB_ICONQUESTION) == IDNO)
	{
		fullscreen=FALSE;							// Windowed Mode
	}
	*/
	if(!CreateGLWindow("OpenGl Window", 640, 480, 16, fullscreen))
	{
		return 0;
	}
	
	while(!quit)
	{
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
			{
				quit = TRUE;
			}
			else
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}
		else
		{
			if(active)
			{
				if(keys[VK_ESCAPE])
				{
					quit = true;
				}
				else
				{
					DrawGLScene();
					SwapBuffers(hDC);
				}
			}
			
			if(keys[VK_F1])
			{
				keys[VK_F1] = FALSE;
				KillGLWindow();
				fullscreen = !fullscreen;
				if(!CreateGLWindow("OpenGL Window", 1024, 768, 16, fullscreen))
				{
					return 0;
				}
			}
		}
	}
	
	KillGLWindow();
	return msg.wParam;
}

The tutorial is here: http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=01
I can't for the life of me figure out what is different (that would cause this error).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.