Jump to content
  • Advertisement
Sign in to follow this  

SDL FPS and Incrementing Issues

This topic is 2854 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

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;

// 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();
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;

if(event.type == SDL_QUIT){
running = false;
// uodating the screen and regulating frameraate

if(1500/fps > start - SDL_GetTicks()){
SDL_Delay(1500/fps - (start - SDL_GetTicks()));

// filling the rectangles

// updating their locations
for(int i = 0; i <daSquares.size(); i++){

daSquares.y += 10;
if(daSquares.y >= 460)
while(daSquares.y >= 10)
{daSquares.y -= 10;}
return 0;}

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!