Posted By

ozkriff on 05/29/09


Tagged


Versions (?)

other_sdl_opengl


 / Published in: C
 

strange, but it's working.

  1. #include "SDL/SDL.h"
  2. #include "SDL/SDL_opengl.h"
  3. #include <math.h>
  4.  
  5. const int SCREEN_WIDTH = 640;
  6. const int SCREEN_HEIGHT = 480;
  7.  
  8. //The attributes of the square
  9. const int SQUARE_SIZE = 20;
  10. const int SQUARE_SPEED = 100;
  11.  
  12. short int ROT_MOVE = 0; // must we move? [-1; 0; 1]
  13. short int QUIT = 0; //QUIT flag [0; 1]
  14.  
  15. //The square
  16. float
  17. x = 0, y = 0, //The offsets of the square
  18. xVel = 0, yVel = 0, //The velocity of the square
  19. rVel = 0, rotation = 0; //The rotation of the square
  20.  
  21. /* ------------------------------------------------------------------------- */
  22. struct unit {
  23. float x, y,
  24. rot_vel, rot,
  25. size,
  26. speed;
  27. short int
  28. move, // must we move? [-1; 0; 1]
  29. live;
  30. };
  31.  
  32. /* ------------------------------------------------------------------------- */
  33. int init_GL()
  34. {
  35. glClearColor( 0, 0, 0, 0 ); //Set clear color
  36.  
  37. //Set projection
  38. glMatrixMode( GL_PROJECTION );
  39. glLoadIdentity();
  40. glOrtho( 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1 );
  41.  
  42. //Initialize modelview matrix
  43. glMatrixMode( GL_MODELVIEW );
  44. glLoadIdentity();
  45.  
  46.  
  47. if ( glGetError() != GL_NO_ERROR ) { //If there was any errors
  48. return 1;
  49. }
  50.  
  51. return 0; //If everything initialized
  52. }
  53.  
  54. /* ------------------------------------------------------------------------- */
  55. int init()
  56. {
  57. if ( SDL_Init( SDL_INIT_EVERYTHING ) < 0 ) {
  58. return 1;
  59. }
  60.  
  61. //Create Window
  62. if ( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT,
  63. 32, SDL_OPENGL ) == NULL ) {
  64. return 2;
  65. }
  66.  
  67. if ( init_GL() != 0 ) {
  68. return 3;
  69. }
  70.  
  71. SDL_WM_SetCaption( "OpenGL Test", NULL );
  72.  
  73. return 0;
  74. }
  75.  
  76. /* ------------------------------------------------------------------------- */
  77. void square_handle_input ( SDL_Event event )
  78. {
  79. if ( event.type == SDL_KEYDOWN ) //If a key was pressed
  80. switch ( event.key.keysym.sym ) {
  81. case SDLK_UP: yVel -= SQUARE_SPEED; break;
  82. case SDLK_DOWN: yVel += SQUARE_SPEED; break;
  83. case SDLK_LEFT: xVel -= SQUARE_SPEED; break;
  84. case SDLK_RIGHT:xVel += SQUARE_SPEED; break;
  85.  
  86. case SDLK_w: ROT_MOVE = 1; break;
  87. case SDLK_a: rVel -= 120; break;
  88. case SDLK_s: ROT_MOVE = -1; break;
  89. case SDLK_d: rVel += 120; break;
  90.  
  91. case SDLK_q: QUIT = 1; break;
  92.  
  93. default: break;
  94. }
  95. else if ( event.type == SDL_KEYUP ) //If a key was released
  96. switch ( event.key.keysym.sym ) {
  97. case SDLK_UP: yVel += SQUARE_SPEED; break;
  98. case SDLK_DOWN: yVel -= SQUARE_SPEED; break;
  99. case SDLK_LEFT: xVel += SQUARE_SPEED; break;
  100. case SDLK_RIGHT:xVel -= SQUARE_SPEED; break;
  101.  
  102. case SDLK_w: ROT_MOVE = 0; break;
  103. case SDLK_a: rVel += 120; break;
  104. case SDLK_s: ROT_MOVE = 0; break;
  105. case SDLK_d: rVel -= 120; break;
  106.  
  107. default: break;
  108. }
  109. }
  110.  
  111. /* ------------------------------------------------------------------------- */
  112. void square_move ( Uint32 d_time )
  113. {
  114. rotation += rVel * ( d_time / 1000.f );
  115. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  116. #if 0
  117. x += xVel * ( d_time / 1000.f ); //Move the square <->
  118. if ( ( x < 0 ) || ( x + SQUARE_SIZE > SCREEN_WIDTH ) ) //If too far
  119. x -= xVel* ( d_time / 1000.f ); //Move back
  120.  
  121. y += yVel * ( d_time / 1000.f ); //Move the square up or down
  122. if ( ( y < 0 ) || ( y + SQUARE_SIZE > SCREEN_HEIGHT ) ) //If too far
  123. y -= yVel* ( d_time / 1000.f ); //Move back
  124. #endif
  125. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  126. x += ( SQUARE_SPEED * sinf(rotation*M_PI/180.0f) )
  127. * ( d_time / 1000.f ) * ROT_MOVE;
  128.  
  129. y -= ( SQUARE_SPEED * cosf(rotation*M_PI/180.0f) )
  130. * ( d_time / 1000.f ) * ROT_MOVE;
  131. }
  132.  
  133. /* ------------------------------------------------------------------------- */
  134. void square_show()
  135. {
  136. //Move to offset
  137. glTranslatef( x, y, 0 );
  138. glRotatef( rotation, 0, 0, 1);
  139.  
  140. glBegin( GL_QUADS );
  141. {
  142. glColor4f( 1.0, 1.0, 1.0, 1.0 ); //Set color to white
  143.  
  144. glVertex3f( -SQUARE_SIZE, -SQUARE_SIZE, 0 );
  145. glVertex3f( SQUARE_SIZE, -SQUARE_SIZE, 0 );
  146. glVertex3f( SQUARE_SIZE, SQUARE_SIZE, 0 );
  147. glVertex3f( -SQUARE_SIZE, SQUARE_SIZE, 0 );
  148. }
  149. glEnd();
  150.  
  151. glLoadIdentity(); //Reset
  152. }
  153.  
  154.  
  155. /* ------------------------------------------------------------------------- */
  156. int main ( int argc, char *argv[] )
  157. {
  158. //Initialize
  159. if ( init() != 0 )
  160. return 1;
  161.  
  162. SDL_Event event; //Event handler
  163.  
  164. int timer = 0;
  165. timer = SDL_GetTicks();
  166. struct unit player;
  167.  
  168. player.x=0,
  169. player.y=0,
  170. player.rot_vel=0,
  171. player.rot=0,
  172. player.size=15,
  173. player.speed=120;
  174. player.move=0, // must we move? [-1; 0; 1]
  175. player.live=1; // [0;1]
  176.  
  177.  
  178. while ( QUIT == 0 ) {
  179. /* While there are events to handle. */
  180. while ( SDL_PollEvent(&event) ) {
  181. square_handle_input(event); //Handle key presses
  182. if ( event.type == SDL_QUIT )
  183. QUIT = 1;
  184. }
  185. square_move( SDL_GetTicks() - timer );
  186. timer = SDL_GetTicks();
  187.  
  188. glClear( GL_COLOR_BUFFER_BIT ); //Clear the screen
  189.  
  190. square_show();
  191.  
  192. SDL_GL_SwapBuffers(); //Update screen
  193. }
  194.  
  195. SDL_QUIT();
  196.  
  197. return 0;
  198. }

Report this snippet  

You need to login to post a comment.