SDL2 saving to binary clarification

Started by
0 comments, last by Ubermeowmix 8 years, 3 months ago

Hi all,

I'm messing around with SDL_RWwrite & SDL_RWread and trying to get my head around how to save arrays of structs or classes.

I have managed to successfully do this with ifstream to binary and back again and retain the structure of a class, is this possible with SDL2's functions or am I barking up the wrong tree.

If anyone can visually represent how the data is saved and why (or knows a link) that would be awesome.

Thanks for reading.

If you get near a point, make it!
Advertisement

RTFM helped with this one, it wasn't clear in the notes that the last variable taken was for the size of array's.

If anyone get's stuck I have included the code I wrote to illustrate a simple saving algorithm in SDL2

struct STRUCT_Data
{
    int INT_health;
    float FLOAT_speed;
};
 

void FUNC_create_and_save_struct_array_to_binary_SDL()
{
    ///SET THE PATH
    const char* CONST_CHAR_file_ref= "saves/SAVE_save_struct_array_to_binary_SDL2.bin";
 
    ///DECLARE THE VARS
    size_t INT_array_maximum= 3;
    size_t INT_array_maximum_loaded= 0; ///ONLY TO SHOW IT'S NOT USING THE PREVIOUS MAX
 
    ///FILL THE STRUCT WITH GENERIC CHUFF
    STRUCT_Data STRUCT_current_save[INT_array_maximum];
    STRUCT_current_save[0].INT_health= 10;
    STRUCT_current_save[0].FLOAT_speed= 33.333;
    STRUCT_current_save[1].INT_health= 255;
    STRUCT_current_save[1].FLOAT_speed= 3.0;
    STRUCT_current_save[2].INT_health= 6;
    STRUCT_current_save[2].FLOAT_speed= 1.55;
 
    ///SAVE THE DATA TO A BINARY FILE
    SDL_RWops *FILE_data_write = SDL_RWFromFile( CONST_CHAR_file_ref, "wb");
    if( FILE_data_write ==NULL )
    {
        ///ON FAIL RETURN NOTHING TO EXIT THE FUNCTION
        return;
    } else {
        SDL_RWwrite( FILE_data_write, &INT_array_maximum, sizeof(size_t), 1 );
        SDL_RWwrite( FILE_data_write, &STRUCT_current_save, sizeof(STRUCT_Data), INT_array_maximum );
        SDL_RWclose( FILE_data_write );
    }
 
    ///ZERO OUT THE VALUES SO YOU KNOW IT LOADS BACK IN OKAY
    STRUCT_current_save[0].INT_health= 0;
    STRUCT_current_save[0].FLOAT_speed= 0.0f;
    STRUCT_current_save[1].INT_health= 0;
    STRUCT_current_save[1].FLOAT_speed= 0.0f;
    STRUCT_current_save[2].INT_health= 0;
    STRUCT_current_save[2].FLOAT_speed= 0.0f;
 
    ///LOAD THE DATA BACK IN TO THE PROGRAM
    SDL_RWops *FILE_data_read = SDL_RWFromFile( CONST_CHAR_file_ref, "rb");
    if( FILE_data_read ==NULL )
    {
        ///ON FAIL RETURN NOTHING TO EXIT THE FUNCTION
        return;
    } else {
        SDL_RWread( FILE_data_read, &INT_array_maximum_loaded, sizeof(size_t), 1 );
        SDL_RWread( FILE_data_read, &STRUCT_current_save, sizeof(STRUCT_Data), INT_array_maximum_loaded );
        SDL_RWclose( FILE_data_read );
    }
 
    ///CHECK IT DISPLAYS OKAY
    std::cout << "STRUCT_current_save[0].INT_health: " << STRUCT_current_save[0].INT_health << std::endl;
    std::cout << "STRUCT_current_save[0].FLOAT_speed: " << STRUCT_current_save[0].FLOAT_speed << std::endl;
    std::cout << "STRUCT_current_save[1].INT_health: " << STRUCT_current_save[1].INT_health << std::endl;
    std::cout << "STRUCT_current_save[1].FLOAT_speed: " << STRUCT_current_save[1].FLOAT_speed << std::endl;
    std::cout << "STRUCT_current_save[2].INT_health: " << STRUCT_current_save[2].INT_health << std::endl;
    std::cout << "STRUCT_current_save[2].FLOAT_speed: " << STRUCT_current_save[2].FLOAT_speed << std::endl;
}

If you get near a point, make it!

This topic is closed to new replies.

Advertisement