main.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #include <cstdlib>
  2. #include <SDL/SDL.h>
  3. #include "Input.h"
  4. #include <SDL/SDL_gfxPrimitives.h>
  5. int main ( int argc, char** argv )
  6. {
  7. // initialize SDL video
  8. if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
  9. {
  10. printf( "Unable to init SDL: %s\n", SDL_GetError() );
  11. return 1;
  12. }
  13. // make sure SDL cleans up before exit
  14. atexit(SDL_Quit);
  15. // create a new window
  16. Uint16 w(1440), h(900);
  17. SDL_Surface* screen = SDL_SetVideoMode(w, h, 32,
  18. SDL_HWSURFACE|SDL_DOUBLEBUF);
  19. if ( !screen )
  20. {
  21. printf("Unable to set video: %s\n", SDL_GetError());
  22. return 1;
  23. }
  24. // variables
  25. SDL_Rect dstrect;
  26. const Uint32 delay(18);
  27. Uint32 tps( SDL_GetTicks() ), tps_old(0);
  28. // graph
  29. int i(0);
  30. int r(128), g(128), b(128);
  31. bool ubr(false), ubg(false), ubb(false);
  32. bool dbr(false), dbg(false), dbb(false);
  33. // program main loop
  34. bool done = false;
  35. while (!done)
  36. {
  37. // message processing loop
  38. SDL_Event event;
  39. while (SDL_PollEvent(&event))
  40. {
  41. // check for messages
  42. switch (event.type)
  43. {
  44. // exit if the window is closed
  45. case SDL_QUIT:
  46. done = true;
  47. break;
  48. // check for keypresses
  49. case SDL_KEYDOWN:
  50. // exit if ESCAPE is pressed
  51. switch ( event.key.keysym.sym )
  52. {
  53. case SDLK_ESCAPE:
  54. done = true;
  55. break;
  56. case SDLK_a:
  57. case SDLK_KP7:
  58. case SDLK_7:
  59. ubr = true;
  60. dbr = false;
  61. break;
  62. case SDLK_q:
  63. case SDLK_KP1:
  64. case SDLK_1:
  65. ubr = false;
  66. dbr = true;
  67. break;
  68. case SDLK_z:
  69. case SDLK_KP8:
  70. case SDLK_8:
  71. ubg = true;
  72. dbg = false;
  73. break;
  74. case SDLK_s:
  75. case SDLK_KP2:
  76. case SDLK_2:
  77. ubg = false;
  78. dbg = true;
  79. break;
  80. case SDLK_e:
  81. case SDLK_KP9:
  82. case SDLK_9:
  83. ubb = true;
  84. dbb = false;
  85. break;
  86. case SDLK_d:
  87. case SDLK_KP3:
  88. case SDLK_3:
  89. ubb = false;
  90. dbb = true;
  91. break;
  92. default:
  93. break;
  94. }
  95. break;
  96. case SDL_KEYUP:
  97. switch ( event.key.keysym.sym )
  98. {
  99. case SDLK_a:
  100. case SDLK_KP7:
  101. case SDLK_7:
  102. ubr = false;
  103. dbr = false;
  104. break;
  105. case SDLK_q:
  106. case SDLK_KP1:
  107. case SDLK_1:
  108. ubr = false;
  109. dbr = false;
  110. break;
  111. case SDLK_z:
  112. case SDLK_KP8:
  113. case SDLK_8:
  114. ubg = false;
  115. dbg = false;
  116. break;
  117. case SDLK_s:
  118. case SDLK_KP2:
  119. case SDLK_2:
  120. ubg = false;
  121. dbg = false;
  122. break;
  123. case SDLK_e:
  124. case SDLK_KP9:
  125. case SDLK_9:
  126. ubb = false;
  127. dbb = false;
  128. break;
  129. case SDLK_d:
  130. case SDLK_KP3:
  131. case SDLK_3:
  132. ubb = false;
  133. dbb = false;
  134. break;
  135. default:
  136. break;
  137. }
  138. break;
  139. } // end switch event tye
  140. } // end of message processing
  141. if ( r < 255 && ubr ) r++;
  142. if ( r > 0 && dbr ) r--;
  143. if ( g < 255 && ubg ) g++;
  144. if ( g > 0 && dbg ) g--;
  145. if ( b < 255 && ubb ) b++;
  146. if ( b > 0 && dbb ) b--;
  147. // calculs
  148. i++;
  149. if ( i > w ) i = 0;
  150. // DRAWING STARTS HERE
  151. vlineRGBA(screen, i, 0, h, r/2, g/2, b/2, 255);
  152. roundedBoxRGBA(screen, 15, 15, w-15, h-400, 15, r, g, b, 255);
  153. for ( int j(1); j<=3; j++ )
  154. {
  155. filledCircleRGBA(screen, j*w/4, h-200, 149, 128, 128, 128, 255);
  156. }
  157. filledPieRGBA(screen, w/4, h-200, 150, 0, r*359/255, 255, 0, 0, 255 );
  158. filledPieRGBA(screen, w/2, h-200, 150, 0, g*359/255, 0, 255, 0, 255 );
  159. filledPieRGBA(screen, 3*w/4, h-200, 150, 0, b*359/255, 0, 0, 255, 255 );
  160. stringRGBA(screen, w/4+140, h-130, "Rouge", 255, 0, 0, 255);
  161. stringRGBA(screen, w/2+140, h-130, "Vert", 0, 255, 0, 255);
  162. stringRGBA(screen, 3*w/4+140, h-130, "Bleu", 0, 0, 255, 255);
  163. // DRAWING ENDS HERE
  164. // finally, update the screen :)
  165. SDL_Flip(screen);
  166. // time management
  167. do
  168. {
  169. tps = SDL_GetTicks();
  170. } while ( tps-tps_old < delay );
  171. tps_old = tps;
  172. } // end main loop
  173. // all is well ;)
  174. printf("Exited cleanly\n");
  175. return 0;
  176. }