Tizen 2.1 base
[platform/upstream/libbullet.git] / Extras / PhysicsEffects / sample / api_physics_effects / 2_stable_parallel / main.cpp
1 /*\r
2 Physics Effects Copyright(C) 2010 Sony Computer Entertainment Inc.\r
3 All rights reserved.\r
4 \r
5 Physics Effects is open software; you can redistribute it and/or\r
6 modify it under the terms of the BSD License.\r
7 \r
8 Physics Effects is distributed in the hope that it will be useful,\r
9 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\r
11 See the BSD License for more details.\r
12 \r
13 A copy of the BSD License is distributed with\r
14 Physics Effects under the filename: physics_effects_license.txt\r
15 */\r
16 \r
17 #include "../common/common.h"\r
18 #include "../common/ctrl_func.h"\r
19 #include "../common/render_func.h"\r
20 #include "../common/perf_func.h"\r
21 #include "physics_func.h"\r
22 #include "barrel.h"\r
23 #include "landscape.h"\r
24 \r
25 #ifdef _WIN32\r
26         #include <gl/gl.h>\r
27         #include <gl/glu.h>\r
28 #endif\r
29 \r
30 // ARA begin insert new code\r
31 #ifdef __ANDROID__\r
32         #include <EGL/egl.h>\r
33         #include <GLES/gl.h>\r
34 #endif\r
35 // ARA end\r
36 \r
37 #define SAMPLE_NAME "api_physics_effects/2_stable"\r
38 \r
39 static bool s_isRunning = true;\r
40 \r
41 int sceneId = 2;\r
42 bool simulating = false;\r
43 \r
44 int landscapeMeshId;\r
45 int convexMeshId;\r
46 \r
47 void render(void)\r
48 {\r
49         render_begin();\r
50         \r
51         for(int i=0;i<physics_get_num_rigidbodies();i++) {\r
52                 const PfxRigidState &state = physics_get_state(i);\r
53                 const PfxCollidable &coll = physics_get_collidable(i);\r
54 \r
55                 PfxTransform3 rbT(state.getOrientation(), state.getPosition());\r
56 \r
57                 PfxShapeIterator itrShape(coll);\r
58                 for(int j=0;j<coll.getNumShapes();j++,++itrShape) {\r
59                         const PfxShape &shape = *itrShape;\r
60                         PfxTransform3 offsetT = shape.getOffsetTransform();\r
61                         PfxTransform3 worldT = rbT * offsetT;\r
62 \r
63                         switch(shape.getType()) {\r
64                                 case kPfxShapeSphere:\r
65                                 render_sphere(\r
66                                         worldT,\r
67                                         PfxVector3(1,1,1),\r
68                                         PfxFloatInVec(shape.getSphere().m_radius));\r
69                                 break;\r
70 \r
71                                 case kPfxShapeBox:\r
72                                 render_box(\r
73                                         worldT,\r
74                                         PfxVector3(1,1,1),\r
75                                         shape.getBox().m_half);\r
76                                 break;\r
77 \r
78                                 case kPfxShapeCapsule:\r
79                                 render_capsule(\r
80                                         worldT,\r
81                                         PfxVector3(1,1,1),\r
82                                         PfxFloatInVec(shape.getCapsule().m_radius),\r
83                                         PfxFloatInVec(shape.getCapsule().m_halfLen));\r
84                                 break;\r
85 \r
86                                 case kPfxShapeCylinder:\r
87                                 render_cylinder(\r
88                                         worldT,\r
89                                         PfxVector3(1,1,1),\r
90                                         PfxFloatInVec(shape.getCylinder().m_radius),\r
91                                         PfxFloatInVec(shape.getCylinder().m_halfLen));\r
92                                 break;\r
93 \r
94                                 case kPfxShapeConvexMesh:\r
95                                 render_mesh(\r
96                                         worldT,\r
97                                         PfxVector3(1,1,1),\r
98                                         convexMeshId);\r
99                                 break;\r
100 \r
101                                 case kPfxShapeLargeTriMesh:\r
102                                 render_mesh(\r
103                                         worldT,\r
104                                         PfxVector3(1,1,1),\r
105                                         landscapeMeshId);\r
106                                 break;\r
107 \r
108                                 default:\r
109                                 break;\r
110                         }\r
111                 }\r
112         }\r
113 \r
114         render_end();\r
115 }\r
116 \r
117 int init(void)\r
118 {\r
119         perf_init();\r
120         ctrl_init();\r
121         render_init();\r
122         physics_init();\r
123 \r
124         landscapeMeshId = render_init_mesh(\r
125                 LargeMeshVtx,sizeof(float)*6,\r
126                 LargeMeshVtx+3,sizeof(float)*6,\r
127                 LargeMeshIdx,sizeof(unsigned short)*3,\r
128                 LargeMeshVtxCount,LargeMeshIdxCount/3);\r
129 \r
130         convexMeshId = render_init_mesh(\r
131                 BarrelVtx,sizeof(float)*6,\r
132                 BarrelVtx+3,sizeof(float)*6,\r
133                 BarrelIdx,sizeof(unsigned short)*3,\r
134                 BarrelVtxCount,BarrelIdxCount/3);\r
135 \r
136         return 0;\r
137 }\r
138 \r
139 static int shutdown(void)\r
140 {\r
141         ctrl_release();\r
142         render_release();\r
143         physics_release();\r
144         perf_release();\r
145 \r
146         return 0;\r
147 }\r
148 \r
149 void update(void)\r
150 {\r
151         float angX,angY,r;\r
152         render_get_view_angle(angX,angY,r);\r
153 \r
154         ctrl_update();\r
155         \r
156         if(ctrl_button_pressed(BTN_UP)) {\r
157                 angX -= 0.05f;\r
158                 if(angX < -1.4f) angX = -1.4f;\r
159                 if(angX > -0.01f) angX = -0.01f;\r
160         }\r
161 \r
162         if(ctrl_button_pressed(BTN_DOWN)) {\r
163                 angX += 0.05f;\r
164                 if(angX < -1.4f) angX = -1.4f;\r
165                 if(angX > -0.01f) angX = -0.01f;\r
166         }\r
167 \r
168         if(ctrl_button_pressed(BTN_LEFT)) {\r
169                 angY -= 0.05f;\r
170         }\r
171 \r
172         if(ctrl_button_pressed(BTN_RIGHT)) {\r
173                 angY += 0.05f;\r
174         }\r
175 \r
176         if(ctrl_button_pressed(BTN_ZOOM_OUT)) {\r
177                 r *= 1.1f;\r
178                 if(r > 500.0f) r = 500.0f;\r
179         }\r
180 \r
181         if(ctrl_button_pressed(BTN_ZOOM_IN)) {\r
182                 r *= 0.9f;\r
183                 if(r < 1.0f) r = 1.0f;\r
184         }\r
185 \r
186         if(ctrl_button_pressed(BTN_SCENE_RESET) == BTN_STAT_DOWN) {\r
187                 physics_create_scene(sceneId);\r
188         }\r
189 \r
190         if(ctrl_button_pressed(BTN_SCENE_NEXT) == BTN_STAT_DOWN) {\r
191                 physics_create_scene(++sceneId);\r
192         }\r
193 \r
194         if(ctrl_button_pressed(BTN_SIMULATION) == BTN_STAT_DOWN) {\r
195                 simulating = !simulating;\r
196         }\r
197 \r
198         if(ctrl_button_pressed(BTN_STEP) == BTN_STAT_DOWN) {\r
199                 simulating = true;\r
200         }\r
201         else if(ctrl_button_pressed(BTN_STEP) == BTN_STAT_UP || ctrl_button_pressed(BTN_STEP) == BTN_STAT_KEEP) {\r
202                 simulating = false;\r
203         }\r
204 \r
205         render_set_view_angle(angX,angY,r);\r
206 }\r
207 \r
208 #ifndef _WIN32\r
209 \r
210 // ARA begin insert new code\r
211 #ifdef  __ANDROID__\r
212 \r
213 ///////////////////////////////////////////////////////////////////////////////\r
214 // sceneChange\r
215 //\r
216 /// This function is used to change the physics scene on Android devices\r
217 ///////////////////////////////////////////////////////////////////////////////\r
218 void sceneChange()\r
219 {\r
220         physics_create_scene(sceneId++);\r
221 }\r
222 \r
223 #else   // __ANDROID__\r
224 // ARA end\r
225 \r
226 ///////////////////////////////////////////////////////////////////////////////\r
227 // Main\r
228 \r
229 int main(void)\r
230 {\r
231         init();\r
232 \r
233         physics_create_scene(sceneId);\r
234         \r
235         printf("## %s: INIT SUCCEEDED ##\n", SAMPLE_NAME);\r
236 \r
237         while (s_isRunning) {\r
238                 update();\r
239                 if(simulating) physics_simulate();\r
240                 render();\r
241 \r
242                 perf_sync();\r
243         }\r
244 \r
245         shutdown();\r
246 \r
247         printf("## %s: FINISHED ##\n", SAMPLE_NAME);\r
248 \r
249         return 0;\r
250 }\r
251 \r
252 // ARA begin insert new code\r
253 #endif  // __ANDROID__\r
254 // ARA end\r
255 \r
256 #else   // _WIN32\r
257 \r
258 ///////////////////////////////////////////////////////////////////////////////\r
259 // WinMain\r
260 \r
261 extern HDC hDC;\r
262 extern HGLRC hRC;\r
263 HWND hWnd;\r
264 HINSTANCE hInstance;\r
265 \r
266 void releaseWindow()\r
267 {\r
268         if(hRC) {\r
269                 wglMakeCurrent(0,0);\r
270                 wglDeleteContext(hRC);\r
271         }\r
272         \r
273         if(hDC) ReleaseDC(hWnd,hDC);\r
274         if(hWnd) DestroyWindow(hWnd);\r
275         \r
276         UnregisterClass(SAMPLE_NAME,hInstance);\r
277 }\r
278 \r
279 LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM     wParam,LPARAM lParam)\r
280 {\r
281         switch(uMsg) {\r
282                 case WM_SYSCOMMAND:\r
283                 {\r
284                         switch (wParam) {\r
285                                 case SC_SCREENSAVE:\r
286                                 case SC_MONITORPOWER:\r
287                                 return 0;\r
288                         }\r
289                         break;\r
290                 }\r
291 \r
292                 case WM_CLOSE:\r
293                 PostQuitMessage(0);\r
294                 return 0;\r
295 \r
296                 case WM_SIZE:\r
297                 render_resize(LOWORD(lParam),HIWORD(lParam));\r
298                 return 0;\r
299         }\r
300 \r
301         return DefWindowProc(hWnd,uMsg,wParam,lParam);\r
302 }\r
303 \r
304 bool createWindow(char* title, int width, int height)\r
305 {\r
306         WNDCLASS wc;\r
307         RECT rect;\r
308         rect.left=0;\r
309         rect.right=width;\r
310         rect.top=0;\r
311         rect.bottom=height;\r
312 \r
313         hInstance = GetModuleHandle(NULL);\r
314         wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;\r
315         wc.lpfnWndProc = (WNDPROC) WndProc;\r
316         wc.cbClsExtra = 0;\r
317         wc.cbWndExtra = 0;\r
318         wc.hInstance = hInstance;\r
319         wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);\r
320         wc.hCursor = LoadCursor(NULL, IDC_ARROW);\r
321         wc.hbrBackground = NULL;\r
322         wc.lpszMenuName = NULL;\r
323         wc.lpszClassName = SAMPLE_NAME;\r
324 \r
325         if(!RegisterClass(&wc)) {\r
326                 return false;\r
327         }\r
328 \r
329         AdjustWindowRectEx(&rect, WS_OVERLAPPEDWINDOW, FALSE, WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);\r
330 \r
331         if(!(hWnd=CreateWindowEx(WS_EX_APPWINDOW|WS_EX_WINDOWEDGE,SAMPLE_NAME,title,\r
332                                                         WS_OVERLAPPEDWINDOW|WS_CLIPSIBLINGS|WS_CLIPCHILDREN,\r
333                                                         0,0,rect.right-rect.left,rect.bottom-rect.top,\r
334                                                         NULL,NULL,hInstance,NULL))) {\r
335                 releaseWindow();\r
336                 return false;\r
337         }\r
338 \r
339     static PIXELFORMATDESCRIPTOR pfd = {\r
340                 sizeof(PIXELFORMATDESCRIPTOR),\r
341                 1,\r
342                 PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,\r
343                 PFD_TYPE_RGBA,\r
344                 32,\r
345                 0, 0,\r
346                 0, 0,\r
347                 0, 0,\r
348                 0, 0,\r
349                 0,\r
350                 0, 0, 0, 0,\r
351                 32,\r
352                 0,\r
353                 0,\r
354                 PFD_MAIN_PLANE,\r
355                 0,\r
356                 0, 0, 0\r
357     };\r
358         \r
359         if(!(hDC=GetDC(hWnd)))\r
360         {\r
361                 releaseWindow();\r
362                 OutputDebugString("");\r
363                 return FALSE;\r
364         }\r
365         \r
366         int pixelformat;\r
367         \r
368     if ( (pixelformat = ChoosePixelFormat(hDC, &pfd)) == 0 ){\r
369                 OutputDebugString("ChoosePixelFormat Failed....");\r
370         return FALSE;\r
371     }\r
372 \r
373     if (SetPixelFormat(hDC, pixelformat, &pfd) == FALSE){\r
374                 OutputDebugString("SetPixelFormat Failed....");\r
375         return FALSE;\r
376     }\r
377 \r
378         if (!(hRC=wglCreateContext(hDC))){\r
379                 OutputDebugString("Creating HGLRC Failed....");\r
380                 return FALSE;\r
381         }\r
382         \r
383         // Set Vsync\r
384         //BOOL (WINAPI *wglSwapIntervalEXT)(int) = NULL;\r
385 \r
386         //if(strstr((char*)glGetString( GL_EXTENSIONS ),"WGL_EXT_swap_control")== 0) {\r
387         //}\r
388         //else {\r
389                 //wglSwapIntervalEXT = (BOOL (WINAPI*)(int))wglGetProcAddress("wglSwapIntervalEXT");\r
390                 //if(wglSwapIntervalEXT) wglSwapIntervalEXT(1);\r
391         //}\r
392         \r
393         wglMakeCurrent(hDC,hRC);\r
394         \r
395         ShowWindow(hWnd,SW_SHOW);\r
396         SetForegroundWindow(hWnd);\r
397         SetFocus(hWnd);\r
398 \r
399         render_resize(width, height);\r
400         \r
401         glClearColor(0.0f,0.0f,0.0f,0.0f);\r
402         glClearDepth(1.0f);\r
403         \r
404         return TRUE;\r
405 }\r
406 \r
407 int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)\r
408 {\r
409         if(!createWindow(SAMPLE_NAME,DISPLAY_WIDTH,DISPLAY_HEIGHT)) {\r
410                 MessageBox(NULL,"Can't create gl window.","ERROR",MB_OK|MB_ICONEXCLAMATION);\r
411                 return 0;\r
412         }\r
413         \r
414         init();\r
415         \r
416         physics_create_scene(sceneId);\r
417         \r
418         SCE_PFX_PRINTF("## %s: INIT SUCCEEDED ##\n", SAMPLE_NAME);\r
419         \r
420         MSG msg;\r
421         while(s_isRunning) {\r
422                 if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) {\r
423                         if(msg.message==WM_QUIT) {\r
424                                 s_isRunning = false;\r
425                         }\r
426                         else {\r
427                                 TranslateMessage(&msg);\r
428                                 DispatchMessage(&msg);\r
429                         }\r
430                 }\r
431                 else {\r
432                         update();\r
433                         if(simulating) physics_simulate();\r
434                         render();\r
435 \r
436                         perf_sync();\r
437                 }\r
438         }\r
439 \r
440         shutdown();\r
441 \r
442         SCE_PFX_PRINTF("## %s: FINISHED ##\n", SAMPLE_NAME);\r
443 \r
444         releaseWindow();\r
445         return (msg.wParam);\r
446 }\r
447 \r
448 #endif\r