Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3622778
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:18:06+00:00 2026-05-18T23:18:06+00:00

this is regarding this tutorial page from lazyfoo’s set of SDL tutorials. There he

  • 0

this is regarding this tutorial page from lazyfoo’s set of SDL tutorials. There he first starts a timer to calculate how much time each frame should stay alive for, before it is refreshed. He does this using the following

if( ( cap == true ) && ( fps.get_ticks() < 1000 / FRAMES_PER_SECOND ) ) { 
 //Sleep the remaining frame time 
 SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() ); 
} 

Although I’ve found that fps.get_ticks() always returns 0 (??) and so isn’t the above not needed(?), cant we just completely leave out the timer and just delay for 1000/FPS.

I’ve tried both ways below and both give me the same thing. What am I missing here, why do we need a timer?.

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <iostream>
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *msg = NULL;
const int FPS = 20;

void initialize(void){
    if (SDL_Init(SDL_INIT_EVERYTHING) == -1 ){
        std::cout<<"could not start sdl"<<std::endl;
    }

    screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
    if (screen == NULL){
        std::cout<<"could not make screen"<<std::endl;
    }

}
void cleanUp(void){
    SDL_Quit();
    SDL_FreeSurface(background);
    SDL_FreeSurface(msg);
}
void loadFiles(void){
    background = IMG_Load("background.bmp");
    msg = IMG_Load("msg.bmp");
    if (background == NULL){
        std::cout<<"could not load background"<<std::endl;
    }
    if (msg == NULL){
        std::cout<<"could not load msg"<<std::endl;
    }
}
void blitSurf(int x,int y,SDL_Surface *source,SDL_Surface *dest){
    SDL_Rect dest_pos;
    dest_pos.x = x;
    dest_pos.y = y;

    if (SDL_BlitSurface(source,NULL,dest,&dest_pos) == -1){
        std::cout<<"could not blit surface"<<std::endl;
    }
}
void update(void){
    if (SDL_Flip(screen) == -1 ){
        std::cout<<"could not update screen"<<std::endl;
    }
}

int main(int argc,char *argv[]){
    initialize();
    loadFiles();

    bool running = true;
    bool cap = false;
    int msg_pos_y = 0;
    int start = 0;
    int temp = 0;
    SDL_Event event;
    while (running == true){
        start = SDL_GetTicks();

        while (SDL_PollEvent(&event)){
            if (event.type == SDL_KEYDOWN){
                if (event.key.keysym.sym == SDLK_c){
                    if(cap == false){
                        cap = true;
                        std::cout<<"cap set to, true"<<std::endl;
                    }else{
                        cap = false;
                        std::cout<<"cap set to, false"<<std::endl;
                    }
                }
            }   
            if (event.type == SDL_QUIT){
                    running = false;
                    std::cout<<"Quit was pressed"<<std::endl;
            }
        }

        blitSurf(0,0,background,screen);
        if (msg_pos_y < 640){
            blitSurf(200,msg_pos_y,msg,screen);
            msg_pos_y++;
        }else{
            msg_pos_y = 0;
            blitSurf(200,msg_pos_y,msg,screen);
        }
        update();


        if ( (cap == true) && ( (SDL_GetTicks()-start) < (1000/FPS) ) ){
            SDL_Delay( (1000/FPS) - (SDL_GetTicks()-start) );
        }

        /* this works as well ??
        if ( cap == true ){
            SDL_Delay(1000/FPS);
        }
        */
    }

    cleanUp();
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T23:18:07+00:00Added an answer on May 18, 2026 at 11:18 pm

    Let’s say you want 50 fps.

    1000miliseconds / 50 = 20miliseconds delay.

    But it takes time to render, compute physics, AI, whatever you are doing. Let’s say that all this stuff I wrote takes 10miliseconds. You have 1000 miliseconds / (20ms delay + 10ms everything else) = 33.3 frames per second. You need to substract this 10ms from the delay.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was just reading Asp.net MVC3 tutorials (Models (Data)) On this page tutorial 4
reading this https://developers.google.com/in-app-payments/docs/tutorial Quoted from the above page: Because you sign the JWT using
In this tutorial I am reading , Dave Ward creates a page that shows
i am reading this page http://www.cplusplus.com/doc/tutorial/exceptions.html it says if i write function() throw(); no
This is regarding Django tutorial - Part 2 http://docs.djangoproject.com/en/dev/intro/tutorial02/ In the section to change
I'm learning by reading this tutorial: Link Here's the code: <?php require_once 'Zend/Loader.php'; class
is it better to do this (regarding performance, not readability...): $('a.updateCartButton').click(function() { $('form[name=updateCartForm]').attr('action', $(this).attr('href')
This is regarding Visual Studio pro 2008 sp1, with resharper and testdriven installed. I've
(this is regarding the Ramaze.net framework) I ran into some really strange problems while
This is regarding AES algorithm. Suppose i have implemented a AES algorithm and encrypt

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.