-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameData.cpp
More file actions
218 lines (189 loc) · 5.87 KB
/
GameData.cpp
File metadata and controls
218 lines (189 loc) · 5.87 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "GameData.h"
#include <cmath>
#include <random>
#include "MapBlock.h"
#include "Utility.h"
#include "RenderUtils.h"
void GameData::populateCoefs()
{
Constants& c = Constants::get();
skyScaleCoefs.clear();
for(size_t i = 0; i < c.horizonCap; ++i) {
skyScaleCoefs.push_back(sinf((float)i / ((float)c.horizonCap / (c.pi / 2))));
}
for(size_t i = skyScaleCoefs.size()-1; i > 0; --i) {
float temp = skyScaleCoefs.at(i);
skyScaleCoefs.push_back(temp);
}
}
static void loadTextures(std::vector<MipmapTex>& txt) {
util::loadMipMap(txt, "wall2.bmp");
util::loadMipMap(txt, "wall.bmp");
util::loadMipMap(txt, "wall3.bmp");
util::loadMipMap(txt, "wall2.bmp");
util::loadMipMap(txt, "hellaworld.bmp");
}
static void loadLightmaps(std::vector<MipmapTex>& lmp) {
util::loadMipMap(lmp, "wall2bumpmap.bmp");
}
static void loadSkyTextures(std::vector<SDL_Surface*>& skyTxt)
{
util::loadTexture(skyTxt, "sky.bmp");
}
static void loadSprites(std::vector<Sprite>& sprts)
{
// Sprite spr({0, 0, 0, 0}, "");
// sprts.push_back(spr);
}
void GameData::fillUpTheStars() {
Constants& c = Constants::get();
std::mt19937 r;
r.seed(1);
stars.clear();
stars.resize(c.starsWidth * c.starsHeight);
for(int i = 0; i < c.starsHeight; ++i) {
for(int j = 0; j < c.starsWidth; ++j) {
if(!(r() % c.screenWidth / 2)) {
stars[i * c.starsWidth + j] = r() & 0xFF;
}
}
}
}
void GameData::loadAllTextures()
{
using namespace util;
if(skyTexturePaths.empty()) {
loadSkyTextures(sky_textures);
} else {
for(auto& path : skyTexturePaths) {
if(!path.empty()) loadTexture(sky_textures, path.c_str());
}
}
if(texturePaths.empty()) {
loadTextures(textures);
} else {
for(auto& path : texturePaths) {
if(!path.empty()) {
loadMipMap(textures, path.c_str());
loadMipMap(m_textures, path.c_str());
}
}
}
if(lightmapPaths.empty()) {
loadLightmaps(lightmaps);
} else {
for(auto& path : lightmapPaths) {
if(!path.empty()) {
loadMipMap(lightmaps, path.c_str());
loadMipMap(m_lightmaps, path.c_str());
}
}
}
for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 3; ++j) {
avgSkyColor = RenderUtils::blend(avgSkyColor,
*RenderUtils::getScaledTexturePixel(sky_textures[0], 3, 3, i, j),
128);
}
}
}
void GameData::allocateScreenSizeSensitiveData()
{
Constants& c = Constants::get();
distances.resize(c.screenWidth);
rayPositions.resize(c.screenWidth);
rays.resize(c.screenWidth);
eyes.resize(c.screenWidth);
fillUpTheStars();
populateCoefs();
}
void freeScreenSizeSensitiveData()
{
}
void GameData::freeTextures()
{
for(auto i = textures.begin(); i != textures.end(); i++) {
for(auto& a : i->mipmaps) {
SDL_FreeSurface(a);
}
}
for(auto i = lightmaps.begin(); i != lightmaps.end(); i++) {
for(auto& a : i->mipmaps) {
SDL_FreeSurface(a);
}
}
for(auto i = m_textures.begin(); i != m_textures.end(); i++) {
for(auto& a : i->mipmaps) {
SDL_FreeSurface(a);
}
}
for(auto i = m_lightmaps.begin(); i != m_lightmaps.end(); i++) {
for(auto& a : i->mipmaps) {
SDL_FreeSurface(a);
}
}
}
GameData::~GameData()
{
freeScreenSizeSensitiveData();
freeTextures();
}
Vector2D<bool> GameData::initMapFromFile(const char* filename)
{
Vector2D<bool> state{0,0}; // If no init happened then both are false
std::ifstream in(filename);
if(!in.good()) return state;
map.clear(); // Map is vector<vector<MapBlock>>
std::string line{};
size_t i = 0;
MapBlock def{2, 1};
MapBlock empty{0};
while(std::getline(in, line) && !in.eof()) {
map.push_back(std::vector<MapBlock>{});
map[i].resize(line.size());
for(size_t j = 0; j < line.size(); ++j) {
char buf = line[j];
if(buf == '0') map[i][j] = def;
if(buf == ' ') map[i][j] = empty;
if(buf == 'I') {
// So multiple occurrences of 'I' won't break anything
if(!state.x) {
player.x = j + 0.5f; // so you start in the middle of a cell, how you'd expect
player.y = i + 0.5f;
state.x = true;
}
map[i][j] = empty;
}
if(buf == 'E') {
// multiple destinations don't make sense either
if(!state.y) {
destination.x = j;
destination.y = i;
//map[i][j] = {100, 0, 0};
map[i][j]= empty;
state.y = true;
}
}
}
++i;
}
Constants::get().mapWidth = map[0].size(); // Possible bug if lines are different sizes.
Constants::get().mapHeight = map.size();
}
// Returns true once finished.
bool GameData::tracePath(std::vector<Vector2D<int>>& path, int frametime)
{
if(lastPos >= path.size()-2) return true;
player.angle += (util::directionToAngle((path[lastPos+1].y - path[lastPos].y),
(path[lastPos+1].x - path[lastPos].x))
- player.angle) * ((float)frametime / millisAtCell);
player.x += (path[lastPos+1].x - path[lastPos].x) *
((float)frametime / millisAtCell);
player.y += (path[lastPos+1].y - path[lastPos].y) *
((float)frametime / millisAtCell);
if((int)(player.x) == path[lastPos+1].x &&
(int)(player.y) == path[lastPos+1].y) {
lastPos += 1;
}
return false;
}