Public Group

#### Archived

This topic is now archived and is closed to further replies.

# where is my logic flawed ??? (making font counter)

This topic is 5276 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

high, im working on a pong clone and now i want to add a score keeper to the top of the screen. basically what i did was i made a bitmap image that looked like this 0123456789 its a .bmp with those numbers next to each other. now, when i go to blit this image, i blit the part starting at the x value of score.digit_#*32 where 32 is the width of a single number. and the y value of 0. and then i blit 32,64 pixels of the image being the width and height of a single number. so, if the score is 0, i start blitting at 32*0 which is the top left corner, where the number 0 is. if the score is 1, it will blit at 32*1, where the number 1 is, etc, etc. NOW here is my logic for the actual score keeping

score.digit_1 = 0; //done in the constructor

score.digit_2 = 0; //same for this

if (enemy_scores)
{

score.digit_2++;

if (score.digit_2 > 9)
{
score.digit_2 = 0;
score.digit_1++;
}

}

basically, it starts at 0 0 .(first digit second digit). if the the enemy scores, it goes to 0 1 , then 0 2, then 0 3, untill it gets to 10 then the second digit will go back to 0 and the first digit will increment, like 0 9, then 1 0. get it? so why the hell is only a 0 displaying on my screen??? the 0 wont inrement. i cant figure out if my logic is flawed in the actual displaying of the image, or the actualy score keeping part. this is killing me. im not that great at debugging (actually have not much a clue how to do it) so im hoping one of you guys can spot my logic flaw. thanks for any help!! im using c++ and SDL [edited by - graveyard filla on February 21, 2004 5:20:57 PM]

##### Share on other sites
bah, i figured it out. well, sort of. i mean i figured out WHERE its not working, but cant figure out WHY. argh, dont you hate that? anyway, i fixed it, but just dont understand how i fixed it, but now i gotta figure out whats broken somewhere else now.

oh, to add something usefull to this thread, apparantly each one of my digits arent completely EVEN in width,(some of the digits get cut off) i thought paint shop pro would space them out evenly for me, but i guess it doesnt. anyone know how to space out numbers evenly so that i could do this properly?? thanks for any help!!!

nevermind - google is my friend. i just couldnt find the damned one on NeHe that ive seen before

[edited by - graveyard filla on February 21, 2004 5:42:13 PM]

[edited by - graveyard filla on February 21, 2004 5:42:37 PM]

##### Share on other sites
i realize im probably talking to myself at this point, but i still cant get the numbers evenly spaced out. how do i do this? i found a bitmap font thing but its a huge block of characters - i only want 0-9 next to each other, evenly spaced. how do i do this?

##### Share on other sites
If you're using SDL, don't bother trying to write your own font library.

This one is good or try
this one.

This you can just use it like this:
char sbuf[32];
int pong_score = 0;
..
sprintf(sbuf,"SCORE: %i",pong_score);
DrawString(screen,0,30,sbuf);

[edited by - Jack Sotac on February 21, 2004 7:25:14 PM]

##### Share on other sites

thi sfont thing seems to be in C and im writing in c++, now i know technically it wont matter, but is it bad to mix the 2? thanks for any help...

also i dont know how to use that print command, ive also noticed that all tutorials, and even the book i bought (focus on SDL) is alll in C. i thought SDL was made for use in C++ OR C, how come i havent seen any c++ tutorials/books ?

[edited by - graveyard filla on February 21, 2004 7:38:58 PM]

##### Share on other sites
I can see what you are trying to do with the counters, but I think the biggest thing wrong is the approach. Instead of having a variable for each digit, why not just have a generic int iScore?

Then, to extract what each digit is, since we are working in base ten here, simply divide by ten and/or do modulus ten

Suppose iScore is 37

37 / 10 = 3
37 % 10 = 7

then simply take those results and show the proper values, via your favorite control structure, hell you can even do it recursively if you want to.

int PrintScores(int iScore1, int iScore2)
{
// sort out your digits below
while (iScore1 isn''t 0 when divided by ten)
{
print_number(iCurrentDigit,x*index,y);
}

// repeat for iScore2

return 0;
}

okay, I didn''t come up with actual code to fill in there, that''s your job. suffice to know it''s possible and kinda fun to find the easiest way, at least it is before 11:00 pm!

have fun!

"Let me just ejaculate some ideas"

##### Share on other sites
good idea, but what would happen if the user scored 10? you could hard code it but then if the player scored 20 it would be treated the same..... unless im missing something.....

##### Share on other sites
You can use sprintf to turn you integer value into a formatted string.
Once you get this string, you can tell the font library to print
the string.

...include the stdlib.h

int score = 387423;
char sbuf[7]; //Must be big enough to hold 6+1 digits.

sprintf(sbuf,"%i",score);
//Now sbuf[0]=''3'' ,sbuf[1]=''8'',...,sbuf[5]=''3'',sbuf[6]=''\0''
//Just pass the ''sbuf'' array to the font library.

SDL was designed to work with C/C++ or any language that can use C/C++ functions. It might be best to read more about it on their website.

##### Share on other sites
quote:
Original post by graveyard filla
good idea, but what would happen if the user scored 10? you could hard code it but then if the player scored 20 it would be treated the same..... unless im missing something.....

Yes, I think you are missing something.
10 / 10 = 1
10 % 10 = 0
ergo, no problem.

##### Share on other sites
well, i still would rather use my way, its only like 2 extra lines of code and i thought of it myself

1. 1
2. 2
frob
16
3. 3
4. 4
5. 5

• 13
• 13
• 61
• 14
• 15
• ### Forum Statistics

• Total Topics
632125
• Total Posts
3004252

×