1
0

main.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. # define MULTI 100.0f
  16. // Local
  17. # include "Car.h"
  18. #include "Terrain.h"
  19. int main(int argc, char** argv)
  20. {
  21. /// [1] Démarrage
  22. // [1.0] Démarrage aléatoire
  23. srand( time(0) );
  24. // [1.1] Démarrages SDL
  25. if ( SDL_Init( SDL_INIT_VIDEO ) < 0)
  26. {
  27. std::cout << "Impossible d'initialiser la SDL: " << SDL_GetError() << std::endl;
  28. return 1;
  29. }
  30. // [1.2] Préparation de fermeture
  31. atexit(SDL_Quit);
  32. // [1.3] Para-fenêtre
  33. SDL_WM_SetCaption("Box2DTests", 0);
  34. /// [2] Préparation des composants SDL
  35. // [2.1] Préparation de la fenêtre
  36. SDL_Surface* screen = SDL_SetVideoMode(1200, 600, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
  37. if ( !screen )
  38. {
  39. std::cout << "Bug à l'initialisation: " << SDL_GetError() << std::endl;
  40. return 1;
  41. }
  42. // [2.2] Préparation variables
  43. SDL_Rect mouse({ 400, 200, 0, 0 });
  44. SDL_Rect pxpos({ 400, 200, 0, 0 });
  45. // [2.3] Préparation surfaces
  46. SDL_Surface* tempo( 0x0 );
  47. // [2.4] Préparation du temps
  48. Uint32 frameRate( 60 );
  49. Uint32 tprev(0), twait( 1000 / frameRate );
  50. bool pause( false );
  51. // [2.5] Préparation des messages
  52. std::string msg("Victor");
  53. /// [3] Box2D
  54. // Trucs
  55. B2_NOT_USED(argc);
  56. B2_NOT_USED(argv);
  57. // Construct a world object
  58. b2Vec2 gravity( 0.0f, 10.0f );
  59. b2World world(gravity);
  60. // Simulation settings
  61. float timeStep = 1.0f / frameRate;
  62. int32 velocityIterations = 8;
  63. int32 positionIterations = 3;
  64. float angle ;
  65. b2Vec2 position, ref ;
  66. // Define the edge body
  67. float areah( (float)screen->h / MULTI );
  68. float areaw( (float)screen->w / MULTI );
  69. # define NB_PLOT 400
  70. Terrain myTerrain( NB_PLOT );
  71. myTerrain.addPlot( 0.0f, 0.0f );
  72. myTerrain.addPlot( 0.0f, areah );
  73. for ( unsigned int i(2); i < NB_PLOT ; i ++ )
  74. {
  75. myTerrain.addPlot( 1.0f * i, areah + 0.03f * i * sin( 0.35f * i ) ) ;
  76. }
  77. myTerrain.build( world );
  78. // Voiture
  79. Car myCar;
  80. myCar.init( world, 3.0f, 1.0f );
  81. /// [4] Boucle principale
  82. bool done = false;
  83. SDL_Event event;
  84. while (!done)
  85. {
  86. // [4.1] Gestion évènements
  87. while (SDL_PollEvent(&event))
  88. {
  89. switch (event.type)
  90. {
  91. case SDL_QUIT:
  92. done = true;
  93. break;
  94. case SDL_KEYDOWN:
  95. switch( event.key.keysym.sym )
  96. {
  97. case SDLK_ESCAPE :
  98. done = true;
  99. break;
  100. case SDLK_BACKSPACE :
  101. // Destroy joints first of all
  102. std::cout << std::endl << "Deleting all entities." << std::endl << std::endl;
  103. break;
  104. case SDLK_SPACE :
  105. pause = !pause ;
  106. break;
  107. case SDLK_RIGHT :
  108. myCar.drive( GO );
  109. break;
  110. case SDLK_LEFT :
  111. myCar.drive( REVERSE );
  112. break;
  113. case SDLK_DOWN :
  114. myCar.drive( BREAK );
  115. break;
  116. }
  117. break;
  118. case SDL_KEYUP:
  119. switch( event.key.keysym.sym )
  120. {
  121. case SDLK_LEFT :
  122. case SDLK_RIGHT :
  123. case SDLK_DOWN :
  124. myCar.drive( FREE );
  125. break;
  126. }
  127. break;
  128. case SDL_MOUSEMOTION:
  129. mouse.x = event.motion.x ;
  130. mouse.y = event.motion.y ;
  131. break;
  132. case SDL_MOUSEBUTTONDOWN:
  133. std::cout << "SDL_MOUSEBUTTONDOWN" << std::endl;
  134. break;
  135. case SDL_MOUSEBUTTONUP:
  136. std::cout << "SDL_MOUSEBUTTONUP" << std::endl;
  137. break;
  138. } // end switch event type
  139. } // end of message processing
  140. // [4.2] Calculs
  141. // It is generally best to keep the time step and iterations fixed.
  142. /*motor->SetMotorSpeed( speed );
  143. motor->SetMaxMotorTorque( torque );*/
  144. if ( !pause )
  145. {
  146. world.Step(timeStep, velocityIterations, positionIterations);
  147. myCar.update();
  148. }
  149. ref = myCar.GetPosition();
  150. ref.Set( ref.x - areaw / 4.0f , ref.y - areah / 2.0f );
  151. // [4.3] Dessin des composants
  152. SDL_FillRect(screen, 0, 0x444444);
  153. myTerrain.draw( screen, ref );
  154. for ( b2Body* b = world.GetBodyList(); b; b = b->GetNext() )
  155. {
  156. position = b->GetPosition() - ref ;
  157. angle = b->GetAngle();
  158. tempo = (SDL_Surface*)b->GetUserData().pointer;
  159. if ( tempo == 0x0 )
  160. continue ;
  161. tempo = rotozoomSurface( tempo, -angle * 180.0f / b2_pi, 1.0f, 0 );
  162. pxpos.x = position.x * MULTI - tempo->w / 2 ;
  163. pxpos.y = position.y * MULTI - tempo->h / 2 ;
  164. SDL_BlitSurface( tempo, 0x0, screen, &pxpos);
  165. SDL_FreeSurface( tempo );
  166. tempo = 0x0 ;
  167. }
  168. // Messages
  169. msg = "Ratio CPU : " ;
  170. msg += std::to_string( ((float)SDL_GetTicks() - tprev) * 100 / twait ) ;
  171. msg += " pourcents." ;
  172. stringRGBA( screen, 16, 16, msg.c_str(), 0, 255, 0, 255 );
  173. msg = "Position : " ;
  174. msg += std::to_string( ref.x ) ;
  175. msg += " m." ;
  176. stringRGBA( screen, 16, 25, msg.c_str(), 0, 255, 0, 255 );
  177. /*msg = "Torque : " ;
  178. msg += std::to_string( torque ) ;
  179. msg += " N.m." ;
  180. stringRGBA( screen, 16, 34, msg.c_str(), 0, 255, 0, 255 );*/
  181. SDL_Flip(screen);
  182. // [4.4] Temps
  183. while( SDL_GetTicks() - tprev < twait )
  184. {
  185. if ( SDL_GetTicks() - tprev < twait/2 )
  186. SDL_Delay( twait/3 );
  187. }
  188. tprev = SDL_GetTicks();
  189. } //fin bcl principale
  190. ///[5] Destruction des composants
  191. return 0;
  192. }