#172688 - radiodee1 - Thu Feb 25, 2010 2:03 pm
I downloaded and installed akaio on my acekard. The game I'm working on seems to work ok BUT a simple group of score saving functions doesn't work right. Is there some problem with fat.h with the akaio or is this sloppy coding on my part that must be re-worked? I'm using fgetc, fputc, fread and fwrite. Has anyone else heard of anything like this? I don't have another card to test this on, just my one acekard. Thanks.
#172690 - Lazy1 - Thu Feb 25, 2010 3:03 pm
You're going to have to post your score saving code :)
#172692 - radiodee1 - Thu Feb 25, 2010 4:38 pm
Code: |
// awesomeguy is the name of the game, and
// awesomeguy.dat is the name of the data
// file. I tried to take out the 'fread' and
// 'fwrite' commands and replaced them with
// fgetc and fputc loops, but I left the
// original code in this listing commented out.
void readData(Score data[]) {
int i , j, k;
char test[12] = "none ";
j = 0;
FILE* users;
for(i = 0; i < 5; i ++) {
j = j + data[i].score;
}
if (j > 0) return;
mkdir("/DATA", S_IRWXU | S_IRWXO | S_IRWXG);
mkdir("/DATA/awesomeguy", S_IRWXU | S_IRWXO | S_IRWXG);
users = fopen("/DATA/awesomeguy/awesomeguy.dat", "rb");
for (i = 0; i < 5; i ++) {
/*
fread(test, 1, 12, users);
for (k = 0; k < 12; k ++)
data[i].name[k] = test[k];
*/
for (k = 0; k < 12; k ++) {
data[i].name[k] = fgetc(users);
}
data[i].score = fgetc( users);
data[i].score += fgetc( users) << 8;
data[i].score += fgetc( users) << 16;
data[i].score += fgetc( users) << 24;
data[i].level = fgetc( users);
data[i].save1 = fgetc( users);
data[i].save2 = fgetc( users);
data[i].save3 = fgetc( users);
}
fclose(users);
}
void writeData(Score data[]) {
int i , k;
FILE* users;
//char test[12] = "0123456789 "; // don't forget null ending.
mkdir("/DATA", S_IRWXU | S_IRWXO | S_IRWXG);
mkdir("/DATA/awesomeguy", S_IRWXU | S_IRWXO | S_IRWXG);
users = fopen("/DATA/awesomeguy/awesomeguy.dat", "wb");
fclose(users);
users = fopen("/DATA/awesomeguy/awesomeguy.dat", "ab");
for (i = 0; i < 5; i ++ ) {
//fwrite(data[i].name, 1, 12, users);
for (k = 0; k < 12; k ++) {
fputc( data[i].name[k] ,users);
}
fputc(data[i].score, users);
fputc(data[i].score >> 8, users);
fputc(data[i].score >> 16, users);
fputc(data[i].score >> 24, users);
fputc(data[i].level, users);
fputc(data[i].save1, users);
fputc(data[i].save2, users);
fputc(data[i].save3, users);
}
fwrite(" END ", 1, 6, users);
fclose(users);
} |
#172693 - radiodee1 - Thu Feb 25, 2010 5:51 pm
Code: |
typedef struct {
// the name of the player.
char name[12];
// the score attained.
u32 score;
// the level the player has played to.
int level;
// three aspects of play that can be saved.
// save1 is lives
// save2 is cycles
// save3 is undefined
int save1,save2,save3;
} Score;
|
Oh, I almost forgot to include that the two functions are passed an array of 5 of these 'Score' structs. That's what data[] is.
#172694 - elhobbs - Thu Feb 25, 2010 6:25 pm
are you sure your file is being opened? there is no error checking. perhaps fopen is failing?
#172697 - radiodee1 - Thu Feb 25, 2010 7:15 pm
I put lines like this:
Code: |
assert(users != NULL); |
after each 'fopen'. The game halts when running under emulation, but runs as before on my DS... with the same messed up contents of the 'data' structs.
#172699 - Azenris - Thu Feb 25, 2010 7:53 pm
not really sure of your problem but i was just wondering why you don't write the whole structure instead of its individual pieces
Code: |
// =================================================================================
// SaveGame:
// <<: returns whether or not it successfully saved
bool CSaveData::SaveGame(void)
{
if (FileSystemOK())
{
// == gather the needed information ==
GatherInformation();
// == save the data ==
FILE *pFile = fopen(TXT_SAVEDATA_FILE, "wb");
if (pFile != NULL)
{
fseek(pFile, 0, SEEK_SET);
fwrite((void*)&m_saveData, sizeof(SAVE_DATA), 1, pFile);
fclose(pFile);
return true;
}
else
{
ASSERT(0, "Cannot create savefile")
return false;
}
}
return false;
}
// =================================================================================
// LoadGame:
// <<: returns whether or not it successfully loaded
bool CSaveData::LoadGame(void)
{
if (FileSystemOK())
{
// == load the data ==
FILE *pFile = fopen(TXT_SAVEDATA_FILE, "rb");
if (pFile != NULL)
{
fseek(pFile, 0, SEEK_SET);
fread((void*)&m_saveData, sizeof(SAVE_DATA), 1, pFile);
fclose(pFile);
// == place the data where needed ==
DistributeInformation();
return true;
}
else
{
return false;
}
}
return false;
} |
Basically I save the whole saveData struct at once, instead of going through its variables.
Also just wondering did you mean
Code: |
users = fopen("/DATA/awesomeguy/awesomeguy.dat", "ab"); |
ab is append file, as in write at the end
http://www.opengroup.org/onlinepubs/007908799/xsh/fopen.html
_________________
My Homebrew Games
#172700 - radiodee1 - Thu Feb 25, 2010 8:22 pm
Azenris wrote: |
not really sure of your problem but i was just wondering why you don't write the whole structure instead of its individual pieces |
I didn't know that you could. btw, can you explain this:
??
Also, about the wb/ab thing... if you don't append the info to the end of the file then the beginning of the file is constantly rewritten. Your way is probably better, but I'm more of a noob (obviously) and my programming skills are lesser. Can you save arrays as well as structs using fwrite? Do you have to serialize them first?
#172701 - Azenris - Thu Feb 25, 2010 9:08 pm
radiodee1 wrote: |
I didn't know that you could. btw, can you explain this:
??
|
That's just a cast, fread/write take a void pointer as their first parameter
http://www.opengroup.org/onlinepubs/007908799/xsh/stdio.h.html
radiodee1 wrote: |
Do you have to serialize them first?
|
I didn't.
radiodee1 wrote: |
Also, about the wb/ab thing... if you don't append the info to the end of the file then the beginning of the file is constantly rewritten. |
Isn't that what you want, to save the data to the file. And read then what you have. If you kept adding to the end the file would grow and grow and the read function is only ever looking at the first 5 entries anyway.
radiodee1 wrote: |
Can you save arrays as well as structs using fwrite?
|
Think so you would have to change the nitems parameter.
http://www.opengroup.org/onlinepubs/007908799/xsh/fwrite.html
_________________
My Homebrew Games
#172702 - radiodee1 - Thu Feb 25, 2010 9:42 pm
this is discouraging as all this worked fine with the old Acekard software... v. 4.18 I think. I still have it, so I may end up going back to it till I can figure this out. btw, I changed the line that said ab to wb with no visible improvement.
#172709 - Lazy1 - Fri Feb 26, 2010 1:52 am
Azenris wrote: |
not really sure of your problem but i was just wondering why you don't write the whole structure instead of its individual pieces
|
I thought there was a good reason for this, it has something to do with alignment and portability between platforms and compilers.
#172722 - radiodee1 - Fri Feb 26, 2010 5:34 pm
I tried AKAIO.1.4.1.Fixed.rar today and found that my code works. You'd think I'd be happy, but I'm still thinking about 1.5.1 (the version that doesn't work). Thanks to posters in this thread, cause my code now looks a lot nicer. The thing that doesn't work for me seems to be fread or fopen with "rb" mode.
#172723 - Lazy1 - Fri Feb 26, 2010 6:21 pm
See what errno is set to after fopen fails.
#172724 - radiodee1 - Fri Feb 26, 2010 6:35 pm
Lazy1 wrote: |
See what errno is set to after fopen fails. |
How do you do this?
#172726 - Normmatt - Fri Feb 26, 2010 8:43 pm
copy the official firmware's ak2 dldi and overwrite the akaio one (it has some problems with certain games).