Morph-SynRJ
view window_fmod_init.cpp @ 0:22de913c2d84
morph-synrj intro
| author | "Cedric Pinson <cedric.pinson@alcove.fr> <mornifle@plopbyte.net>" |
|---|---|
| date | Tue Nov 27 15:23:52 2007 +0100 (2007-11-27) |
| parents | |
| children |
line source
1 #include <stdio.h>
3 #include <stdlib.h>
5 #include <windows.h>
7 #include <math.h>
9 #include <gl/gl.h>
11 #include <gl/glu.h>
14 HDC hDC = NULL;
16 HGLRC hRC = NULL;
18 HWND hWnd = NULL;
20 HINSTANCE hInstance;
22 SYSTEMTIME tm;
26 BOOL fullscreen = TRUE;
28 BOOL done = TRUE;
32 typedef struct
34 {
36 int length;
38 int pos;
40 void *data;
42 } MEMFILE;
48 unsigned int memopen(char *name)
50 {
52 MEMFILE *memfile;
56 memfile = (MEMFILE *)calloc(sizeof(MEMFILE),1);
60 // hey look some load from resource code!
62 HRSRC rec;
64 HGLOBAL handle;
68 rec = FindResource(NULL, name, RT_RCDATA);
70 handle = LoadResource(NULL, rec);
74 memfile->data = LockResource(handle);
76 memfile->length = SizeofResource(NULL, rec);
78 memfile->pos = 0;
82 return (unsigned int)memfile;
84 }
88 void memclose(unsigned int handle)
90 {
92 MEMFILE *memfile = (MEMFILE *)handle;
96 free(memfile);
98 }
102 int memread(void *buffer, int size, unsigned int handle)
104 {
106 MEMFILE *memfile = (MEMFILE *)handle;
110 if (memfile->pos + size >= memfile->length)
112 size = memfile->length - memfile->pos;
116 memcpy(buffer, (char *)memfile->data+memfile->pos, size);
118 memfile->pos += size;
122 return size;
124 }
128 void memseek(unsigned int handle, int pos, signed char mode)
130 {
132 MEMFILE *memfile = (MEMFILE *)handle;
136 if (mode == SEEK_SET)
138 memfile->pos = pos;
140 else if (mode == SEEK_CUR)
142 memfile->pos += pos;
144 else if (mode == SEEK_END)
146 memfile->pos = memfile->length + pos;
150 if (memfile->pos > memfile->length)
152 memfile->pos = memfile->length;
154 }
158 int memtell(unsigned int handle)
160 {
162 MEMFILE *memfile = (MEMFILE *)handle;
166 return memfile->pos;
168 }
172 LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
174 {
176 switch (uMsg)
178 {
180 case WM_CREATE :
182 break;
186 case WM_CLOSE:
188 {
190 PostQuitMessage(0);
192 done = NULL;
194 return 0;
196 }
200 case WM_QUIT:
202 {
204 PostQuitMessage(0);
206 done = NULL;
208 return 0;
210 }
214 case WM_KEYDOWN :
216 {
218 switch ( wParam )
220 {
222 case VK_ESCAPE :
224 done = NULL;
226 PostQuitMessage( 0 );
227 break;
229 }
231 break;
233 }
237 }
241 return DefWindowProc(hWnd,uMsg,wParam,lParam);
243 }
249 void KillGLWindow( void )
251 {
253 if ( fullscreen )
255 {
257 ChangeDisplaySettings( NULL, 0 );
259 ShowCursor( TRUE );
261 }
265 if ( hRC )
267 {
269 if ( !wglMakeCurrent( NULL, NULL ) )
271 {
273 MessageBox( NULL, "Release Of DC And RC Failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION );
275 }
279 if ( !wglDeleteContext( hRC ) )
281 {
283 MessageBox( NULL, "Release Rendering Context Failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION );
285 }
287 hRC = NULL;
289 }
291 }
295 BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
297 {
299 GLuint PixelFormat;
301 WNDCLASS wc;
303 DWORD dwExStyle;
305 DWORD dwStyle;
307 RECT WindowRect;
309 WindowRect.left=(long)0;
311 WindowRect.right=(long)width;
313 WindowRect.top=(long)0;
315 WindowRect.bottom=(long)height;
319 fullscreen = fullscreenflag;
323 hInstance = GetModuleHandle(NULL);
325 wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
327 wc.lpfnWndProc = (WNDPROC) WndProc;
329 wc.cbClsExtra = 0;
331 wc.cbWndExtra = 0;
333 wc.hInstance = hInstance;
335 wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
337 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
339 wc.hbrBackground = NULL;
341 wc.lpszMenuName = NULL;
343 wc.lpszClassName = "OpenGL";
347 if (!RegisterClass(&wc))
349 {
351 MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
353 return FALSE;
355 }
359 if (fullscreen)
361 {
363 DEVMODE dmScreenSettings;
365 memset(&dmScreenSettings,0,sizeof(dmScreenSettings));
367 dmScreenSettings.dmSize=sizeof(dmScreenSettings);
369 dmScreenSettings.dmPelsWidth = width;
371 dmScreenSettings.dmPelsHeight = height;
373 dmScreenSettings.dmBitsPerPel = bits;
375 dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
381 if (ChangeDisplaySettings( &dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL )
383 {
387 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)
389 {
391 fullscreen=FALSE;
393 }
395 else
397 {
401 MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
403 return FALSE;
405 }
407 }
409 }
413 if (fullscreen) // Are We Still In Fullscreen Mode?
415 {
417 dwExStyle=WS_EX_APPWINDOW; // Window Extended Style
419 dwStyle=WS_POPUP; // Windows Style
421 ShowCursor(FALSE); // Hide Mouse Pointer
423 }
425 else
427 {
429 dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style
431 dwStyle=WS_OVERLAPPEDWINDOW; // Windows Style
433 }
437 AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size
441 // Create The Window
443 if (!(hWnd=CreateWindowEx( dwExStyle, // Extended Style For The Window
445 "OpenGL", // Class Name
447 title, // Window Title
449 dwStyle | // Defined Window Style
451 WS_CLIPSIBLINGS | // Required Window Style
453 WS_CLIPCHILDREN, // Required Window Style
455 0, 0, // Window Position
457 WindowRect.right-WindowRect.left, // Calculate Window Width
459 WindowRect.bottom-WindowRect.top, // Calculate Window Height
461 NULL, // No Parent Window
463 NULL, // No Menu
465 hInstance, // Instance
467 NULL))) // Dont Pass Anything To WM_CREATE
469 {
471 KillGLWindow(); // Reset The Display
473 MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
475 return FALSE; // Return FALSE
477 }
481 static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be
483 {
485 sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
487 1, // Version Number
489 PFD_DRAW_TO_WINDOW | // Format Must Support Window
491 PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
493 PFD_DOUBLEBUFFER, // Must Support Double Buffering
495 PFD_TYPE_RGBA, // Request An RGBA Format
497 bits, // Select Our Color Depth
499 0, 0, 0, 0, 0, 0, // Color Bits Ignored
501 0, // No Alpha Buffer
503 0, // Shift Bit Ignored
505 0, // No Accumulation Buffer
507 0, 0, 0, 0, // Accumulation Bits Ignored
509 32, // 16Bit Z-Buffer (Depth Buffer)
511 32, // No Stencil Buffer
513 0, // No Auxiliary Buffer
515 PFD_MAIN_PLANE, // Main Drawing Layer
517 0, // Reserved
519 0, 0, 0 // Layer Masks Ignored
521 };
525 if (!(hDC=GetDC(hWnd))) // Did We Get A Device Context?
527 {
529 KillGLWindow(); // Reset The Display
531 MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
533 return FALSE; // Return FALSE
535 }
539 if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // Did Windows Find A Matching Pixel Format?
541 {
543 KillGLWindow(); // Reset The Display
545 MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
547 return FALSE; // Return FALSE
549 }
553 if(!SetPixelFormat(hDC,PixelFormat,&pfd)) // Are We Able To Set The Pixel Format?
555 {
557 KillGLWindow(); // Reset The Display
559 MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
561 return FALSE; // Return FALSE
563 }
567 if (!(hRC=wglCreateContext(hDC))) // Are We Able To Get A Rendering Context?
569 {
571 KillGLWindow(); // Reset The Display
573 MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
575 return FALSE; // Return FALSE
577 }
581 if(!wglMakeCurrent(hDC,hRC)) // Try To Activate The Rendering Context
583 {
585 KillGLWindow(); // Reset The Display
587 MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
589 return FALSE; // Return FALSE
591 }
595 ShowWindow(hWnd,SW_SHOW); // Show The Window
597 SetForegroundWindow(hWnd); // Slightly Higher Priority
599 SetFocus(hWnd); // Sets Keyboard Focus To The Window
603 return TRUE; // Success
605 }
