forked from Barnold1953/GraphicsTutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevel.cpp
More file actions
104 lines (84 loc) · 3.02 KB
/
Copy pathLevel.cpp
File metadata and controls
104 lines (84 loc) · 3.02 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
#include "Level.h"
#include <Bengine/BengineErrors.h>
#include <fstream>
#include <iostream>
#include <Bengine/ResourceManager.h>
Level::Level(const std::string& fileName) {
std::ifstream file;
file.open(fileName);
// Error checking
if (file.fail()) {
Bengine::fatalError("Failed to open " + fileName);
}
// Throw away the first string in tmp
std::string tmp;
file >> tmp >> _numHumans;
std::getline(file, tmp); // Throw away the rest of the first line
// Read the level data
while (std::getline(file, tmp)) {
_levelData.emplace_back(tmp);
}
_spriteBatch.init();
_spriteBatch.begin();
glm::vec4 uvRect(0.0f, 0.0f, 1.0f, 1.0f);
Bengine::ColorRGBA8 whiteColor;
whiteColor.r = 255;
whiteColor.g = 255;
whiteColor.b = 255;
whiteColor.a = 255;
// Render all the tiles
for (int y = 0; y < _levelData.size(); y++) {
for (int x = 0; x < _levelData[y].size(); x++) {
// Grab the tile
char tile = _levelData[y][x];
// Get dest rect
glm::vec4 destRect(x * TILE_WIDTH, y * TILE_WIDTH, TILE_WIDTH, TILE_WIDTH);
// Process the tile
switch (tile) {
case 'B':
case 'R':
_spriteBatch.draw(destRect,
uvRect,
Bengine::ResourceManager::getTexture("Textures/red_bricks.png").id,
0.0f,
whiteColor);
break;
case 'G':
_spriteBatch.draw(destRect,
uvRect,
Bengine::ResourceManager::getTexture("Textures/glass.png").id,
0.0f,
whiteColor);
break;
case 'L':
_spriteBatch.draw(destRect,
uvRect,
Bengine::ResourceManager::getTexture("Textures/light_bricks.png").id,
0.0f,
whiteColor);
break;
case '@':
_levelData[y][x] = '.'; /// So we dont collide with a @
_startPlayerPos.x = x * TILE_WIDTH;
_startPlayerPos.y = y * TILE_WIDTH;
break;
case 'Z':
_levelData[y][x] = '.'; /// So we dont collide with a Z
_zombieStartPositions.emplace_back(x * TILE_WIDTH, y * TILE_WIDTH);
break;
case '.':
break;
default:
std::printf("Unexpected symbol %c at (%d,%d)", tile, x, y);
break;
}
}
}
_spriteBatch.end();
}
Level::~Level()
{
}
void Level::draw() {
_spriteBatch.renderBatch();
}