Archived

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

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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!!!

maybe a link to where i can get pre-made bitmap fonts??

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 this post


Link to post
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 this post


Link to post
Share on other sites
thanks for the link!!

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 this post


Link to post
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 this post


Link to post
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.

Read up on the syntax of printf() function to learn more.

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 this post


Link to post
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 this post


Link to post
Share on other sites
Fair enough, but LuckyNewbie''s method is much safer and more robust, as it does not rely on any previous state. Not to mention the fact that it can be used in situations where the score increments by more than one point at a time.

Share this post


Link to post
Share on other sites
I know the Pong clone is done, but I just wanted to say to LuckyNewbie that the score display algorithm is a pretty smart idea also. I never thought of getting digits recursively, either (based on his aside). Good job!

[edited by - Sluginator on March 4, 2004 6:38:31 PM]

Share this post


Link to post
Share on other sites
I would recommend using a regular integer, and then learning how to use SDL_TTF. SDL_TTF accepts a character array as the display text, so just use the itoa function (found in stdlib.h) to convert the integer to a proper displaying char array. It's rather simple to do, and it is much easier to manage an actual integer for the score.

By the way: Focus on SDL is written in generic C/C++, until the later chapters, where you are given some nice example classes / class concepts.

[edited by - xempest on March 5, 2004 9:31:11 AM]

Share this post


Link to post
Share on other sites