TinyGL 0.4.1 for MinGW
mingw32.c
Go to the documentation of this file.
1/*
2 * Demonstration program for Win32 GDI graphics.
3 *
4 * @orignal
5 *
6 * OpenGL Legacy Tutorials : Lesson 01
7 *
8 * This Code Was Created By Jeff Molofee 2000
9 * A HUGE Thanks To Fredric Echols For Cleaning Up
10 * And Optimizing This Code, Making It More Flexible!
11 * If You've Found This Code Useful, Please Let Me Know.
12 * Visit My Site At nehe.gamedev.net
13 *
14 * @modified 2017-Aug-11
15 * @author Atmarkartworks t.m
16 *
17 */
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21
22#undef UNICODE
23#undef _UNICODE
24
25#include <windows.h>
26
27#include <GL/gl.h>
28#include <GL/gdi.h>
29
30#include "ui.h"
31
32
33
34/* ----------------------------------------------------------------------------------------------------------------------------- */
35HDC hDC=NULL; // Private GDI Device Context
36HWND hWnd=NULL; // Holds Our Window Handle
37HINSTANCE hInstance; // Holds The Instance Of The Application
39
40
41BOOL keys[256]; // Array Used For The Keyboard Routine
42BOOL active=TRUE; // Window Active Flag Set To TRUE By Default
43BOOL fullscreen=TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default
44
45LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc s
46
47#define GLvoid void
48
49
50GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
51{
52 if (height==0) // Prevent A Divide By Zero By
53 {
54 height=1; // Making Height Equal One
55 }
56
57 reshape(width, height);
58}
59
60int InitGL(GLvoid) // All Setup For OpenGL Goes Here
61{
62 init();
63
64 return TRUE; // Initialization Went OK
65}
66
67int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
68{
69 draw();
70
71 return TRUE;
72}
73
74GLvoid KillGLWindow(GLvoid) // Properly Kill The Window
75{
76 if (fullscreen) // Are We In Fullscreen Mode?
77 {
78 ChangeDisplaySettings(NULL,0); // If So Switch Back To The Desktop
79 ShowCursor(TRUE); // Show Mouse Pointer
80 }
81
82
83 if (hDC && !ReleaseDC(hWnd,hDC)) // Are We Able To Release The DC
84 {
85 MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
86 hDC=NULL; // Set DC To NULL
87 }
88
89 if (hWnd && !DestroyWindow(hWnd)) // Are We Able To Destroy The Window?
90 {
91 MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
92 hWnd=NULL; // Set hWnd To NULL
93 }
94
95 if (!UnregisterClass("OpenGL",hInstance)) // Are We Able To Unregister Class
96 {
97 MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
98 hInstance=NULL; // Set hInstance To NULL
99 }
100}
101
102/* This Code Creates Our OpenGL Window. Parameters Are: *
103 * title - Title To Appear At The Top Of The Window *
104 * width - Width Of The GL Window Or Fullscreen Mode *
105 * height - Height Of The GL Window Or Fullscreen Mode *
106 * bits - Number Of Bits To Use For Color (8/16/24/32) *
107 * fullscreenflag - Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE) */
108
109BOOL CreateGLWindow(char* title, int width, int height, int bits, BOOL fullscreenflag)
110{
111
112 GLuint PixelFormat; // Holds The Results After Searching For A Match
113 WNDCLASS wc; // Windows Class Structure
114 DWORD dwExStyle; // Window Extended Style
115 DWORD dwStyle; // Window Style
116 RECT WindowRect; // Grabs Rectangle Upper Left / Lower Right Values
117 WindowRect.left=(long)0; // Set Left Value To 0
118 WindowRect.right=(long)width; // Set Right Value To Requested Width
119 WindowRect.top=(long)0; // Set Top Value To 0
120 WindowRect.bottom=(long)height; // Set Bottom Value To Requested Height
121
122 fullscreen=fullscreenflag; // Set The Global Fullscreen Flag
123
124 hInstance = GetModuleHandle(NULL); // Grab An Instance For Our Window
125 wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Size, And Own DC For Window.
126 wc.lpfnWndProc = (WNDPROC) WndProc; // WndProc Handles Messages
127 wc.cbClsExtra = 0; // No Extra Window Data
128 wc.cbWndExtra = 0; // No Extra Window Data
129 wc.hInstance = hInstance; // Set The Instance
130 wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Load The Default Icon
131 wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer
132 wc.hbrBackground = NULL; // No Background Required For GL
133 wc.lpszMenuName = NULL; // We Don't Want A Menu
134 wc.lpszClassName = "OpenGL"; // Set The Class Name
135
136 if (!RegisterClass(&wc)) // Attempt To Register The Window Class
137 {
138 MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
139 return FALSE; // Return FALSE
140 }
141
142 if (fullscreen) // Attempt Fullscreen Mode?
143 {
144 DEVMODE dmScreenSettings; // Device Mode
145 memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); // Makes Sure Memory's Cleared
146 dmScreenSettings.dmSize=sizeof(dmScreenSettings); // Size Of The Devmode Structure
147 dmScreenSettings.dmPelsWidth = width; // Selected Screen Width
148 dmScreenSettings.dmPelsHeight = height; // Selected Screen Height
149 dmScreenSettings.dmBitsPerPel = bits; // Selected Bits Per Pixel
150 dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
151
152 // Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
153 if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
154 {
155 // If The Mode Fails, Offer Two Options. Quit Or Use Windowed Mode.
156 if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
157 {
158 fullscreen=FALSE; // Windowed Mode Selected. Fullscreen = FALSE
159 }
160 else
161 {
162 // Pop Up A Message Box Letting User Know The Program Is Closing.
163 MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
164 return FALSE; // Return FALSE
165 }
166 }
167 }
168
169 if (fullscreen) // Are We Still In Fullscreen Mode?
170 {
171 dwExStyle=WS_EX_APPWINDOW; // Window Extended Style
172 dwStyle=WS_POPUP; // Windows Style
173 ShowCursor(FALSE); // Hide Mouse Pointer
174 }
175 else
176 {
177 dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style
178 dwStyle=WS_OVERLAPPEDWINDOW; // Windows Style
179 }
180
181 AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size
182
183 // Create The Window
184 if (!(hWnd=CreateWindowEx( dwExStyle, // Extended Style For The Window
185 "OpenGL", // Class Name
186 title, // Window Title
187 dwStyle | // Defined Window Style
188 WS_CLIPSIBLINGS | // Required Window Style
189 WS_CLIPCHILDREN, // Required Window Style
190 0, 0, // Window Position
191 WindowRect.right-WindowRect.left, // Calculate Window Width
192 WindowRect.bottom-WindowRect.top, // Calculate Window Height
193 NULL, // No Parent Window
194 NULL, // No Menu
195 hInstance, // Instance
196 NULL))) // Dont Pass Anything To WM_CREATE
197 {
198 KillGLWindow(); // Reset The Display
199 MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
200 return FALSE; // Return FALSE
201 }
202
203
204 if (!(hDC=GetDC(hWnd))) // Did We Get A Device Context?
205 {
206 KillGLWindow(); // Reset The Display
207 MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
208 return FALSE; // Return FALSE
209 }
210
211 if (!(cx=gdiCreateContext(NULL, 0))) // Are We Able To Get A Rendering Context?
212 {
213 KillGLWindow(); // Reset The Display
214 MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
215 return FALSE; // Return FALSE
216 }
217
218 if(!gdiMakeCurrent(hWnd,cx, width, height)) // Try To Activate The Rendering Context
219 {
220 KillGLWindow(); // Reset The Display
221 MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
222 return FALSE; // Return FALSE
223 }
224
225 ShowWindow(hWnd,SW_SHOW); // Show The Window
226 SetForegroundWindow(hWnd); // Slightly Higher Priority
227 SetFocus(hWnd); // Sets Keyboard Focus To The Window
228 ReSizeGLScene(width, height); // Set Up Our Perspective GL Screen
229
230 if (!InitGL()) // Initialize Our Newly Created GL Window
231 {
232 KillGLWindow(); // Reset The Display
233 MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
234 return FALSE; // Return FALSE
235 }
236
237 return TRUE; // Success
238}
239
240LRESULT CALLBACK WndProc( HWND hWnd, // Handle For This Window
241 UINT uMsg, // Message For This Window
242 WPARAM wParam, // Additional Message Information
243 LPARAM lParam) // Additional Message Information
244{
245
246 switch (uMsg) // Check For Windows Messages
247 {
248 case WM_ACTIVATE: // Watch For Window Activate Message
249 {
250 if (!HIWORD(wParam)) // Check Minimization State
251 {
252 active=TRUE; // Program Is Active
253
254 }
255 else
256 {
257 active=FALSE; // Program Is No Longer Active
258 }
259
260 return 0; // Return To The Message Loop
261 }
262
263 case WM_SYSCOMMAND: // Intercept System Commands
264 {
265 switch (wParam) // Check System Calls
266 {
267 case SC_SCREENSAVE: // Screensaver Trying To Start?
268 case SC_MONITORPOWER: // Monitor Trying To Enter Powersave?
269 return 0; // Prevent From Happening
270 }
271 break; // Exit
272 }
273
274 case WM_CLOSE: // Did We Receive A Close Message?
275 {
277 PostQuitMessage(0); // Send A Quit Message
278 return 0; // Jump Back
279 }
280
281 case WM_KEYDOWN: // Is A Key Being Held Down?
282 {
283 keys[wParam] = TRUE; // If So, Mark It As TRUE
284 return 0; // Jump Back
285 }
286
287 case WM_KEYUP: // Has A Key Been Released?
288 {
289 keys[wParam] = FALSE; // If So, Mark It As FALSE
290 return 0; // Jump Back
291 }
292
293 case WM_SIZE: // Resize The OpenGL Window
294 {
295 ReSizeGLScene(LOWORD(lParam),HIWORD(lParam)); // LoWord=Width, HiWord=Height
296 return 0; // Jump Back
297 }
298 }
299
300 // Pass All Unhandled Messages To DefWindowProc
301 return DefWindowProc(hWnd,uMsg,wParam,lParam);
302}
303
304
306{
308}
309
310int ui_loop(int argc,char **argv, const char *name)
311{
312 MSG msg; // Windows Message Structure
313 BOOL done=FALSE; // Bool Variable To Exit Loop
315 int width, height, k;
316
317 width = 640;
318 height = 480;
319
320 // Ask The User Which Screen Mode They Prefer
321 if (MessageBox(NULL,"Would You Like To Run In Fullscreen Mode?", "Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO)
322 {
323 fullscreen=FALSE; // Windowed Mode
324 }
325
326 // Create Our OpenGL Window
327 if (!CreateGLWindow("TinyGL MinGW32",width,height,16,fullscreen))
328 {
329 return 0; // Quit If Window Was Not Created
330 }
331
332
333 while(!done) // Loop That Runs While done=FALSE
334 {
335 if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting?
336 {
337 if (msg.message==WM_QUIT) // Have We Received A Quit Message?
338 {
339 done=TRUE; // If So done=TRUE
340 }
341 else // If Not, Deal With Window Messages
342 {
343 TranslateMessage(&msg); // Translate The Message
344 DispatchMessage(&msg); // Dispatch The Message
345 }
346 }
347 else // If There Are No Messages
348 {
349 idle();
350
351 // Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene()
352 if ((active && !DrawGLScene()) || keys[VK_ESCAPE]) // Active? Was There A Quit Received?
353 {
354 done=TRUE; // ESC or DrawGLScene Signalled A Quit
355 }
356 else // Not Time To Quit, Update Screen
357 {
358 SwapBuffers(hDC); // Swap Buffers (Double Buffering)
359 }
360
361 if (keys[VK_F1]) // Is F1 Being Pressed?
362 {
363 keys[VK_F1]=FALSE; // If So Make Key FALSE
364 KillGLWindow(); // Kill Our Current Window
365 fullscreen=!fullscreen; // Toggle Fullscreen / Windowed Mode
366 // Recreate Our OpenGL Window
367 if (!CreateGLWindow("NeHe's First Polygon Tutorial",640,480,16,fullscreen))
368 {
369 return 0; // Quit If Window Was Not Created
370 }
371 }
372 }
373 }
374
375 // Shutdown
376 KillGLWindow(); // Kill The Window
377 return (msg.wParam); // Exit The Program
378}
void gdiDestroyContext(GDIContext ctx)
Definition: gdi.c:49
GDIContext gdiCreateContext(GDIContext shareList, int flags)
Definition: gdi.c:34
void gdiSwapBuffers(HWND drawable)
Definition: gdi.c:171
void * GDIContext
Definition: gdi.h:11
int gdiMakeCurrent(HWND drawable, GDIContext ctx, int width, int height)
Definition: gdi.c:109
void idle(void)
Definition: gears.c:203
void init(void)
Definition: gears.c:257
void reshape(int width, int height)
Definition: gears.c:242
void draw(void)
Definition: gears.c:163
void GLvoid
Definition: gl.h:663
int GLsizei
Definition: gl.h:673
unsigned int GLuint
Definition: gl.h:670
BOOL active
Definition: mingw32.c:42
int DrawGLScene(GLvoid)
Definition: mingw32.c:67
BOOL keys[256]
Definition: mingw32.c:41
HINSTANCE hInstance
Definition: mingw32.c:37
GDIContext cx
Definition: mingw32.c:38
HDC hDC
Definition: mingw32.c:35
int ui_loop(int argc, char **argv, const char *name)
Definition: mingw32.c:310
GLvoid ReSizeGLScene(GLsizei width, GLsizei height)
Definition: mingw32.c:50
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM)
Definition: mingw32.c:240
int InitGL(GLvoid)
Definition: mingw32.c:60
BOOL fullscreen
Definition: mingw32.c:43
void tkSwapBuffers(void)
Definition: mingw32.c:305
BOOL CreateGLWindow(char *title, int width, int height, int bits, BOOL fullscreenflag)
Definition: mingw32.c:109
GLvoid KillGLWindow(GLvoid)
Definition: mingw32.c:74
HWND hWnd
Definition: mingw32.c:36