gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > displaying values

#46501 - wiz - Sun Jun 26, 2005 10:37 am

Hello once again,

I have been thinking of methods to display text and numbers on screen using sprites. So far everything is going really well.

In the past, on the PC, to display a number I would convert it to a string and loop through it getting the character value of each character.

But wouldn't this be rather slow on the GBA? I'm trying to think up a way to display a number (lets say 12345) but I cant see how to get each number in order (as it would need to be split up like 1 then 2 then 3 then 4 then 5)

Maybe the conversion to string is the best method afterall?

#46506 - Quirky - Sun Jun 26, 2005 12:48 pm

What I would do would be to try the 'slow' approach and see if it fitted my needs. If it does, yay! if not, then start thinking of optimising. For a first draft you could even use sprintf, or the int only version siprintf.

#46509 - Fatnickc - Sun Jun 26, 2005 1:05 pm

There are some other things you could do. I have only really used these on 2 digit numbers, but once you have some of the numbers, you can get the rest.
For example, if you had 3456, you would do this (note you don't work in order) :

6 :
Code:
while(x>=10)
x=x-10;
fy=x;


(reset x)

5 :
Code:
x=x-fy;
while(x>=100)
x=x-100;
thy=x/10;


(reset x)

4 :
Code:
x=x-((thy*10)+fy);
while(x>=1000)
x=x-1000;
ty=x/100;


(reset x)

3 :
Code:
x=x-((ty*100)+(thy*10)+fy);
oy=x/1000;


And there you have all of the individual digits.
I'm very sure you can see the pattern for getting the xth digit, and you could expand it to meet your every need!

*** oy=first digit
*** ty=second digit
*** thy=third digit
*** fy=fourth digit

#46517 - poslundc - Sun Jun 26, 2005 5:21 pm

Check out posprintf for GBA-optimized quick-and-easy conversion from base-2 to base-10.

You can search the forums for other techiques as well, but one thing I definitely would recommend is sticking to the standard of printing the number to an ASCII string somehow, then just displaying that string using whatever text rendering technique you use. Numbers and text should probably not be treated differently as far as displaying them goes.

Dan.

#46655 - ScottLininger - Tue Jun 28, 2005 6:10 pm

Here's a cheesy, unoptimized version of decimal printing. I use this function in my text engine. It assumes you have a function called BlitCharacter that takes an ASCII value and spits a single character onto your screen...

Code:
void printDec(int decimalNumber) {
   
   int place = 10; // change to 100 for 3-digit output, etc.
   int lastAmount = 0;
   int nextDigit;
   do {
    
     nextDigit = (decimalNumber-lastAmount)/place;
     lastAmount = lastAmount + (nextDigit*place);
     BlitCharacter(nextDigit + 48); // since numbers start at 48 in ascii
     place= place/10;
    
   } while(place > 0);
}



-Scott