Guest User

Untitled

a guest
Dec 22nd, 2011
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.42 KB | None | 0 0
  1. //The headers
  2. #include "SDL/SDL.h"
  3. #include "SDL/SDL_opengl.h"
  4. #include "stdio.h"
  5.  
  6. //Screen attributes
  7. const int SCREEN_WIDTH = 640;
  8. const int SCREEN_HEIGHT = 480;
  9. const int SCREEN_BPP = 32;
  10.  
  11. //The frame rate
  12. const int FRAMES_PER_SECOND = 60;
  13.  
  14. //The attributes of the square
  15. const int SQUARE_WIDTH = 20;
  16. const int SQUARE_HEIGHT = 20;
  17.  
  18. //Event handler
  19. SDL_Event event;
  20.  
  21. //The square
  22. class Square
  23. {
  24.     private:
  25.     //The offsets
  26.     int x, y;
  27.  
  28.     //The velocity of the square
  29.     int xVel, yVel;
  30.  
  31.     GLuint texture;
  32.  
  33.     public:
  34.     //Initializes
  35.     Square();
  36.  
  37.     //Handles key presses
  38.     void handle_input();
  39.  
  40.     //Moves the square
  41.     void move();
  42.  
  43.     //Shows the square on the screen
  44.     void show();
  45.  
  46.     void getTexture();
  47. };
  48.  
  49. //The timer
  50. class Timer
  51. {
  52.     private:
  53.     //The clock time when the timer started
  54.     int startTicks;
  55.  
  56.     //The ticks stored when the timer was paused
  57.     int pausedTicks;
  58.  
  59.     //The timer status
  60.     bool paused;
  61.     bool started;
  62.  
  63.     public:
  64.     //Initializes variables
  65.     Timer();
  66.  
  67.     //The various clock actions
  68.     void start();
  69.     void stop();
  70.     void pause();
  71.     void unpause();
  72.  
  73.     //Gets the timer's time
  74.     int get_ticks();
  75.  
  76.     //Checks the status of the timer
  77.     bool is_started();
  78.     bool is_paused();
  79. };
  80.  
  81. bool init_GL()
  82. {
  83.     //Set clear color
  84.     glClearColor( 0, 0, 0, 0 );
  85.  
  86.     //Set projection
  87.     glMatrixMode( GL_PROJECTION );
  88.     glLoadIdentity();
  89.     glOrtho( 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1 );
  90.  
  91.     //Initialize modelview matrix
  92.     glMatrixMode( GL_MODELVIEW );
  93.     glLoadIdentity();
  94.  
  95.     //If there was any errors
  96.     if( glGetError() != GL_NO_ERROR )
  97.     {
  98.         return false;
  99.     }
  100.  
  101.     //If everything initialized
  102.     return true;
  103. }
  104.  
  105. bool init()
  106. {
  107.     //Initialize SDL
  108.     if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
  109.     {
  110.         return false;
  111.     }
  112.  
  113.     //Create Window
  114.     if( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL )
  115.     {
  116.         return false;
  117.     }
  118.  
  119.     //Initialize OpenGL
  120.     if( init_GL() == false )
  121.     {
  122.         return false;
  123.     }
  124.  
  125.     //Set caption
  126.     SDL_WM_SetCaption( "OpenGL Test", NULL );
  127.  
  128.     return true;
  129. }
  130.  
  131. void clean_up()
  132. {
  133.     //Quit SDL
  134.     SDL_Quit();
  135. }
  136.  
  137. Square::Square():
  138.     x(0), y(0), xVel(0), yVel(0)
  139. {
  140.     getTexture();
  141. }
  142.  
  143. void Square::handle_input()
  144. {
  145.     //If a key was pressed
  146.     if( event.type == SDL_KEYDOWN )
  147.     {
  148.         //Adjust the velocity
  149.         switch( event.key.keysym.sym )
  150.         {
  151.             case SDLK_UP: yVel -= SQUARE_HEIGHT / 2; break;
  152.             case SDLK_DOWN: yVel += SQUARE_HEIGHT / 2; break;
  153.             case SDLK_LEFT: xVel -= SQUARE_WIDTH / 2; break;
  154.             case SDLK_RIGHT: xVel += SQUARE_WIDTH / 2; break;
  155.         }
  156.     }
  157.     //If a key was released
  158.     else if( event.type == SDL_KEYUP )
  159.     {
  160.         //Adjust the velocity
  161.         switch( event.key.keysym.sym )
  162.         {
  163.             case SDLK_UP: yVel += SQUARE_HEIGHT / 2; break;
  164.             case SDLK_DOWN: yVel -= SQUARE_HEIGHT / 2; break;
  165.             case SDLK_LEFT: xVel += SQUARE_WIDTH / 2; break;
  166.             case SDLK_RIGHT: xVel -= SQUARE_WIDTH / 2; break;
  167.         }
  168.     }
  169. }
  170.  
  171. void Square::move()
  172. {
  173.     //Move the square left or right
  174.     x += xVel;
  175.  
  176.     //If the square went too far
  177.     if( ( x < 0 ) || ( x + SQUARE_WIDTH > SCREEN_WIDTH ) )
  178.     {
  179.         //Move back
  180.         x -= xVel;
  181.     }
  182.  
  183.     //Move the square up or down
  184.     y += yVel;
  185.  
  186.     //If the square went too far
  187.     if( ( y < 0 ) || ( y + SQUARE_HEIGHT > SCREEN_HEIGHT ) )
  188.     {
  189.         //Move back
  190.         y -= yVel;
  191.     }
  192. }
  193.  
  194. void Square::getTexture()
  195. {
  196.     SDL_Surface *surface; // Gives us the information to make the texture
  197.  
  198.     if ( (surface = SDL_LoadBMP("image.bmp")) ) {
  199.  
  200.         // Check that the image's width is a power of 2
  201.         if ( (surface->w & (surface->w - 1)) != 0 ) {
  202.             printf("warning: image.bmp's width is not a power of 2\n");
  203.         }
  204.  
  205.         // Also check if the height is a power of 2
  206.         if ( (surface->h & (surface->h - 1)) != 0 ) {
  207.             printf("warning: image.bmp's height is not a power of 2\n");
  208.         }
  209.  
  210.         // Have OpenGL generate a texture object handle for us
  211.         glGenTextures( 1, &texture );
  212.  
  213.         // Bind the texture object
  214.         glBindTexture( GL_TEXTURE_2D, texture );
  215.  
  216.         // Set the texture's stretching properties
  217.         glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  218.         glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  219.  
  220.         // Edit the texture object's image data using the information SDL_Surface gives us
  221.         glTexImage2D( GL_TEXTURE_2D, 0, 3, surface->w, surface->h, 0,
  222.                       GL_BGR, GL_UNSIGNED_BYTE, surface->pixels );
  223.     }    
  224.  
  225.     // Free the SDL_Surface only if it was successfully created
  226.     if ( surface ) {
  227.         SDL_FreeSurface( surface );
  228.     }
  229.  
  230.     // Bind the texture to which subsequent calls refer to
  231.  
  232. }
  233.  
  234. void Square::show()
  235. {
  236.     glEnable(GL_TEXTURE_2D);
  237.     glBindTexture( GL_TEXTURE_2D, texture );
  238.  
  239.     //Move to offset
  240.     glTranslatef( x, y, 0 );
  241.  
  242.     //Start quad
  243.     glBegin( GL_QUADS );
  244.         // Top-left vertex (corner)
  245.         glTexCoord2i( 0, 0 );
  246.         glVertex3f( 100, 100, 0 );
  247.  
  248.         // Bottom-left vertex (corner)
  249.         glTexCoord2i( 1, 0 );
  250.         glVertex3f( 228, 100, 0 );
  251.  
  252.         // Bottom-right vertex (corner)
  253.         glTexCoord2i( 1, 1 );
  254.         glVertex3f( 228, 228, 0 );
  255.  
  256.         // Top-right vertex (corner)
  257.         glTexCoord2i( 0, 1 );
  258.         glVertex3f( 100, 228, 0 );
  259.     glEnd();
  260.  
  261.     //Reset
  262.     glLoadIdentity();
  263. }
  264.  
  265. Timer::Timer()
  266. {
  267.     //Initialize the variables
  268.     startTicks = 0;
  269.     pausedTicks = 0;
  270.     paused = false;
  271.     started = false;
  272. }
  273.  
  274. void Timer::start()
  275. {
  276.     //Start the timer
  277.     started = true;
  278.  
  279.     //Unpause the timer
  280.     paused = false;
  281.  
  282.     //Get the current clock time
  283.     startTicks = SDL_GetTicks();
  284. }
  285.  
  286. void Timer::stop()
  287. {
  288.     //Stop the timer
  289.     started = false;
  290.  
  291.     //Unpause the timer
  292.     paused = false;
  293. }
  294.  
  295. void Timer::pause()
  296. {
  297.     //If the timer is running and isn't already paused
  298.     if( ( started == true ) && ( paused == false ) )
  299.     {
  300.         //Pause the timer
  301.         paused = true;
  302.  
  303.         //Calculate the paused ticks
  304.         pausedTicks = SDL_GetTicks() - startTicks;
  305.     }
  306. }
  307.  
  308. void Timer::unpause()
  309. {
  310.     //If the timer is paused
  311.     if( paused == true )
  312.     {
  313.         //Unpause the timer
  314.         paused = false;
  315.  
  316.         //Reset the starting ticks
  317.         startTicks = SDL_GetTicks() - pausedTicks;
  318.  
  319.         //Reset the paused ticks
  320.         pausedTicks = 0;
  321.     }
  322. }
  323.  
  324. int Timer::get_ticks()
  325. {
  326.     //If the timer is running
  327.     if( started == true )
  328.     {
  329.         //If the timer is paused
  330.         if( paused == true )
  331.         {
  332.             //Return the number of ticks when the timer was paused
  333.             return pausedTicks;
  334.         }
  335.         else
  336.         {
  337.             //Return the current time minus the start time
  338.             return SDL_GetTicks() - startTicks;
  339.         }
  340.     }
  341.  
  342.     //If the timer isn't running
  343.     return 0;
  344. }
  345.  
  346. bool Timer::is_started()
  347. {
  348.     return started;
  349. }
  350.  
  351. bool Timer::is_paused()
  352. {
  353.     return paused;
  354. }
  355.  
  356. int main( int argc, char *argv[] )
  357. {
  358.     //Quit flag
  359.     bool quit = false;
  360.  
  361.     //Initialize
  362.     if( init() == false )
  363.     {
  364.         return 1;
  365.     }
  366.  
  367.     //Our square object
  368.     Square square;
  369.  
  370.     //The frame rate regulator
  371.     Timer fps;
  372.  
  373.     //Wait for user exit
  374.     while( quit == false )
  375.     {
  376.         //Start the frame timer
  377.         fps.start();
  378.  
  379.         //While there are events to handle
  380.         while( SDL_PollEvent( &event ) )
  381.         {
  382.             //Handle key presses
  383.             square.handle_input();
  384.  
  385.             if( event.type == SDL_QUIT )
  386.             {
  387.                 quit = true;
  388.             }
  389.         }
  390.  
  391.         //Move the square
  392.         square.move();
  393.  
  394.         //Clear the screen
  395.         glClear( GL_COLOR_BUFFER_BIT );
  396.  
  397.         //Show the square
  398.         square.show();
  399.  
  400.         //Update screen
  401.         SDL_GL_SwapBuffers();
  402.  
  403.         //Cap the frame rate
  404.         if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
  405.         {
  406.             SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
  407.         }
  408.     }
  409.  
  410.     //Clean up
  411.     clean_up();
  412.  
  413.     return 0;
  414. }
Advertisement
Add Comment
Please, Sign In to add comment