-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaveLoad.h
More file actions
68 lines (52 loc) · 1.59 KB
/
Copy pathSaveLoad.h
File metadata and controls
68 lines (52 loc) · 1.59 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#pragma once
#include <string>
#include <fstream>
#include "maths.h"
struct ObjectState {
Matrix worldMatrix;
};
struct GameState {
Vec3 playerPosition;
std::vector<ObjectState> objects;
};
class SaveSystem {
public:
void saveGame(const GameState& state, const std::string& filename) {
std::ofstream file("SaveData/" + filename);
if (!file.is_open()) return;
file << state.playerPosition.x << " "
<< state.playerPosition.y << " "
<< state.playerPosition.z << "\n";
file << state.objects.size() << "\n";
for (const auto& obj : state.objects) {
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
file << obj.worldMatrix.a[i][j] << " ";
file << "\n";
}
file.close();
}
GameState loadGame(const std::string& filename) {
GameState state;
std::ifstream file("SaveData/" + filename);
if (!file.is_open()) return state;
file >> state.playerPosition.x
>> state.playerPosition.y
>> state.playerPosition.z;
unsigned int count;
file >> count;
state.objects.resize(count);
for (unsigned int i = 0; i < count; i++)
{
for (int j = 0; j < 4; j++)
for (int k = 0; k < 4; k++)
file >> state.objects[i].worldMatrix.a[j][k];
}
file.close();
return state;
}
bool findSave(std::string save) {
std::wifstream saveSlot("SaveData/" + save);
return (saveSlot.is_open());
}
};