Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Demos / OpenGL / Win32AppMain.cpp
1 #ifdef _WINDOWS
2 /*
3 Bullet Continuous Collision Detection and Physics Library
4 Copyright (c) 2003-2010 Erwin Coumans  http://bulletphysics.org
5
6 This software is provided 'as-is', without any express or implied warranty.
7 In no event will the authors be held liable for any damages arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose, 
9 including commercial applications, and to alter it and redistribute it freely, 
10 subject to the following restrictions:
11
12 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.
13 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
14 3. This notice may not be removed or altered from any source distribution.
15 */
16
17
18 #include <windows.h>
19 #include <gl/gl.h>
20
21
22 #include "DemoApplication.h"
23
24 #include "GLDebugDrawer.h"
25 #include "GLDebugFont.h"
26
27 #include "BulletDynamics/Dynamics/btDynamicsWorld.h"
28
29 /// This Win32AppMain is shared code between all demos. 
30 /// The actual demo, derived from DemoApplication is created using 'createDemo', in a separate .cpp file
31 DemoApplication* gDemoApplication = 0;
32 DemoApplication*        createDemo();
33
34
35 // Function Declarations
36
37 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
38 void EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC);
39 void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC);
40 static bool sOpenGLInitialized = false;
41 static int sWidth = 0;
42 static int sHeight =0;
43 static int quitRequest = 0;
44
45 // WinMain
46
47 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
48                                    LPSTR lpCmdLine, int iCmdShow)
49 {
50         WNDCLASS wc;
51         HWND hWnd;
52         HDC hDC;
53         HGLRC hRC;
54         MSG msg;
55         BOOL quit = FALSE;
56         float theta = 0.0f;
57         
58         gDemoApplication = createDemo();
59         
60
61         // register window class
62         wc.style = CS_OWNDC;
63         wc.lpfnWndProc = WndProc;
64         wc.cbClsExtra = 0;
65         wc.cbWndExtra = 0;
66         wc.hInstance = hInstance;
67         wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
68         wc.hCursor = LoadCursor( NULL, IDC_ARROW );
69         wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
70         wc.lpszMenuName = NULL;
71         wc.lpszClassName = "BulletPhysics";
72         RegisterClass( &wc );
73         
74         // create main window
75         hWnd = CreateWindow( 
76                 "BulletPhysics", "Bullet Physics Sample. http://bulletphysics.org", 
77                 WS_CAPTION | WS_VISIBLE | WS_OVERLAPPEDWINDOW,
78 //              0, 0, 640, 480,
79                 0, 0, 1024, 768,
80                 NULL, NULL, hInstance, NULL );
81         
82         // enable OpenGL for the window
83         EnableOpenGL( hWnd, &hDC, &hRC );
84         
85         
86         GLDebugDrawer debugDraw;
87         gDemoApplication->myinit();
88         //gDemoApplication->reshape(1024, 768);
89         gDemoApplication->initPhysics();
90         if (gDemoApplication->getDynamicsWorld())
91                 gDemoApplication->getDynamicsWorld()->setDebugDrawer(&debugDraw);
92         
93         gDemoApplication->reshape(sWidth,sHeight);
94
95         // program main loop
96         while ( !quit )
97         {
98                 
99                 // check for messages
100                 if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE )  )
101                 {
102                         
103                         // handle or dispatch messages
104                         if ( msg.message == WM_QUIT ) 
105                         {
106                                 quit = TRUE;
107                         } 
108                         else 
109                         {
110                                 TranslateMessage( &msg );
111                                 DispatchMessage( &msg );
112                         }
113                         
114 //                      gDemoApplication->displayCallback();
115                         
116
117                 };
118                 
119                 // OpenGL animation code goes here
120                 
121                 glClearColor( .7f, 0.7f, 0.7f, 1.f );
122                 
123                 gDemoApplication->moveAndDisplay();
124
125
126                 SwapBuffers( hDC );
127                 
128                 theta += 1.0f;
129         
130                 
131         }
132         
133
134
135         // shutdown OpenGL
136         DisableOpenGL( hWnd, hDC, hRC );
137         
138         // destroy the window explicitly
139         DestroyWindow( hWnd );
140
141         delete gDemoApplication;
142
143         return msg.wParam;
144         
145 }
146
147 // Window Procedure
148
149 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
150 {
151         
152         
153
154         switch (message)
155         {
156
157         case WM_SYSKEYDOWN:
158                 {
159                         if (lParam & 1<<29)
160                         {
161                                 gDemoApplication->m_modifierKeys = VK_LMENU;
162                         }
163                         break;
164                 }
165         case WM_SYSKEYUP:
166                 {
167                         if (lParam & 1<<29)
168                         {
169                                 gDemoApplication->m_modifierKeys = VK_LMENU;
170                         } else
171                         {
172                                 gDemoApplication->m_modifierKeys = 0;
173                         }
174                         
175                         break;
176                 }
177
178                 
179                 case WM_SIZE:                                                                                                   // Size Action Has Taken Place
180
181                         switch (wParam)                                                                                         // Evaluate Size Action
182                         {
183                                 case SIZE_MINIMIZED:                                                                    // Was Window Minimized?
184                                 return 0;                                                                                               // Return
185
186                                 case SIZE_MAXIMIZED:                                                                    // Was Window Maximized?
187                                         sWidth = LOWORD (lParam);
188                                         sHeight = HIWORD (lParam);
189                                         if (sOpenGLInitialized)
190                                         {
191                                                 gDemoApplication->reshape(sWidth,sHeight);
192                                         }
193                                 return 0;                                                                                               // Return
194
195                                 case SIZE_RESTORED:                                                                             // Was Window Restored?
196                                         sWidth = LOWORD (lParam);
197                                         sHeight = HIWORD (lParam);
198                                         if (sOpenGLInitialized)
199                                         {
200                                                 gDemoApplication->reshape(sWidth,sHeight);
201                                         }
202                                 return 0;                                                                                               // Return
203                         }
204                 break;  
205
206         case WM_CREATE:
207                 return 0;
208         
209         case WM_MBUTTONUP:
210         {
211                         int xPos = LOWORD(lParam); 
212                         int yPos = HIWORD(lParam); 
213                         gDemoApplication->mouseFunc(1,1,xPos,yPos);
214                 break;
215         }
216         case WM_MBUTTONDOWN:
217         {
218                         int xPos = LOWORD(lParam); 
219                         int yPos = HIWORD(lParam); 
220                         gDemoApplication->mouseFunc(1,0,xPos,yPos);
221                 break;
222         }
223
224         case WM_LBUTTONUP:
225         {
226                         int xPos = LOWORD(lParam); 
227                         int yPos = HIWORD(lParam); 
228                         gDemoApplication->mouseFunc(0,1,xPos,yPos);
229                 break;
230         }
231         case 0x020A://WM_MOUSEWHEEL:
232         {
233
234                 int  zDelta = (short)HIWORD(wParam);
235                 int xPos = LOWORD(lParam); 
236                 int yPos = HIWORD(lParam); 
237                 if (zDelta>0)
238                         gDemoApplication->zoomIn();
239                 else
240                         gDemoApplication->zoomOut();
241                 break;
242         }
243
244         case WM_MOUSEMOVE:
245                 {
246                                 int xPos = LOWORD(lParam); 
247                                 int yPos = HIWORD(lParam); 
248                                 gDemoApplication->mouseMotionFunc(xPos,yPos);
249                         break;
250                 }
251         case WM_RBUTTONUP:
252         {
253                         int xPos = LOWORD(lParam); 
254                         int yPos = HIWORD(lParam); 
255                         gDemoApplication->mouseFunc(2,1,xPos,yPos);
256                 break;
257         }
258         case WM_RBUTTONDOWN:
259         {
260                         int xPos = LOWORD(lParam); 
261                         int yPos = HIWORD(lParam); 
262                         gDemoApplication->mouseFunc(2,0,xPos,yPos);
263                 break;
264         }
265         case WM_LBUTTONDOWN:
266                 {
267                                 int xPos = LOWORD(lParam); 
268                                 int yPos = HIWORD(lParam); 
269                                 gDemoApplication->mouseFunc(0,0,xPos,yPos);
270                         break;
271                 }
272 /*#define WM_LBUTTONUP                    0x0202
273 #define WM_LBUTTONDBLCLK                0x0203
274 #define WM_RBUTTONDOWN                  0x0204
275 #define WM_RBUTTONUP                    0x0205
276 #define WM_RBUTTONDBLCLK                0x0206
277 #define WM_MBUTTONDOWN                  0x0207
278 #define WM_MBUTTONUP                    0x0208
279 #define WM_MBUTTONDBLCLK                0x0209
280 */
281
282
283
284         case WM_CLOSE:
285                 PostQuitMessage( 0 );
286                 return 0;
287                 
288         case WM_DESTROY:
289                 return 0;
290                 
291         case WM_KEYUP:
292                 switch ( wParam )
293                 {
294                         
295                 case VK_PRIOR:
296                 case VK_NEXT:
297                 case VK_END:
298                 case VK_HOME:
299                 case VK_LEFT:
300                 case VK_UP:
301                 case VK_RIGHT:
302                 case VK_DOWN:
303                         {
304                                 if (gDemoApplication)
305                                         gDemoApplication->specialKeyboardUp(wParam,0,0);
306                                 return 0;
307                         }
308                         default:
309                                 {
310                                         gDemoApplication->keyboardUpCallback(tolower(wParam),0,0);
311                                 }
312                         return DefWindowProc( hWnd, message, wParam, lParam );
313                 }
314
315         case WM_KEYDOWN:
316                 printf("bla\n");
317                 switch ( wParam )
318                 {
319                 case VK_CONTROL:
320                 case VK_PRIOR:
321                 case VK_NEXT:
322                 case VK_END:
323                 case VK_HOME:
324                 case VK_LEFT:
325                 case VK_UP:
326                 case VK_RIGHT:
327                 case VK_DOWN:
328                         {
329                                 if (gDemoApplication)
330                                         gDemoApplication->specialKeyboard(wParam,0,0);
331                                 break;
332                         }
333
334                 case ' ':
335                         {
336                                 if (gDemoApplication)
337                                         gDemoApplication->clientResetScene();
338                                 break;
339                         }
340                 case 'Q':
341                 case VK_ESCAPE:
342                         {
343                                 quitRequest = 1;
344                                 PostQuitMessage(0);
345                         }
346                         return 0;
347                         
348                 }
349                 return 0;
350                 
351         case WM_CHAR:
352                 if (!quitRequest)
353                         gDemoApplication->keyboardCallback(wParam,0,0);
354                 break;
355         
356         default:
357                 return DefWindowProc( hWnd, message, wParam, lParam );
358                         
359         }
360         return 0;
361 }
362
363 // Enable OpenGL
364
365 void EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC)
366 {
367         PIXELFORMATDESCRIPTOR pfd;
368         int format;
369         
370         // get the device context (DC)
371         *hDC = GetDC( hWnd );
372         
373         // set the pixel format for the DC
374         ZeroMemory( &pfd, sizeof( pfd ) );
375         pfd.nSize = sizeof( pfd );
376         pfd.nVersion = 1;
377         pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
378         pfd.iPixelType = PFD_TYPE_RGBA;
379         pfd.cColorBits = 24;
380         pfd.cDepthBits = 16;
381         pfd.cStencilBits = 1;
382         pfd.iLayerType = PFD_MAIN_PLANE;
383         format = ChoosePixelFormat( *hDC, &pfd );
384         SetPixelFormat( *hDC, format, &pfd );
385         
386         // create and enable the render context (RC)
387         *hRC = wglCreateContext( *hDC );
388         wglMakeCurrent( *hDC, *hRC );
389         sOpenGLInitialized = true;
390         
391         
392 }
393
394 // Disable OpenGL
395
396 void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC)
397 {
398         sOpenGLInitialized = false;
399
400         wglMakeCurrent( NULL, NULL );
401         wglDeleteContext( hRC );
402         ReleaseDC( hWnd, hDC );
403 }
404
405 #endif //_WINDOWS