-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorld.cpp
134 lines (112 loc) · 2.65 KB
/
World.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
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
#include "World.h"
World::World(ThreadManager* manager, Camera* ref, unsigned x, unsigned z) : EngineObject(), player(ref),
chunkX(x), chunkZ(z), allocator(16), initialized(false), ground(string("assets/textures/Test_tex2.bmp")), _heightmap( chunkX * 4, chunkZ * 4)
{
}
void World::load(RessourceHandler& loader)
{
ground.load(loader);
}
void World::init(KeyMap& map)
{
if (initialized)
return;
initialized = true;
ground.glDownload();
}
void World::update(ThreadManager& mgr)
{
{
lock_guard<mutex> lock(safeguard);
setPos(toWorldPos(player->getPos()));
}
for (Terrain& terr : allocator)
terr.update(mgr);
}
void World::render(Shader& s, RenderingEngine::RenderState state)
{
s.bind();
ground.bind( s);
for (Terrain& terr : allocator)
{
if (terr.isInit())
terr.Draw();
else
terr.init();
}
}
World::~World()
{
}
unsigned World::getMemPosForTerrain(int x, int z, bool invX, bool invZ)
{
int bitX, bitZ;
bitX = x % 4;
bitZ = z % 4;
if (invX)
bitX = 4 - bitX;
if (invZ)
bitZ = 4 - bitZ;
return (bitX | (bitZ << 2)) % 16;
}
void World::TerrainForPos(vec2 p, int xO, int zO)
{
int xOff = (xO - 2) * chunkX
, zOff = (zO - 2) * chunkZ;
unsigned pos = getMemPosForTerrain((int)(abs(xOff) / chunkX), (int)(abs(zOff) / chunkZ), xOff < 0, zOff < 0);
if (!allocator[pos].isPos(xOff, zOff)) //checks if cache is correct
{
allocator[pos].setPos(chunkX, chunkZ, xOff, zOff, _heightmap.get((((pos & 12) >> 2) * chunkZ), ((pos & 3) * chunkX)));
}
}
void World::setPos(vec2 pos)
{
for (unsigned char x = 0; x < 4; x++)
for (unsigned char z = 0; z < 4; z++)
TerrainForPos(pos, x, z);
}
World::Heightmap::Heightmap(unsigned x, unsigned z) : _x(x), _z(z)
{
_mem.reset(new float[x * z]);
_pos0 = _mem.get();
}
World::Heightmap::Heightmap(const Heightmap &other) : _x(other._x), _z(other._z)
{
_mem.reset(new float[_x * _z]);
_pos0 = _mem.get();
memcpy(_pos0, other._pos0, sizeof(float) * _x * _z);
}
World::Heightmap::Heightmap(Heightmap &&other) : _x(other._x), _z(other._z)
{
_mem.reset(other._mem.release());
_pos0 = _mem.get();
}
World::Heightmap & World::Heightmap::operator=(const Heightmap &other)
{
_x = other._x;
_z = other._z;
_mem.reset(new float[_x * _z]);
_pos0 = _mem.get();
memcpy(_pos0, other._pos0, sizeof(float) * _x * _z);
return *this;
}
World::Heightmap & World::Heightmap::operator=(Heightmap &&other)
{
_x = other._x;
_z = other._z;
_mem.reset(other._mem.release());
_pos0 = _mem.get();
return *this;
}
float * World::Heightmap::get()
{
return _pos0;
}
float * World::Heightmap::get(unsigned x, unsigned z)
{
return &_pos0[(x * _z) + z];
}
float * World::Heightmap::operator[](unsigned x)
{
return &_pos0[x * _z];
}