#5447 - Psyk - Mon Apr 28, 2003 8:16 am
So far i have a text background that is displaying text. I can make it display a single character at a time but how do i split a string up into individual characters?
#5452 - niltsair - Mon Apr 28, 2003 1:56 pm
It depends on how many character you want to display.
Say you wanted to display 4 lines of text, then you could use 8 64x64sprites. You align them beside each other, and create a font fo 8pixels. You send your string to a function that put each pixels data in tile memory used by those 4 sprites. You would have to copy in the tiles data the pixels value of:
1st : char 0,1,2,3 of line 0
2d : char 0,1,2,3 of line 1
3rd : char 0,1,2,3 of line 2
4th : char 0,1,2,3 of line 3
5th : char 4,5,6,7 of line 0
6th : char 4,5,6,7 of line 1
....
etc...
This method would be using the 4 sprites objetcs and ~256 tiles(the right edge is clipped since it'S over 240pixels). Would allow you to display 240 characters.
#5457 - Psyk - Mon Apr 28, 2003 7:11 pm
Well I already have a way of displaying text but the problem is I have to do it character by character. I want to make a function that takes a string, splits it into the seperate characters and then runs my display character function. It's more of a C++ problem rather than GBA programming specifically.
#5460 - niltsair - Mon Apr 28, 2003 8:12 pm
Then i don't see the problem.
A string is just an array of characters, just acess each one, one by one untill you find the value 0. (or untill the specified string lenght)
#5477 - Sweex - Tue Apr 29, 2003 12:54 pm
In case you don't get it...
Code: |
const char* myText = "Hello woild!";
for (int i=0;i<strlen(myText);i++)
{
// Replace the printf with your function to display a character
printf(
"%c",
myText[i] // THIS is the most important line!
);
}
|
#5480 - Psyk - Tue Apr 29, 2003 3:29 pm
thats great. thanks
#5481 - niltsair - Tue Apr 29, 2003 4:10 pm
A detail about the last example.
The usuall way to represent a string is to use an array of char and the value 0 signify that it's the end of the string. "strlen() " basicly does that, loop untill it fidn the value 0, and return the position of it.
Now, makes sure the strings you are using have this 0 value, else your string will be equal to everything in memory untill it finds a 0.
When the compiler encounter a thing like this :
char text[] = "Some random Text";
it will know to insert a 0, so no needs to worry about it. In fact, the only place to worry about it, is mostly when you import data from some external sources, like say your Pc. If you encounter weird text string, then it'S most probably a forgotten 0.
#5489 - tepples - Tue Apr 29, 2003 6:43 pm
Here's a faster string output loop that implements what niltsair was talking about
Code: |
const char* myText = "hello";
for (const char *cur = myText; *cur != 0; cur++)
{
/* replace putchar() with your character output method
unless you're using devkitadv R5 and have hooked stdout */
putchar(*cur);
} |
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.