/ Published in: C++

<?xml>
Expand |
Embed | Plain Text
/*This source code copyrighted by Lazy Foo' Productions (2004-2009) and may not be redestributed without written permission.*/ //The headers #include "SDL/SDL.h" #include "SDL/SDL_opengl.h" //Screen attributes const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; const int SCREEN_BPP = 32; //The attributes of the square const int SQUARE_SIZE = 20; const int SQUARE_SPEED = 100; //Event handler SDL_Event event; //Quit flag bool quit = false; int timer = 0; //The square class Square { private: //The offsets float x, y; //The velocity of the square float xVel, yVel; //The rotatioon float rVel, rotation; public: //Initializes Square(); //Handles key presses void handle_input(); //Moves the square void move(Uint32 deltaTicks); //Shows the square on the screen void show(); }; bool init_GL() { //Set clear color glClearColor( 0, 0, 0, 0 ); //Set projection glMatrixMode( GL_PROJECTION ); glLoadIdentity(); glOrtho( 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1 ); //Initialize modelview matrix glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); //If there was any errors if ( glGetError() != GL_NO_ERROR ) { return false; } //If everything initialized return true; } bool init() { //Initialize SDL if ( SDL_Init( SDL_INIT_EVERYTHING ) < 0 ) { return false; } //Create Window if ( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL ) { return false; } //Initialize OpenGL if ( init_GL() == false ) { return false; } //Set caption SDL_WM_SetCaption( "OpenGL Test", NULL ); return true; } void clean_up() { //Quit SDL SDL_Quit(); } Square::Square() { //Initialize offsets x = 0; y = 0; //Initialize velocity xVel = 0; yVel = 0; rVel = 0; rotation = 0; } void Square::handle_input() { //If a key was pressed if ( event.type == SDL_KEYDOWN ) { //Adjust the velocity switch ( event.key.keysym.sym ) { case SDLK_UP: yVel -= SQUARE_SPEED; break; case SDLK_DOWN: yVel += SQUARE_SPEED; break; case SDLK_LEFT: xVel -= SQUARE_SPEED; break; case SDLK_RIGHT: xVel += SQUARE_SPEED; break; case SDLK_w: rVel += 60; break; case SDLK_e: rVel -= 60; break; case SDLK_q: quit = true; break; } } //If a key was released else if ( event.type == SDL_KEYUP ) { //Adjust the velocity switch ( event.key.keysym.sym ) { case SDLK_UP: yVel += SQUARE_SPEED; break; case SDLK_DOWN: yVel -= SQUARE_SPEED; break; case SDLK_LEFT: xVel += SQUARE_SPEED; break; case SDLK_RIGHT: xVel -= SQUARE_SPEED; break; case SDLK_w: rVel -= 60; break; case SDLK_e: rVel += 60; break; } } } void Square::move(Uint32 deltaTicks) { //Move the square left or right x += xVel* ( deltaTicks / 1000.f ); //If the square went too far if ( ( x < 0 ) || ( x + SQUARE_SIZE > SCREEN_WIDTH ) ) { //Move back x -= xVel* ( deltaTicks / 1000.f ); } //Move the square up or down y += yVel* ( deltaTicks / 1000.f ); //If the square went too far if ( ( y < 0 ) || ( y + SQUARE_SIZE > SCREEN_HEIGHT ) ) { //Move back y -= yVel* ( deltaTicks / 1000.f ); } rotation += rVel* ( deltaTicks / 1000.f ); } void Square::show() { //Move to offset glTranslatef( x, y, 0 ); glRotatef( rotation, 0, 0, 1); //Start quad glBegin( GL_QUADS ); { //Set color to white glColor4f( 1.0, 1.0, 1.0, 1.0 ); //Draw square glVertex3f( -SQUARE_SIZE, -SQUARE_SIZE, 0 ); glVertex3f( SQUARE_SIZE, -SQUARE_SIZE, 0 ); glVertex3f( SQUARE_SIZE, SQUARE_SIZE, 0 ); glVertex3f( -SQUARE_SIZE, SQUARE_SIZE, 0 ); } //End quad glEnd(); //Reset glLoadIdentity(); } int main ( int argc, char *argv[] ) { //Initialize if ( init() == false ) { return 1; } //Our square object Square square; timer = SDL_GetTicks(); //Wait for user exit while ( quit == false ) { //While there are events to handle while ( SDL_PollEvent( &event ) ) { //Handle key presses square.handle_input(); if ( event.type == SDL_QUIT ) { quit = true; } } //Move the square square.move( SDL_GetTicks() - timer ); timer = SDL_GetTicks(); //Clear the screen glClear( GL_COLOR_BUFFER_BIT ); //Show the square square.show(); //Update screen SDL_GL_SwapBuffers(); } //Clean up clean_up(); return 0; }
You need to login to post a comment.