Homebrew Save hiscore

  • Thread starter Thread starter Cid2mizard
  • Start date Start date
  • Views Views 1,370
  • Replies Replies 3

Cid2mizard

Well-Known Member
Member
Joined
Aug 16, 2007
Messages
401
Reaction score
264
Trophies
2
Age
45
Location
Maubeuge
XP
3,021
Country
France
Hello,

I wish I save the scores in a file on the SD card , as simply as possible ... the save file is created but it does 1ko and contains nothing ...

Code:
//A sauvergarder
bool niveau_unlocked[MAX_LVL];
bool niveau_solved[MAX_LVL];
u8 niveau_best[MAX_LVL];
//End save
 
void sauvegarder()
{
    fsInit();
 
    u32 bytesWrite;
    Handle fileHandle;
 
    char filename[15] = "/TILES_2DS.SAV";
 
    //setup SDMC archive
    FS_archive sdmcArchive = (FS_archive){ARCH_SDMC, (FS_path){PATH_EMPTY, 1, (u8*)""}};
 
    //create file path struct
    FS_path filePath = FS_makePath(PATH_CHAR, filename);
 
    //create file
    FSUSER_OpenFileDirectly(NULL, &fileHandle, sdmcArchive, filePath, FS_OPEN_READ|FS_OPEN_WRITE|FS_OPEN_CREATE, FS_ATTRIBUTE_NONE);
 
    //Write file
    for (i = 0; i < MAX_LVL; i++)
    {
        FSFILE_Write(fileHandle, &bytesWrite, 0x0, &niveau_unlocked[i], sizeof(niveau_unlocked[i]), FS_WRITE_FLUSH);
        FSFILE_Write(fileHandle, &bytesWrite, 0x0, &niveau_solved[i], sizeof(niveau_solved[i]), FS_WRITE_FLUSH);
        FSFILE_Write(fileHandle, &bytesWrite, 0x0, &niveau_best[i], sizeof(niveau_best[i]), FS_WRITE_FLUSH);
    }
 
    //Close file
    FSFILE_Close(fileHandle);
    svcCloseHandle(fileHandle);
}
 
Hello,

I wish I save the scores in a file on the SD card , as simply as possible ... the save file is created but it does 1ko and contains nothing ...

Code:
//A sauvergarder
bool niveau_unlocked[MAX_LVL];
bool niveau_solved[MAX_LVL];
u8 niveau_best[MAX_LVL];
//End save
 
void sauvegarder()
{
    fsInit();
 
    u32 bytesWrite;
    Handle fileHandle;
 
    char filename[15] = "/TILES_2DS.SAV";
 
    //setup SDMC archive
    FS_archive sdmcArchive = (FS_archive){ARCH_SDMC, (FS_path){PATH_EMPTY, 1, (u8*)""}};
 
    //create file path struct
    FS_path filePath = FS_makePath(PATH_CHAR, filename);
 
    //create file
    FSUSER_OpenFileDirectly(NULL, &fileHandle, sdmcArchive, filePath, FS_OPEN_READ|FS_OPEN_WRITE|FS_OPEN_CREATE, FS_ATTRIBUTE_NONE);
 
    //Write file
    for (i = 0; i < MAX_LVL; i++)
    {
        FSFILE_Write(fileHandle, &bytesWrite, 0x0, &niveau_unlocked[i], sizeof(niveau_unlocked[i]), FS_WRITE_FLUSH);
        FSFILE_Write(fileHandle, &bytesWrite, 0x0, &niveau_solved[i], sizeof(niveau_solved[i]), FS_WRITE_FLUSH);
        FSFILE_Write(fileHandle, &bytesWrite, 0x0, &niveau_best[i], sizeof(niveau_best[i]), FS_WRITE_FLUSH);
    }
 
    //Close file
    FSFILE_Close(fileHandle);
    svcCloseHandle(fileHandle);
}
You can look at the source of my Guess the Number. The only well-written part are the "files.h" and "files.c" files which contain exactly the methods you need. I think that you can write simply serveral saves by using one archive that contains serveral text files containing the scores.
 
  • Like
Reactions: Cid2mizard
You can also simply use standard functions like fopen, fwrite, and fclose (they're just simple wrappers in ctrulib as far as I know, but I don't see much of a need to use all of that messy 3DS init code when it's handled in the background for you), here's an example:

https://gist.github.com/thedax/e5f0f9f588fb8b5c3b49
 
Ok thank you both , it works now ...

Code:
void sauvegarder()
{
    char filename[15] = "/TILES_2DS.sav";
 
    FILE *file = fopen(filename,"w+b");
 
    //Write file
    for (i = 0; i < MAX_LVL; i++)
    {
        fwrite(&niveau_unlocked[i], sizeof(niveau_unlocked[i]), 1, file);
        fwrite(&niveau_solved[i], sizeof(niveau_solved[i]), 1, file);
        fwrite(&niveau_best[i], sizeof(niveau_best[i]), 1, file);
    }
 
    //Close file
    fclose(file);
}
 
 
void charger()
{
    char filename[15] = "/TILES_2DS.sav";
    u8 erreur = 0;
 
    FILE* file = fopen(filename, "r+b");
 
    if (file != 0)
    {
        for (i = 0; i < MAX_LVL; i++)
        {
            fread(&niveau_unlocked[i], 1, sizeof(niveau_unlocked[i]), file);
            fread(&niveau_solved[i], 1, sizeof(niveau_solved[i]), file);
            fread(&niveau_best[i], 1, sizeof(niveau_best[i]), file);
        }
        fclose(file);
    }
    else
    {
        erreur = 1;
    }
 
    if (erreur == 1)
    {
        sauvegarder();
    }
}
 

Site & Scene News

Popular threads in this forum