char Buffer[255];
sprintf(Buffer, "Time Taken: %d:%d ",CURRENT_SECONDS/60,DISPLAY_SECONDS);
text.drawText(game->screen, Buffer, small_font, 255, 255, 255, 5, 2);
but I am unsure on how to manipulate the string
thanks for your time
hope you can help
ian :)
displaying second in the right format
Hi there
I have being working on my first game.
it is really simple but I am have trouble display the
seconds in the correct way
ie
if DISPLAY_SECOND <= 9
print "0" + DISPLAY_SECOND
else
print DISPLAY_SECOND
int mins = current_seconds/60;
int secs = current_seconds - mins*60;
sprintf(buffer,"Time %d:%c%d",mins, secs < 10 ? '0':'''',secs);
int secs = current_seconds - mins*60;
sprintf(buffer,"Time %d:%c%d",mins, secs < 10 ? '0':'''',secs);
thanks buckeye I didn't think of using an inline if
but I'm getting an empty character constant
don;t think c++ like the :'' part of the if statement
secs < 10 ? '0':''
cheers
ian
but I'm getting an empty character constant
don;t think c++ like the :'' part of the if statement
secs < 10 ? '0':''
cheers
ian
Ah. Use a string specifier.
sprintf(buffer,"Time %d:%s%d",mins, secs < 10 ? "0":"",secs);
EDIT: szecs' is better.
sprintf(buffer,"Time %d:%s%d",mins, secs < 10 ? "0":"",secs);
EDIT: szecs' is better.
I humbly propose this as an even better solution: ;)
The calculation of the seconds looks nicer and a potential buffer overflow was avoided. Still not good with negative values for current_seconds.
char buffer[255];int mins = current_seconds/60;int secs = current_seconds%60;snprintf(buffer, sizeof(buffer), "Time %02d:%02d", mins, secs);
The calculation of the seconds looks nicer and a potential buffer overflow was avoided. Still not good with negative values for current_seconds.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement