Skip to content

Commit a48127b

Browse files
committed
refactor: make text renderer a singleton in play state passed to whatever systems that use it
1 parent 2f4356c commit a48127b

4 files changed

Lines changed: 28 additions & 26 deletions

File tree

src/game/components/health.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace gameplay {
77
class HealthComponent : public our::Component {
88
public:
99
float maxHealth = 100.0f;
10-
float currentHealth = 100.0f;
10+
float currentHealth = 75.0f;
1111
float invulnerabilityTimer = 0.0f;
1212
bool isDead = false;
1313

src/game/systems/player-hud.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ namespace gameplay {
4545
weaponMaterial->pipelineState.blending.sourceFactor = GL_SRC_ALPHA;
4646
weaponMaterial->pipelineState.blending.destinationFactor = GL_ONE_MINUS_SRC_ALPHA;
4747

48-
textRenderer.initialize();
4948
testFont.load(fontPath, fontTexturePath);
5049
}
5150

@@ -76,7 +75,7 @@ namespace gameplay {
7675
fontTexturePath = data.value("fontTexturePath", fontTexturePath);
7776
}
7877

79-
void PlayerHUDSystem::render(our::World* world, our::Entity* playerEntity, glm::ivec2 windowSize) {
78+
void PlayerHUDSystem::render(our::World* world, our::Entity* playerEntity, glm::ivec2 windowSize, our::TextRenderer* textRenderer) {
8079
if (!playerEntity) return;
8180

8281
HealthComponent* playerHealth = playerEntity->getComponent<HealthComponent>();
@@ -126,25 +125,27 @@ namespace gameplay {
126125
}
127126

128127
// Ammo Counter
129-
std::string ammoText = std::to_string(playerComp->currentAmmo) + "-" + std::to_string(playerComp->maxAmmo);
130-
131-
glm::vec4 outlineColor = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
132-
133-
// PS2 ahh way to make outline, I can't find a solution to balance outline with scale, this is good enough :)
134-
// (draws the text blackened four times to create an outline)
135-
our::UIRect tr = ammoTextRect; tr.offset += glm::vec2(outlineSpread, outlineSpread);
136-
textRenderer.drawText(&testFont, ammoText, tr, windowSize, textScale, orthoVP, outlineColor);
137-
138-
our::UIRect bl = ammoTextRect; bl.offset += glm::vec2(-outlineSpread, -outlineSpread);
139-
textRenderer.drawText(&testFont, ammoText, bl, windowSize, textScale, orthoVP, outlineColor);
140-
141-
our::UIRect tl = ammoTextRect; tl.offset += glm::vec2(-outlineSpread, outlineSpread);
142-
textRenderer.drawText(&testFont, ammoText, tl, windowSize, textScale, orthoVP, outlineColor);
143-
144-
our::UIRect br = ammoTextRect; br.offset += glm::vec2(outlineSpread, -outlineSpread);
145-
textRenderer.drawText(&testFont, ammoText, br, windowSize, textScale, orthoVP, outlineColor);
146-
147-
textRenderer.drawText(&testFont, ammoText, ammoTextRect, windowSize, textScale, orthoVP, ammoColor);
128+
if (textRenderer) {
129+
std::string ammoText = std::to_string(playerComp->currentAmmo) + "-" + std::to_string(playerComp->maxAmmo);
130+
131+
glm::vec4 outlineColor = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
132+
133+
// PS2 ahh way to make outline, I can't find a solution to balance outline with scale, this is good enough :)
134+
// (draws the text blackened four times to create an outline)
135+
our::UIRect tr = ammoTextRect; tr.offset += glm::vec2(outlineSpread, outlineSpread);
136+
textRenderer->drawText(&testFont, ammoText, tr, windowSize, textScale, orthoVP, outlineColor);
137+
138+
our::UIRect bl = ammoTextRect; bl.offset += glm::vec2(-outlineSpread, -outlineSpread);
139+
textRenderer->drawText(&testFont, ammoText, bl, windowSize, textScale, orthoVP, outlineColor);
140+
141+
our::UIRect tl = ammoTextRect; tl.offset += glm::vec2(-outlineSpread, outlineSpread);
142+
textRenderer->drawText(&testFont, ammoText, tl, windowSize, textScale, orthoVP, outlineColor);
143+
144+
our::UIRect br = ammoTextRect; br.offset += glm::vec2(outlineSpread, -outlineSpread);
145+
textRenderer->drawText(&testFont, ammoText, br, windowSize, textScale, orthoVP, outlineColor);
146+
147+
textRenderer->drawText(&testFont, ammoText, ammoTextRect, windowSize, textScale, orthoVP, ammoColor);
148+
}
148149
}
149150

150151
void PlayerHUDSystem::destroy() {
@@ -158,7 +159,6 @@ namespace gameplay {
158159
delete weaponMaterial;
159160
}
160161

161-
textRenderer.destroy();
162162
testFont.destroy();
163163
}
164164

src/game/systems/player-hud.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ namespace gameplay {
1616
our::ShaderProgram* texturedShader = nullptr;
1717
our::TexturedMaterial* weaponMaterial = nullptr;
1818

19-
our::TextRenderer textRenderer;
2019
our::Font testFont;
2120

2221
our::UIRect weaponRect = { {1.0f, 0.0f}, {1.0f, 0.0f}, {-20.0f, 20.0f}, {80.0f, 80.0f} };
@@ -35,7 +34,7 @@ namespace gameplay {
3534
public:
3635
void initialize();
3736
void deserialize(const nlohmann::json& data);
38-
void render(our::World* world, our::Entity* playerEntity, glm::ivec2 windowSize);
37+
void render(our::World* world, our::Entity* playerEntity, glm::ivec2 windowSize, our::TextRenderer* textRenderer);
3938
void destroy();
4039
};
4140

src/states/play-state.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
class Playstate : public our::State {
1919
our::World world;
2020
our::ForwardRenderer renderer;
21+
our::TextRenderer textRenderer;
2122
our::FreeCameraControllerSystem cameraController;
2223
our::MovementSystem movementSystem;
2324
ALuint reloadSource = 0; // TODO: remove when implementing a proper weapon/player system
@@ -88,6 +89,7 @@ class Playstate : public our::State {
8889
// Then we initialize the renderer
8990
auto size = getApp()->getFrameBufferSize();
9091
renderer.initialize(size, config["renderer"]);
92+
textRenderer.initialize();
9193
collisionSystem.initialize();
9294

9395
enemySpawner.deserialize(config);
@@ -106,7 +108,7 @@ class Playstate : public our::State {
106108
// And finally we use the renderer system to draw the scene
107109
collisionSystem.update(&world);
108110
renderer.render(&world);
109-
playerHud.render(&world,playerEntity, getApp()->getFrameBufferSize());
111+
playerHud.render(&world, playerEntity, getApp()->getFrameBufferSize(), &textRenderer);
110112

111113
#ifdef COLLISION_DEBUG_DRAW
112114
// Toggle debug draw with F3
@@ -151,6 +153,7 @@ class Playstate : public our::State {
151153
void onDestroy() override {
152154
collisionSystem.destroy();
153155
renderer.destroy();
156+
textRenderer.destroy();
154157
playerHud.destroy();
155158
// On exit, we call exit for the camera controller system to make sure that the mouse is unlocked
156159
cameraController.exit();

0 commit comments

Comments
 (0)