Skip to content

Commit 231e573

Browse files
committed
renderer: Add RenderEntity base class for common methods.
1 parent f6bcd95 commit 231e573

File tree

9 files changed

+135
-189
lines changed

9 files changed

+135
-189
lines changed

libopenage/renderer/stages/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
add_sources(libopenage
2+
render_entity.cpp
3+
)
4+
15
add_subdirectory(camera/)
26
add_subdirectory(hud/)
37
add_subdirectory(screen/)
Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023-2023 the openage authors. See copying.md for legal info.
1+
// Copyright 2023-2024 the openage authors. See copying.md for legal info.
22

33
#include "render_entity.h"
44

@@ -8,8 +8,7 @@
88
namespace openage::renderer::hud {
99

1010
HudDragRenderEntity::HudDragRenderEntity(const coord::input drag_start) :
11-
changed{false},
12-
last_update{0.0},
11+
RenderEntity{},
1312
drag_pos{nullptr, 0, "", nullptr, drag_start},
1413
drag_start{drag_start} {
1514
}
@@ -24,12 +23,6 @@ void HudDragRenderEntity::update(const coord::input drag_pos,
2423
this->changed = true;
2524
}
2625

27-
time::time_t HudDragRenderEntity::get_update_time() {
28-
std::shared_lock lock{this->mutex};
29-
30-
return this->last_update;
31-
}
32-
3326
const curve::Continuous<coord::input> &HudDragRenderEntity::get_drag_pos() {
3427
return this->drag_pos;
3528
}
@@ -38,16 +31,4 @@ const coord::input &HudDragRenderEntity::get_drag_start() {
3831
return this->drag_start;
3932
}
4033

41-
bool HudDragRenderEntity::is_changed() {
42-
std::shared_lock lock{this->mutex};
43-
44-
return this->changed;
45-
}
46-
47-
void HudDragRenderEntity::clear_changed_flag() {
48-
std::unique_lock lock{this->mutex};
49-
50-
this->changed = false;
51-
}
52-
5334
} // namespace openage::renderer::hud

libopenage/renderer/stages/hud/render_entity.h

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@
33
#pragma once
44

55
#include <cstdint>
6-
#include <shared_mutex>
7-
#include <string>
86

97
#include "coord/pixel.h"
108
#include "curve/continuous.h"
11-
#include "time/time.h"
9+
#include "renderer/stages/render_entity.h"
1210

1311

1412
namespace openage::renderer::hud {
1513

1614
/**
1715
* Render entity for pushing drag selection updates to the HUD renderer.
1816
*/
19-
class HudDragRenderEntity {
17+
class HudDragRenderEntity final : public renderer::RenderEntity {
2018
public:
2119
/**
2220
* Create a new render entity for drag selection in the HUD.
@@ -36,13 +34,6 @@ class HudDragRenderEntity {
3634
void update(const coord::input drag_pos,
3735
const time::time_t time = 0.0);
3836

39-
/**
40-
* Get the time of the last update.
41-
*
42-
* @return Time of last update.
43-
*/
44-
time::time_t get_update_time();
45-
4637
/**
4738
* Get the position of the dragged corner.
4839
*
@@ -57,31 +48,7 @@ class HudDragRenderEntity {
5748
*/
5849
const coord::input &get_drag_start();
5950

60-
/**
61-
* Check whether the render entity has received new updates.
62-
*
63-
* @return true if updates have been received, else false.
64-
*/
65-
bool is_changed();
66-
67-
/**
68-
* Clear the update flag by setting it to false.
69-
*/
70-
void clear_changed_flag();
71-
7251
private:
73-
/**
74-
* Flag for determining if the render entity has been updated by the
75-
* corresponding gamestate entity. Set to true every time \p update()
76-
* is called.
77-
*/
78-
bool changed;
79-
80-
/**
81-
* Time of the last update call.
82-
*/
83-
time::time_t last_update;
84-
8552
/**
8653
* Position of the dragged corner.
8754
*/
@@ -91,10 +58,5 @@ class HudDragRenderEntity {
9158
* Position of the start corner.
9259
*/
9360
coord::input drag_start;
94-
95-
/**
96-
* Mutex for protecting threaded access.
97-
*/
98-
std::shared_mutex mutex;
9961
};
10062
} // namespace openage::renderer::hud
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2024-2024 the openage authors. See copying.md for legal info.
2+
3+
#include "render_entity.h"
4+
5+
#include <mutex>
6+
7+
8+
namespace openage::renderer {
9+
10+
RenderEntity::RenderEntity() :
11+
changed{false},
12+
last_update{time::time_t::zero()} {
13+
}
14+
15+
time::time_t RenderEntity::get_update_time() {
16+
std::shared_lock lock{this->mutex};
17+
18+
return this->last_update;
19+
}
20+
21+
bool RenderEntity::is_changed() {
22+
std::shared_lock lock{this->mutex};
23+
24+
return this->changed;
25+
}
26+
27+
void RenderEntity::clear_changed_flag() {
28+
std::unique_lock lock{this->mutex};
29+
30+
this->changed = false;
31+
}
32+
33+
std::shared_lock<std::shared_mutex> RenderEntity::get_read_lock() {
34+
return std::shared_lock{this->mutex};
35+
}
36+
37+
} // namespace openage::renderer
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright 2024-2024 the openage authors. See copying.md for legal info.
2+
3+
#pragma once
4+
5+
#include <mutex>
6+
#include <shared_mutex>
7+
8+
#include <eigen3/Eigen/Dense>
9+
10+
#include "time/time.h"
11+
12+
13+
namespace openage::renderer {
14+
15+
/**
16+
* Interface for render entities that allow pushing updates from game simulation
17+
* to renderer.
18+
*/
19+
class RenderEntity {
20+
public:
21+
~RenderEntity() = default;
22+
23+
/**
24+
* Get the time of the last update.
25+
*
26+
* Accessing the update time is thread-safe.
27+
*
28+
* @return Time of last update.
29+
*/
30+
time::time_t get_update_time();
31+
32+
/**
33+
* Check whether the render entity has received new updates from the
34+
* gamestate.
35+
*
36+
* @return true if updates have been received, else false.
37+
*/
38+
bool is_changed();
39+
40+
/**
41+
* Clear the update flag by setting it to false.
42+
*/
43+
void clear_changed_flag();
44+
45+
/**
46+
* Get a shared lock for thread-safe reading from the render entity.
47+
*
48+
* The caller is responsible for unlocking the mutex after reading.
49+
*
50+
* @return Lock for the render entity.
51+
*/
52+
std::shared_lock<std::shared_mutex> get_read_lock();
53+
54+
protected:
55+
/**
56+
* Create a new render entity.
57+
*
58+
* Members are initialized to these values by default:
59+
* - \p changed: false
60+
* - \p last_update: time::time_t::zero()
61+
*
62+
* Declared as protected to prevent direct instantiation of this class.
63+
*/
64+
RenderEntity();
65+
66+
/**
67+
* Flag for determining if the render entity has been updated by the
68+
* corresponding gamestate entity. Set to true every time \p update()
69+
* is called.
70+
*/
71+
bool changed;
72+
73+
/**
74+
* Time of the last update call.
75+
*/
76+
time::time_t last_update;
77+
78+
/**
79+
* Mutex for protecting threaded access.
80+
*/
81+
std::shared_mutex mutex;
82+
};
83+
} // namespace openage::renderer

libopenage/renderer/stages/terrain/render_entity.cpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
namespace openage::renderer::terrain {
1111

1212
TerrainRenderEntity::TerrainRenderEntity() :
13-
changed{false},
13+
RenderEntity{},
1414
size{0, 0},
1515
tiles{},
16-
last_update{0.0},
1716
terrain_paths{},
1817
vertices{}
1918
// terrain_path{nullptr, 0},
@@ -132,16 +131,4 @@ const util::Vector2s &TerrainRenderEntity::get_size() {
132131
return this->size;
133132
}
134133

135-
bool TerrainRenderEntity::is_changed() {
136-
std::shared_lock lock{this->mutex};
137-
138-
return this->changed;
139-
}
140-
141-
void TerrainRenderEntity::clear_changed_flag() {
142-
std::unique_lock lock{this->mutex};
143-
144-
this->changed = false;
145-
}
146-
147134
} // namespace openage::renderer::terrain

libopenage/renderer/stages/terrain/render_entity.h

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22

33
#pragma once
44

5-
#include <memory>
6-
#include <shared_mutex>
75
#include <string>
86
#include <unordered_set>
97
#include <vector>
108

119
#include "coord/scene.h"
1210
#include "coord/tile.h"
1311
#include "curve/discrete.h"
14-
#include "time/time.h"
12+
#include "renderer/stages/render_entity.h"
1513
#include "util/vector.h"
1614

1715

@@ -20,7 +18,7 @@ namespace openage::renderer::terrain {
2018
/**
2119
* Render entity for pushing updates to the Terrain renderer.
2220
*/
23-
class TerrainRenderEntity {
21+
class TerrainRenderEntity final : public renderer::RenderEntity {
2422
public:
2523
TerrainRenderEntity();
2624
~TerrainRenderEntity() = default;
@@ -84,27 +82,7 @@ class TerrainRenderEntity {
8482
*/
8583
const util::Vector2s &get_size();
8684

87-
/**
88-
* Check whether the render entity has received new updates from the
89-
* gamestate.
90-
*
91-
* @return true if updates have been received, else false.
92-
*/
93-
bool is_changed();
94-
95-
/**
96-
* Clear the update flag by setting it to false.
97-
*/
98-
void clear_changed_flag();
99-
10085
private:
101-
/**
102-
* Flag for determining if the render entity has been updated by the
103-
* corresponding gamestate entity. Set to true every time \p update()
104-
* is called.
105-
*/
106-
bool changed;
107-
10886
/**
10987
* Chunk dimensions (width x height).
11088
*/
@@ -115,11 +93,6 @@ class TerrainRenderEntity {
11593
*/
11694
tiles_t tiles;
11795

118-
/**
119-
* Time of the last update call.
120-
*/
121-
time::time_t last_update;
122-
12396
/**
12497
* Terrain texture paths used in \p tiles .
12598
*/
@@ -129,10 +102,5 @@ class TerrainRenderEntity {
129102
* Terrain vertices (ingame coordinates).
130103
*/
131104
std::vector<coord::scene3> vertices;
132-
133-
/**
134-
* Mutex for protecting threaded access.
135-
*/
136-
std::shared_mutex mutex;
137105
};
138106
} // namespace openage::renderer::terrain

0 commit comments

Comments
 (0)