Jump to content
  • Advertisement
Sign in to follow this  
kzar

SDL_GetTicks() problem

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

Can you guys see what I'm doing wrong here? I wrote this example program to show you how I'm doing things with less code. The problem is that the time_pased is always 2, 1, (or normaly) 0. I would think it should be somthing more accurate and I don't see why the time passed should ever be 0. Also does the example variable look ok? (Is mixing floats and defines and the time_passed like that ok?) Thanks, and here is the example
#include <SDL/SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define DELTA 0.01
#define SCREENWIDTH 640
#define SCREENHEIGHT 480

bool running;
Uint32 current_time, last_time, time_passed;
SDL_Surface *screen;
void get_input();

void loop();

int main(int argc, char *argv[])
{
    printf("test");
    running = false;
    
    /* Initialize SDL */
    if (SDL_Init(SDL_INIT_VIDEO) != 0)
    {
        printf("Unable to initialize SDL\n");
        return false;
    }
    
    /* Set the window size up */
    screen = SDL_SetVideoMode(SCREENWIDTH, SCREENHEIGHT, 16, SDL_DOUBLEBUF);
    if (screen == NULL)
    {
        printf("Unable to set video mode\n");
        return false;
    }
    
    /* Get the time */
    current_time = SDL_GetTicks();
    
    running = true;
    
    while (running == true)
        loop();
    
    SDL_Quit();
}

void loop()
{
    float x_vol, example;
    x_vol = 15;
    
    /* Update Time stuff */
    last_time = current_time;
    current_time = SDL_GetTicks();
    time_passed = current_time - last_time;
        
    example = x_vol * time_passed * DELTA;
    
    printf("Time_passed: %d, example: %f\n", time_passed, example);

    /* Check for close attempt */
    get_input();
}
    
void get_input()
{
    SDL_Event event;
    SDL_keysym keysym;
    
    while (SDL_PollEvent(&event))
    {
        switch (event.type)
        {
            case SDL_QUIT:
                running = false;
                break;
           
            case SDL_KEYDOWN:
                keysym = event.key.keysym;
				switch(keysym.sym) 
                {
                    case SDLK_ESCAPE:
                        running = false;
                        break;      
                }       
                break;                 
        }
    }
}  


Share this post


Link to post
Share on other sites
Advertisement
You've declared time_passed to be an integer type, but you are multiplying the difference by 0.01. (Why are you doing that anyway?) Since the diference between sucessive ticks is usually less than 100 this makes the value less than one, which when cast to an integer will be zero.

Incidently, the logic you are using for your time_passed won't work right for some cases because the resolution on SDL_GetTicks() really isn't that good. You'll probably get a lot of sequential calls being the same tick count. You may want to keep a running average over a number of frames instead.

Share this post


Link to post
Share on other sites
The idea is that to make somthing move over time i do x_vol * timepassed and y_vol * timepassed, but to make it controllable I add the DELTA. If this is a backwards way of doing it could you show me a little example of a good way? Thanks

edit: By the way I'm going for framerate independent movement and its (will be) a real time action game. By that I meen I want things to move fluently in real time not by tiles or somthing.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
You've declared time_passed to be an integer type, but you are multiplying the difference by 0.01. (Why are you doing that anyway?) Since the diference between sucessive ticks is usually less than 100 this makes the value less than one, which when cast to an integer will be zero.

Incidently, the logic you are using for your time_passed won't work right for some cases because the resolution on SDL_GetTicks() really isn't that good. You'll probably get a lot of sequential calls being the same tick count. You may want to keep a running average over a number of frames instead.

actually time_passed shouldn't be 0 because time_passed is calculated using current_time - last_time. The DELTA constant(0.01) is used in the calculation of example not time_passed. BTW, printf do a hard disk operation,because SDL redirect stdout and stderr streams to stdout.txt and stderr.txt, respectively, so time_passed, definitively, has to be greater than 0.

Share this post


Link to post
Share on other sites
It could be that the subsystem isn't being initialized

Try using this:
[source lang="cpp"    /* Initialize SDL */
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
printf("Unable to initialize SDL\n");
return false;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Lazy Foo
It could be that the subsystem isn't being initialized

Try using this:
*** Source Snippet Removed ***


No change :( Also I should say that my actual game is doing the same thing and it does more things in each loop.

Share this post


Link to post
Share on other sites
Is this yet another timer resolution post?
I said they should have stickyfied one of the others.

Isn't SDL_GetTicks() just a frontend for GetTickCount() or any of the other windows timer funtions?
If it is, it as limited resolution, on my computer i can only get updates at the most 66 times per second, if you're implementing framerate independent movement, you're bound to have a higher framerate then 66, and therefore some times the counter will not update, you'll get a DELTA of 0, if your framerate is particularly high you'll get lots of 0 DELTAs.

To solve that look into high resolution performance counters in the windows SDK.
QueryPerformanceCounter();
QueryFrequencyCounter();

Share this post


Link to post
Share on other sites
Quote:

Isn't SDL_GetTicks() just a frontend for GetTickCount() or any of the other windows timer funtions?


I hear from most sources (people that checked the SDL code) that SDL_GetTicks() will use a performance counter if there is one. If not, it will fall back on GetTickCount().
I also seem to recall that a while ago, the SDL DOC said there were no guarantees that SDL_GetTicks() had a higher resolution than 10ms, but that does not necesarily have to mean that it can't have higher resolution.


kzar: You say that time_passed never get high, but couldn't it simply be that the program executes so fast that it never go one ms between every loop? Depending of what you do when the program is looping, like flip around through windows and such, it could "halt" your programs execution a bit, thus resulting in that 2ms delay? I'm just thinking out loud here, but if i didn't read your code wrong, your program should run so there's only a few ns between every time you update time_passed...and since you get time in ms, it should be zero.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!