ADD: WindowResizeEvent on Window Resizing; remove dependency of Application for renderers
All checks were successful
Tests / test (push) Successful in 2m33s

This commit is contained in:
Sebastian Böckelmann 2026-04-20 10:12:46 +02:00
parent 67fb1be3b5
commit 738d424df9
14 changed files with 47 additions and 36 deletions

View File

@ -6,6 +6,8 @@
#include <iostream> #include <iostream>
#include "../../core/events/EventBus.h"
static bool s_GLFWINITIALIZED = false; static bool s_GLFWINITIALIZED = false;
Window* Window::Create(const WindowProps& props) Window* Window::Create(const WindowProps& props)
@ -68,6 +70,7 @@ void GLFWWindow::Init(const WindowProps& props)
// Später: // Später:
// EventSystem::Dispatch(WindowResizeEvent(width, height)); // EventSystem::Dispatch(WindowResizeEvent(width, height));
EventBus::getInstance().emit(WindowResizeEvent(width, height));
} }
); );
std::cout << "OpenGL Info:\n"; std::cout << "OpenGL Info:\n";

View File

@ -6,7 +6,12 @@
#define DICEWARS_SIEDLER_GLFWWINDOW_H #define DICEWARS_SIEDLER_GLFWWINDOW_H
#include "../../core/Window.h" #include "../../core/Window.h"
#include "GLFW/glfw3.h" #include "GLFW/glfw3.h"
class WindowResizeEvent {
public:
int updatedWidth, updatedHeight;
WindowResizeEvent(int width, int height) : updatedWidth(width), updatedHeight(height) {};
};
class GLFWWindow : public Window { class GLFWWindow : public Window {
public: public:

View File

@ -14,17 +14,17 @@ void MasterRenderer::render(const Light &light, const Camera &camera) {
glClearColor(0.3254901961, 0.6431372549, 0.9254901961f, 1.0f); glClearColor(0.3254901961, 0.6431372549, 0.9254901961f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
entityRenderer->prepare(camera, light); entityRenderer->prepare(camera, light, projectionMatrix);
entityRenderer->renderEntities(entities); entityRenderer->renderEntities(entities);
entityRenderer->finalizeFrame(); entityRenderer->finalizeFrame();
entities.clear(); entities.clear();
terrainRenderer->prepare(camera, light); terrainRenderer->prepare(camera, light, projectionMatrix);
terrainRenderer->renderTerrainTiles(terrainTiles); terrainRenderer->renderTerrainTiles(terrainTiles);
terrainRenderer->finalizeFrame(); terrainRenderer->finalizeFrame();
terrainTiles.clear(); terrainTiles.clear();
worldSpriteRenderer->prepare(camera); worldSpriteRenderer->prepare(camera, projectionMatrix);
worldSpriteRenderer->render(worldSprites, camera); worldSpriteRenderer->render(worldSprites, camera);
worldSpriteRenderer->finalize(); worldSpriteRenderer->finalize();
worldSprites.clear(); worldSprites.clear();
@ -68,8 +68,9 @@ void MasterRenderer::submitWorldSprite(
} }
glm::mat4 MasterRenderer::createProjectionMatrix() { glm::mat4 MasterRenderer::createProjectionMatrix(const float width, const float height) {
float aspectRatio = static_cast<float>(Application::getInstance().getWindow().GetWidth()) / static_cast<float>(Application::getInstance().getWindow().GetHeight()); std::cout << "Creating projection matrix with width: " << width << " and height: " << height << std::endl;
const float aspectRatio = width / height;
glm::mat4 projection = glm::perspective( glm::mat4 projection = glm::perspective(
glm::radians(FOV), glm::radians(FOV),

View File

@ -11,11 +11,14 @@
#include "Renderer.h" #include "Renderer.h"
#include "TerrainRenderer.h" #include "TerrainRenderer.h"
#include "WorldSpriteRenderer.h" #include "WorldSpriteRenderer.h"
#include "../core/events/EventBus.h"
#include "../layer/entities/Entity.h" #include "../layer/entities/Entity.h"
#include "../platform/glfw/GLFWWindow.h"
#include "model/RenderTargets.h" #include "model/RenderTargets.h"
#include "model/TexturedModel.h" #include "model/TexturedModel.h"
struct RenderContext;
class Camera; class Camera;
class Light; class Light;
@ -34,17 +37,19 @@ private:
constexpr static float NEAR_PLANE = 0.1f; constexpr static float NEAR_PLANE = 0.1f;
constexpr static float FAR_PLANE = 1000.0f; constexpr static float FAR_PLANE = 1000.0f;
static glm::mat4 createProjectionMatrix(); static glm::mat4 createProjectionMatrix(const float width, const float height);
public: public:
MasterRenderer() : projectionMatrix(createProjectionMatrix()), entityRenderer(std::make_unique<Renderer>(projectionMatrix)), explicit MasterRenderer(const float width, const float height) : projectionMatrix(createProjectionMatrix(width, height)), entityRenderer(std::make_unique<Renderer>(projectionMatrix)),
terrainRenderer(std::make_unique<TerrainRenderer>(projectionMatrix)), worldSpriteRenderer(std::make_unique<WorldSpriteRenderer>(projectionMatrix)), terrainRenderer(std::make_unique<TerrainRenderer>(projectionMatrix)), worldSpriteRenderer(std::make_unique<WorldSpriteRenderer>()),
minimapRenderer(std::make_unique<MinimapRenderer>(200,200)) minimapRenderer(std::make_unique<MinimapRenderer>(200,200))
{ {
glEnable(GL_CULL_FACE); glEnable(GL_CULL_FACE);
glCullFace(GL_BACK); glCullFace(GL_BACK);
EventBus::getInstance().subscribe<WindowResizeEvent>([this](const WindowResizeEvent& event) {
projectionMatrix = createProjectionMatrix(event.updatedWidth, event.updatedHeight);
});
RenderTargets::instance().setMinimapTexture(minimapRenderer->getMinimapTexture()); RenderTargets::instance().setMinimapTexture(minimapRenderer->getMinimapTexture());
}; };
void render(const Light &light, const Camera &camera); void render(const Light &light, const Camera &camera);

View File

@ -14,12 +14,12 @@
#include "model/TexturedModel.h" #include "model/TexturedModel.h"
void Renderer::prepare(const Camera& camera, const Light& light, glm::mat4 projectionMatrix) {
void Renderer::prepare(const Camera& camera, const Light& light) {
const glm::mat4 viewMatrix = MathUtils::createViewMatrix(camera); const glm::mat4 viewMatrix = MathUtils::createViewMatrix(camera);
staticShader.start(); staticShader.start();
staticShader.loadLight(light.getPosition(), light.getColor()); staticShader.loadLight(light.getPosition(), light.getColor());
staticShader.loadViewMatrix(viewMatrix); staticShader.loadViewMatrix(viewMatrix);
staticShader.loadProjectionMatrix(projectionMatrix);
} }

View File

@ -21,7 +21,7 @@ public:
staticShader.loadProjectionMatrix(projectionMatrix); staticShader.loadProjectionMatrix(projectionMatrix);
staticShader.stop(); staticShader.stop();
}; };
void prepare(const ::Camera &camera, const Light& light); void prepare(const ::Camera &camera, const ::Light &light, glm::mat4 projectionMatrix);
void renderEntities(const std::unordered_map<TexturedModel *, std::vector<std::unique_ptr<Entity>>> &entities); void renderEntities(const std::unordered_map<TexturedModel *, std::vector<std::unique_ptr<Entity>>> &entities);
void finalizeFrame(); void finalizeFrame();

View File

@ -6,11 +6,12 @@
#include "../toolbox/MathUtils.h" #include "../toolbox/MathUtils.h"
void TerrainRenderer::prepare(const Camera &camera, const Light &light) { void TerrainRenderer::prepare(const Camera &camera, const Light &light, glm::mat4 projectionMatrix) {
const glm::mat4 viewMatrix = MathUtils::createViewMatrix(camera); const glm::mat4 viewMatrix = MathUtils::createViewMatrix(camera);
terrainShader.start(); terrainShader.start();
terrainShader.loadLight(light.getPosition(), light.getColor()); terrainShader.loadLight(light.getPosition(), light.getColor());
terrainShader.loadViewMatrix(viewMatrix); terrainShader.loadViewMatrix(viewMatrix);
terrainShader.loadProjectionMatrix(projectionMatrix);
} }
void TerrainRenderer::renderTerrainTiles(const std::unordered_map<TexturedModel *, std::vector<std::unique_ptr<TerrainRenderingData>>> &terrainTiles) { void TerrainRenderer::renderTerrainTiles(const std::unordered_map<TexturedModel *, std::vector<std::unique_ptr<TerrainRenderingData>>> &terrainTiles) {

View File

@ -22,7 +22,7 @@ public:
terrainShader.stop(); terrainShader.stop();
} }
void prepare(const Camera &camera, const Light &light); void prepare(const Camera &camera, const Light &light, glm::mat4 projectionMatrix);
void renderTerrainTiles(const std::unordered_map<TexturedModel*, std::vector<std::unique_ptr<TerrainRenderingData>>>& terrainTiles); void renderTerrainTiles(const std::unordered_map<TexturedModel*, std::vector<std::unique_ptr<TerrainRenderingData>>>& terrainTiles);
void finalizeFrame(); void finalizeFrame();
private: private:

View File

@ -9,32 +9,32 @@
#include "../core/gui/text/Font.h" #include "../core/gui/text/Font.h"
#include "glm/ext/matrix_clip_space.hpp" #include "glm/ext/matrix_clip_space.hpp"
void TextRenderer::renderGuiTexts(const std::vector<std::shared_ptr<GUIText>> &texts) { void TextRenderer::renderGuiTexts(const std::vector<std::shared_ptr<GUIText>> &texts, const float width, const float height) {
shader.start(); shader.start();
shader.loadProjectionMatrix(calculateOrthographicProjectionMatrix()); shader.loadProjectionMatrix(calculateOrthographicProjectionMatrix(width, height));
glEnable(GL_BLEND); glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
for (const auto &text : texts) { for (const auto &text : texts) {
renderGuiText(*text); renderGuiText(*text, width, height);
} }
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glDisable(GL_BLEND); glDisable(GL_BLEND);
shader.stop(); shader.stop();
} }
void TextRenderer::renderGuiText(const GUIText &text) { void TextRenderer::renderGuiText(const GUIText &text, float width, float height) {
glm::vec2 textPos = text.getPosition(); glm::vec2 textPos = text.getPosition();
glm::vec2 textSize = text.getSize(); glm::vec2 textSize = text.getSize();
const Font& font = text.getFont(); const Font& font = text.getFont();
const std::string& string = text.getText(); const std::string& string = text.getText();
float screenX = textPos.x * Application::getInstance().getWindow().GetWidth(); float screenX = textPos.x * width;
float screenY = (1.f - textPos.y) * Application::getInstance().getWindow().GetHeight(); float screenY = (1.f - textPos.y) * height;
float textHeight = font.getAscent() - font.getDescent(); // ascent - descent float textHeight = font.getAscent() - font.getDescent(); // ascent - descent
float buttonWidthPx = textSize.x * Application::getInstance().getWindow().GetWidth(); float buttonWidthPx = textSize.x * width;
float x = screenX + (buttonWidthPx - font.getTextWidth(text.getText(), 1.0f)) * 0.5f; float x = screenX + (buttonWidthPx - font.getTextWidth(text.getText(), 1.0f)) * 0.5f;
float y = screenY - textHeight - font.getDescent(); float y = screenY - textHeight - font.getDescent();
@ -82,10 +82,7 @@ void TextRenderer::renderGuiText(const GUIText &text) {
glm::mat4 TextRenderer::calculateOrthographicProjectionMatrix() { glm::mat4 TextRenderer::calculateOrthographicProjectionMatrix(float width, float height) {
const auto screenWidth = static_cast<float>(Application::getInstance().getWindow().GetWidth()); const glm::mat4 projectionMat = glm::ortho(0.f, width, 0.0f, height);
const auto screenHeight = static_cast<float>(Application::getInstance().getWindow().GetHeight());
const glm::mat4 projectionMat = glm::ortho(0.f, screenWidth, 0.0f, screenHeight);
return projectionMat; return projectionMat;
} }

View File

@ -16,13 +16,13 @@
class TextRenderer { class TextRenderer {
public: public:
TextRenderer() : textModel(Loader::instance().loadTextModel()) {}; TextRenderer() : textModel(Loader::instance().loadTextModel()) {};
void renderGuiTexts(const std::vector<std::shared_ptr<GUIText>> & texts); void renderGuiTexts(const std::vector<std::shared_ptr<GUIText>> & texts, float width, float height);
void renderGuiText(const GUIText& text); void renderGuiText(const GUIText& text, float width, float height);
private: private:
TextShader shader; TextShader shader;
TextQuadModel textModel; TextQuadModel textModel;
static glm::mat4 calculateOrthographicProjectionMatrix(); static glm::mat4 calculateOrthographicProjectionMatrix(float width, float height);
}; };

View File

@ -11,7 +11,7 @@
#include "glm/ext/matrix_transform.hpp" #include "glm/ext/matrix_transform.hpp"
#include "loader/Loader.h" #include "loader/Loader.h"
WorldSpriteRenderer::WorldSpriteRenderer(const glm::mat4& projectionMatrix) : projectionMatrix(projectionMatrix) { WorldSpriteRenderer::WorldSpriteRenderer() {
std::vector<float> positions = { std::vector<float> positions = {
-0.5f, 0.5f, 0.0f, // oben links -0.5f, 0.5f, 0.0f, // oben links
-0.5f, -0.5f, 0.0f, // unten links -0.5f, -0.5f, 0.0f, // unten links
@ -34,7 +34,7 @@ WorldSpriteRenderer::WorldSpriteRenderer(const glm::mat4& projectionMatrix) : pr
quadModel = std::make_unique<RawModel>(Loader::instance().loadToVAO(positions, uvs, indices)); quadModel = std::make_unique<RawModel>(Loader::instance().loadToVAO(positions, uvs, indices));
} }
void WorldSpriteRenderer::prepare(const Camera &camera) { void WorldSpriteRenderer::prepare(const Camera &camera, glm::mat4& projectionMatrix) {
glm::mat4 viewMatrix = MathUtils::createViewMatrix(camera); glm::mat4 viewMatrix = MathUtils::createViewMatrix(camera);
glm::mat4 projectionViewMatrix = projectionMatrix * viewMatrix; glm::mat4 projectionViewMatrix = projectionMatrix * viewMatrix;
worldSpriteShader.start(); worldSpriteShader.start();

View File

@ -18,14 +18,13 @@ class Camera;
class WorldSpriteRenderer { class WorldSpriteRenderer {
public: public:
WorldSpriteRenderer(const glm::mat4& projectionMatrix); WorldSpriteRenderer();
void prepare(const Camera &camera); void prepare(const Camera &camera, glm::mat4& projectionMatrix);
void render(std::unordered_map<ModelTexture *, std::vector<WorldSpriteRenderingData>> worldSprites, const Camera &camera); void render(std::unordered_map<ModelTexture *, std::vector<WorldSpriteRenderingData>> worldSprites, const Camera &camera);
void finalize(); void finalize();
private: private:
std::unique_ptr<RawModel> quadModel; std::unique_ptr<RawModel> quadModel;
WorldSpriteShader worldSpriteShader; WorldSpriteShader worldSpriteShader;
glm::mat4 projectionMatrix;
static glm::mat4 buildWorldSpriteMatrix(const TransformComponent& objectTransform, const WorldSpriteComponent& worldSpriteComponent, const Camera& camera); static glm::mat4 buildWorldSpriteMatrix(const TransformComponent& objectTransform, const WorldSpriteComponent& worldSpriteComponent, const Camera& camera);
}; };

View File

@ -34,7 +34,7 @@ private:
std::unique_ptr<Camera> camera; std::unique_ptr<Camera> camera;
std::unique_ptr<Light> light; std::unique_ptr<Light> light;
std::unique_ptr<MousePicker> mousePicker; std::unique_ptr<MousePicker> mousePicker;
std::unique_ptr<MasterRenderer> renderer = std::make_unique<MasterRenderer>(); std::unique_ptr<MasterRenderer> renderer = std::make_unique<MasterRenderer>(800.f,800.f);
std::shared_ptr<TexturedModel> hexModelDefault; std::shared_ptr<TexturedModel> hexModelDefault;
std::shared_ptr<TexturedModel> hexModelWood; std::shared_ptr<TexturedModel> hexModelWood;

View File

@ -165,5 +165,5 @@ void UILayer::onRender() {
guiRenderer->render(guis); guiRenderer->render(guis);
auto renderTexts = renderBundle.getGUITexts(); auto renderTexts = renderBundle.getGUITexts();
textRenderer->renderGuiTexts(renderTexts); textRenderer->renderGuiTexts(renderTexts, Application::getInstance().getWindow().GetWidth(), Application::getInstance().getWindow().GetHeight());
} }