-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.cpp
50 lines (40 loc) · 1.02 KB
/
config.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "config.h"
#include <nlohmann/json.hpp>
#include <cstdio>
#include <memory>
using namespace std;
using json = nlohmann::json;
namespace Config
{
int width = 800;
int height = 600;
bool windowed = true;
float nearPlane = 0.1f;
float farPlane = 1000;
bool load()
{
FILE* infile;
if (fopen_s(&infile, "config.json", "rt"))
return false;
if (fseek(infile, 0, SEEK_END))
return false;
const long numBytes = ftell(infile);
if (fseek(infile, 0, SEEK_SET))
return false;
const unique_ptr<char> buffer(new char[numBytes + 1]);
char* const pBuffer = buffer.get();
const size_t bytesRead = fread(pBuffer, sizeof(char), numBytes, infile);
if (!bytesRead)
return false;
if (fclose(infile))
return false;
pBuffer[bytesRead] = 0;
json data = json::parse(pBuffer);
width = data["width"].get<int>();
height = data["height"].get<int>();
windowed = data["windowed"].get<bool>();
nearPlane = data["nearPlane"].get<float>();
farPlane = data["farPlane"].get<float>();
return true;
}
}