Jump to content
  • Advertisement
Sign in to follow this  
WaleedH95

SDL FPS and Incrementing Issues

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!