Save hiscore

Discussion in '3DS - Homebrew Development and Emulators' started by Cid2mizard, Apr 1, 2015.

  1. Cid2mizard
    OP

    Cid2mizard GBAtemp Fan

    Member
    364
    210
    Aug 16, 2007
    France
    Maubeuge
    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);
    }
     
  2. Technicmaster0

    Technicmaster0 GBAtemp Psycho!

    Member
    3,282
    680
    Oct 22, 2011
    Gambia, The
    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.
     
    Cid2mizard likes this.
  3. daxtsu

    daxtsu GBAtemp Guru

    Member
    5,546
    3,956
    Jun 9, 2007
    Antarctica
    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
     
    filfat and Cid2mizard like this.
  4. Cid2mizard
    OP

    Cid2mizard GBAtemp Fan

    Member
    364
    210
    Aug 16, 2007
    France
    Maubeuge
    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();
        }
    }