{{:raylib:jsonsaveandload.png?600|JSON Save and Load}}
===== Raylib Example =====
[[https://github.com/raysan5/raylib/blob/master/examples/core/core_storage_values.c|Raylib's suggest]] is like this.
It is intended to make binary file.
Note : I modified integer to float
// Save integer value to storage file (to defined position)
// NOTE: Storage positions is directly related to file memory layout (4 bytes each integer)
bool SaveStorageValue(unsigned int position, float value, const char* _fileName)
{
bool success = false;
unsigned int dataSize = 0;
unsigned int newDataSize = 0;
unsigned char *fileData = LoadFileData(_fileName, &dataSize);
unsigned char *newFileData = NULL;
if (fileData != NULL)
{
if (dataSize <= (position*sizeof(float)))
{
// Increase data size up to position and store value
newDataSize = (position + 1)*sizeof(float);
newFileData = (unsigned char *)RL_REALLOC(fileData, newDataSize);
if (newFileData != NULL)
{
// RL_REALLOC succeded
float *dataPtr = (float *)newFileData;
dataPtr[position] = value;
}
else
{
// RL_REALLOC failed
TraceLog(LOG_WARNING, "FILEIO: [%s] Failed to realloc data (%u), position in bytes (%u) bigger than actual file size", *_fileName, dataSize, position*sizeof(int));
// We store the old size of the file
newFileData = fileData;
newDataSize = dataSize;
}
}
else
{
// Store the old size of the file
newFileData = fileData;
newDataSize = dataSize;
// Replace value on selected position
float *dataPtr = (float *)newFileData;
dataPtr[position] = value;
}
success = SaveFileData(_fileName, newFileData, newDataSize);
RL_FREE(newFileData);
TraceLog(LOG_INFO, "FILEIO: [%s] Saved storage value: %i", _fileName, value);
}
else
{
TraceLog(LOG_INFO, "FILEIO: [%s] File created successfully", _fileName);
dataSize = (position + 1)*sizeof(float);
fileData = (unsigned char *)RL_MALLOC(dataSize);
float *dataPtr = (float *)fileData;
dataPtr[position] = value;
success = SaveFileData(_fileName, fileData, dataSize);
UnloadFileData(fileData);
TraceLog(LOG_INFO, "FILEIO: [%s] Saved storage value: %i", _fileName, value);
}
return success;
}
// Load float value from storage file (from defined position)
// NOTE: If requested position could not be found, value 0 is returned
float LoadStorageValue(unsigned int position, const char* _fileName)
{
float value = 0;
unsigned int dataSize = 0;
unsigned char *fileData = LoadFileData(_fileName, &dataSize);
if (fileData != NULL)
{
if (dataSize < (position*sizeof(float))) TraceLog(LOG_WARNING, "FILEIO: [%s] Failed to find storage position: %i", _fileName, position);
else
{
float *dataPtr = (float *)fileData;
value = dataPtr[position];
}
UnloadFileData(fileData);
TraceLog(LOG_INFO, "FILEIO: [%s] Loaded storage value: %f", _fileName, value);
}
return value;
}
But by this way, saved file is binary. I don't like this binary file.
===== JSON File Example =====
==== 1. External Library ====
I think [[https://github.com/nlohmann/json|nlohmann Json Library]] is good json parser library for c++.
Just download single json.hpp file from [[https://github.com/nlohmann/json/tree/develop/single_include/nlohmann|singleinclude ]]
==== 2. required header file ====
this is required header file
#include "utils/json.hpp"
#include
#include
and this is my real c++ header file
#pragma once
#ifndef __CONFIG__
#define __CONFIG__
#include "utils/json.hpp"
#include
#include
// 로고, 메인메뉴, 게임 화면 등 각각의 씬
enum class Scene
{
logo = 0, titleMenu, game, scoreScene, settingScreen
};
// 전역적으로 사용할 싱글톤 클래스
class SceneManager
{
private:
SceneManager() {}
static SceneManager* instance;
public:
static SceneManager* GetInstance()
{
if (instance != nullptr)
{
return instance;
}else
{
instance = new SceneManager();
return instance;
}
}
bool SaveJson();
bool LoadJson();
bool gameExit;
Scene scene;
Scene lastSce;
float volume = 1.0f;
bool isLog = false;
};
#endif
==== 3. using json library ====
By this way, You can save and load json file
#include "config.h"
// Save Setting to json file
bool SceneManager::SaveJson()
{
using namespace nlohmann;
json j;
j["Name"] = "DK Flappy Bird!";
j["SoundVolume"] = volume;
j["bLog"] = isLog;
// write prettified JSON to another file
std::ofstream o("settings.json");
if (o.bad())
{
return false;
}
o << std::setw(4) << j << std::endl;
o.close();
return true;
}
bool SceneManager::LoadJson()
{
using namespace nlohmann;
std::ifstream f("settings.json");
if (f.bad())
{
return false;
}
json data = json::parse(f);
volume = data["SoundVolume"];
isLog = data["bLog"];
f.close();
return true;
}
===== Related Tutorial =====
[[raylib:json_파일_저장_및_읽기|Raylib Json File save and load]]