/ Published in: C
strange, but it's working.
Expand |
Embed | Plain Text
#include "SDL/SDL.h" #include "SDL/SDL_opengl.h" #include <math.h> const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; //The attributes of the square const int SQUARE_SIZE = 20; const int SQUARE_SPEED = 100; short int ROT_MOVE = 0; // must we move? [-1; 0; 1] short int QUIT = 0; //QUIT flag [0; 1] //The square float x = 0, y = 0, //The offsets of the square xVel = 0, yVel = 0, //The velocity of the square rVel = 0, rotation = 0; //The rotation of the square /* ------------------------------------------------------------------------- */ struct unit { float x, y, rot_vel, rot, size, speed; short int move, // must we move? [-1; 0; 1] live; }; /* ------------------------------------------------------------------------- */ int init_GL() { glClearColor( 0, 0, 0, 0 ); //Set clear color //Set projection glMatrixMode( GL_PROJECTION ); glLoadIdentity(); glOrtho( 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1 ); //Initialize modelview matrix glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); if ( glGetError() != GL_NO_ERROR ) { //If there was any errors return 1; } return 0; //If everything initialized } /* ------------------------------------------------------------------------- */ int init() { if ( SDL_Init( SDL_INIT_EVERYTHING ) < 0 ) { return 1; } //Create Window if ( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_OPENGL ) == NULL ) { return 2; } if ( init_GL() != 0 ) { return 3; } SDL_WM_SetCaption( "OpenGL Test", NULL ); return 0; } /* ------------------------------------------------------------------------- */ void square_handle_input ( SDL_Event event ) { if ( event.type == SDL_KEYDOWN ) //If a key was pressed 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: ROT_MOVE = 1; break; case SDLK_a: rVel -= 120; break; case SDLK_s: ROT_MOVE = -1; break; case SDLK_d: rVel += 120; break; case SDLK_q: QUIT = 1; break; default: break; } else if ( event.type == SDL_KEYUP ) //If a key was released 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: ROT_MOVE = 0; break; case SDLK_a: rVel += 120; break; case SDLK_s: ROT_MOVE = 0; break; case SDLK_d: rVel -= 120; break; default: break; } } /* ------------------------------------------------------------------------- */ void square_move ( Uint32 d_time ) { rotation += rVel * ( d_time / 1000.f ); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ #if 0 x += xVel * ( d_time / 1000.f ); //Move the square <-> if ( ( x < 0 ) || ( x + SQUARE_SIZE > SCREEN_WIDTH ) ) //If too far x -= xVel* ( d_time / 1000.f ); //Move back y += yVel * ( d_time / 1000.f ); //Move the square up or down if ( ( y < 0 ) || ( y + SQUARE_SIZE > SCREEN_HEIGHT ) ) //If too far y -= yVel* ( d_time / 1000.f ); //Move back #endif /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ x += ( SQUARE_SPEED * sinf(rotation*M_PI/180.0f) ) * ( d_time / 1000.f ) * ROT_MOVE; y -= ( SQUARE_SPEED * cosf(rotation*M_PI/180.0f) ) * ( d_time / 1000.f ) * ROT_MOVE; } /* ------------------------------------------------------------------------- */ void square_show() { //Move to offset glTranslatef( x, y, 0 ); glRotatef( rotation, 0, 0, 1); glBegin( GL_QUADS ); { glColor4f( 1.0, 1.0, 1.0, 1.0 ); //Set color to white glVertex3f( -SQUARE_SIZE, -SQUARE_SIZE, 0 ); glVertex3f( SQUARE_SIZE, -SQUARE_SIZE, 0 ); glVertex3f( SQUARE_SIZE, SQUARE_SIZE, 0 ); glVertex3f( -SQUARE_SIZE, SQUARE_SIZE, 0 ); } glEnd(); glLoadIdentity(); //Reset } /* ------------------------------------------------------------------------- */ int main ( int argc, char *argv[] ) { //Initialize if ( init() != 0 ) return 1; SDL_Event event; //Event handler int timer = 0; timer = SDL_GetTicks(); struct unit player; player.x=0, player.y=0, player.rot_vel=0, player.rot=0, player.size=15, player.speed=120; player.move=0, // must we move? [-1; 0; 1] player.live=1; // [0;1] while ( QUIT == 0 ) { /* While there are events to handle. */ while ( SDL_PollEvent(&event) ) { square_handle_input(event); //Handle key presses if ( event.type == SDL_QUIT ) QUIT = 1; } square_move( SDL_GetTicks() - timer ); timer = SDL_GetTicks(); glClear( GL_COLOR_BUFFER_BIT ); //Clear the screen square_show(); SDL_GL_SwapBuffers(); //Update screen } SDL_QUIT(); return 0; }
You need to login to post a comment.
