main.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // Basiques
  2. # include <iostream>
  3. # include <cmath>
  4. # include <vector>
  5. // Random
  6. # include <ctime>
  7. # include <cstdlib>
  8. // SDL
  9. # include <SDL/SDL.h>
  10. # undef main
  11. # include <SDL/SDL_rotozoom.h>
  12. # include <SDL/SDL_gfxPrimitives.h>
  13. // bBox2D
  14. # include <box2d/box2d.h>
  15. // Creater
  16. # include "creater.h"
  17. int main(int argc, char** argv)
  18. {
  19. /// [1] Démarrage
  20. // [1.0] Démarrage aléatoire
  21. srand( time(0) );
  22. // [1.1] Démarrages SDL
  23. if ( SDL_Init( SDL_INIT_VIDEO ) < 0)
  24. {
  25. std::cout << "Impossible d'initialiser la SDL: " << SDL_GetError() << std::endl;
  26. return 1;
  27. }
  28. // [1.2] Préparation de fermeture
  29. atexit(SDL_Quit);
  30. // [1.3] Para-fenêtre
  31. SDL_WM_SetCaption("Box2DTests", 0);
  32. /// [2] Préparation des composants SDL
  33. // [2.1] Préparation de la fenêtre
  34. SDL_Surface* screen = SDL_SetVideoMode(600, 600, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
  35. if ( !screen )
  36. {
  37. std::cout << "Bug à l'initialisation: " << SDL_GetError() << std::endl;
  38. return 1;
  39. }
  40. // [2.2] Préparation variables
  41. SDL_Rect mouse({ 400, 200, 0, 0 });
  42. SDL_Rect pxpos({ 400, 200, 0, 0 });
  43. // [2.3] Préparation surfaces
  44. SDL_Surface* blc = 0x0;
  45. SDL_Surface* bar = 0x0;
  46. SDL_Surface* wheel = 0x0;
  47. SDL_Surface* tempo = 0x0;
  48. blc = SDL_LoadBMP("Mur.bmp");
  49. bar = SDL_LoadBMP("Bar.bmp");
  50. wheel = SDL_LoadBMP("Wheel.bmp");
  51. if ( !blc || !bar || !wheel )
  52. std::cout << "Pb avec la texture." << std::endl;
  53. // [2.4] Préparation du temps
  54. Uint32 frameRate( 60 );
  55. Uint32 tprev(0), twait( 1000 / frameRate );
  56. bool pause( false );
  57. // [2.5] Préparation des messages
  58. std::string msg("Victor");
  59. /// [3] Box2D
  60. // Trucs
  61. B2_NOT_USED(argc);
  62. B2_NOT_USED(argv);
  63. // Construct a world object
  64. b2Vec2 gravity(0.0f, 10.0f);
  65. b2World world(gravity);
  66. // Define the dynamic body container
  67. std::vector<b2Body*> tbody;
  68. // Simulation settings
  69. float timeStep = 1.0f / frameRate;
  70. int32 velocityIterations = 8;
  71. int32 positionIterations = 3;
  72. // Variables
  73. b2Vec2 position;
  74. float angle;
  75. // Define the edge body
  76. b2BodyDef areaDef;
  77. areaDef.position.Set(0.0f, 0.0f);
  78. b2Body* areaBody = world.CreateBody(&areaDef);
  79. b2Vec2 vs[4];
  80. float areah( (float)screen->h / MULTI );
  81. float areaw( (float)screen->w / MULTI );
  82. vs[0].Set( 0.0f, 0.0f );
  83. vs[1].Set( 0.0f, areah );
  84. vs[2].Set( areaw, areah );
  85. vs[3].Set( areaw, 0.0f );
  86. b2ChainShape chain;
  87. chain.CreateLoop(vs, 4);
  88. areaBody->CreateFixture(&chain, 0.0f);
  89. // Joint stuff
  90. std::vector<b2Joint*> tjoint ;
  91. b2Vec2 posA, posB;
  92. Sint16 x1, y1, x2, y2;
  93. Uint16 constraint;
  94. /// [4] Boucle principale
  95. bool done = false;
  96. SDL_Event event;
  97. while (!done)
  98. {
  99. // [4.1] Gestion évènements
  100. while (SDL_PollEvent(&event))
  101. {
  102. switch (event.type)
  103. {
  104. case SDL_QUIT:
  105. done = true;
  106. break;
  107. case SDL_KEYDOWN:
  108. switch( event.key.keysym.sym )
  109. {
  110. case SDLK_ESCAPE :
  111. done = true;
  112. break;
  113. case SDLK_BACKSPACE :
  114. // Destroy joints first of all
  115. for ( unsigned int i(0); i < tjoint.size(); i ++ )
  116. {
  117. world.DestroyJoint( tjoint[i] );
  118. tjoint[i] = 0x0 ;
  119. }
  120. tjoint.clear();
  121. // Destroy bodies
  122. for ( unsigned int i(0); i < tbody.size(); i ++ )
  123. {
  124. world.DestroyBody( tbody[i] );
  125. tbody[i] = 0x0 ;
  126. }
  127. tbody.clear();
  128. std::cout << std::endl << "Deleting all entities." << std::endl << std::endl;
  129. break;
  130. case SDLK_SPACE :
  131. pause = !pause ;
  132. break;
  133. case SDLK_a :
  134. createBloc( world, tbody, blc, (float)(rand() % 600) / MULTI, 1.5f + (float)(rand() % 100) / MULTI );
  135. break;
  136. case SDLK_z :
  137. link2ByDist( world, tbody, tjoint );
  138. break;
  139. case SDLK_e :
  140. link3ByDist( world, tbody, tjoint );
  141. break;
  142. case SDLK_UP :
  143. std::cout << "SDLK_UP" << std::endl;
  144. break;
  145. }
  146. break;
  147. case SDL_MOUSEMOTION:
  148. mouse.x = event.motion.x ;
  149. mouse.y = event.motion.y ;
  150. break;
  151. case SDL_MOUSEBUTTONDOWN:
  152. std::cout << "SDL_MOUSEBUTTONDOWN" << std::endl;
  153. break;
  154. case SDL_MOUSEBUTTONUP:
  155. std::cout << "SDL_MOUSEBUTTONUP" << std::endl;
  156. break;
  157. } // end switch event type
  158. } // end of message processing
  159. // [4.2] Calculs
  160. // It is generally best to keep the time step and iterations fixed.
  161. if ( !pause )
  162. world.Step(timeStep, velocityIterations, positionIterations);
  163. // [4.3] Dessin des composants
  164. SDL_FillRect(screen, 0, 0x000000);
  165. for ( unsigned int i(0); i < tbody.size(); i ++ )
  166. {
  167. position = tbody[i]->GetPosition();
  168. angle = tbody[i]->GetAngle();
  169. tempo = (SDL_Surface*) tbody[i]->GetUserData().pointer;
  170. tempo = rotozoomSurface( tempo, -angle * 180.0f / b2_pi, 2.15f, 0 );
  171. pxpos.x = position.x * MULTI - tempo->w / 2 ;
  172. pxpos.y = position.y * MULTI - tempo->h / 2 ;
  173. SDL_BlitSurface( tempo, 0x0, screen, &pxpos);
  174. SDL_FreeSurface( tempo );
  175. tempo = 0x0;
  176. }
  177. for ( unsigned int i(0); i < tjoint.size(); i ++ )
  178. {
  179. posA = tjoint[i]->GetAnchorA();
  180. posB = tjoint[i]->GetAnchorB();
  181. x1 = posA.x * MULTI ;
  182. y1 = posA.y * MULTI ;
  183. x2 = posB.x * MULTI ;
  184. y2 = posB.y * MULTI ;
  185. constraint = tjoint[i]->GetReactionForce( 1.0f / timeStep ).Length() * 100.0f;
  186. if ( constraint > 255 )
  187. constraint = 255 ;
  188. lineRGBA (screen, x1, y1, x2, y2, constraint, 0, 255 - constraint, 255);
  189. }
  190. // Messages
  191. msg = "Ratio CPU : " ;
  192. msg += std::to_string( ((float)SDL_GetTicks() - tprev) * 100 / twait ) ;
  193. msg += " pourcents." ;
  194. stringRGBA( screen, 16, 16, msg.c_str(), 0, 255, 0, 255 );
  195. msg = "Nombre de blocs : " ;
  196. msg += std::to_string( tbody.size() ) ;
  197. msg += "." ;
  198. stringRGBA( screen, 16, 25, msg.c_str(), 0, 255, 0, 255 );
  199. SDL_Flip(screen);
  200. // [4.4] Temps
  201. while( SDL_GetTicks() - tprev < twait )
  202. {
  203. if ( SDL_GetTicks() - tprev < twait/2 )
  204. SDL_Delay( twait/3 );
  205. }
  206. tprev = SDL_GetTicks();
  207. } //fin bcl principale
  208. ///[5] Destruction des composants
  209. SDL_FreeSurface(screen);
  210. SDL_FreeSurface(blc);
  211. SDL_FreeSurface(bar);
  212. SDL_FreeSurface(wheel);
  213. return 0;
  214. }