#Which one is faster ?

10 messages · Page 1 of 1 (latest)

zinc knot
#

Im making my first game loop in SDL, and I want to know which is a better design choice for the update() function.

first option would be to declare last_frame_time as a global variable

uint32_t last_frame_time=0;

void update(SDL_Rect* input_rect)
{
    while (!SDL_TICKS_PASSED(SDL_GetTicks() , last_frame_time + TARGET_FRAME_TIME));
    
    last_frame_time = SDL_GetTicks();

    input_rect->x++;
    if (input_rect->y < 500)
        input_rect->y++;
}

or I could pass the last_frame_time as a parameter to update

void update(SDL_Rect* input_rect , int* last_frame_time)
{
    while (!SDL_TICKS_PASSED(SDL_GetTicks() , last_frame_time + TARGET_FRAME_TIME));
    
    *last_frame_time = SDL_GetTicks();

    input_rect->x++;
    if (input_rect->y < 500)
        input_rect->y++;
}

which way would be better in terms of speed and readability and design everything else ?

steady shadowBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

twilit marlin
#

id advice against a global variable

#

you could use static int last_frame_time

#

also there
while (!SDL_TICKS_PASSED(SDL_GetTicks() , last_frame_time + TARGET_FRAME_TIME));
you prolly meant
while (!SDL_TICKS_PASSED(SDL_GetTicks() , *last_frame_time + TARGET_FRAME_TIME));

zinc knot
twilit marlin
#

it acts like a global var just the scope is limited to the function

#

;compile

#include <assert.h>
int fn() {
    static int i;
    return i++;
}
int main() {
    assert(fn() == 0); // default initialized to 0 like a global variable would
    assert(fn() == 1); // i was 1
    // i is now 2
}
mortal ridgeBOT
#
Compilation successful

No output.