Posted By

ozkriff on 05/28/09


Tagged


Versions (?)

test-c::b


 / Published in: C++
 

<?xml>

  1. /*This source code copyrighted by Lazy Foo' Productions (2004-2009) and may not
  2. be redestributed without written permission.*/
  3.  
  4. //The headers
  5. #include "SDL/SDL.h"
  6. #include "SDL/SDL_opengl.h"
  7.  
  8. //Screen attributes
  9. const int SCREEN_WIDTH = 640;
  10. const int SCREEN_HEIGHT = 480;
  11. const int SCREEN_BPP = 32;
  12.  
  13. //The attributes of the square
  14. const int SQUARE_SIZE = 20;
  15. const int SQUARE_SPEED = 100;
  16.  
  17. //Event handler
  18. SDL_Event event;
  19.  
  20. //Quit flag
  21. bool quit = false;
  22. int timer = 0;
  23.  
  24. //The square
  25. class Square
  26. {
  27. private:
  28. //The offsets
  29. float x, y;
  30.  
  31. //The velocity of the square
  32. float xVel, yVel;
  33.  
  34. //The rotatioon
  35. float rVel, rotation;
  36.  
  37. public:
  38. //Initializes
  39. Square();
  40.  
  41. //Handles key presses
  42. void handle_input();
  43.  
  44. //Moves the square
  45. void move(Uint32 deltaTicks);
  46.  
  47. //Shows the square on the screen
  48. void show();
  49. };
  50.  
  51. bool init_GL()
  52. {
  53. //Set clear color
  54. glClearColor( 0, 0, 0, 0 );
  55.  
  56. //Set projection
  57. glMatrixMode( GL_PROJECTION );
  58. glLoadIdentity();
  59. glOrtho( 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1 );
  60.  
  61. //Initialize modelview matrix
  62. glMatrixMode( GL_MODELVIEW );
  63. glLoadIdentity();
  64.  
  65. //If there was any errors
  66. if ( glGetError() != GL_NO_ERROR )
  67. {
  68. return false;
  69. }
  70.  
  71. //If everything initialized
  72. return true;
  73. }
  74.  
  75. bool init()
  76. {
  77. //Initialize SDL
  78. if ( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
  79. {
  80. return false;
  81. }
  82.  
  83. //Create Window
  84. if ( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL )
  85. {
  86. return false;
  87. }
  88.  
  89. //Initialize OpenGL
  90. if ( init_GL() == false )
  91. {
  92. return false;
  93. }
  94.  
  95. //Set caption
  96. SDL_WM_SetCaption( "OpenGL Test", NULL );
  97.  
  98. return true;
  99. }
  100.  
  101. void clean_up()
  102. {
  103. //Quit SDL
  104. SDL_Quit();
  105. }
  106.  
  107. Square::Square()
  108. {
  109. //Initialize offsets
  110. x = 0;
  111. y = 0;
  112.  
  113. //Initialize velocity
  114. xVel = 0;
  115. yVel = 0;
  116.  
  117. rVel = 0;
  118. rotation = 0;
  119. }
  120.  
  121. void Square::handle_input()
  122. {
  123. //If a key was pressed
  124. if ( event.type == SDL_KEYDOWN )
  125. {
  126. //Adjust the velocity
  127. switch ( event.key.keysym.sym )
  128. {
  129. case SDLK_UP:
  130. yVel -= SQUARE_SPEED;
  131. break;
  132. case SDLK_DOWN:
  133. yVel += SQUARE_SPEED;
  134. break;
  135. case SDLK_LEFT:
  136. xVel -= SQUARE_SPEED;
  137. break;
  138. case SDLK_RIGHT:
  139. xVel += SQUARE_SPEED;
  140. break;
  141. case SDLK_w:
  142. rVel += 60;
  143. break;
  144. case SDLK_e:
  145. rVel -= 60;
  146. break;
  147. case SDLK_q:
  148. quit = true;
  149. break;
  150. }
  151. }
  152. //If a key was released
  153. else if ( event.type == SDL_KEYUP )
  154. {
  155. //Adjust the velocity
  156. switch ( event.key.keysym.sym )
  157. {
  158. case SDLK_UP:
  159. yVel += SQUARE_SPEED;
  160. break;
  161. case SDLK_DOWN:
  162. yVel -= SQUARE_SPEED;
  163. break;
  164. case SDLK_LEFT:
  165. xVel += SQUARE_SPEED;
  166. break;
  167. case SDLK_RIGHT:
  168. xVel -= SQUARE_SPEED;
  169. break;
  170. case SDLK_w:
  171. rVel -= 60;
  172. break;
  173. case SDLK_e:
  174. rVel += 60;
  175. break;
  176. }
  177. }
  178. }
  179.  
  180. void Square::move(Uint32 deltaTicks)
  181. {
  182. //Move the square left or right
  183. x += xVel* ( deltaTicks / 1000.f );
  184.  
  185. //If the square went too far
  186. if ( ( x < 0 ) || ( x + SQUARE_SIZE > SCREEN_WIDTH ) )
  187. {
  188. //Move back
  189. x -= xVel* ( deltaTicks / 1000.f );
  190. }
  191.  
  192. //Move the square up or down
  193. y += yVel* ( deltaTicks / 1000.f );
  194.  
  195. //If the square went too far
  196. if ( ( y < 0 ) || ( y + SQUARE_SIZE > SCREEN_HEIGHT ) )
  197. {
  198. //Move back
  199. y -= yVel* ( deltaTicks / 1000.f );
  200. }
  201.  
  202. rotation += rVel* ( deltaTicks / 1000.f );
  203. }
  204.  
  205. void Square::show()
  206. {
  207. //Move to offset
  208. glTranslatef( x, y, 0 );
  209. glRotatef( rotation, 0, 0, 1);
  210.  
  211. //Start quad
  212. glBegin( GL_QUADS );
  213. {
  214. //Set color to white
  215. glColor4f( 1.0, 1.0, 1.0, 1.0 );
  216.  
  217. //Draw square
  218. glVertex3f( -SQUARE_SIZE, -SQUARE_SIZE, 0 );
  219. glVertex3f( SQUARE_SIZE, -SQUARE_SIZE, 0 );
  220. glVertex3f( SQUARE_SIZE, SQUARE_SIZE, 0 );
  221. glVertex3f( -SQUARE_SIZE, SQUARE_SIZE, 0 );
  222. }
  223. //End quad
  224. glEnd();
  225.  
  226. //Reset
  227. glLoadIdentity();
  228. }
  229.  
  230.  
  231.  
  232. int main ( int argc, char *argv[] )
  233. {
  234. //Initialize
  235. if ( init() == false )
  236. {
  237. return 1;
  238. }
  239.  
  240. //Our square object
  241. Square square;
  242.  
  243.  
  244. timer = SDL_GetTicks();
  245.  
  246. //Wait for user exit
  247. while ( quit == false )
  248. {
  249. //While there are events to handle
  250. while ( SDL_PollEvent( &event ) )
  251. {
  252. //Handle key presses
  253. square.handle_input();
  254.  
  255. if ( event.type == SDL_QUIT )
  256. {
  257. quit = true;
  258. }
  259. }
  260.  
  261. //Move the square
  262. square.move( SDL_GetTicks() - timer );
  263. timer = SDL_GetTicks();
  264.  
  265. //Clear the screen
  266. glClear( GL_COLOR_BUFFER_BIT );
  267.  
  268. //Show the square
  269. square.show();
  270.  
  271. //Update screen
  272. SDL_GL_SwapBuffers();
  273. }
  274.  
  275. //Clean up
  276. clean_up();
  277.  
  278. return 0;
  279. }

Report this snippet  

You need to login to post a comment.