Skip to content

Commit 8eacc71

Browse files
committed
renderer: Return copies instead of referenced in terrain render enity.
Makes the updates thread-safe.
1 parent 914b32c commit 8eacc71

File tree

4 files changed

+47
-22
lines changed

4 files changed

+47
-22
lines changed

libopenage/renderer/stages/terrain/chunk.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,19 @@ void TerrainChunk::fetch_updates(const time::time_t & /* time */) {
3131
if (not this->render_entity->is_changed()) {
3232
return;
3333
}
34+
35+
// Get the terrain data from the render entity
36+
auto terrain_size = this->render_entity->get_size();
37+
auto terrain_paths = this->render_entity->get_terrain_paths();
38+
auto tiles = this->render_entity->get_tiles();
39+
auto heightmap_verts = this->render_entity->get_vertices();
40+
41+
// Recreate the mesh data
3442
// TODO: Change mesh instead of recreating it
3543
// TODO: Multiple meshes
3644
this->meshes.clear();
37-
for (const auto &terrain_path : this->render_entity->get_terrain_paths()) {
38-
auto new_mesh = this->create_mesh(terrain_path);
45+
for (const auto &terrain_path : terrain_paths) {
46+
auto new_mesh = this->create_mesh(terrain_size, tiles, heightmap_verts, terrain_path);
3947
new_mesh->create_model_matrix(this->offset);
4048
this->meshes.push_back(new_mesh);
4149
}
@@ -59,13 +67,12 @@ const std::vector<std::shared_ptr<TerrainRenderMesh>> &TerrainChunk::get_meshes(
5967
return this->meshes;
6068
}
6169

62-
std::shared_ptr<TerrainRenderMesh> TerrainChunk::create_mesh(const std::string &texture_path) {
63-
auto size = this->render_entity->get_size();
64-
auto v_width = size[0];
65-
auto v_height = size[1];
66-
67-
auto tiles = this->render_entity->get_tiles();
68-
auto heightmap_verts = this->render_entity->get_vertices();
70+
std::shared_ptr<TerrainRenderMesh> TerrainChunk::create_mesh(const util::Vector2s vert_size,
71+
const RenderEntity::tiles_t &tiles,
72+
const std::vector<coord::scene3> &heightmap_verts,
73+
const std::string &texture_path) {
74+
auto v_width = vert_size[0];
75+
auto v_height = vert_size[1];
6976

7077
// vertex data for the mesh
7178
std::vector<float> mesh_verts{};

libopenage/renderer/stages/terrain/chunk.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <vector>
88

99
#include "coord/scene.h"
10+
#include "renderer/stages/terrain/render_entity.h"
1011
#include "time/time.h"
1112
#include "util/vector.h"
1213

@@ -19,7 +20,6 @@ class AssetManager;
1920

2021
namespace terrain {
2122
class TerrainRenderMesh;
22-
class RenderEntity;
2323

2424
/**
2525
* Stores the state of a terrain chunk in the terrain render stage.
@@ -85,9 +85,17 @@ class TerrainChunk {
8585
/**
8686
* Create a terrain mesh from the data provided by the render entity.
8787
*
88+
* @param vert_size Size of the terrain in vertices.
89+
* @param tiles Data for each tile (elevation, terrain path).
90+
* @param heightmap_verts Position of each vertex in the chunk.
91+
* @param texture_path Path to the texture for the terrain.
92+
*
8893
* @return New terrain mesh.
8994
*/
90-
std::shared_ptr<TerrainRenderMesh> create_mesh(const std::string &texture_path);
95+
std::shared_ptr<TerrainRenderMesh> create_mesh(const util::Vector2s vert_size,
96+
const RenderEntity::tiles_t &tiles,
97+
const std::vector<coord::scene3> &heightmap_verts,
98+
const std::string &texture_path);
9199

92100
/**
93101
* Size of the chunk in tiles (width x height).

libopenage/renderer/stages/terrain/render_entity.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ RenderEntity::RenderEntity() :
1414
size{0, 0},
1515
tiles{},
1616
terrain_paths{},
17-
vertices{}
18-
// terrain_path{nullptr, 0},
19-
{
17+
vertices{} {
2018
}
2119

2220
void RenderEntity::update_tile(const util::Vector2s size,
@@ -107,25 +105,25 @@ void RenderEntity::update(const util::Vector2s size,
107105
this->changed = true;
108106
}
109107

110-
const std::vector<coord::scene3> &RenderEntity::get_vertices() {
108+
const std::vector<coord::scene3> RenderEntity::get_vertices() {
111109
std::shared_lock lock{this->mutex};
112110

113111
return this->vertices;
114112
}
115113

116-
const RenderEntity::tiles_t &RenderEntity::get_tiles() {
114+
const RenderEntity::tiles_t RenderEntity::get_tiles() {
117115
std::shared_lock lock{this->mutex};
118116

119117
return this->tiles;
120118
}
121119

122-
const std::unordered_set<std::string> &RenderEntity::get_terrain_paths() {
120+
const std::unordered_set<std::string> RenderEntity::get_terrain_paths() {
123121
std::shared_lock lock{this->mutex};
124122

125123
return this->terrain_paths;
126124
}
127125

128-
const util::Vector2s &RenderEntity::get_size() {
126+
const util::Vector2s RenderEntity::get_size() {
129127
std::shared_lock lock{this->mutex};
130128

131129
return this->size;

libopenage/renderer/stages/terrain/render_entity.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class RenderEntity final : public renderer::RenderEntity {
3030
* Update a single tile of the displayed terrain (chunk) with information from the
3131
* gamestate.
3232
*
33+
* Updating the render entity with this method is thread-safe.
34+
*
3335
* @param size Size of the terrain in tiles (width x length)
3436
* @param pos Position of the tile in the chunk.
3537
* @param elevation Height of terrain tile.
@@ -46,6 +48,8 @@ class RenderEntity final : public renderer::RenderEntity {
4648
* Update the full grid of the displayed terrain (chunk) with information from the
4749
* gamestate.
4850
*
51+
* Updating the render entity with this method is thread-safe.
52+
*
4953
* @param size Size of the terrain in tiles (width x length)
5054
* @param tiles Animation data for each tile (elevation, terrain path).
5155
* @param time Simulation time of the update.
@@ -57,30 +61,38 @@ class RenderEntity final : public renderer::RenderEntity {
5761
/**
5862
* Get the vertices of the terrain.
5963
*
64+
* Accessing the terrain vertices is thread-safe.
65+
*
6066
* @return Vector of vertex coordinates.
6167
*/
62-
const std::vector<coord::scene3> &get_vertices();
68+
const std::vector<coord::scene3> get_vertices();
6369

6470
/**
6571
* Get the tiles of the terrain.
6672
*
73+
* Accessing the terrain tiles is thread-safe.
74+
*
6775
* @return Terrain tiles.
6876
*/
69-
const tiles_t &get_tiles();
77+
const tiles_t get_tiles();
7078

7179
/**
7280
* Get the terrain paths used in the terrain.
7381
*
82+
* Accessing the terrain paths is thread-safe.
83+
*
7484
* @return Terrain paths.
7585
*/
76-
const std::unordered_set<std::string> &get_terrain_paths();
86+
const std::unordered_set<std::string> get_terrain_paths();
7787

7888
/**
7989
* Get the number of vertices on each side of the terrain.
8090
*
91+
* Accessing the vertices size is thread-safe.
92+
*
8193
* @return Vector with width as first element and height as second element.
8294
*/
83-
const util::Vector2s &get_size();
95+
const util::Vector2s get_size();
8496

8597
private:
8698
/**

0 commit comments

Comments
 (0)