main.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // Basiques
  2. # include <iostream>
  3. # include <vector>
  4. # include <string>
  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. // Local
  16. # include "Cars/PickUp.h"
  17. # include "Cars/PinkLiner.h"
  18. # include "Terrain.h"
  19. // SnapFile
  20. #include "SnapFile.h"
  21. int main(int argc, char** argv)
  22. {
  23. /// [1] Démarrage
  24. // [1.0] Démarrage aléatoire
  25. srand( time(0) );
  26. // [1.1] Démarrages SDL
  27. if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
  28. {
  29. std::cout << "Impossible d'initialiser la SDL: " << SDL_GetError() << std::endl;
  30. return 1;
  31. }
  32. // [1.2] Préparation de fermeture
  33. atexit(SDL_Quit);
  34. // [1.3] Para-fenêtre
  35. SDL_WM_SetCaption("Bouncy Driver!", 0);
  36. /// [2] Préparation des composants SDL
  37. // [2.1] Préparation de la fenêtre
  38. SDL_Surface* screen = SDL_SetVideoMode(1200, 600, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
  39. if ( !screen )
  40. {
  41. std::cout << "Bug à l'initialisation: " << SDL_GetError() << std::endl;
  42. return 1;
  43. }
  44. // [2.2] Préparation variables
  45. SDL_Rect mouse({ 400, 200, 0, 0 });
  46. SDL_Rect pxpos({ 400, 200, 0, 0 });
  47. // [2.3] Préparation surfaces
  48. SDL_Surface* tempo( 0x0 );
  49. // [2.4] Préparation du temps
  50. Uint32 frameRate( 60 );
  51. Uint32 tprev(0), twait( 1000 / frameRate );
  52. bool pause( false );
  53. // [2.5] Préparation des messages
  54. std::string msg("Victor");
  55. /// [3] Box2D
  56. // Trucs
  57. B2_NOT_USED(argc);
  58. B2_NOT_USED(argv);
  59. // Construct a world object
  60. b2Vec2 gravity( 0.0f, 10.0f );
  61. b2World world(gravity);
  62. // Simulation settings
  63. float timeStep = 1.0f / frameRate;
  64. int32 velocityIterations = 8;
  65. int32 positionIterations = 3;
  66. float angle, multi(MULTI);
  67. b2Vec2 position, ref, decalage;
  68. // Define the edge body
  69. float areah( (float)screen->h / multi );
  70. float areaw( (float)screen->w / multi );
  71. // Terrain
  72. //std::cout << "Quel niveau ?" << std::endl;
  73. std::string lvl("NiveauInstant");
  74. std::string indexName("Niveaux/Level.idx");
  75. //std::cin >> lvl;
  76. Terrain myTerrain;
  77. myTerrain.load( "Niveaux/" + lvl + ".lvl" );
  78. myTerrain.build( world );
  79. myTerrain.textureLoad( screen );
  80. // Voiture
  81. PinkLiner myCar;
  82. myCar.init( world, 3.0f, -1.0f );
  83. // Évènements
  84. bool spin_right( false );
  85. bool spin_left( false );
  86. /// [4] Boucle principale
  87. bool done = false;
  88. SDL_Event event;
  89. while (!done)
  90. {
  91. // [4.1] Gestion évènements
  92. while (SDL_PollEvent(&event))
  93. {
  94. switch (event.type)
  95. {
  96. case SDL_QUIT:
  97. done = true;
  98. break;
  99. case SDL_KEYDOWN:
  100. switch( event.key.keysym.sym )
  101. {
  102. case SDLK_ESCAPE :
  103. done = true;
  104. break;
  105. case SDLK_BACKSPACE :
  106. // Destroy joints first of all
  107. myCar.init(world, 3.0f, -1.0f );
  108. std::cout << std::endl << "Respawn !" << std::endl << std::endl;
  109. break;
  110. case SDLK_a :
  111. case SDLK_q :
  112. spin_left = true;
  113. break;
  114. case SDLK_d :
  115. spin_right = true;
  116. break;
  117. case SDLK_p :
  118. pause = !pause ;
  119. break;
  120. case SDLK_RIGHT :
  121. myCar.drive( GO );
  122. break;
  123. case SDLK_LEFT :
  124. myCar.drive( REVERSE );
  125. break;
  126. case SDLK_DOWN :
  127. myCar.drive( BREAK );
  128. break;
  129. case SDLK_l:
  130. SF_choose( indexName, lvl, screen );
  131. if ( lvl != "" )
  132. {
  133. myCar.init( world, 3.0f, -1.0f );
  134. myTerrain.load( "Niveaux/" + lvl );
  135. myTerrain.build( world );
  136. }
  137. case SDLK_SPACE :
  138. if ( myCar.GetIsOnGround() )
  139. myCar.jump();
  140. break;
  141. default :
  142. break;
  143. }
  144. break;
  145. case SDL_KEYUP:
  146. switch( event.key.keysym.sym )
  147. {
  148. case SDLK_LEFT :
  149. case SDLK_RIGHT :
  150. case SDLK_DOWN :
  151. myCar.drive( FREE );
  152. break;
  153. case SDLK_a :
  154. case SDLK_q :
  155. spin_left = false;
  156. break;
  157. case SDLK_d :
  158. spin_right = false;
  159. break;
  160. default :
  161. break ;
  162. }
  163. break;
  164. case SDL_MOUSEMOTION:
  165. mouse.x = event.motion.x ;
  166. mouse.y = event.motion.y ;
  167. break;
  168. case SDL_MOUSEBUTTONDOWN:
  169. std::cout << "SDL_MOUSEBUTTONDOWN" << std::endl;
  170. break;
  171. case SDL_MOUSEBUTTONUP:
  172. std::cout << "SDL_MOUSEBUTTONUP" << std::endl;
  173. break;
  174. default:
  175. break;
  176. } // end switch event type
  177. } // end of message processing
  178. // [4.2] Calculs
  179. // It is generally best to keep the time step and iterations fixed.
  180. /*motor->SetMotorSpeed( speed );
  181. motor->SetMaxMotorTorque( torque );*/
  182. if ( !pause )
  183. {
  184. // Spin
  185. if ( spin_left )
  186. myCar.spin( -0.005f );
  187. if ( spin_right )
  188. myCar.spin( 0.005f );
  189. // Physique
  190. world.Step(timeStep, velocityIterations, positionIterations);
  191. myCar.update();
  192. }
  193. // Référentiel
  194. decalage = 0.95f * decalage + 0.005f * myCar.GetVelocity();
  195. ref = myCar.GetPosition();
  196. ref.Set( ref.x - areaw / 5.0f , ref.y - areah / 2.0f );
  197. ref += decalage;
  198. // Zoom
  199. multi = MULTI * (1.0f - 0.2f * decalage.Length());
  200. myTerrain.setMulti( multi );
  201. // [4.3] Dessin des composants
  202. SDL_FillRect(screen, 0, 0x00000000 );
  203. myTerrain.drawArtist( screen, ref );//drawUni
  204. for ( b2Body* b = world.GetBodyList(); b; b = b->GetNext() )
  205. {
  206. position = b->GetPosition() - ref ;
  207. angle = b->GetAngle();
  208. tempo = (SDL_Surface*)b->GetUserData().pointer;
  209. if ( tempo == 0x0 )
  210. continue ;
  211. tempo = rotozoomSurface( tempo, -angle * 180.0f / b2_pi, multi / MULTI, 0 );
  212. pxpos.x = position.x * multi - tempo->w / 2 ;
  213. pxpos.y = position.y * multi - tempo->h / 2 ;
  214. SDL_BlitSurface( tempo, 0x0, screen, &pxpos);
  215. SDL_FreeSurface( tempo );
  216. tempo = 0x0 ;
  217. }
  218. // Messages
  219. msg = "Niveau : " ;
  220. msg += lvl ;
  221. stringRGBA( screen, 16, 7, msg.c_str(), 0, 255, 0, 255 );
  222. msg = "Ratio CPU : " ;
  223. msg += std::to_string( ((float)SDL_GetTicks() - tprev) * 100 / twait ) ;
  224. msg += " pourcents." ;
  225. stringRGBA( screen, 16, 16, msg.c_str(), 0, 255, 0, 255 );
  226. msg = "Position : " ;
  227. msg += std::to_string( ref.x ) ;
  228. msg += " m." ;
  229. stringRGBA( screen, 16, 25, msg.c_str(), 0, 255, 0, 255 );
  230. msg = "Torque : " ;
  231. msg += std::to_string( myCar.GetTorque() ) ;
  232. msg += " N.m." ;
  233. stringRGBA( screen, 16, 34, msg.c_str(), 0, 255, 0, 255 );
  234. msg = "Speed : " ;
  235. msg += std::to_string( myCar.GetSpeed() ) ;
  236. msg += " rad/s." ;
  237. stringRGBA( screen, 16, 43, msg.c_str(), 0, 255, 0, 255 );
  238. msg = "Is on air : " ;
  239. msg += std::to_string( myCar.GetIsOnGround() ) ;
  240. msg += "." ;
  241. stringRGBA( screen, 16, 52, msg.c_str(), 0, 255, 0, 255 );
  242. SDL_Flip(screen);
  243. // [4.4] Temps
  244. while( SDL_GetTicks() - tprev < twait )
  245. {
  246. if ( SDL_GetTicks() - tprev < twait/2 )
  247. SDL_Delay( twait/3 );
  248. }
  249. tprev = SDL_GetTicks();
  250. } //fin bcl principale
  251. ///[5] Destruction des composants
  252. return 0;
  253. }