Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //http://www.youtube.com/user/thecplusplusguy
- //frame independent movement
- #include <iostream>
- #include <SDL/SDL.h>
- int main()
- {
- SDL_Init(SDL_INIT_EVERYTHING);
- SDL_Surface* screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
- bool running=true;
- SDL_Event event;
- const int SPEED=50;
- Uint32 time;
- float xpos=50.0,ypos=50.0;
- SDL_Rect pos;
- pos.w=30;
- pos.h=30;
- while(running)
- {
- while(SDL_PollEvent(&event))
- {
- if(event.type==SDL_QUIT)
- {
- running=false;
- break;
- }
- }
- Uint8* keys=SDL_GetKeyState(0);
- double deltaTimeInSeconds=(SDL_GetTicks()-time)/1000.0;
- if(keys[SDLK_a])
- {
- xpos-=SPEED*(deltaTimeInSeconds); //v*t, t in seconds
- }
- if(keys[SDLK_s])
- {
- ypos+=SPEED*(deltaTimeInSeconds); //v*t, t in seconds
- }
- if(keys[SDLK_d])
- {
- xpos+=SPEED*(deltaTimeInSeconds); //v*t, t in seconds
- }
- if(keys[SDLK_w])
- {
- ypos-=SPEED*(deltaTimeInSeconds); //v*t, t in seconds
- }
- time=SDL_GetTicks();
- pos.x=xpos;
- pos.y=ypos;
- SDL_FillRect(screen,0,0);
- SDL_FillRect(screen,&pos,SDL_MapRGB(screen->format,255,0,0));
- SDL_Flip(screen);
- //SDL_Delay(10); //uncomend these, to see, that the square mave with the same speed
- //SDL_Delay(100);
- }
- SDL_Quit();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement