1
0
mirror of https://github.com/blupi-games/planetblupi synced 2024-12-30 10:15:36 +01:00

Convert ini file to json

This commit is contained in:
Mathieu Schroeter 2017-08-13 23:01:04 +02:00
parent 7f3f7e4f37
commit 7308e6ef00
3 changed files with 40 additions and 73 deletions

View File

@ -1,3 +0,0 @@
FullScreen=1
SpeedRate=1
Timer=50ms

View File

@ -0,0 +1,5 @@
{
"fullscreen": 1,
"speedrate": 1,
"timerinterval": 50
}

View File

@ -21,6 +21,7 @@
#include <SDL2/SDL_image.h> #include <SDL2/SDL_image.h>
#include <argagg/argagg.hpp> #include <argagg/argagg.hpp>
#include <curl/curl.h> #include <curl/curl.h>
#include <fstream>
#include <iostream> #include <iostream>
#include <iterator> #include <iterator>
#include <sstream> #include <sstream>
@ -97,25 +98,6 @@ static std::vector<std::string> split (const std::string & s, char delim)
return elems; return elems;
} }
/**
* \brief Read an integer from a string.
*
* \param[in] p - Input string.
* \returns the integer.
*/
static Sint32 GetNum (char * p)
{
Sint32 n = 0;
while (*p >= '0' && *p <= '9')
{
n *= 10;
n += (*p++) - '0';
}
return n;
}
/** /**
* \brief Read the config file. * \brief Read the config file.
* *
@ -123,67 +105,50 @@ static Sint32 GetNum (char * p)
*/ */
static bool ReadConfig () static bool ReadConfig ()
{ {
FILE * file = nullptr; const auto config = GetBaseDir () + "data/config.json";
char buffer[200];
char * pText;
size_t nb;
const auto config = GetBaseDir () + "data/config.ini"; std::ifstream file (config, std::ifstream::in);
nlohmann::json j;
file >> j;
file = fopen (config.c_str (), "rb"); if (
if (file == nullptr) !(g_settingsOverload & SETTING_SPEEDRATE) &&
return false; j.find ("speedrate") != j.end ())
nb = fread (buffer, sizeof (char), 200 - 1, file);
buffer[nb] = 0;
fclose (file);
if (!(g_settingsOverload & SETTING_SPEEDRATE))
{ {
pText = strstr (buffer, "SpeedRate="); g_speedRate = j["speedrate"].get<int> ();
if (pText != nullptr) if (g_speedRate < 1)
{ g_speedRate = 1;
g_speedRate = GetNum (pText + 10); if (g_speedRate > 2)
if (g_speedRate < 1) g_speedRate = 2;
g_speedRate = 1;
if (g_speedRate > 2)
g_speedRate = 2;
}
} }
if (!(g_settingsOverload & SETTING_TIMERINTERVAL)) if (
!(g_settingsOverload & SETTING_TIMERINTERVAL) &&
j.find ("timerinterval") != j.end ())
{ {
pText = strstr (buffer, "Timer="); g_timerInterval = j["timerinterval"].get<int> ();
if (pText != nullptr) if (g_timerInterval < 10)
{ g_timerInterval = 10;
g_timerInterval = GetNum (pText + 6); if (g_timerInterval > 1000)
if (g_timerInterval < 10) g_timerInterval = 1000;
g_timerInterval = 10;
if (g_timerInterval > 1000)
g_timerInterval = 1000;
}
} }
if (!(g_settingsOverload & SETTING_FULLSCREEN)) if (
!(g_settingsOverload & SETTING_FULLSCREEN) &&
j.find ("fullscreen") != j.end ())
{ {
pText = strstr (buffer, "FullScreen="); g_bFullScreen = !j["fullscreen"].get<bool> ();
if (pText != nullptr) if (g_bFullScreen != 0)
{ g_bFullScreen = 1;
g_bFullScreen = !!GetNum (pText + 11);
if (g_bFullScreen != 0)
g_bFullScreen = 1;
}
} }
if (!(g_settingsOverload & SETTING_RENDERER)) if (
!(g_settingsOverload & SETTING_RENDERER) && j.find ("renderer") != j.end ())
{ {
pText = strstr (buffer, "Renderer="); if (j["renderer"] == "software")
if (pText) g_rendererType = SDL_RENDERER_SOFTWARE;
{ else if (j["renderer"] == "accelerated")
if (!strncmp (pText + 9, "software", 8)) g_rendererType = SDL_RENDERER_ACCELERATED;
g_rendererType = SDL_RENDERER_SOFTWARE;
else if (!strncmp (pText + 9, "accelerated", 11))
g_rendererType = SDL_RENDERER_ACCELERATED;
}
} }
return true; return true;
@ -618,7 +583,7 @@ static int DoInit (int argc, char * argv[], bool & exit)
RECT rcRect; RECT rcRect;
bool bOK; bool bOK;
bOK = ReadConfig (); // lit le fichier config.ini bOK = ReadConfig (); // lit le fichier config.json
auto res = SDL_Init (SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER); auto res = SDL_Init (SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER);
if (res < 0) if (res < 0)
@ -655,7 +620,7 @@ static int DoInit (int argc, char * argv[], bool & exit)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (!bOK) // Something wrong with config.ini file? if (!bOK) // Something wrong with config.json file?
{ {
InitFail ("Game not correctly installed"); InitFail ("Game not correctly installed");
return EXIT_FAILURE; return EXIT_FAILURE;