summaryrefslogtreecommitdiff
path: root/Build/source/utils/asymptote/cudareflect/tinyexr/examples/exrview/OpenGLWindow/Win32Window.cpp
blob: 029a098f2c3c35ebc1dafd327ba221c7f2eeefa5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
#ifdef _WIN32
/*
Copyright (c) 2012 Advanced Micro Devices, Inc.  

This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, 
including commercial applications, and to alter it and redistribute it freely, 
subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
//Originally written by Erwin Coumans


#include "Win32Window.h"

#include "OpenGLInclude.h"

#include <wchar.h>
static InternalData2* sData = 0;

#include "Win32InternalWindowData.h"


enum 
{
	INTERNAL_SHIFT_MODIFIER=1,
	INTERNAL_ALT_MODIFIER=2,
	INTERNAL_CONTROL_MODIFIER=4,
};

void Win32Window::pumpMessage()
{
	MSG msg;
		// check for messages
	//'if' instead of 'while' can make mainloop smoother. 
	//@todo: use separate threads for input and rendering
		while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE )  )
		{
			
			// handle or dispatch messages
			if ( msg.message == WM_QUIT ) 
			{
				m_data->m_quit = TRUE;
			} 
			else 
			{
				TranslateMessage( &msg );
				DispatchMessage( &msg );
			}
			
//			gDemoApplication->displayCallback();
			

		};
}

int getSpecialKeyFromVirtualKeycode(int virtualKeyCode)
{
	int keycode = -1;
	if (virtualKeyCode >= 'A' &&  virtualKeyCode <= 'Z')
	{
		return virtualKeyCode+32;//todo: fix the ascii A vs a input
	}

	switch (virtualKeyCode)
	{
		case VK_RETURN: {keycode = B3G_RETURN; break; };
		case VK_F1: {keycode = B3G_F1; break;}
		case VK_F2: {keycode = B3G_F2; break;}
		case VK_F3: {keycode = B3G_F3; break;}
		case VK_F4: {keycode = B3G_F4; break;}
		case VK_F5: {keycode = B3G_F5; break;}
		case VK_F6: {keycode = B3G_F6; break;}
		case VK_F7: {keycode = B3G_F7; break;}
		case VK_F8: {keycode = B3G_F8; break;}
		case VK_F9: {keycode = B3G_F9; break;}
		case VK_F10: {keycode= B3G_F10; break;}

		//case VK_SPACE: {keycode= ' '; break;}
		
		case VK_NEXT:	{keycode= B3G_PAGE_DOWN; break;}
		case VK_PRIOR:	{keycode= B3G_PAGE_UP; break;}
		
		case VK_INSERT: {keycode= B3G_INSERT; break;}
		case VK_BACK: {keycode= B3G_BACKSPACE; break;}
		case VK_DELETE: {keycode= B3G_DELETE; break;}

		case VK_END:{keycode= B3G_END; break;}
		case VK_HOME:{keycode= B3G_HOME; break;}
		case VK_LEFT:{keycode= B3G_LEFT_ARROW; break;}
		case VK_UP:{keycode= B3G_UP_ARROW; break;}
		case VK_RIGHT:{keycode= B3G_RIGHT_ARROW; break;}
		case VK_DOWN:{keycode= B3G_DOWN_ARROW; break;}
		case VK_SHIFT:{keycode=B3G_SHIFT;break;}
		case VK_MENU:{keycode=B3G_ALT;break;}
		case VK_CONTROL:{keycode=B3G_CONTROL;break;}
		default:
			{
				//keycode = MapVirtualKey( virtualKeyCode, MAPVK_VK_TO_CHAR ) & 0x0000FFFF;
			}
	};

	return keycode;
}


int getAsciiCodeFromVirtualKeycode(int virtualKeyCode)
{
	int keycode = 0xffffffff;
	
	if (virtualKeyCode >= 'a' &&  virtualKeyCode <= 'z')
	{
		return virtualKeyCode;
	}
	
	if (virtualKeyCode >= 'A' &&  virtualKeyCode <= 'Z')
	{
		return virtualKeyCode+32;//todo: fix the ascii A vs a input
	}
	
	return keycode;
}

bool Win32Window::isModifierKeyPressed(int key)
{
	bool isPressed = false;

	switch (key)
	{
		case B3G_ALT:
		{
			isPressed = ((sData->m_internalKeyModifierFlags&INTERNAL_ALT_MODIFIER)!=0);
			break;
		};
		case B3G_SHIFT:
		{
			isPressed = ((sData->m_internalKeyModifierFlags&INTERNAL_SHIFT_MODIFIER)!=0);
			break;
		};
		case B3G_CONTROL:
		{
			isPressed = ((sData->m_internalKeyModifierFlags&INTERNAL_CONTROL_MODIFIER)!=0);
			break;
		};

		default:
		{
		}
	};
	return isPressed;//m_internalKeyModifierFlags
}


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	//printf("msg = %d\n", message);
	switch (message)
	{
		
	case WM_PAINT:
		{
			PAINTSTRUCT ps;
			BeginPaint(hWnd, &ps);
			EndPaint(hWnd, &ps);
		}
		return 1;

	case WM_ERASEBKGND:
		return 1;
	
	case WM_CLOSE:
		if (sData)
			sData->m_quit = true;
		//PostQuitMessage(0);
		return 1;

	case WM_DESTROY:
		if (sData)
			sData->m_quit = true;
		//PostQuitMessage(0);
		return 1;

	case WM_SYSKEYUP:
	case WM_KEYUP:
	{

			int keycode = getSpecialKeyFromVirtualKeycode(wParam);
			switch (keycode)
			{
				case B3G_ALT:
				{
					sData->m_internalKeyModifierFlags&=~INTERNAL_ALT_MODIFIER;
					break;
				};
				case B3G_SHIFT:
				{
					sData->m_internalKeyModifierFlags &= ~INTERNAL_SHIFT_MODIFIER;
					break;
				};
				case B3G_CONTROL:
				{
					sData->m_internalKeyModifierFlags &=~INTERNAL_CONTROL_MODIFIER;
					break;
				};
			}

			if (keycode>=0 && sData && sData->m_keyboardCallback )
			{
				int state=0;
				(*sData->m_keyboardCallback)(keycode,state);
			}
			return 0;
		}
	case WM_CHAR:
		{
			//skip 'enter' key, it is processed in WM_KEYUP/WM_KEYDOWN 
			int keycode = getAsciiCodeFromVirtualKeycode(wParam);
			if (keycode < 0)
			{
				if (sData && sData->m_keyboardCallback && ((HIWORD(lParam) & KF_REPEAT) == 0))
				{
					int state = 1;
					(*sData->m_keyboardCallback)(wParam, state);
				}
			}
			return 0;
		}
	case WM_SYSKEYDOWN:
	case WM_KEYDOWN:
		{
			int keycode = getSpecialKeyFromVirtualKeycode(wParam);
			switch (keycode)
			{
				case B3G_ALT:
				{
					sData->m_internalKeyModifierFlags|=INTERNAL_ALT_MODIFIER;
					break;
				};
				case B3G_SHIFT:
				{
					sData->m_internalKeyModifierFlags |= INTERNAL_SHIFT_MODIFIER;
					break;
				};
				case B3G_CONTROL:
				{
					sData->m_internalKeyModifierFlags |=INTERNAL_CONTROL_MODIFIER;
					break;
				};
			}
			if (keycode>=0 && sData && sData->m_keyboardCallback)// && ((HIWORD(lParam) & KF_REPEAT) == 0))
			{
				int state = 1;
				(*sData->m_keyboardCallback)(keycode,state);
				return 1;
			}
			return 0;
		}

		case WM_MBUTTONUP:
	{
			int xPos = LOWORD(lParam); 
			int yPos = HIWORD(lParam); 
			if (sData)
			{
				sData->m_mouseMButton=0;
				sData->m_mouseXpos = xPos;
				sData->m_mouseYpos = yPos;
				if (sData && sData->m_mouseButtonCallback)
					(*sData->m_mouseButtonCallback)(1,0,xPos,yPos);
			}
		break;
	}
	case WM_MBUTTONDOWN:
	{
			int xPos = LOWORD(lParam); 
			int yPos = HIWORD(lParam); 
			if (sData)
			{
				sData->m_mouseMButton=1;
				sData->m_mouseXpos = xPos;
				sData->m_mouseYpos = yPos;
				if (sData && sData->m_mouseButtonCallback)
					(*sData->m_mouseButtonCallback)(1,1,xPos,yPos);
			}
		break;
	}

	case WM_LBUTTONUP:
	{
			int xPos = LOWORD(lParam); 
			int yPos = HIWORD(lParam); 
			if (sData)
			{
				sData->m_mouseLButton=0;
				sData->m_mouseXpos = xPos;
				sData->m_mouseYpos = yPos;
				
				if (sData && sData->m_mouseButtonCallback)
					(*sData->m_mouseButtonCallback)(0,0,xPos,yPos);

			}
		//	gDemoApplication->mouseFunc(0,1,xPos,yPos);
		break;
	}
	case WM_LBUTTONDOWN:
		{
				int xPos = LOWORD(lParam); 
				int yPos = HIWORD(lParam); 
			if (sData)
			{
				sData->m_mouseLButton=1;
				sData->m_mouseXpos = xPos;
				sData->m_mouseYpos = yPos;

				if (sData && sData->m_mouseButtonCallback)
					(*sData->m_mouseButtonCallback)(0,1,xPos,yPos);
			}
			break;
		}

	case 0x020e://WM_MOUSEWHEEL_LEFT_RIGHT
	{

		int  zDelta = (short)HIWORD(wParam);
		int xPos = LOWORD(lParam); 
		int yPos = HIWORD(lParam); 
		//m_cameraDistance -= zDelta*0.01;
		if (sData && sData->m_wheelCallback)
			(*sData->m_wheelCallback)(-float(zDelta)*0.05f,0);
		return 1;
		break;
	}
	case 0x020A://WM_MOUSEWHEEL:
	{
		
		int  zDelta = (short)HIWORD(wParam);
		int xPos = LOWORD(lParam); 
		int yPos = HIWORD(lParam); 
		//m_cameraDistance -= zDelta*0.01;
		if (sData && sData->m_wheelCallback)
			(*sData->m_wheelCallback)(0,float(zDelta)*0.05f);
		return 1;
		break;
	}

	case WM_MOUSEMOVE:
		{
				int xPos = LOWORD(lParam); 
				int yPos = HIWORD(lParam); 
				sData->m_mouseXpos = xPos;
				sData->m_mouseYpos = yPos;

				if (sData && sData->m_mouseMoveCallback)
					(*sData->m_mouseMoveCallback)(xPos,yPos);

			break;
		}
	case WM_RBUTTONUP:
	{
			int xPos = LOWORD(lParam); 
			int yPos = HIWORD(lParam); 
			sData->m_mouseRButton = 1;

			if (sData && sData->m_mouseButtonCallback)
				(*sData->m_mouseButtonCallback)(2,0,sData->m_mouseXpos,sData->m_mouseYpos);

			//gDemoApplication->mouseFunc(2,1,xPos,yPos);
		break;
	}
	case WM_RBUTTONDOWN:
	{
			int xPos = LOWORD(lParam); 
			int yPos = HIWORD(lParam); 
			sData->m_mouseRButton = 0;
			if (sData && sData->m_mouseButtonCallback)
				(*sData->m_mouseButtonCallback)(2,1,sData->m_mouseXpos,sData->m_mouseYpos);

		break;
	}
	case WM_QUIT:
		{
			return 0;
			break;
		}
	case WM_SIZE:													// Size Action Has Taken Place

			RECT clientRect;
			GetClientRect(hWnd,&clientRect);

			switch (wParam)												// Evaluate Size Action
			{

				case SIZE_MINIMIZED:									// Was Window Minimized?
				return 0;												// Return

				case SIZE_MAXIMIZED:									// Was Window Maximized?
				case SIZE_RESTORED:										// Was Window Restored?
					RECT wr;
					GetWindowRect(hWnd,&wr);
					
					sData->m_fullWindowWidth = wr.right-wr.left;
					sData->m_fullWindowHeight = wr.bottom-wr.top;//LOWORD (lParam) HIWORD (lParam);
					sData->m_openglViewportWidth = clientRect.right;
					sData->m_openglViewportHeight = clientRect.bottom;
					glViewport(0, 0, sData->m_openglViewportWidth, sData->m_openglViewportHeight);

					if (sData->m_resizeCallback)
						(*sData->m_resizeCallback)(sData->m_openglViewportWidth,sData->m_openglViewportHeight);
					//if (sOpenGLInitialized)
					//{
					//	//gDemoApplication->reshape(sWidth,sHeight);
					//}
				return 0;												// Return
			}
		break;

	default:{
				

			}
	};

	return DefWindowProc(hWnd, message, wParam, lParam);
}



void Win32Window::setWindowTitle(const char* titleChar)
{
	
	wchar_t  windowTitle[1024];
	swprintf(windowTitle, 1024, L"%hs", titleChar);

	DWORD dwResult;

#ifdef _WIN64
		SetWindowTextW(m_data->m_hWnd, windowTitle);
#else
		SendMessageTimeoutW(m_data->m_hWnd, WM_SETTEXT, 0,
				reinterpret_cast<LPARAM>(windowTitle),
				SMTO_ABORTIFHUNG, 2000, &dwResult);
#endif
}

void	Win32Window::createWindow(const b3gWindowConstructionInfo& ci)
{
	int oglViewportWidth = ci.m_width;
	int oglViewportHeight = ci.m_height;
	bool fullscreen = ci.m_fullscreen;
	int colorBitsPerPixel = ci.m_colorBitsPerPixel;
	void* windowHandle = ci.m_windowHandle;

	// get handle to exe file
	HINSTANCE hInstance = GetModuleHandle(0);


	// create the window if we need to and we do not use the null device
	if (!windowHandle)
	{
#ifdef UNICODE
		const wchar_t * ClassName = L"DeviceWin32";
		const wchar_t*  emptyString= L"";
#else
		const char* ClassName = "DeviceWin32";
		const char* emptyString = "";
#endif
		// Register Class
		WNDCLASSEX wcex;
		wcex.cbSize		= sizeof(WNDCLASSEX);
		wcex.style		= CS_HREDRAW | CS_VREDRAW;
		wcex.lpfnWndProc	= WndProc;
		wcex.cbClsExtra		= 0;
		wcex.cbWndExtra		= 0;
		wcex.hInstance		= hInstance;
		wcex.hIcon		= LoadIcon( NULL, IDI_APPLICATION ); //(HICON)LoadImage(hInstance, "bullet_ico.ico", IMAGE_ICON, 0,0, LR_LOADTRANSPARENT);//LR_LOADFROMFILE);
		wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
		wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
		wcex.lpszMenuName	= 0;
		wcex.lpszClassName	= ClassName;
		wcex.hIconSm		= 0;

		// if there is an icon, load it
//		wcex.hIcon = (HICON)LoadImage(hInstance, "bullet.ico", IMAGE_ICON, 0,0, LR_LOADFROMFILE);

		RegisterClassEx(&wcex);

		// calculate client size

		RECT clientSize;
		clientSize.top = 0;
		clientSize.left = 0;
		clientSize.right = oglViewportWidth;
		clientSize.bottom = oglViewportHeight;

		DWORD style = WS_POPUP;

		if (!fullscreen)
			style = WS_SYSMENU | WS_BORDER | WS_CAPTION | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SIZEBOX;

		AdjustWindowRect(&clientSize, style, false);
		
		m_data->m_fullWindowWidth = clientSize.right - clientSize.left;
		m_data->m_fullWindowHeight = clientSize.bottom - clientSize.top;

		int windowLeft = (GetSystemMetrics(SM_CXSCREEN) - m_data->m_fullWindowWidth) / 2;
		int windowTop = (GetSystemMetrics(SM_CYSCREEN) - m_data->m_fullWindowHeight) / 2;

		if (fullscreen)
		{
			windowLeft = 0;
			windowTop = 0;
		}

		// create window

		m_data->m_hWnd = CreateWindow( ClassName, emptyString, style, windowLeft, windowTop,
			m_data->m_fullWindowWidth, m_data->m_fullWindowHeight,NULL, NULL, hInstance, NULL);

		
		RECT clientRect;
		GetClientRect(m_data->m_hWnd,&clientRect);



		ShowWindow(m_data->m_hWnd, SW_SHOW);
		UpdateWindow(m_data->m_hWnd);

		MoveWindow(m_data->m_hWnd, windowLeft, windowTop, m_data->m_fullWindowWidth, m_data->m_fullWindowHeight, TRUE);

		GetClientRect(m_data->m_hWnd,&clientRect);
		int w = clientRect.right-clientRect.left;
		int h = clientRect.bottom-clientRect.top;
//		printf("actual client OpenGL viewport width / height = %d, %d\n",w,h);
		m_data->m_openglViewportHeight = h;
		m_data->m_openglViewportWidth = w;
		
	}
	else if (windowHandle)
	{
		// attach external window
		m_data->m_hWnd = static_cast<HWND>(windowHandle);
		RECT r;
		GetWindowRect(m_data->m_hWnd, &r);
		m_data->m_fullWindowWidth = r.right - r.left;
		m_data->m_fullWindowHeight= r.bottom - r.top;


		//sFullScreen = false;
		//sExternalWindow = true;
	}


	if (fullscreen)
	{
		DEVMODE dm;
		memset(&dm, 0, sizeof(dm));
		dm.dmSize = sizeof(dm);
		// use default values from current setting
		EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm);
		m_data->m_oldScreenWidth = dm.dmPelsWidth;
		m_data->m_oldHeight = dm.dmPelsHeight;
		m_data->m_oldBitsPerPel = dm.dmBitsPerPel;

		dm.dmPelsWidth = oglViewportWidth;
		dm.dmPelsHeight = oglViewportHeight;
		if (colorBitsPerPixel)
		{
			dm.dmBitsPerPel = colorBitsPerPixel;
		}
		dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY;

		LONG res = ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
		if (res != DISP_CHANGE_SUCCESSFUL)
		{ // try again without forcing display frequency
			dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
			res = ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
		}

	}


}


void	Win32Window::switchFullScreen(bool fullscreen,int width,int height,int colorBitsPerPixel)
{
	
	LONG res;
	DEVMODE dm;
	memset(&dm, 0, sizeof(dm));
	dm.dmSize = sizeof(dm);
	// use default values from current setting
	EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm);

	dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY;

	if (fullscreen && !m_data->m_oldScreenWidth)
	{
		m_data->m_oldScreenWidth = dm.dmPelsWidth;
		m_data->m_oldHeight = dm.dmPelsHeight;
		m_data->m_oldBitsPerPel = dm.dmBitsPerPel;

		if (width && height)
		{
			dm.dmPelsWidth = width;
			dm.dmPelsHeight = height;
		} else
		{
			dm.dmPelsWidth = m_data->m_fullWindowWidth;
			dm.dmPelsHeight = m_data->m_fullWindowHeight;
		}
		if (colorBitsPerPixel)
		{
			dm.dmBitsPerPel = colorBitsPerPixel;
		}
	} else
	{
		if (m_data->m_oldScreenWidth)
		{
			dm.dmPelsWidth =	m_data->m_oldScreenWidth;
			dm.dmPelsHeight=	m_data->m_oldHeight;
			dm.dmBitsPerPel =   m_data->m_oldBitsPerPel;
		}
	}

	if (fullscreen)
	{

		res = ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
		if (!res)
		{
			dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
			res = ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
		}

		DWORD style = WS_POPUP;
		SetWindowLong(m_data->m_hWnd,  GWL_STYLE, style);

		MoveWindow(m_data->m_hWnd, 0, 0, m_data->m_fullWindowWidth, m_data->m_fullWindowHeight, TRUE);
		
		SetWindowPos(m_data->m_hWnd, NULL,0,0, (int)width, (int)height,
                         SWP_FRAMECHANGED |SWP_SHOWWINDOW);//|SWP_NOACTIVATE | SWP_NOCOPYBITS | SWP_NOOWNERZORDER | SWP_NOREPOSITION | SWP_NOZORDER);


	} else
	{
		res = ChangeDisplaySettings(&dm, 0);

		DWORD style = WS_SYSMENU | WS_BORDER | WS_CAPTION | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SIZEBOX;
		SetWindowLong(m_data->m_hWnd,  GWL_STYLE, style);

		SetWindowPos(m_data->m_hWnd, NULL,0,0, (int)width, (int)height,
                         SWP_FRAMECHANGED |SWP_SHOWWINDOW);
		//|SWP_NOACTIVATE | SWP_NOCOPYBITS | SWP_NOOWNERZORDER | SWP_NOREPOSITION | SWP_NOZORDER);

	}


}



Win32Window::Win32Window()
{
	m_data = new InternalData2();
	sData = m_data;
	
}

Win32Window::~Win32Window()
{
	setKeyboardCallback(0);
	setMouseMoveCallback(0);
	setMouseButtonCallback(0);
	setWheelCallback(0);
	setResizeCallback(0);
	
	sData = 0;
	delete m_data;
	
}

void Win32Window::setRenderCallback( b3RenderCallback renderCallback)
{

}

void	Win32Window::closeWindow()
{
	setKeyboardCallback(0);
	setMouseMoveCallback(0);
	setMouseButtonCallback(0);
	setWheelCallback(0);
	setResizeCallback(0);
	setRenderCallback(0);
	
	
	DestroyWindow(this->m_data->m_hWnd);
}

void Win32Window::getMouseCoordinates(int& x, int& y)
{
	x = m_data->m_mouseXpos;
	y = m_data->m_mouseYpos;

}

void Win32Window::runMainLoop()
{

}


void	Win32Window::startRendering()
{
		pumpMessage();

//		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);	//clear buffers
		
		//glCullFace(GL_BACK);
		//glFrontFace(GL_CCW);
	//	glEnable(GL_DEPTH_TEST);



}


void	Win32Window::renderAllObjects()
{
}

void	Win32Window::endRendering()
{
	SwapBuffers( m_data->m_hDC );
}

float	Win32Window::getTimeInSeconds()
{
	return 0.f;
}

void	Win32Window::setDebugMessage(int x,int y,const char* message)
{
}

void	Win32Window::setRequestExit()
{
	m_data->m_quit = true;
}
bool Win32Window::requestedExit() const
{
	return m_data->m_quit;
}

void Win32Window::setWheelCallback(b3WheelCallback wheelCallback)
{
	m_data->m_wheelCallback = wheelCallback;
}

void Win32Window::setMouseMoveCallback(b3MouseMoveCallback	mouseCallback)
{
	m_data->m_mouseMoveCallback = mouseCallback;
}

void Win32Window::setMouseButtonCallback(b3MouseButtonCallback	mouseCallback)
{
	m_data->m_mouseButtonCallback = mouseCallback;
}

void Win32Window::setResizeCallback(b3ResizeCallback	resizeCallback)
{
	m_data->m_resizeCallback = resizeCallback;
	if (m_data->m_resizeCallback)
		(*m_data->m_resizeCallback)(m_data->m_openglViewportWidth,m_data->m_openglViewportHeight);
}

void Win32Window::setKeyboardCallback( b3KeyboardCallback	keyboardCallback)
{
	m_data->m_keyboardCallback = keyboardCallback;
	
}

b3KeyboardCallback	Win32Window::getKeyboardCallback()
{
	return m_data->m_keyboardCallback;
}

b3MouseMoveCallback Win32Window::getMouseMoveCallback()
{
	return m_data->m_mouseMoveCallback;
}
b3MouseButtonCallback Win32Window::getMouseButtonCallback()
{
	return m_data->m_mouseButtonCallback;
}
b3ResizeCallback Win32Window::getResizeCallback()
{
	return m_data->m_resizeCallback;
}
b3WheelCallback Win32Window::getWheelCallback()
{
	return m_data->m_wheelCallback;
}
#endif