#11099 - GBACoder - Fri Sep 26, 2003 1:44 am
fopen returns a null FILE* when I try to use in it main(). Has anybody else
used it with success? Any suggestions? Thanks.
#11102 - tepples - Fri Sep 26, 2003 2:04 am
The GBA itself does not have a filesystem unless you implement your own. What are you trying to fopen()? Please post a code sample.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#11108 - GBACoder - Fri Sep 26, 2003 2:38 am
I am trying to open a text file for reading dialogue in an rpg.
FILE* f = fopen(file, "rt");
If not using fopen to do this then how would you go about reading dialogue, like as is done in Sphere (using JScript). Any ideas?
#11109 - tepples - Fri Sep 26, 2003 2:48 am
A GBA doesn't have access to any files other than the running .gba file. To include files containing scripts, maps, graphics, sounds, music sequences, etc. in your program, you can link the text files into your executable image by using some sort of bin2o tool, or you can use my GBFS system to append them in a structure resembling a tar file.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#11110 - GBACoder - Fri Sep 26, 2003 3:31 am
I downloaded your gbfs and I'll try using that.
Thanks for the help.
#12604 - sgeos - Wed Nov 19, 2003 8:31 am
GBACoder wrote: |
I am trying to open a text file for reading dialogue in an rpg.
FILE* f = fopen(file, "rt");
If not using fopen to do this then how would you go about reading dialogue, like as is done in Sphere (using JScript). Any ideas? |
As tepples noted, the GBA does not have a file system. That means that all resources need to be compiled into the ROM. I'll pretend gbfs does not exist and explain the coding solution I'd use. I create a dialogue.c file, and put a function for each dialogue in it. The functions would look something like this:
Code: |
/*** dia0
*
* A very basic function.
*/
void dia0(void)
{
char s[] = "Only you can prevent forest fires.";
display_text(s);
}
/*** dia1
*
* Takes game state into account.
*/
void dia1(void)
{
char s[] = "You have 99999 GP?!";
sprintf(s, "You have %d GP?!", get_player_gp());
display_text(s);
}
/*** dia2
*
* The buffer needs to be large enoough to display any of these messages...
* so the longest message will be in the buffer by default.
* These are the message that can be displayed:
*
* "...\f" "Go kill some monsters!"
* "So far, you have killed 999 monsters."
* "...\f" "I lost count!"
*/
void dia2(void)
{
char s[] = "So far, you have killed 999 monsters.";
int n;
n = get_number_of_killed_monsters();
if (n < 1)
sprintf(s, "...\f" "Go kill some monsters!");
else if (n < 1000)
sprintf(s, "So far, you have killed %d monsters.", n);
else /* n > 999 */
sprintf(s, ""...\f" "I lost count!"");
display_text(s);
}
/*** dia3
*
* Using functions allows much freedom.
* For example, this function randomly displays another message.
*/
void dia3(void)
{
void (*fp[])(void) = {dia0, dia1, dia2}; /* array of function pointers */
int n;
n = get_a_random_number_between_and_including(0, 2);
/* No need to call display_text() here. The function called via the pointer will do that.*/
fp[n]();
} |
Every villager wants to have a function pointer to a message. Now, I have a question. Should I be declaring my buffers like this if I am going to sprintf into them?
Code: |
char s[sizeof("Really...\nthis is...\n my longest message.")]; |
I would divide that by sizeof(char), but if I recall correctly, sizeof(char) is always one. The goal is to avoid initializing the buffer when I don't need to. As far as speed goes, would strcpy be faster than printf when the game state does not need to be taken into account?
-Brendan
#12608 - tepples - Wed Nov 19, 2003 1:56 pm
sgeos wrote: |
As far as speed goes, would strcpy be faster than printf when the game state does not need to be taken into account?
-Brendan |
You should usually be using siprintf(), which does not handle floating-point formats and does not bring in the floating-point library, instead of sprintf(), which does handle floating-point formats and does bring in the floating-point library. You're right about strncpy(), which is faster than siprintf(), which is slightly faster and much smaller than sprintf().
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#12609 - poslundc - Wed Nov 19, 2003 4:25 pm
tepples wrote: |
You should usually be using siprintf(), which does not handle floating-point formats and does not bring in the floating-point library, instead of sprintf(), which does handle floating-point formats and does bring in the floating-point library. You're right about strncpy(), which is faster than siprintf(), which is slightly faster and much smaller than sprintf(). |
This seems like the perfect opportunity to plug my new sprintf() variant, posprintf(). It's similar to siprintf() but with a little less functionality and targetted more at GBA development. It's also coded entirely in Thumb assembler, and should be faster than most general library functions overall.
http://www.danposluns.com/gbadev/posprintf/
Dan.