main.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include "spawn.h"
  2. #include "intToStr.h"
  3. #define NB_GAMERS 2
  4. #define NB_GROW 6
  5. #define NB_BASE 10
  6. #define NEXT_ROUND spawn( &obs, NB_BASE+lvl*NB_GROW, w, h )
  7. #define GAMERS_POS (1+t)*(h/(1+NB_GAMERS))
  8. int main ( int argc, char** argv )
  9. {
  10. /// initialize SDL video
  11. if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
  12. {
  13. printf( "Unable to init SDL: %s\n", SDL_GetError() );
  14. return 1;
  15. }
  16. SDL_ShowCursor( SDL_DISABLE );
  17. /// make sure SDL cleans up before exit
  18. atexit(SDL_Quit);
  19. /// init random list
  20. srand( time(0) );
  21. /// create a new window
  22. Uint16 w, h;
  23. SDL_Surface *buffer(0x0), *screen(0x0);
  24. { // spare useless variables
  25. const SDL_VideoInfo* fenetreInfo = SDL_GetVideoInfo();
  26. w = fenetreInfo->current_w;
  27. h = fenetreInfo->current_h;
  28. }
  29. screen = SDL_SetVideoMode(w, h, 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN);
  30. std::cout << "Set " << w << 'x' << h << " video." << std::endl;
  31. if ( !screen )
  32. {
  33. std::cout << "Unable to set video " << SDL_GetError() << std::endl;
  34. return 1;
  35. }
  36. else
  37. {
  38. buffer = SDL_CreateRGBSurface( SDL_HWSURFACE, w, h, 32, 0, 0, 0, 0 );
  39. }
  40. /// variables
  41. const Uint32 delay(18);
  42. Uint32 tps( SDL_GetTicks() ), tps_old(0);
  43. Uint32 fdis_time(0), fdis_count(0), fdis_rslt(0);
  44. bool alive[NB_GAMERS];
  45. bool running;
  46. int lvl( 1 );
  47. /// graph
  48. int i(0), x[NB_GAMERS], y[NB_GAMERS];
  49. Uint8 r(0), g(255), b(255);
  50. bool up[NB_GAMERS], down[NB_GAMERS];
  51. /// init
  52. for ( int t(0); t < NB_GAMERS; t++ )
  53. {
  54. alive[t] = true;
  55. x[t] = -1;
  56. y[t] = GAMERS_POS;
  57. up[t] = down[t] = false;
  58. }
  59. /// obstacles
  60. std::vector<SDL_Rect> obs(0);
  61. NEXT_ROUND;
  62. /// program main loop
  63. bool done = false;
  64. while (!done)
  65. {
  66. // message processing loop
  67. SDL_Event event;
  68. while (SDL_PollEvent(&event))
  69. {
  70. // check for messages
  71. switch (event.type)
  72. {
  73. // exit if the window is closed
  74. case SDL_QUIT:
  75. done = true;
  76. break;
  77. // check for keypresses
  78. case SDL_KEYDOWN:
  79. // exit if ESCAPE is pressed
  80. switch ( event.key.keysym.sym )
  81. {
  82. case SDLK_ESCAPE:
  83. done = true;
  84. break;
  85. case SDLK_UP:
  86. case SDLK_LEFT:
  87. up[0] = true;
  88. break;
  89. case SDLK_DOWN:
  90. case SDLK_RIGHT:
  91. down[0] = true;
  92. break;
  93. case SDLK_BACKSPACE:
  94. alive[0] = true;
  95. x[0] = w+1;
  96. lvl = 0;
  97. #if NB_GAMERS > 1
  98. alive[1] = true;
  99. x[1] = w+1;
  100. break;
  101. case SDLK_w:
  102. case SDLK_a:
  103. up[1] = true;
  104. break;
  105. case SDLK_s:
  106. case SDLK_d:
  107. down[1] = true;
  108. break;
  109. #endif
  110. default:
  111. break;
  112. }
  113. break;
  114. case SDL_KEYUP:
  115. switch ( event.key.keysym.sym )
  116. {
  117. case SDLK_UP:
  118. case SDLK_LEFT:
  119. up[0] = false;
  120. break;
  121. case SDLK_DOWN:
  122. case SDLK_RIGHT:
  123. down[0] = false;
  124. break;
  125. #if NB_GAMERS > 1
  126. case SDLK_w:
  127. case SDLK_a:
  128. up[1] = false;
  129. break;
  130. case SDLK_s:
  131. case SDLK_d:
  132. down[1] = false;
  133. break;
  134. #endif
  135. default:
  136. break;
  137. }
  138. break;
  139. } // end switch event tye
  140. } // end of message processing
  141. // color computing
  142. if ( i < 256 )
  143. {
  144. r = i;
  145. g = 255 - i%256;
  146. // b = 255
  147. }
  148. else if ( i < 512 )
  149. {
  150. //r = 255
  151. g = i%256;
  152. b = 255 - i%256;
  153. }
  154. else if ( i < 768 )
  155. {
  156. r = 255 - i%256;
  157. //g = 255
  158. b = i%256;
  159. }
  160. // still run ?
  161. running = alive[0];
  162. #if NB_GAMERS > 1
  163. running &= alive[1];
  164. #endif // NB_GAMERS
  165. // direction
  166. for ( int t(0); t<NB_GAMERS; t++ )
  167. for ( int j(0); j < 5 && running; j++ )
  168. {
  169. x[t]++;
  170. if ( up[t] ) y[t]--;
  171. else if ( down[t] ) y[t]++;
  172. if ( y[t] < 0 ) y[t] = h-1;
  173. if ( y[t] >= h ) y[t] = 0;
  174. if ( t == 0) pixelRGBA(buffer, x[0], y[0], r, g, b, 255);
  175. else pixelRGBA(buffer, x[1], y[1], g, b, r, 255);
  176. }
  177. // next round
  178. if ( x[0] > w )
  179. {
  180. lvl++;
  181. obs.clear();
  182. NEXT_ROUND;
  183. SDL_FillRect( buffer, 0, 0x000000 );
  184. for ( int t(0); t<NB_GAMERS; t++ )
  185. {
  186. x[t] = 0;
  187. y[t] = GAMERS_POS;
  188. }
  189. }
  190. /// -- Buffer
  191. /// calculs
  192. i++;
  193. for ( int t(0); t<NB_GAMERS; t++ )
  194. if ( !alive[t] )
  195. stringRGBA(buffer, x[t]+8, y[t]+8, "GAME OVER", r/3, g/3, b/3, 255);
  196. if ( i > 767 ) i = 0;
  197. /// draw obstacles
  198. for ( Uint16 j(0); j < obs.size(); j++ )
  199. {
  200. SDL_FillRect( buffer, &obs[j], b + (g << 8) + (r << 16) );
  201. for ( int t(0); t<NB_GAMERS; t++ )
  202. if (x[t] > obs[j].x &&
  203. x[t] < obs[j].x + obs[j].w &&
  204. y[t] > obs[j].y &&
  205. y[t] < obs[j].y + obs[j].h) alive[t] = false;
  206. }
  207. stringRGBA(buffer, 12, 12, ("Level "+intToStr( lvl )).c_str(), r, g, b, 255);
  208. for ( int t(0); t<NB_GAMERS; t++ )
  209. if ( !alive[t] )
  210. stringRGBA(buffer, x[t]+6, y[t]+6, "GAME OVER", 255, 4, 23, 255);
  211. /// past buffer
  212. SDL_BlitSurface( buffer, 0, screen, 0 );
  213. /// fps count
  214. fdis_count++;
  215. if ( SDL_GetTicks() - fdis_time > 1000) {
  216. fdis_rslt = fdis_count;
  217. fdis_count = 0;
  218. fdis_time = SDL_GetTicks();
  219. }
  220. stringRGBA(screen, 12, 24, ("Fps: "+intToStr( fdis_rslt )).c_str(), r, g, b, 255);
  221. /// finally, update the screen :)
  222. SDL_Flip(screen);
  223. /// time management
  224. do
  225. {
  226. tps = SDL_GetTicks();
  227. } while ( tps-tps_old < delay );
  228. tps_old = tps;
  229. } // end main loop
  230. /// all is well ;)
  231. std::cout << "Exited cleanly." << std::endl;
  232. return 0;
  233. }