SDL_GetTicks() problem

Started by
7 comments, last by Android_s 18 years, 8 months ago
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;                 
        }
    }
}  


Advertisement
i dont see what the problem is...
you mean that it doesnt take 1ms to set an int?
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.
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.
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.
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;    }

Learn to make games with my SDL 2 Tutorials

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.
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();
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.
----------------------------------------------------------------------------------------------------------------------"Ask not what humanity can do for you, ask what you can do for humanity." - By: Richard D. Colbert Jr.

This topic is closed to new replies.

Advertisement