Search code examples
ctimersdl

SDL Timer in C to a char string


In Lazy Foo's tutorial about timers he used std::stringstream timeText;. However, since I'm coding in C, I was wondering if there was anything similar I can use? Ultimately, if the result gets stored as a string, that would be perfect because I can render it through TTF. Right now, getticks() returns a uint32; how can I convert that to a string?


Solution

  • The example is just using a string stream in order to then use the << operators to build s string.

    In C, you could use, sprintf:

    char timeText[100];
    
    sprintf(timeText,"Milliseconds since start time %lu", (SDL_GetTicks() - startTime);
    .
    .
    if( !gTimeTextTexture.loadFromRenderedText( timeText, textColor ) )
    .
    .