1
0

HelloWorld.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. //Basiques
  2. #include <iostream>
  3. #include <cmath>
  4. #include <vector>
  5. //SDL
  6. #include <SDL/SDL.h>
  7. #include <SDL/SDL_rotozoom.h>
  8. #include <SDL/SDL_gfxPrimitives.h>
  9. #undef main
  10. //Box2D
  11. #include <box2d/box2d.h>
  12. int main(int argc, char** argv)
  13. {
  14. /// [0] Box2D
  15. // Trucs
  16. B2_NOT_USED(argc);
  17. B2_NOT_USED(argv);
  18. // Define the gravity vector.
  19. b2Vec2 gravity(0.0f, 10.0f);
  20. // Construct a world object, which will hold and simulate the rigid bodies.
  21. b2World world(gravity);
  22. float multi( 100.0f ); // Pour passer de Box2D à pixels
  23. int incr( 0 );
  24. // Define the ground body.
  25. /*b2BodyDef solDef;
  26. solDef.position.Set(0.0f, 6.0f);
  27. b2Body* solBody = world.CreateBody(&solDef);
  28. b2PolygonShape solBox;
  29. solBox.SetAsBox(50.0f, 1.0f);
  30. solBody->CreateFixture(&solBox, 0.0f);*/
  31. // Define the dynamic body. We set its position and call the body factory.
  32. std::vector<b2Body*> tbody;
  33. float xstart( 3.0f );
  34. float zoom( 2.0f );
  35. b2BodyDef bodyDef;
  36. bodyDef.type = b2_dynamicBody;
  37. // Define another box shape for our dynamic body.
  38. b2PolygonShape dynamicBox;
  39. b2FixtureDef fixtureDef;
  40. fixtureDef.shape = &dynamicBox;
  41. fixtureDef.density = 1.0f;
  42. fixtureDef.friction = 0.3f;
  43. // Prepare for simulation
  44. float timeStep = 1.0f / 60.0f;
  45. int32 velocityIterations = 8;
  46. int32 positionIterations = 3;
  47. // Variables
  48. b2Vec2 position;
  49. float angle;
  50. bool pause( false );
  51. bool enable_rotation( true );
  52. std::string msg("Victor");
  53. /// [1] Démarrage
  54. // [1.1] Démarrages SDL
  55. if ( SDL_Init( SDL_INIT_VIDEO ) < 0)
  56. {
  57. std::cout << "Impossible d'initialiser la SDL: " << SDL_GetError() << std::endl;
  58. return 1;
  59. }
  60. // [1.2] Préparation de fermeture
  61. atexit(SDL_Quit);
  62. // [1.3] Para-fenêtre
  63. SDL_WM_SetCaption("Box2DTests", 0);
  64. /// [2] Préparation des composants
  65. // [2.1] Préparation de la fenêtre
  66. SDL_Surface* screen = SDL_SetVideoMode(600, 600, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
  67. if ( !screen )
  68. {
  69. std::cout << "Bug à l'initialisation: " << SDL_GetError() << std::endl;
  70. return 1;
  71. }
  72. // [2.2] Préparation variables
  73. SDL_Rect mouse({ 400, 200, 0, 0 });
  74. SDL_Rect pxpos({ 400, 200, 0, 0 });
  75. std::vector< SDL_Surface* > trot ;
  76. std::vector< SDL_Surface* > timg ;
  77. // [2.3] Préparation surfaces
  78. SDL_Surface* blc = 0x0;
  79. SDL_Surface* bar = 0x0;
  80. SDL_Surface* ice = 0x0;
  81. SDL_Surface* heavy = 0x0;
  82. SDL_Surface* slim = 0x0;
  83. SDL_Surface* megaSlim = 0x0;
  84. SDL_Surface* zero = 0x0;
  85. SDL_Surface* anti = 0x0;
  86. blc = SDL_LoadBMP("Mur.bmp");
  87. bar = SDL_LoadBMP("Bar.bmp");
  88. ice = SDL_LoadBMP("Glace.bmp");
  89. heavy = SDL_LoadBMP("Heavy.bmp");
  90. slim = SDL_LoadBMP("Slim.bmp");
  91. megaSlim = SDL_LoadBMP("MegaSlim.bmp");
  92. zero = SDL_LoadBMP("Zero.bmp");
  93. anti = SDL_LoadBMP("Anti.bmp");
  94. if ( !blc || !bar || !ice || !heavy || !slim || !megaSlim || !zero || !anti )
  95. std::cout << "Pb avec la texture." << std::endl;
  96. // [2.4] Préparation du temps
  97. Uint32 tprev(0), twait( timeStep * 1000.0f );
  98. std::cout << twait << std::endl ;
  99. // Define the edge body
  100. b2BodyDef areaDef;
  101. areaDef.position.Set(0.0f, 0.0f);
  102. b2Body* areaBody = world.CreateBody(&areaDef);
  103. b2Vec2 vs[4];
  104. float areah( (float)screen->h / multi );
  105. float areaw( (float)screen->w / multi );
  106. vs[0].Set( 0.0f, 0.0f );
  107. vs[1].Set( 0.0f, areah );
  108. vs[2].Set( areaw, areah );
  109. vs[3].Set( areaw, 0.0f );
  110. b2ChainShape chain;
  111. chain.CreateLoop(vs, 4);
  112. areaBody->CreateFixture(&chain, 0.0f);
  113. /// [3] Boucle principale
  114. bool done = false;
  115. SDL_Event event;
  116. while (!done)
  117. {
  118. // [3.1] Gestion évènements
  119. while (SDL_PollEvent(&event))
  120. {
  121. switch (event.type)
  122. {
  123. case SDL_QUIT:
  124. done = true;
  125. break;
  126. case SDL_KEYDOWN:
  127. switch( event.key.keysym.sym )
  128. {
  129. case SDLK_ESCAPE :
  130. done = true;
  131. break;
  132. case SDLK_BACKSPACE :
  133. for ( unsigned int i(0); i < tbody.size(); i ++ )
  134. {
  135. world.DestroyBody( tbody[i] );
  136. tbody[i] = 0x0 ;
  137. }
  138. tbody.clear();
  139. for ( unsigned int i(0); i < trot.size(); i ++ )
  140. {
  141. SDL_FreeSurface( trot[i] ) ;
  142. }
  143. trot.clear();
  144. timg.clear();
  145. std::cout << std::endl << "Deleting all blocks." << std::endl << std::endl;
  146. break;
  147. case SDLK_SPACE :
  148. pause = !pause ;
  149. break;
  150. case SDLK_LEFT :
  151. enable_rotation = !enable_rotation ;
  152. break;
  153. case SDLK_UP :
  154. std::cout << "SDLK_UP" << std::endl;
  155. break;
  156. case SDLK_a :
  157. dynamicBox.SetAsBox( (float)blc->w * zoom / multi / 2 , (float)blc->h * zoom / multi / 2 );
  158. bodyDef.position.Set( (float)mouse.x / multi, (float)mouse.y / multi );
  159. bodyDef.gravityScale = 1.0f;
  160. fixtureDef.density = 1.0f;
  161. fixtureDef.friction = 0.3f;
  162. fixtureDef.restitution = 0.0f;
  163. tbody.push_back( 0x0 );
  164. tbody.back() = world.CreateBody(&bodyDef);
  165. tbody.back()->CreateFixture(&fixtureDef);
  166. timg.push_back( blc );
  167. trot.push_back( rotozoomSurface( blc, 0.0f, 2.0f, 0 ) ) ;
  168. std::cout << "Standard bloc number " << tbody.size() << "." << std::endl;
  169. break;
  170. case SDLK_q :
  171. dynamicBox.SetAsBox( (float)bar->w * zoom / multi / 2 , (float)bar->h * zoom / multi / 2 );
  172. bodyDef.position.Set( (float)mouse.x / multi, (float)mouse.y / multi );
  173. bodyDef.gravityScale = 1.0f;
  174. fixtureDef.density = 1.0f;
  175. fixtureDef.friction = 0.3f;
  176. fixtureDef.restitution = 0.0f;
  177. tbody.push_back( 0x0 );
  178. tbody.back() = world.CreateBody(&bodyDef);
  179. tbody.back()->CreateFixture(&fixtureDef);
  180. timg.push_back( bar );
  181. trot.push_back( rotozoomSurface( bar, 0.0f, 2.0f, 0 ) ) ;
  182. std::cout << "Standard bar number " << tbody.size() << "." << std::endl;
  183. break;
  184. case SDLK_z :
  185. case SDLK_w :
  186. dynamicBox.SetAsBox( (float)ice->w * zoom / multi / 2 , (float)ice->h * zoom / multi / 2 );
  187. bodyDef.position.Set( (float)mouse.x / multi, (float)mouse.y / multi );
  188. bodyDef.gravityScale = 1.0f;
  189. fixtureDef.density = 0.5f;
  190. fixtureDef.friction = 0.005f;
  191. fixtureDef.restitution = 0.0f;
  192. tbody.push_back( 0x0 );
  193. tbody.back() = world.CreateBody(&bodyDef);
  194. tbody.back()->CreateFixture(&fixtureDef);
  195. timg.push_back( ice );
  196. trot.push_back( rotozoomSurface( ice, 0.0f, 2.0f, 0 ) ) ;
  197. std::cout << "Bloc of ice " << tbody.size() <<" : able to slide !" << std::endl;
  198. break;
  199. case SDLK_e :
  200. dynamicBox.SetAsBox( (float)heavy->w * zoom / multi / 2 , (float)heavy->h * zoom / multi / 2 );
  201. bodyDef.position.Set( (float)mouse.x / multi, (float)mouse.y / multi );
  202. bodyDef.gravityScale = 1.0f;
  203. fixtureDef.density = 100.0f;
  204. fixtureDef.friction = 0.3f;
  205. fixtureDef.restitution = 0.0f;
  206. tbody.push_back( 0x0 );
  207. tbody.back() = world.CreateBody(&bodyDef);
  208. tbody.back()->CreateFixture(&fixtureDef);
  209. timg.push_back( heavy );
  210. trot.push_back( rotozoomSurface( heavy, 0.0f, 2.0f, 0 ) ) ;
  211. std::cout << "Heavy bloc " << tbody.size() << " : very massive bloc." << std::endl;
  212. break;
  213. case SDLK_r :
  214. dynamicBox.SetAsBox( (float)slim->w * zoom / multi / 2 , (float)slim->h * zoom / multi / 2 );
  215. bodyDef.position.Set( (float)mouse.x / multi, (float)mouse.y / multi );
  216. bodyDef.gravityScale = 1.0f;
  217. fixtureDef.density = 1.0f;
  218. fixtureDef.friction = 0.3f;
  219. fixtureDef.restitution = 0.95f;
  220. tbody.push_back( 0x0 );
  221. tbody.back() = world.CreateBody(&bodyDef);
  222. tbody.back()->CreateFixture(&fixtureDef);
  223. timg.push_back( slim );
  224. trot.push_back( rotozoomSurface( slim, 0.0f, 2.0f, 0 ) ) ;
  225. std::cout << "Slim bloc " << tbody.size() << " : able to bounce !" << std::endl;
  226. break;
  227. case SDLK_t :
  228. dynamicBox.SetAsBox( (float)megaSlim->w * zoom / multi / 2 , (float)megaSlim->h * zoom / multi / 2 );
  229. bodyDef.position.Set( (float)mouse.x / multi, (float)mouse.y / multi );
  230. bodyDef.gravityScale = 1.0f;
  231. fixtureDef.density = 1.0f;
  232. fixtureDef.friction = 0.3f;
  233. fixtureDef.restitution = 1.4f;
  234. tbody.push_back( 0x0 );
  235. tbody.back() = world.CreateBody(&bodyDef);
  236. tbody.back()->CreateFixture(&fixtureDef);
  237. timg.push_back( megaSlim );
  238. trot.push_back( rotozoomSurface( megaSlim, 0.0f, 2.0f, 0 ) ) ;
  239. std::cout << "MegaSlim bloc " << tbody.size() << " : able to bounce over physic limits !" << std::endl;
  240. break;
  241. case SDLK_y :
  242. dynamicBox.SetAsBox( (float)zero->w * zoom / multi / 2 , (float)zero->h * zoom / multi / 2 );
  243. bodyDef.position.Set( (float)mouse.x / multi, (float)mouse.y / multi );
  244. bodyDef.gravityScale = 0.0f;
  245. fixtureDef.density = 1.0f;
  246. fixtureDef.friction = 0.3f;
  247. fixtureDef.restitution = 0.0f;
  248. tbody.push_back( 0x0 );
  249. tbody.back() = world.CreateBody(&bodyDef);
  250. tbody.back()->CreateFixture(&fixtureDef);
  251. timg.push_back( zero );
  252. trot.push_back( rotozoomSurface( zero, 0.0f, 2.0f, 0 ) ) ;
  253. std::cout << "Zero gravity bloc " << tbody.size() << " : able to bypass gravity !" << std::endl;
  254. break;
  255. case SDLK_u :
  256. dynamicBox.SetAsBox( (float)anti->w * zoom / multi / 2 , (float)anti->h * zoom / multi / 2 );
  257. bodyDef.position.Set( (float)mouse.x / multi, (float)mouse.y / multi );
  258. bodyDef.gravityScale = -1.0f;
  259. fixtureDef.density = 1.0f;
  260. fixtureDef.friction = 0.3f;
  261. fixtureDef.restitution = 0.0f;
  262. tbody.push_back( 0x0 );
  263. tbody.back() = world.CreateBody(&bodyDef);
  264. tbody.back()->CreateFixture(&fixtureDef);
  265. timg.push_back( anti );
  266. trot.push_back( rotozoomSurface( anti, 0.0f, 2.0f, 0 ) ) ;
  267. std::cout << "Anti gravity bloc number " << tbody.size() << " : counter gravity !" << std::endl;
  268. break;
  269. }
  270. break;
  271. case SDL_MOUSEMOTION:
  272. mouse.x = event.motion.x ;
  273. mouse.y = event.motion.y ;
  274. break;
  275. case SDL_MOUSEBUTTONDOWN:
  276. std::cout << "SDL_MOUSEBUTTONDOWN" << std::endl;
  277. break;
  278. case SDL_MOUSEBUTTONUP:
  279. std::cout << "SDL_MOUSEBUTTONUP" << std::endl;
  280. break;
  281. } // end switch event type
  282. } // end of message processing
  283. // [3.2] Calculs
  284. // It is generally best to keep the time step and iterations fixed.
  285. if ( !pause )
  286. world.Step(timeStep, velocityIterations, positionIterations);
  287. // [3.3] Dessin des composants
  288. SDL_FillRect(screen, 0, 0x000000);
  289. for ( unsigned int i(0); i < tbody.size(); i ++ )
  290. {
  291. position = tbody[i]->GetPosition();
  292. angle = tbody[i]->GetAngle();
  293. if ( /*tbody[i]->IsAwake() &&*/ enable_rotation )
  294. {
  295. SDL_FreeSurface( trot[i] ) ;
  296. trot[i] = rotozoomSurface( timg[i], -angle * 180.0f / b2_pi, zoom + 0.15f, 0 );
  297. }
  298. pxpos.x = position.x * multi - trot[i]->w / 2 ;
  299. pxpos.y = position.y * multi - trot[i]->h / 2 ;
  300. SDL_BlitSurface( trot[i], 0x0, screen, &pxpos);
  301. }
  302. // Messages
  303. msg = "Ratio CPU : " ;
  304. msg += std::to_string( ((float)SDL_GetTicks() - tprev) * 100 / twait ) ;
  305. msg += " pourcents." ;
  306. stringRGBA( screen, 16, 16, msg.c_str(), 0, 255, 0, 255 );
  307. msg = "Nombre de blocs : " ;
  308. msg += std::to_string( trot.size() ) ;
  309. msg += "." ;
  310. stringRGBA( screen, 16, 25, msg.c_str(), 0, 255, 0, 255 );
  311. msg = "Nombre de décès : " ;
  312. msg += std::to_string( incr ) ;
  313. msg += "." ;
  314. stringRGBA( screen, 16, 34, msg.c_str(), 0, 255, 0, 255 );
  315. msg = "Rotations : " ;
  316. msg += std::to_string( enable_rotation ) ;
  317. msg += "." ;
  318. stringRGBA( screen, 16, 43, msg.c_str(), 0, 255, 0, 255 );
  319. SDL_Flip(screen);
  320. // [3.4] Temps
  321. if ( SDL_GetTicks() - tprev > twait )
  322. {
  323. incr ++;
  324. }
  325. while( SDL_GetTicks() - tprev < twait )
  326. {
  327. if ( SDL_GetTicks() - tprev < twait/2 )
  328. SDL_Delay( twait/3 );
  329. }
  330. tprev = SDL_GetTicks();
  331. } //fin bcl principale
  332. ///[4] Destruction des composants
  333. SDL_FreeSurface(screen);
  334. SDL_FreeSurface(blc);
  335. SDL_FreeSurface(bar);
  336. SDL_FreeSurface(ice);
  337. SDL_FreeSurface(heavy);
  338. SDL_FreeSurface(slim);
  339. SDL_FreeSurface(megaSlim);
  340. SDL_FreeSurface(zero);
  341. SDL_FreeSurface(anti);
  342. return 0;
  343. }