Public Group

# SDL FPS and Incrementing Issues

This topic is 2855 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

[size=2]Ok, so what I want to accomplish here is everytime I click on the screen a square (SDL_Rect) appears and moves downwards, once it reaches the end of the screen it's supposed to go back up to the top and back down again, and so on. But I have two problems. First, the rate at which the square is moving downwards varies, like first it'll go down slowly suddenly speed up and slow down again. Second, when the square reaches the end of the window I increment it's y value by -10 but all it does is go straight back up, doesn't even move backwards. Thanks in advance.

#include <SDL.h> #include <SDL_mixer.h> #include <SDL_image.h> #include <vector> using namespace std; int main(int argc,char*args[]){ SDL_Event event; bool running = true; SDL_Init(SDL_INIT_EVERYTHING); // the rectangle vector<SDL_Rect> daSquares; SDL_Rect daSquare; // keys inputted Uint8*keys = SDL_GetKeyState(NULL); // regulating fps int start; int fps = 20; // the screen SDL_Surface* screen; screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE); while(running == true){ start = SDL_GetTicks(); while(SDL_PollEvent(&event)){ if(event.type == SDL_MOUSEBUTTONDOWN){ if(event.button.button == SDL_BUTTON_LEFT){ int x = event.button.x; int y = event.button.y; daSquare.x = x; daSquare.y = y; daSquare.w = 20; daSquare.h = 20; daSquares.push_back(daSquare); } } if(event.type == SDL_QUIT){ running = false; } } // uodating the screen and regulating frameraate SDL_Flip(screen); if(1500/fps > start - SDL_GetTicks()){ SDL_Delay(1500/fps - (start - SDL_GetTicks())); } // filling the rectangles SDL_FillRect(screen,&screen->clip_rect,SDL_MapRGB(screen->format,0,0,0)); // updating their locations for(int i = 0; i <daSquares.size(); i++){ SDL_FillRect(screen,&daSquares,SDL_MapRGB(screen->format,0,200,0)); daSquares.y += 10; if(daSquares.y >= 460) { while(daSquares.y >= 10) {daSquares.y -= 10;} } } } SDL_Quit(); return 0;}

##### Share on other sites
Bro, there's no need to cross post.

##### Share on other sites
I wasn't sure where this belonged.

##### Share on other sites

I wasn't sure where this belonged.

It only belongs in one forum, that much you should know. If it's not in the right one, a moderator can move it for you.

And about your problem: I'm gonna guess VSync is limiting you to a certain FPS, which may be 60 at one point, and 30 at another, and maybe 15 at another, and back to 60, etc. Usually in a game, you want to render as fast as you can, and then update at regular intervals. I would suggest changing your loop so it doesn't use SDL_Delay, and instead renders every time the loop iterates, and then every some-odd number of milliseconds (~16ms for 60 updates/second), update your program (i.e. advance the the square's position). This is a really trivial solution, and more advanced solutions would interpolate/extrapolate movements in the rendering function, and then update at regular intervals.

##### Share on other sites
And how do I fix that?

##### Share on other sites

And how do I fix that?

I edited my post to include a little more info since I first wrote it. Here's a pretty popular article. And here's another article which I just discovered (I'm not sure how great it is, as I haven't read it, but skimming it, it looks interesting).

##### Share on other sites
Thanks alot, bro.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 11
• 9
• 24
• 45