1 /*
2 *		This Code Was Created By Jeff Molofee 2000
3 *		A HUGE Thanks To Fredric Echols For Cleaning Up
4 *		And Optimizing This Code, Making It More Flexible!
5 *		If You've Found This Code Useful, Please Let Me Know.
6 *		Visit My Site At nehe.gamedev.net
7 */
8 
9 #include <windows.h>		// Header File For Windows
10 #include <gl\gl.h>			// Header File For The OpenGL32 Library
11 #include <gl\glu.h>			// Header File For The GLu32 Library
12 
13 HDC			hDC=NULL;		// Private GDI Device Context
14 HGLRC		hRC=NULL;		// Permanent Rendering Context
15 HWND		hWnd=NULL;		// Holds Our Window Handle
16 HINSTANCE	hInstance;		// Holds The Instance Of The Application
17 int			windowWidth = 0, windowHeight = 0;
18 
19 bool	keys[256];			// Array Used For The Keyboard Routine
20 bool	active=TRUE;		// Window Active Flag Set To TRUE By Default
21 bool	fullscreen=TRUE;	// Fullscreen Flag Set To Fullscreen Mode By Default
22 
MessageBoxPrivate(HWND hWnd,const char * pMessage,const char * pTitle,int flags)23 static void MessageBoxPrivate( HWND hWnd, const char* pMessage, const char* pTitle, int flags )
24 {
25 	printf( "%s: %s\n", pTitle, pMessage );
26 }
27 
28 LRESULT	CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);	// Declaration For WndProc
29 
ReSizeGLScene(GLsizei width,GLsizei height)30 GLvoid ReSizeGLScene(GLsizei width, GLsizei height)		// Resize And Initialize The GL Window
31 {
32 	if (height==0)										// Prevent A Divide By Zero By
33 	{
34 		height=1;										// Making Height Equal One
35 	}
36 
37 	glViewport(0,0,width,height);						// Reset The Current Viewport
38 
39 	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
40 	glLoadIdentity();									// Reset The Projection Matrix
41 
42 	// Calculate The Aspect Ratio Of The Window
43 	gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
44 
45 	glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
46 	glLoadIdentity();									// Reset The Modelview Matrix
47 }
48 
InitGL(GLvoid)49 int InitGL(GLvoid)										// All Setup For OpenGL Goes Here
50 {
51 	glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
52 	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);				// Black Background
53 	glClearDepth(1.0f);									// Depth Buffer Setup
54 	glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
55 	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
56 	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations
57 	return TRUE;										// Initialization Went OK
58 }
59 
DrawGLScene(GLvoid)60 int DrawGLScene(GLvoid)									// Here's Where We Do All The Drawing
61 {
62 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer
63 	glLoadIdentity();									// Reset The Current Modelview Matrix
64 	return TRUE;										// Everything Went OK
65 }
66 
KillGLWindow(GLvoid)67 GLvoid KillGLWindow(GLvoid)								// Properly Kill The Window
68 {
69 	if (fullscreen)										// Are We In Fullscreen Mode?
70 	{
71 		ChangeDisplaySettings(NULL,0);					// If So Switch Back To The Desktop
72 		ShowCursor(TRUE);								// Show Mouse Pointer
73 	}
74 
75 	if (hRC)											// Do We Have A Rendering Context?
76 	{
77 		if (!wglMakeCurrent(NULL,NULL))					// Are We Able To Release The DC And RC Contexts?
78 		{
79 			MessageBoxPrivate(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
80 		}
81 
82 		if (!wglDeleteContext(hRC))						// Are We Able To Delete The RC?
83 		{
84 			MessageBoxPrivate(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
85 		}
86 		hRC=NULL;										// Set RC To NULL
87 	}
88 
89 	if (hDC && !ReleaseDC(hWnd,hDC))					// Are We Able To Release The DC
90 	{
91 		MessageBoxPrivate(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
92 		hDC=NULL;										// Set DC To NULL
93 	}
94 
95 	if (hWnd && !DestroyWindow(hWnd))					// Are We Able To Destroy The Window?
96 	{
97 		MessageBoxPrivate(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
98 		hWnd=NULL;										// Set hWnd To NULL
99 	}
100 
101 	if (!UnregisterClass((LPCWSTR)L"OpenGL",hInstance))			// Are We Able To Unregister Class
102 	{
103 		MessageBoxPrivate(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
104 		hInstance=NULL;									// Set hInstance To NULL
105 	}
106 }
107 
108 /*	This Code Creates Our OpenGL Window.  Parameters Are:					*
109 *	title			- Title To Appear At The Top Of The Window				*
110 *	width			- Width Of The GL Window Or Fullscreen Mode				*
111 *	height			- Height Of The GL Window Or Fullscreen Mode			*
112 *	bits			- Number Of Bits To Use For Color (8/16/24/32)			*
113 *	fullscreenflag	- Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE)	*/
114 
CreateGLWindow(char * title,int width,int height,int bits,bool fullscreenflag,bool hideWindow=true)115 BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag, bool hideWindow=true)
116 {
117 	GLuint		PixelFormat;			// Holds The Results After Searching For A Match
118 	WNDCLASS	wc;						// Windows Class Structure
119 	DWORD		dwExStyle;				// Window Extended Style
120 	DWORD		dwStyle;				// Window Style
121 	RECT		WindowRect;				// Grabs Rectangle Upper Left / Lower Right Values
122 	WindowRect.left=(long)0;			// Set Left Value To 0
123 	WindowRect.right=(long)width;		// Set Right Value To Requested Width
124 	WindowRect.top=(long)0;				// Set Top Value To 0
125 	WindowRect.bottom=(long)height;		// Set Bottom Value To Requested Height
126 
127 	fullscreen=fullscreenflag;			// Set The Global Fullscreen Flag
128 
129 	hInstance			= GetModuleHandle(NULL);				// Grab An Instance For Our Window
130 	wc.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;	// Redraw On Size, And Own DC For Window.
131 	wc.lpfnWndProc		= (WNDPROC) WndProc;					// WndProc Handles Messages
132 	wc.cbClsExtra		= 0;									// No Extra Window Data
133 	wc.cbWndExtra		= 0;									// No Extra Window Data
134 	wc.hInstance		= hInstance;							// Set The Instance
135 	wc.hIcon			= LoadIcon(NULL, IDI_WINLOGO);			// Load The Default Icon
136 	wc.hCursor			= LoadCursor(NULL, IDC_ARROW);			// Load The Arrow Pointer
137 	wc.hbrBackground	= NULL;									// No Background Required For GL
138 	wc.lpszMenuName		= NULL;									// We Don't Want A Menu
139 	wc.lpszClassName	= L"OpenGL";							// Set The Class Name
140 
141 	if (!RegisterClass(&wc))									// Attempt To Register The Window Class
142 	{
143 		MessageBoxPrivate(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
144 		return FALSE;											// Return FALSE
145 	}
146 
147 	if (fullscreen)												// Attempt Fullscreen Mode?
148 	{
149 		DEVMODE dmScreenSettings;								// Device Mode
150 		memset(&dmScreenSettings,0,sizeof(dmScreenSettings));	// Makes Sure Memory's Cleared
151 		dmScreenSettings.dmSize=sizeof(dmScreenSettings);		// Size Of The Devmode Structure
152 		dmScreenSettings.dmPelsWidth	= width;				// Selected Screen Width
153 		dmScreenSettings.dmPelsHeight	= height;				// Selected Screen Height
154 		dmScreenSettings.dmBitsPerPel	= bits;					// Selected Bits Per Pixel
155 		dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
156 
157 		// Try To Set Selected Mode And Get Results.  NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
158 		if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
159 		{
160 			/*
161 			// If The Mode Fails, Offer Two Options.  Quit Or Use Windowed Mode.
162 			if (MessageBoxPrivate(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
163 			{
164 			fullscreen=FALSE;		// Windowed Mode Selected.  Fullscreen = FALSE
165 			}
166 			else
167 			{
168 			// Pop Up A Message Box Letting User Know The Program Is Closing.
169 			MessageBoxPrivate(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
170 			return FALSE;									// Return FALSE
171 			}
172 			*/
173 			fullscreen=FALSE;
174 		}
175 	}
176 
177 	if (fullscreen)												// Are We Still In Fullscreen Mode?
178 	{
179 		dwExStyle=WS_EX_APPWINDOW;								// Window Extended Style
180 		dwStyle=WS_POPUP;										// Windows Style
181 		ShowCursor(FALSE);										// Hide Mouse Pointer
182 	}
183 	else
184 	{
185 		dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;			// Window Extended Style
186 		dwStyle=WS_OVERLAPPEDWINDOW;							// Windows Style
187 	}
188 
189 	AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);		// Adjust Window To True Requested Size
190 
191 	// Create The Window
192 	if (!(hWnd=CreateWindowEx(	dwExStyle,							// Extended Style For The Window
193 		L"OpenGL",							// Class Name
194 		L"DXTCompress",						// Window Title
195 		dwStyle |							// Defined Window Style
196 		WS_CLIPSIBLINGS |					// Required Window Style
197 		WS_CLIPCHILDREN,					// Required Window Style
198 		0, 0,								// Window Position
199 		WindowRect.right-WindowRect.left,	// Calculate Window Width
200 		WindowRect.bottom-WindowRect.top,	// Calculate Window Height
201 		NULL,								// No Parent Window
202 		NULL,								// No Menu
203 		hInstance,							// Instance
204 		NULL)))								// Dont Pass Anything To WM_CREATE
205 	{
206 		KillGLWindow();								// Reset The Display
207 		MessageBoxPrivate(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
208 		return FALSE;								// Return FALSE
209 	}
210 
211 	static	PIXELFORMATDESCRIPTOR pfd=				// pfd Tells Windows How We Want Things To Be
212 	{
213 		sizeof(PIXELFORMATDESCRIPTOR),				// Size Of This Pixel Format Descriptor
214 		1,											// Version Number
215 		PFD_DRAW_TO_WINDOW |						// Format Must Support Window
216 		PFD_SUPPORT_OPENGL |						// Format Must Support OpenGL
217 		PFD_DOUBLEBUFFER,							// Must Support Double Buffering
218 		PFD_TYPE_RGBA,								// Request An RGBA Format
219 		bits,										// Select Our Color Depth
220 		0, 0, 0, 0, 0, 0,							// Color Bits Ignored
221 		0,											// No Alpha Buffer
222 		0,											// Shift Bit Ignored
223 		0,											// No Accumulation Buffer
224 		0, 0, 0, 0,									// Accumulation Bits Ignored
225 		16,											// 16Bit Z-Buffer (Depth Buffer)
226 		0,											// No Stencil Buffer
227 		0,											// No Auxiliary Buffer
228 		PFD_MAIN_PLANE,								// Main Drawing Layer
229 		0,											// Reserved
230 		0, 0, 0										// Layer Masks Ignored
231 	};
232 
233 	if (!(hDC=GetDC(hWnd)))							// Did We Get A Device Context?
234 	{
235 		KillGLWindow();								// Reset The Display
236 		MessageBoxPrivate(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
237 		return FALSE;								// Return FALSE
238 	}
239 
240 	if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd)))	// Did Windows Find A Matching Pixel Format?
241 	{
242 		KillGLWindow();								// Reset The Display
243 		MessageBoxPrivate(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
244 		return FALSE;								// Return FALSE
245 	}
246 
247 	if(!SetPixelFormat(hDC,PixelFormat,&pfd))		// Are We Able To Set The Pixel Format?
248 	{
249 		KillGLWindow();								// Reset The Display
250 		MessageBoxPrivate(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
251 		return FALSE;								// Return FALSE
252 	}
253 
254 	if (!(hRC=wglCreateContext(hDC)))				// Are We Able To Get A Rendering Context?
255 	{
256 		KillGLWindow();								// Reset The Display
257 		MessageBoxPrivate(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
258 		return FALSE;								// Return FALSE
259 	}
260 
261 	if(!wglMakeCurrent(hDC,hRC))					// Try To Activate The Rendering Context
262 	{
263 		KillGLWindow();								// Reset The Display
264 		MessageBoxPrivate(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
265 		return FALSE;								// Return FALSE
266 	}
267 
268 	if( hideWindow )
269 		ShowWindow(hWnd,SW_HIDE);						// Show The Window
270 	else
271 		ShowWindow(hWnd,SW_SHOW);						// Show The Window
272 	SetForegroundWindow(hWnd);						// Slightly Higher Priority
273 	SetFocus(hWnd);									// Sets Keyboard Focus To The Window
274 	ReSizeGLScene(width, height);					// Set Up Our Perspective GL Screen
275 
276 	if (!InitGL())									// Initialize Our Newly Created GL Window
277 	{
278 		KillGLWindow();								// Reset The Display
279 		MessageBoxPrivate(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
280 		return FALSE;								// Return FALSE
281 	}
282 
283 	return TRUE;									// Success
284 }
285 
WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)286 LRESULT CALLBACK WndProc(	HWND	hWnd,			// Handle For This Window
287 						 UINT	uMsg,			// Message For This Window
288 						 WPARAM	wParam,			// Additional Message Information
289 						 LPARAM	lParam)			// Additional Message Information
290 {
291 	switch (uMsg)									// Check For Windows Messages
292 	{
293 	case WM_ACTIVATE:							// Watch For Window Activate Message
294 		{
295 			if (!HIWORD(wParam))					// Check Minimization State
296 			{
297 				active=TRUE;						// Program Is Active
298 			}
299 			else
300 			{
301 				active=FALSE;						// Program Is No Longer Active
302 			}
303 
304 			return 0;								// Return To The Message Loop
305 		}
306 
307 	case WM_SYSCOMMAND:							// Intercept System Commands
308 		{
309 			switch (wParam)							// Check System Calls
310 			{
311 			case SC_SCREENSAVE:					// Screensaver Trying To Start?
312 			case SC_MONITORPOWER:				// Monitor Trying To Enter Powersave?
313 				return 0;							// Prevent From Happening
314 			}
315 			break;									// Exit
316 		}
317 
318 	case WM_CLOSE:								// Did We Receive A Close Message?
319 		{
320 			PostQuitMessage(0);						// Send A Quit Message
321 			return 0;								// Jump Back
322 		}
323 
324 	case WM_KEYDOWN:							// Is A Key Being Held Down?
325 		{
326 			keys[wParam] = TRUE;					// If So, Mark It As TRUE
327 			return 0;								// Jump Back
328 		}
329 
330 	case WM_KEYUP:								// Has A Key Been Released?
331 		{
332 			keys[wParam] = FALSE;					// If So, Mark It As FALSE
333 			return 0;								// Jump Back
334 		}
335 
336 	case WM_SIZE:								// Resize The OpenGL Window
337 		{
338 			windowWidth = LOWORD(lParam);
339 			windowHeight = HIWORD(lParam);
340 			ReSizeGLScene(windowWidth, windowHeight);  // LoWord=Width, HiWord=Height
341 			return 0;								// Jump Back
342 		}
343 	}
344 
345 	// Pass All Unhandled Messages To DefWindowProc
346 	return DefWindowProc(hWnd,uMsg,wParam,lParam);
347 }
348