/ Published in: C++
=)
Expand |
Embed | Plain Text
/********************************************************************** compillig: g++ new.cpp -lGL -lGLU -lSDL -I/usr/include/SDL -o test **********************************************************************/ //The headers #include "SDL/SDL.h" #include "SDL/SDL_opengl.h" #include <stdio.h> //for debug #include <math.h> //for sin, cos GLuint texture; // Texture object handle SDL_Surface *surface; // Gives us the information to make the texture //Screen attributes const int SCREEN_WIDTH = 800; const int SCREEN_HEIGHT = 600; const int SCREEN_BPP = 32; //The frame rate const int FRAMES_PER_SECOND = 60; const int MAN_SIZE = SCREEN_WIDTH/6; //rotate the Man? int rot = 0; // How fast? int rot_speed = 5; //Quit flag bool quit = false; //Event handler SDL_Event event; //The Man class Man { private: //The offsets int x, y; //The velocity of the Man int Vel_x, Vel_y; int w; //The rotation angle int rot_angle; public: //Initializes Man(); //Handles key presses void handle_input(); //Moves the Man void move(); //Shows the Man on the screen void show(); }; //The timer class Timer { private: //The clock time when the timer started int startTicks; //The ticks stored when the timer was paused int pausedTicks; //The timer status bool paused; bool started; public: //Initializes variables Timer(); //The various clock actions void start(); void stop(); void pause(); void unpause(); //Gets the timer's time int get_ticks(); //Checks the status of the timer bool is_started(); bool is_paused(); }; bool init_GL() { //Set clear color glClearColor( 0, 0, 0, 0 ); // Need this to display a texture glEnable( GL_TEXTURE_2D ); glViewport( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT ); 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 load_img() { // Load the OpenGL texture //moved to global variables: /* //~ GLuint texture; // Texture object handle //~ SDL_Surface *surface; // Gives us the information to make the texture */ if ( (surface = SDL_LoadBMP("image.bmp")) ) { // Check that the image's width is a power of 2 if ( (surface->w & (surface->w - 1)) != 0 ) { printf("warning: image.bmp's width is not a power of 2\n"); } // Also check if the height is a power of 2 if ( (surface->h & (surface->h - 1)) != 0 ) { printf("warning: image.bmp's height is not a power of 2\n"); } // Have OpenGL generate a texture object handle for us glGenTextures( 1, &texture ); // Bind the texture object glBindTexture( GL_TEXTURE_2D, texture ); // Set the texture's stretching properties glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); // Edit the texture object's image data using the information SDL_Surface gives us glTexImage2D( GL_TEXTURE_2D, 0, 3, surface->w, surface->h, 0, GL_BGR, GL_UNSIGNED_BYTE, surface->pixels ); } else { printf("SDL could not load image.bmp: %s\n", SDL_GetError()); SDL_Quit(); return 1; } // Free the SDL_Surface only if it was successfully created if ( surface ) { SDL_FreeSurface( surface ); } return true; } bool init() { //Initialize SDL if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) { printf("Unable to initialize SDL: %s\n", SDL_GetError()); return false; } //Create Window if( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL ) { printf("Unable to set video mode: %s\n", SDL_GetError()); return false; } //Initialize OpenGL if( init_GL() == false ) { return false; } //Set caption SDL_WM_SetCaption( "OpenGL/SDL Test", NULL ); return true; } void clean_up() { // Delete the OpenGL texture glDeleteTextures( 1, &texture ); //Quit SDL SDL_Quit(); } Man::Man() { //Initialize offsets x = SCREEN_WIDTH/2; y = SCREEN_HEIGHT/2; //Initialize velocity Vel_x = 0; Vel_y = 0; w = 0; //Init rot angl rot_angle = 0; } void Man::handle_input() { //If a key was pressed if( event.type == SDL_KEYDOWN ) { //Adjust the velocity switch( event.key.keysym.sym ) { case SDLK_UP: Vel_y -= MAN_SIZE / 20; break; case SDLK_DOWN: Vel_y += MAN_SIZE / 20; break; case SDLK_LEFT: Vel_x -= MAN_SIZE / 20; break; case SDLK_RIGHT: Vel_x += MAN_SIZE / 20; break; case SDLK_d: rot = 1; break; //right case SDLK_a: rot = -1; break; //left case SDLK_q: quit = true; break; case SDLK_w: w = 1; break; case SDLK_s: w = -1; break; } } //If a key was released else if( event.type == SDL_KEYUP ) { //Adjust the velocity switch( event.key.keysym.sym ) { case SDLK_UP: Vel_y += MAN_SIZE / 20; break; case SDLK_DOWN: Vel_y -= MAN_SIZE / 20; break; case SDLK_LEFT: Vel_x += MAN_SIZE / 20; break; case SDLK_RIGHT: Vel_x -= MAN_SIZE / 20; break; case SDLK_d: rot = 0; break; case SDLK_a: rot = 0; break; case SDLK_w: w = 0; break; case SDLK_s: w = 0; break; } } } void Man::move() { int a, b, c = MAN_SIZE / 20; //tmp b= c*sin( rot_angle ); a= c*cos( rot_angle ); switch ( w ) { case 1: x += b; y += a; break; case -1: x -= b; y -= a; break; default: break; } //Move the Man left or right x += Vel_x; //Rotate the Man switch ( rot ) { case 1: rot_angle += rot_speed; break; case -1: rot_angle -= rot_speed; break; default: break; } //--------- //If the Man went too far horizontally if( ( x < MAN_SIZE/2 ) || ( x + MAN_SIZE/2 > SCREEN_WIDTH ) ) { //Move back x -= Vel_x; } //Move the Man up or down y += Vel_y; //If the Man went too far vertically if( ( y < MAN_SIZE/2 ) || ( y + MAN_SIZE/2 > SCREEN_HEIGHT ) ) { //Move back y -= Vel_y; } } void Man::show() { //Move to offset glTranslatef( x, y, 0 ); glRotatef(rot_angle,0.0f,0.0f,1.0f); // Bind the texture to which subsequent calls refer to glBindTexture( GL_TEXTURE_2D, texture ); //Start quad glBegin( GL_QUADS ); //Draw Man glTexCoord2i( 0, 0 ); glVertex2f( -MAN_SIZE/2,-MAN_SIZE/2); glTexCoord2i( 1, 0 ); glVertex2f( MAN_SIZE/2, -MAN_SIZE/2 ); glTexCoord2i( 1, 1 ); glVertex2f( MAN_SIZE/2, MAN_SIZE/2); glTexCoord2i( 0, 1 ); glVertex2f( -MAN_SIZE/2,MAN_SIZE/2); //End quad glEnd(); //Reset glLoadIdentity(); } Timer::Timer() { //Initialize the variables startTicks = 0; pausedTicks = 0; paused = false; started = false; } void Timer::start() { //Start the timer started = true; //Unpause the timer paused = false; //Get the current clock time startTicks = SDL_GetTicks(); } void Timer::stop() { //Stop the timer started = false; //Unpause the timer paused = false; } void Timer::pause() { //If the timer is running and isn't already paused if( ( started == true ) && ( paused == false ) ) { //Pause the timer paused = true; //Calculate the paused ticks pausedTicks = SDL_GetTicks() - startTicks; } } void Timer::unpause() { //If the timer is paused if( paused == true ) { //Unpause the timer paused = false; //Reset the starting ticks startTicks = SDL_GetTicks() - pausedTicks; //Reset the paused ticks pausedTicks = 0; } } int Timer::get_ticks() { //If the timer is running if( started == true ) { //If the timer is paused if( paused == true ) { //Return the number of ticks when the timer was paused return pausedTicks; } else { //Return the current time minus the start time return SDL_GetTicks() - startTicks; } } //If the timer isn't running return 0; } bool Timer::is_started() { return started; } bool Timer::is_paused() { return paused; } int main( int argc, char *argv[] ) { //Initialize if( init() == false ) { return 1; } if( load_img() == false ) return 1; //Our Man object Man Man; //The frame rate regulator Timer fps; //Wait for user exit while( quit == false ) { //Start the frame timer fps.start(); //While there are events to handle while( SDL_PollEvent( &event ) ) { //Handle key presses Man.handle_input(); if( event.type == SDL_QUIT ) { quit = true; } } //Move the Man Man.move(); //Clear the screen glClear( GL_COLOR_BUFFER_BIT ); //Show the Man Man.show(); //Update screen SDL_GL_SwapBuffers(); //Cap the frame rate if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND ) { SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() ); } } //Clean up clean_up(); return 0; }
You need to login to post a comment.
