3 Bullet Continuous Collision Detection and Physics Library
4 Copyright (c) 2003-2010 Erwin Coumans http://bulletphysics.org
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:
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.
22 #include "DemoApplication.h"
24 #include "GLDebugDrawer.h"
25 #include "GLDebugFont.h"
27 #include "BulletDynamics/Dynamics/btDynamicsWorld.h"
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();
35 // Function Declarations
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;
47 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
48 LPSTR lpCmdLine, int iCmdShow)
58 gDemoApplication = createDemo();
61 // register window class
63 wc.lpfnWndProc = WndProc;
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";
76 "BulletPhysics", "Bullet Physics Sample. http://bulletphysics.org",
77 WS_CAPTION | WS_VISIBLE | WS_OVERLAPPEDWINDOW,
80 NULL, NULL, hInstance, NULL );
82 // enable OpenGL for the window
83 EnableOpenGL( hWnd, &hDC, &hRC );
86 GLDebugDrawer debugDraw;
87 gDemoApplication->myinit();
88 //gDemoApplication->reshape(1024, 768);
89 gDemoApplication->initPhysics();
90 if (gDemoApplication->getDynamicsWorld())
91 gDemoApplication->getDynamicsWorld()->setDebugDrawer(&debugDraw);
93 gDemoApplication->reshape(sWidth,sHeight);
100 if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
103 // handle or dispatch messages
104 if ( msg.message == WM_QUIT )
110 TranslateMessage( &msg );
111 DispatchMessage( &msg );
114 // gDemoApplication->displayCallback();
119 // OpenGL animation code goes here
121 glClearColor( .7f, 0.7f, 0.7f, 1.f );
123 gDemoApplication->moveAndDisplay();
136 DisableOpenGL( hWnd, hDC, hRC );
138 // destroy the window explicitly
139 DestroyWindow( hWnd );
141 delete gDemoApplication;
149 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
161 gDemoApplication->m_modifierKeys = VK_LMENU;
169 gDemoApplication->m_modifierKeys = VK_LMENU;
172 gDemoApplication->m_modifierKeys = 0;
179 case WM_SIZE: // Size Action Has Taken Place
181 switch (wParam) // Evaluate Size Action
183 case SIZE_MINIMIZED: // Was Window Minimized?
186 case SIZE_MAXIMIZED: // Was Window Maximized?
187 sWidth = LOWORD (lParam);
188 sHeight = HIWORD (lParam);
189 if (sOpenGLInitialized)
191 gDemoApplication->reshape(sWidth,sHeight);
195 case SIZE_RESTORED: // Was Window Restored?
196 sWidth = LOWORD (lParam);
197 sHeight = HIWORD (lParam);
198 if (sOpenGLInitialized)
200 gDemoApplication->reshape(sWidth,sHeight);
211 int xPos = LOWORD(lParam);
212 int yPos = HIWORD(lParam);
213 gDemoApplication->mouseFunc(1,1,xPos,yPos);
218 int xPos = LOWORD(lParam);
219 int yPos = HIWORD(lParam);
220 gDemoApplication->mouseFunc(1,0,xPos,yPos);
226 int xPos = LOWORD(lParam);
227 int yPos = HIWORD(lParam);
228 gDemoApplication->mouseFunc(0,1,xPos,yPos);
231 case 0x020A://WM_MOUSEWHEEL:
234 int zDelta = (short)HIWORD(wParam);
235 int xPos = LOWORD(lParam);
236 int yPos = HIWORD(lParam);
238 gDemoApplication->zoomIn();
240 gDemoApplication->zoomOut();
246 int xPos = LOWORD(lParam);
247 int yPos = HIWORD(lParam);
248 gDemoApplication->mouseMotionFunc(xPos,yPos);
253 int xPos = LOWORD(lParam);
254 int yPos = HIWORD(lParam);
255 gDemoApplication->mouseFunc(2,1,xPos,yPos);
260 int xPos = LOWORD(lParam);
261 int yPos = HIWORD(lParam);
262 gDemoApplication->mouseFunc(2,0,xPos,yPos);
267 int xPos = LOWORD(lParam);
268 int yPos = HIWORD(lParam);
269 gDemoApplication->mouseFunc(0,0,xPos,yPos);
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
285 PostQuitMessage( 0 );
304 if (gDemoApplication)
305 gDemoApplication->specialKeyboardUp(wParam,0,0);
310 gDemoApplication->keyboardUpCallback(tolower(wParam),0,0);
312 return DefWindowProc( hWnd, message, wParam, lParam );
329 if (gDemoApplication)
330 gDemoApplication->specialKeyboard(wParam,0,0);
336 if (gDemoApplication)
337 gDemoApplication->clientResetScene();
353 gDemoApplication->keyboardCallback(wParam,0,0);
357 return DefWindowProc( hWnd, message, wParam, lParam );
365 void EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC)
367 PIXELFORMATDESCRIPTOR pfd;
370 // get the device context (DC)
371 *hDC = GetDC( hWnd );
373 // set the pixel format for the DC
374 ZeroMemory( &pfd, sizeof( pfd ) );
375 pfd.nSize = sizeof( pfd );
377 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
378 pfd.iPixelType = PFD_TYPE_RGBA;
381 pfd.cStencilBits = 1;
382 pfd.iLayerType = PFD_MAIN_PLANE;
383 format = ChoosePixelFormat( *hDC, &pfd );
384 SetPixelFormat( *hDC, format, &pfd );
386 // create and enable the render context (RC)
387 *hRC = wglCreateContext( *hDC );
388 wglMakeCurrent( *hDC, *hRC );
389 sOpenGLInitialized = true;
396 void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC)
398 sOpenGLInitialized = false;
400 wglMakeCurrent( NULL, NULL );
401 wglDeleteContext( hRC );
402 ReleaseDC( hWnd, hDC );