main.cpp 3.9 KB

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