Posted By

ozkriff on 10/20/08


Tagged


Versions (?)

ogl+sdl(man)


 / Published in: C++
 

=)

  1. /**********************************************************************
  2. compillig:
  3. g++ new.cpp -lGL -lGLU -lSDL -I/usr/include/SDL -o test
  4. **********************************************************************/
  5.  
  6. //The headers
  7. #include "SDL/SDL.h"
  8. #include "SDL/SDL_opengl.h"
  9. #include <stdio.h> //for debug
  10. #include <math.h> //for sin, cos
  11.  
  12.  
  13. GLuint texture; // Texture object handle
  14. SDL_Surface *surface; // Gives us the information to make the texture
  15.  
  16. //Screen attributes
  17. const int SCREEN_WIDTH = 800;
  18. const int SCREEN_HEIGHT = 600;
  19. const int SCREEN_BPP = 32;
  20.  
  21. //The frame rate
  22. const int FRAMES_PER_SECOND = 60;
  23.  
  24. const int MAN_SIZE = SCREEN_WIDTH/6;
  25.  
  26. //rotate the Man?
  27. int rot = 0;
  28. // How fast?
  29. int rot_speed = 5;
  30.  
  31. //Quit flag
  32. bool quit = false;
  33.  
  34. //Event handler
  35. SDL_Event event;
  36.  
  37. //The Man
  38. class Man
  39. {
  40. private:
  41. //The offsets
  42. int x, y;
  43. //The velocity of the Man
  44. int Vel_x, Vel_y;
  45.  
  46. int w;
  47. //The rotation angle
  48. int rot_angle;
  49.  
  50. public:
  51. //Initializes
  52. Man();
  53. //Handles key presses
  54. void handle_input();
  55. //Moves the Man
  56. void move();
  57. //Shows the Man on the screen
  58. void show();
  59. };
  60.  
  61. //The timer
  62. class Timer
  63. {
  64. private:
  65. //The clock time when the timer started
  66. int startTicks;
  67.  
  68. //The ticks stored when the timer was paused
  69. int pausedTicks;
  70.  
  71. //The timer status
  72. bool paused;
  73. bool started;
  74.  
  75. public:
  76. //Initializes variables
  77. Timer();
  78.  
  79. //The various clock actions
  80. void start();
  81. void stop();
  82. void pause();
  83. void unpause();
  84.  
  85. //Gets the timer's time
  86. int get_ticks();
  87.  
  88. //Checks the status of the timer
  89. bool is_started();
  90. bool is_paused();
  91. };
  92.  
  93. bool init_GL()
  94. {
  95. //Set clear color
  96. glClearColor( 0, 0, 0, 0 );
  97.  
  98. // Need this to display a texture
  99. glEnable( GL_TEXTURE_2D );
  100.  
  101. glViewport( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT );
  102.  
  103. glMatrixMode( GL_PROJECTION );
  104. glLoadIdentity();
  105. glOrtho( 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1 );
  106.  
  107. //Initialize modelview matrix
  108. glMatrixMode( GL_MODELVIEW );
  109. glLoadIdentity();
  110.  
  111. //If there was any errors
  112. if( glGetError() != GL_NO_ERROR )
  113. {
  114. return false;
  115. }
  116.  
  117. //If everything initialized
  118. return true;
  119. }
  120.  
  121. bool load_img()
  122. {
  123. // Load the OpenGL texture
  124.  
  125.  
  126. //moved to global variables:
  127. /*
  128. //~ GLuint texture; // Texture object handle
  129. //~ SDL_Surface *surface; // Gives us the information to make the texture
  130. */
  131. if ( (surface = SDL_LoadBMP("image.bmp")) )
  132. {
  133.  
  134. // Check that the image's width is a power of 2
  135. if ( (surface->w & (surface->w - 1)) != 0 )
  136. {
  137. printf("warning: image.bmp's width is not a power of 2\n");
  138. }
  139.  
  140. // Also check if the height is a power of 2
  141. if ( (surface->h & (surface->h - 1)) != 0 )
  142. {
  143. printf("warning: image.bmp's height is not a power of 2\n");
  144. }
  145.  
  146. // Have OpenGL generate a texture object handle for us
  147. glGenTextures( 1, &texture );
  148.  
  149. // Bind the texture object
  150. glBindTexture( GL_TEXTURE_2D, texture );
  151.  
  152. // Set the texture's stretching properties
  153. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  154. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  155.  
  156. // Edit the texture object's image data using the information SDL_Surface gives us
  157. glTexImage2D( GL_TEXTURE_2D, 0, 3, surface->w, surface->h, 0,
  158. GL_BGR, GL_UNSIGNED_BYTE, surface->pixels );
  159. }
  160. else
  161. {
  162. printf("SDL could not load image.bmp: %s\n", SDL_GetError());
  163. SDL_Quit();
  164. return 1;
  165. }
  166.  
  167. // Free the SDL_Surface only if it was successfully created
  168. if ( surface )
  169. {
  170. SDL_FreeSurface( surface );
  171. }
  172.  
  173. return true;
  174. }
  175.  
  176. bool init()
  177. {
  178. //Initialize SDL
  179. if ( SDL_Init(SDL_INIT_VIDEO) != 0 )
  180. {
  181. printf("Unable to initialize SDL: %s\n", SDL_GetError());
  182. return false;
  183. }
  184.  
  185. //Create Window
  186. if( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL )
  187. {
  188. printf("Unable to set video mode: %s\n", SDL_GetError());
  189. return false;
  190. }
  191.  
  192. //Initialize OpenGL
  193. if( init_GL() == false )
  194. {
  195. return false;
  196. }
  197.  
  198. //Set caption
  199. SDL_WM_SetCaption( "OpenGL/SDL Test", NULL );
  200.  
  201. return true;
  202. }
  203.  
  204. void clean_up()
  205. {
  206. // Delete the OpenGL texture
  207. glDeleteTextures( 1, &texture );
  208.  
  209. //Quit SDL
  210. SDL_Quit();
  211. }
  212.  
  213. Man::Man()
  214. {
  215. //Initialize offsets
  216. x = SCREEN_WIDTH/2;
  217. y = SCREEN_HEIGHT/2;
  218.  
  219. //Initialize velocity
  220. Vel_x = 0;
  221. Vel_y = 0;
  222. w = 0;
  223.  
  224. //Init rot angl
  225. rot_angle = 0;
  226. }
  227.  
  228. void Man::handle_input()
  229. {
  230. //If a key was pressed
  231. if( event.type == SDL_KEYDOWN )
  232. {
  233. //Adjust the velocity
  234. switch( event.key.keysym.sym )
  235. {
  236. case SDLK_UP: Vel_y -= MAN_SIZE / 20; break;
  237. case SDLK_DOWN: Vel_y += MAN_SIZE / 20; break;
  238. case SDLK_LEFT: Vel_x -= MAN_SIZE / 20; break;
  239. case SDLK_RIGHT: Vel_x += MAN_SIZE / 20; break;
  240. case SDLK_d: rot = 1; break; //right
  241. case SDLK_a: rot = -1; break; //left
  242. case SDLK_q: quit = true; break;
  243. case SDLK_w: w = 1; break;
  244. case SDLK_s: w = -1; break;
  245. }
  246. }
  247. //If a key was released
  248. else if( event.type == SDL_KEYUP )
  249. {
  250. //Adjust the velocity
  251. switch( event.key.keysym.sym )
  252. {
  253. case SDLK_UP: Vel_y += MAN_SIZE / 20; break;
  254. case SDLK_DOWN: Vel_y -= MAN_SIZE / 20; break;
  255. case SDLK_LEFT: Vel_x += MAN_SIZE / 20; break;
  256. case SDLK_RIGHT: Vel_x -= MAN_SIZE / 20; break;
  257. case SDLK_d: rot = 0; break;
  258. case SDLK_a: rot = 0; break;
  259. case SDLK_w: w = 0; break;
  260. case SDLK_s: w = 0; break;
  261. }
  262. }
  263. }
  264.  
  265. void Man::move()
  266. {
  267. int a, b, c = MAN_SIZE / 20; //tmp
  268.  
  269. b= c*sin( rot_angle );
  270. a= c*cos( rot_angle );
  271. switch ( w )
  272. {
  273. case 1:
  274. x += b;
  275. y += a;
  276. break;
  277. case -1:
  278. x -= b;
  279. y -= a;
  280. break;
  281. default:
  282. break;
  283. }
  284.  
  285. //Move the Man left or right
  286. x += Vel_x;
  287.  
  288. //Rotate the Man
  289. switch ( rot )
  290. {
  291. case 1: rot_angle += rot_speed; break;
  292. case -1: rot_angle -= rot_speed; break;
  293. default: break;
  294. }
  295.  
  296. //---------
  297.  
  298. //If the Man went too far horizontally
  299. if( ( x < MAN_SIZE/2 ) || ( x + MAN_SIZE/2 > SCREEN_WIDTH ) )
  300. {
  301. //Move back
  302. x -= Vel_x;
  303. }
  304.  
  305. //Move the Man up or down
  306. y += Vel_y;
  307.  
  308. //If the Man went too far vertically
  309. if( ( y < MAN_SIZE/2 ) || ( y + MAN_SIZE/2 > SCREEN_HEIGHT ) )
  310. {
  311. //Move back
  312. y -= Vel_y;
  313. }
  314. }
  315.  
  316. void Man::show()
  317. {
  318. //Move to offset
  319. glTranslatef( x, y, 0 );
  320.  
  321. glRotatef(rot_angle,0.0f,0.0f,1.0f);
  322.  
  323. // Bind the texture to which subsequent calls refer to
  324. glBindTexture( GL_TEXTURE_2D, texture );
  325.  
  326. //Start quad
  327. glBegin( GL_QUADS );
  328.  
  329. //Draw Man
  330. glTexCoord2i( 0, 0 );
  331. glVertex2f( -MAN_SIZE/2,-MAN_SIZE/2);
  332.  
  333. glTexCoord2i( 1, 0 );
  334. glVertex2f( MAN_SIZE/2, -MAN_SIZE/2 );
  335.  
  336. glTexCoord2i( 1, 1 );
  337. glVertex2f( MAN_SIZE/2, MAN_SIZE/2);
  338.  
  339. glTexCoord2i( 0, 1 );
  340. glVertex2f( -MAN_SIZE/2,MAN_SIZE/2);
  341.  
  342. //End quad
  343. glEnd();
  344.  
  345. //Reset
  346. glLoadIdentity();
  347. }
  348.  
  349. Timer::Timer()
  350. {
  351. //Initialize the variables
  352. startTicks = 0;
  353. pausedTicks = 0;
  354. paused = false;
  355. started = false;
  356. }
  357.  
  358. void Timer::start()
  359. {
  360. //Start the timer
  361. started = true;
  362.  
  363. //Unpause the timer
  364. paused = false;
  365.  
  366. //Get the current clock time
  367. startTicks = SDL_GetTicks();
  368. }
  369.  
  370. void Timer::stop()
  371. {
  372. //Stop the timer
  373. started = false;
  374.  
  375. //Unpause the timer
  376. paused = false;
  377. }
  378.  
  379. void Timer::pause()
  380. {
  381. //If the timer is running and isn't already paused
  382. if( ( started == true ) && ( paused == false ) )
  383. {
  384. //Pause the timer
  385. paused = true;
  386.  
  387. //Calculate the paused ticks
  388. pausedTicks = SDL_GetTicks() - startTicks;
  389. }
  390. }
  391.  
  392. void Timer::unpause()
  393. {
  394. //If the timer is paused
  395. if( paused == true )
  396. {
  397. //Unpause the timer
  398. paused = false;
  399.  
  400. //Reset the starting ticks
  401. startTicks = SDL_GetTicks() - pausedTicks;
  402.  
  403. //Reset the paused ticks
  404. pausedTicks = 0;
  405. }
  406. }
  407.  
  408. int Timer::get_ticks()
  409. {
  410. //If the timer is running
  411. if( started == true )
  412. {
  413. //If the timer is paused
  414. if( paused == true )
  415. {
  416. //Return the number of ticks when the timer was paused
  417. return pausedTicks;
  418. }
  419. else
  420. {
  421. //Return the current time minus the start time
  422. return SDL_GetTicks() - startTicks;
  423. }
  424. }
  425.  
  426. //If the timer isn't running
  427. return 0;
  428. }
  429.  
  430. bool Timer::is_started()
  431. {
  432. return started;
  433. }
  434.  
  435. bool Timer::is_paused()
  436. {
  437. return paused;
  438. }
  439.  
  440. int main( int argc, char *argv[] )
  441. {
  442. //Initialize
  443. if( init() == false )
  444. {
  445. return 1;
  446. }
  447.  
  448. if( load_img() == false ) return 1;
  449.  
  450. //Our Man object
  451. Man Man;
  452.  
  453. //The frame rate regulator
  454. Timer fps;
  455.  
  456. //Wait for user exit
  457. while( quit == false )
  458. {
  459. //Start the frame timer
  460. fps.start();
  461.  
  462. //While there are events to handle
  463. while( SDL_PollEvent( &event ) )
  464. {
  465. //Handle key presses
  466. Man.handle_input();
  467.  
  468. if( event.type == SDL_QUIT )
  469. {
  470. quit = true;
  471. }
  472. }
  473.  
  474. //Move the Man
  475. Man.move();
  476.  
  477. //Clear the screen
  478. glClear( GL_COLOR_BUFFER_BIT );
  479.  
  480. //Show the Man
  481. Man.show();
  482.  
  483. //Update screen
  484. SDL_GL_SwapBuffers();
  485.  
  486. //Cap the frame rate
  487. if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
  488. {
  489. SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
  490. }
  491. }
  492.  
  493. //Clean up
  494. clean_up();
  495.  
  496. return 0;
  497. }

Report this snippet  

You need to login to post a comment.