-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockManager.cpp
More file actions
125 lines (93 loc) · 3.48 KB
/
BlockManager.cpp
File metadata and controls
125 lines (93 loc) · 3.48 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "Managers.h"
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
vector<string> &split(const string &s, char delim, vector<string> &elems) {
stringstream ss(s);
string item;
while (getline(ss, item, delim))
elems.push_back(item);
return elems;
}
vector<string> split(const string &s, char delim) {
vector<string> elems;
return split(s, delim, elems);
}
const char *textureFaces[TEXTURES_NUM][7] = {
{"skybox", "media/xpos.tga", "media/xneg.tga", "media/ypos.tga", "media/yneg.tga", "media/zpos.tga", "media/zneg.tga"},
{"grass", "media/grassSide.tga", "media/grassSide.tga", "media/grass.tga", "media/dirt.tga", "media/grassSide.tga", "media/grassSide.tga"},
{"dirt", "media/dirt.tga", "media/dirt.tga", "media/dirt.tga", "media/dirt.tga", "media/dirt.tga", "media/dirt.tga"},
{"stone", "media/stone.tga", "media/stone.tga", "media/stone.tga", "media/stone.tga", "media/stone.tga", "media/stone.tga"},
{"plank", "media/plank.tga", "media/plank.tga", "media/plank.tga", "media/plank.tga", "media/plank.tga", "media/plank.tga"},
{"wood", "media/woodSide.tga", "media/woodSide.tga", "media/wood.tga", "media/wood.tga", "media/woodSide.tga", "media/woodSide.tga"},
{"leaves", "media/leaves.tga", "media/leaves.tga", "media/leaves.tga", "media/leaves.tga", "media/leaves.tga", "media/leaves.tga"},
{"sand", "media/sand.tga", "media/sand.tga", "media/sand.tga", "media/sand.tga", "media/sand.tga", "media/sand.tga"}
};
BlockManager::BlockManager() {}
BlockManager::~BlockManager() {}
void BlockManager::loadBlocks(InputManager *inputManager) {
string line;
vector<string> elems;
fstream save;
save.open("save.dat");
float camX, camY, camZ;
if (save.is_open()) {
getline(save, line);
elems = split(line, ' ');
int xMax, yMax, zMax;
xMax = atoi(elems[0].c_str());
yMax = atoi(elems[1].c_str());
zMax = atoi(elems[2].c_str());
getline(save, line);
elems = split(line, ' ');
camX = atof(elems[0].c_str());
camY = atof(elems[1].c_str());
camZ = atof(elems[2].c_str());
for (int y = 0; y < yMax; y++)
for (int x = 0; x < xMax; x++) {
getline(save, line);
elems = split(line, ' ');
for (int z = 0; z < zMax; z++) {
cubes[x][z][y].frame.SetOrigin(x, y, z);
cubes[x][z][y].textureId = atoi(elems[z].c_str());
cubes[x][z][y].modelMatrix.LoadIdentity();
cubes[x][z][y].modelMatrix.MultMatrix(cubes[x][z][y].frame);
}
}
} else {
cout << "Unable to load\n";
exit(0);
}
inputManager->setCamXPos(camX);
inputManager->setCamYPos(camY);
inputManager->setCamZPos(camZ);
}
void BlockManager::saveBlocks(InputManager *inputManager) {
fstream save;
save.open("save.dat");
if (save.is_open()) {
save << SIZE_X << " " << SIZE_Y << " " << SIZE_Z << endl;
save << inputManager->getCamXPos() << " " << inputManager->getCamYPos() << " " << inputManager->getCamZPos() << endl;
for (int y = 0; y < SIZE_Y; y++)
for (int x = 0; x < SIZE_X; x++) {
for (int z = 0; z < SIZE_Z; z++)
save << cubes[x][z][y].textureId << " ";
save << endl;
}
save.close();
} else
cout << "Unable to save\n";
}
bool BlockManager::hasBlock(int x, int y, int z) {
return !(x >= SIZE_X || x < 0 || y >= SIZE_Y || y < 0 || z >= SIZE_Z || z < 0 || cubes[x][z][y].textureId == 0);
}
cube* BlockManager::getBlock(int x, int y, int z) {
return &cubes[x][z][y];
}
void BlockManager::setBlock(int x, int y, int z, int textureId) {
cubes[x][z][y].textureId = textureId;
}