-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathGameMap.cpp
68 lines (49 loc) · 1.46 KB
/
GameMap.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <random>
#include <algorithm>
#include "GameMap.h"
#include "GameManager.h"
#include "Parsers/Gm1File.h"
#include "Parsers/TgxFile.h"
#include "Rendering/TextureAtlas.h"
#include "Rendering/Tileset.h"
#include "Rendering/Renderer.h"
#include "Rendering/Camera.h"
using namespace Sourcehold::Game;
GameMap::GameMap(MapDimension type) {
static const int dimensions[] = {160, 200, 300, 400};
dim = dimensions[(int)type];
}
GameMap::GameMap(ghc::filesystem::path path) {
// TODO //
LoadFromDisk(path);
dim = 160;
}
GameMap::~GameMap() {
}
void GameMap::LoadFromDisk(ghc::filesystem::path path) {
MapFile::LoadFromDisk(path);
gm1_tile = GetGm1("gm/tile_land8.gm1");
tileset = gm1_tile->GetTileset();
tiles.resize(dim * dim);
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> dist(0, 8);
auto GetRandomTile = [&]() {
return tileset->GetTile(dist(rng));
};
std::generate(begin(tiles), end(tiles), GetRandomTile);
}
void GameMap::Render() {
Camera& cam = Camera::instance();
mult = cam.zoomLevel == ZOOM_NEAR ? 2 : 1;
for (uint32_t i = 0; i < tiles.size(); i++) {
SDL_Rect clip = tiles[i];
int iy = i / dim;
int ix = i % dim;
int y = (8 * iy);
int x = (30 * ix) + (iy % 2 == 0 ? 15 : 0);
Rendering::Render(*tileset, int(mult * x - cam.positionX),
int(mult * y - cam.positionY), 30 * mult, 16 * mult,
&clip);
}
}