ohohvi

Member
  • Content count

    29
  • Joined

  • Last visited

Community Reputation

122 Neutral

About ohohvi

  • Rank
    Member
  1. SDL event loop woes

    Finally, I figured it out. Somehow event.motion.x gets messed up when I called SDL_GetKeyState(), so when I use it to determine the distance between the ship and the cursor after SDL_GetKeyState, I was getting bogus numbers. Thanks for the help everyone.
  2. SDL event loop woes

    The wierd part is that even if the line reads : if( keys[SDLK_UP ); with nothing after it, the ship will still fly off when up is pressed. I just don't understand...
  3. SDL event loop woes

    Well that fixed my mouse lag, which is a big plus. However, the ship still likes to fly off to the right whenever I push up or down. Should SDL_GetKeyState be included in the while(SDL_PollEvent()) loop or should it be after the switch and outside the loop?
  4. I am trying to write a simple game in SDL but I'm having some problems. Basically, the event loop from this program is modified from cone3d, which isn't quite working now. The mouse controls the x movement of the ship, and the ship moves towards the cursor at a speed inversely proportional to the ship's distance from the cursor. The y movements are controlled by the up/down arrows. The ship moves along the x axis just fine, but as soon as I hit up or down, it goes flying off to the right, which shouldnt be affected by the y movements, only the mouse. Anyways, another problem I've been having is the mouse has a lag between when I move the mouse and when the cursor actually moves. This is pretty much my first real program so feel free to critique any problems you see with the code. Thanks. int main( int argc, char* argv[] ) { SDL_Surface* screen; //Sprite class basically just loads BMP, converts it to display format, and draws it when necessary Sprite* background; Sprite* cursor; //Ship class controls position of ship as well as its own sprite class Ship* ship; SDL_Rect shiprect, bgrect, cursorrect; int running = 1; SDL_Event event; Uint8* keys; int distance, one = 1; float angle; if( SDL_Init( SDL_INIT_VIDEO ) == -1 ){ cout<<"Error initializing SDL"<<endl; exit(-1); } atexit(SDL_Quit); screen = SDL_SetVideoMode( 800, 600, 24, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_ANYFORMAT | SDL_FULLSCREEN ); if( screen == NULL ) { cout<<"Unable to initialize screen"<<endl; exit(1); } shiprect.x = 0; shiprect.y = 0; shiprect.w = 32; shiprect.h = 32; ship = new Ship( "ship.bmp", &shiprect ); ship->SetPos( 300, 400 ); bgrect.x = 0; bgrect.y = 0; bgrect.w = 800; bgrect.h = 600; background = new Sprite( "stars.bmp", &bgrect ); SDL_ShowCursor(0); SDL_WM_GrabInput( SDL_GRAB_ON ); cursorrect.x = 0; cursorrect.y = 0; cursorrect.w = 32; cursorrect.h = 32; cursor = new Sprite( "cursor.bmp", &cursorrect ); while( running == 1 ) { SDL_PollEvent( &event ); switch( event.type ) { case SDL_QUIT: running = 0; break; case SDL_KEYDOWN: if( event.key.keysym.sym == SDLK_ESCAPE ) running = 0; break; case SDL_MOUSEMOTION: cursorrect.x = event.motion.x-16; cursorrect.y = event.motion.y-16; break; default: break; } keys = SDL_GetKeyState( NULL ); if( keys[SDLK_UP] ) ship->ChangePos( 0, -3 ); if( keys[SDLK_DOWN] ) { if( ship->GetYPos() < 568 ) ship->ChangePos( 0, 3 ); } //determine distance between ship and cursor and move ship accordingly distance = (event.motion.x-ship->GetXPos()-16)/10; if( distance < 0 ) { one = -1; distance *= one; } else one = 1; cout<<distance<<endl; if( distance != 0 ) ship->ChangePos( (one * (int)sqrt((double)distance)), 0); //angle between cursor and ship, not yet implemented (double)angle = atan( (double)(event.motion.y-ship->GetYPos()-16)/(ship->GetXPos()-event.motion.x+16) ); cout<<angle<<endl; background->DrawSprite( screen, &bgrect ); ship->DrawShip( screen ); cursor->DrawSprite( screen, &cursorrect ); SDL_Flip( screen ); } delete ship; delete background; delete cursor; return 1; }