Posted By

ozkriff on 10/08/08


Tagged


Versions (?)

sdl+opengl test


 / Published in: C++
 

  1. /*This source code copyrighted by Lazy Foo' Productions (2004-2008) and may not
  2.  
  3. be redestributed without written permission.*/
  4.  
  5.  
  6.  
  7.  
  8.  
  9. /*compillig on riff's system:
  10.  
  11. g++ main.cpp -lGL -lGLU -lSDL -I/usr/include/SDL -o test
  12.  
  13. */
  14.  
  15.  
  16.  
  17. //The headers
  18.  
  19. #include "SDL/SDL.h"
  20.  
  21. #include "SDL/SDL_opengl.h"
  22.  
  23.  
  24.  
  25. //Screen attributes
  26.  
  27. const int SCREEN_WIDTH = 800;
  28.  
  29. const int SCREEN_HEIGHT = 600;
  30.  
  31. const int SCREEN_BPP = 32;
  32.  
  33.  
  34.  
  35. //The frame rate
  36.  
  37. const int FRAMES_PER_SECOND = 60;
  38.  
  39.  
  40.  
  41. //The attributes of the square
  42.  
  43. const int SQUARE_WIDTH = 20;
  44.  
  45. const int SQUARE_HEIGHT = 20;
  46.  
  47.  
  48.  
  49. //Event handler
  50.  
  51. SDL_Event event;
  52.  
  53.  
  54.  
  55. //The square
  56.  
  57. class Square
  58.  
  59. {
  60.  
  61. private:
  62.  
  63. //The offsets
  64.  
  65. int x, y;
  66.  
  67.  
  68.  
  69. //The velocity of the square
  70.  
  71. int xVel, yVel;
  72.  
  73.  
  74.  
  75. public:
  76.  
  77. //Initializes
  78.  
  79. Square();
  80.  
  81.  
  82.  
  83. //Handles key presses
  84.  
  85. void handle_input();
  86.  
  87.  
  88.  
  89. //Moves the square
  90.  
  91. void move();
  92.  
  93.  
  94.  
  95. //Shows the square on the screen
  96.  
  97. void show();
  98.  
  99. };
  100.  
  101.  
  102.  
  103. //The timer
  104.  
  105. class Timer
  106.  
  107. {
  108.  
  109. private:
  110.  
  111. //The clock time when the timer started
  112.  
  113. int startTicks;
  114.  
  115.  
  116.  
  117. //The ticks stored when the timer was paused
  118.  
  119. int pausedTicks;
  120.  
  121.  
  122.  
  123. //The timer status
  124.  
  125. bool paused;
  126.  
  127. bool started;
  128.  
  129.  
  130.  
  131. public:
  132.  
  133. //Initializes variables
  134.  
  135. Timer();
  136.  
  137.  
  138.  
  139. //The various clock actions
  140.  
  141. void start();
  142.  
  143. void stop();
  144.  
  145. void pause();
  146.  
  147. void unpause();
  148.  
  149.  
  150.  
  151. //Gets the timer's time
  152.  
  153. int get_ticks();
  154.  
  155.  
  156.  
  157. //Checks the status of the timer
  158.  
  159. bool is_started();
  160.  
  161. bool is_paused();
  162.  
  163. };
  164.  
  165.  
  166.  
  167. bool init_GL()
  168.  
  169. {
  170.  
  171. //Set clear color
  172.  
  173. glClearColor( 0, 0, 0, 0 );
  174.  
  175.  
  176.  
  177. //Set projection
  178.  
  179. glMatrixMode( GL_PROJECTION );
  180.  
  181. glLoadIdentity();
  182.  
  183. glOrtho( 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1 );
  184.  
  185.  
  186.  
  187. //Initialize modelview matrix
  188.  
  189. glMatrixMode( GL_MODELVIEW );
  190.  
  191. glLoadIdentity();
  192.  
  193.  
  194.  
  195. //If there was any errors
  196.  
  197. if( glGetError() != GL_NO_ERROR )
  198.  
  199. {
  200.  
  201. return false;
  202.  
  203. }
  204.  
  205.  
  206.  
  207. //If everything initialized
  208.  
  209. return true;
  210.  
  211. }
  212.  
  213.  
  214.  
  215. bool init()
  216.  
  217. {
  218.  
  219. //Initialize SDL
  220.  
  221. if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
  222.  
  223. {
  224.  
  225. return false;
  226.  
  227. }
  228.  
  229.  
  230.  
  231. //Create Window
  232.  
  233. if( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL )
  234.  
  235. {
  236.  
  237. return false;
  238.  
  239. }
  240.  
  241.  
  242.  
  243. //Initialize OpenGL
  244.  
  245. if( init_GL() == false )
  246.  
  247. {
  248.  
  249. return false;
  250.  
  251. }
  252.  
  253.  
  254.  
  255. //Set caption
  256.  
  257. SDL_WM_SetCaption( "OpenGL Test", NULL );
  258.  
  259.  
  260.  
  261. return true;
  262.  
  263. }
  264.  
  265.  
  266.  
  267. void clean_up()
  268.  
  269. {
  270.  
  271. //Quit SDL
  272.  
  273. SDL_Quit();
  274.  
  275. }
  276.  
  277.  
  278.  
  279. Square::Square()
  280.  
  281. {
  282.  
  283. //Initialize offsets
  284.  
  285. x = 0;
  286.  
  287. y = 0;
  288.  
  289.  
  290.  
  291. //Initialize velocity
  292.  
  293. xVel = 0;
  294.  
  295. yVel = 0;
  296.  
  297. }
  298.  
  299.  
  300.  
  301. void Square::handle_input()
  302.  
  303. {
  304.  
  305. //If a key was pressed
  306.  
  307. if( event.type == SDL_KEYDOWN )
  308.  
  309. {
  310.  
  311. //Adjust the velocity
  312.  
  313. switch( event.key.keysym.sym )
  314.  
  315. {
  316.  
  317. case SDLK_UP: yVel -= SQUARE_HEIGHT / 2; break;
  318.  
  319. case SDLK_DOWN: yVel += SQUARE_HEIGHT / 2; break;
  320.  
  321. case SDLK_LEFT: xVel -= SQUARE_WIDTH / 2; break;
  322.  
  323. case SDLK_RIGHT: xVel += SQUARE_WIDTH / 2; break;
  324.  
  325. }
  326.  
  327. }
  328.  
  329. //If a key was released
  330.  
  331. else if( event.type == SDL_KEYUP )
  332.  
  333. {
  334.  
  335. //Adjust the velocity
  336.  
  337. switch( event.key.keysym.sym )
  338.  
  339. {
  340.  
  341. case SDLK_UP: yVel += SQUARE_HEIGHT / 2; break;
  342.  
  343. case SDLK_DOWN: yVel -= SQUARE_HEIGHT / 2; break;
  344.  
  345. case SDLK_LEFT: xVel += SQUARE_WIDTH / 2; break;
  346.  
  347. case SDLK_RIGHT: xVel -= SQUARE_WIDTH / 2; break;
  348.  
  349. }
  350.  
  351. }
  352.  
  353. }
  354.  
  355.  
  356.  
  357. void Square::move()
  358.  
  359. {
  360.  
  361. //Move the square left or right
  362.  
  363. x += xVel;
  364.  
  365.  
  366.  
  367. //If the square went too far
  368.  
  369. if( ( x < 0 ) || ( x + SQUARE_WIDTH > SCREEN_WIDTH ) )
  370.  
  371. {
  372.  
  373. //Move back
  374.  
  375. x -= xVel;
  376.  
  377. }
  378.  
  379.  
  380.  
  381. //Move the square up or down
  382.  
  383. y += yVel;
  384.  
  385.  
  386.  
  387. //If the square went too far
  388.  
  389. if( ( y < 0 ) || ( y + SQUARE_HEIGHT > SCREEN_HEIGHT ) )
  390.  
  391. {
  392.  
  393. //Move back
  394.  
  395. y -= yVel;
  396.  
  397. }
  398.  
  399. }
  400.  
  401.  
  402.  
  403. void Square::show()
  404.  
  405. {
  406.  
  407. //Move to offset
  408.  
  409. glTranslatef( x, y, 0 );
  410.  
  411.  
  412.  
  413. //Start quad
  414.  
  415. glBegin( GL_QUADS );
  416.  
  417.  
  418.  
  419. //Set color to white
  420.  
  421. glColor4f( 1.0, 1.0, 1.0, 1.0 );
  422.  
  423.  
  424.  
  425. //Draw square
  426.  
  427. glVertex3f( 0, 0, 0 );
  428.  
  429. glVertex3f( SQUARE_WIDTH, 0, 0 );
  430.  
  431. glVertex3f( SQUARE_WIDTH, SQUARE_HEIGHT, 0 );
  432.  
  433. glVertex3f( 0, SQUARE_HEIGHT, 0 );
  434.  
  435.  
  436.  
  437. //End quad
  438.  
  439. glEnd();
  440.  
  441.  
  442.  
  443. //Reset
  444.  
  445. glLoadIdentity();
  446.  
  447. }
  448.  
  449.  
  450.  
  451. Timer::Timer()
  452.  
  453. {
  454.  
  455. //Initialize the variables
  456.  
  457. startTicks = 0;
  458.  
  459. pausedTicks = 0;
  460.  
  461. paused = false;
  462.  
  463. started = false;
  464.  
  465. }
  466.  
  467.  
  468.  
  469. void Timer::start()
  470.  
  471. {
  472.  
  473. //Start the timer
  474.  
  475. started = true;
  476.  
  477.  
  478.  
  479. //Unpause the timer
  480.  
  481. paused = false;
  482.  
  483.  
  484.  
  485. //Get the current clock time
  486.  
  487. startTicks = SDL_GetTicks();
  488.  
  489. }
  490.  
  491.  
  492.  
  493. void Timer::stop()
  494.  
  495. {
  496.  
  497. //Stop the timer
  498.  
  499. started = false;
  500.  
  501.  
  502.  
  503. //Unpause the timer
  504.  
  505. paused = false;
  506.  
  507. }
  508.  
  509.  
  510.  
  511. void Timer::pause()
  512.  
  513. {
  514.  
  515. //If the timer is running and isn't already paused
  516.  
  517. if( ( started == true ) && ( paused == false ) )
  518.  
  519. {
  520.  
  521. //Pause the timer
  522.  
  523. paused = true;
  524.  
  525.  
  526.  
  527. //Calculate the paused ticks
  528.  
  529. pausedTicks = SDL_GetTicks() - startTicks;
  530.  
  531. }
  532.  
  533. }
  534.  
  535.  
  536.  
  537. void Timer::unpause()
  538.  
  539. {
  540.  
  541. //If the timer is paused
  542.  
  543. if( paused == true )
  544.  
  545. {
  546.  
  547. //Unpause the timer
  548.  
  549. paused = false;
  550.  
  551.  
  552.  
  553. //Reset the starting ticks
  554.  
  555. startTicks = SDL_GetTicks() - pausedTicks;
  556.  
  557.  
  558.  
  559. //Reset the paused ticks
  560.  
  561. pausedTicks = 0;
  562.  
  563. }
  564.  
  565. }
  566.  
  567.  
  568.  
  569. int Timer::get_ticks()
  570.  
  571. {
  572.  
  573. //If the timer is running
  574.  
  575. if( started == true )
  576.  
  577. {
  578.  
  579. //If the timer is paused
  580.  
  581. if( paused == true )
  582.  
  583. {
  584.  
  585. //Return the number of ticks when the timer was paused
  586.  
  587. return pausedTicks;
  588.  
  589. }
  590.  
  591. else
  592.  
  593. {
  594.  
  595. //Return the current time minus the start time
  596.  
  597. return SDL_GetTicks() - startTicks;
  598.  
  599. }
  600.  
  601. }
  602.  
  603.  
  604.  
  605. //If the timer isn't running
  606.  
  607. return 0;
  608.  
  609. }
  610.  
  611.  
  612.  
  613. bool Timer::is_started()
  614.  
  615. {
  616.  
  617. return started;
  618.  
  619. }
  620.  
  621.  
  622.  
  623. bool Timer::is_paused()
  624.  
  625. {
  626.  
  627. return paused;
  628.  
  629. }
  630.  
  631.  
  632.  
  633. int main( int argc, char *argv[] )
  634.  
  635. {
  636.  
  637. //Quit flag
  638.  
  639. bool quit = false;
  640.  
  641.  
  642.  
  643. //Initialize
  644.  
  645. if( init() == false )
  646.  
  647. {
  648.  
  649. return 1;
  650.  
  651. }
  652.  
  653.  
  654.  
  655. //Our square object
  656.  
  657. Square square;
  658.  
  659.  
  660.  
  661. //The frame rate regulator
  662.  
  663. Timer fps;
  664.  
  665.  
  666.  
  667. //Wait for user exit
  668.  
  669. while( quit == false )
  670.  
  671. {
  672.  
  673. //Start the frame timer
  674.  
  675. fps.start();
  676.  
  677.  
  678.  
  679. //While there are events to handle
  680.  
  681. while( SDL_PollEvent( &event ) )
  682.  
  683. {
  684.  
  685. //Handle key presses
  686.  
  687. square.handle_input();
  688.  
  689.  
  690.  
  691. if( event.type == SDL_QUIT )
  692.  
  693. {
  694.  
  695. quit = true;
  696.  
  697. }
  698.  
  699. }
  700.  
  701.  
  702.  
  703. //Move the square
  704.  
  705. square.move();
  706.  
  707.  
  708.  
  709. //Clear the screen
  710.  
  711. glClear( GL_COLOR_BUFFER_BIT );
  712.  
  713.  
  714.  
  715. //Show the square
  716.  
  717. square.show();
  718.  
  719.  
  720.  
  721. //Update screen
  722.  
  723. SDL_GL_SwapBuffers();
  724.  
  725.  
  726.  
  727. //Cap the frame rate
  728.  
  729. if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
  730.  
  731. {
  732.  
  733. SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
  734.  
  735. }
  736.  
  737. }
  738.  
  739.  
  740.  
  741. //Clean up
  742.  
  743. clean_up();
  744.  
  745.  
  746.  
  747. return 0;
  748.  
  749. }

Report this snippet  

You need to login to post a comment.