RSS LJ

October 6, 2004

This is what I have to work with ()

by fluffy at 11:24 PM
Seriously.

char buffer[10];
		
int mins = m_timeLeft / 60;
int secs = m_timeLeft % 60;
		
int doubleMin;
int singleMin;
if(mins >= 10)
{
	doubleMin = mins / 10;
	singleMin = mins % 10;
}
else
{
	doubleMin = 0;
	singleMin = mins;
}

int doubleSec;
int singleSec;
if(secs >= 10)
{
	doubleSec = secs / 10;
	singleSec = secs % 10;
}
else
{
	doubleSec = 0;
	singleSec = secs;
}
		
short offset = 13;
short start = 255;

sprintf(buffer, "%d", singleSec);
ge->DrawText(buffer, 0, 0, 255, 0, GraphicsEngine::J_RIGHT);
start -= 2*offset;

sprintf(buffer, "%d", doubleSec);
ge->DrawText(buffer, 0, 0, start, 0, GraphicsEngine::J_RIGHT);
start -= offset;

sprintf(buffer, ":");
ge->DrawText(buffer, 0, 0, start, 0, GraphicsEngine::J_RIGHT);
start -= offset - 7;

sprintf(buffer, "%d", singleMin);
ge->DrawText(buffer, 0, 0, start, 0, GraphicsEngine::J_RIGHT);
start -= offset;

if(doubleMin)
{
	sprintf(buffer, "%d", doubleMin);
	ge->DrawText(buffer, 0, 0, start, 0, GraphicsEngine::J_RIGHT);
}
Replaced with:
char buffer[10];
		
int mins = m_timeLeft / 60;
int secs = m_timeLeft % 60;
		
sprintf(buffer, "%d:%02d", mins, secs);
ge->DrawText(buffer, 0, 0, 255, 0, GraphicsEngine::J_RIGHT);
You probably don't want to see how he was formatting a percentage value.

Comments

#3654 10/06/2004 08:27 pm Jebus!
Of course, I have no earthly clue what any of that means, but it does look a lot shorter!
#3655 10/06/2004 08:31 pm
Code for formatting and displaying a time.
#3656 10/06/2004 08:37 pm Is that C?
It looks like C. I tried to learn that once. I may do so again at some point in the future.
#3657 10/06/2004 08:41 pm
C++, technically.
#3659 Lionfire (unregistered) 10/06/2004 10:13 pm
The only reason you'd want to do it the way he's doing it is if the font wasn't monowidth and you wanted to make the digits always line up neatly.

If it's always going to be a monowidth font, I'll shoot him for you Smile
#3660 10/06/2004 10:21 pm
The font is not monospace, but his way of doing it was still fucking retarded. I had suggested a better way to him when he asked me how to deal with the occasional 1-pixel difference but he ignored me and just did it stupidly. Even if he were to do it a character at a time he could have just done something like


char buf[2] = "\0";
buf[0] = time/600 + '0';
ge->DrawText(buf,...);
buf[1] = (time/60)%10 + '0';
ge->DrawText(buf,...);
// etc.


instead of calling fucking sprintf() for single digits. Also, DrawText() does allow you to draw a single character at a time from a longer string.

Tomorrow I'll change it to the less-retarded method which I'd originally suggested that he use to begin with, or maybe center the text within a box, or something. I just wanted to get rid of that ugly-ass eyesore, and maybe he'll stumble across the code and realize that, hey, the printf() family already knows how to deal with leading 0s and stuff...
#3661 10/07/2004 02:43 am
Wow, that's worse than I'd even imagined.

And (OT) <div class="code"> matches with </div>, not </code>. (Now fixed. The gods of HTML thank you. My browser thanks you. And I thank you.)
#3662 10/07/2004 05:01 am
Wow, that brings back memories. And nightmares. =)
#3665 10/07/2004 07:50 am gack
That is pretty retarded. Somebody needs to spend some time studying sprintf formatting codes.
#3671 10/07/2004 07:59 pm
What's with all these codes and stuff? Question Question Question
#3672 10/07/2004 08:32 pm
I'm a programmer, as are many of the friends of mine who read my weblog, and so there tends to be a heavy programming focus around here. If you can't handle the occasional bit of source code you should probably find somewhere else to hang out.
#3681 Anonymous 10/10/2004 02:05 am
Holy mother of jebus.

This would certainly be a worthy candidate for The Daily WTF, though by the end of the day they'd probably have a dozen comments defending it.

-ajf