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

View File

@ -6,7 +6,12 @@
#define DICEWARS_SIEDLER_GLFWWINDOW_H
#include "../../core/Window.h"
#include "GLFW/glfw3.h"
class WindowResizeEvent {
public:
int updatedWidth, updatedHeight;
WindowResizeEvent(int width, int height) : updatedWidth(width), updatedHeight(height) {};
};
class GLFWWindow : public Window {
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);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
entityRenderer->prepare(camera, light);
entityRenderer->prepare(camera, light, projectionMatrix);
entityRenderer->renderEntities(entities);
entityRenderer->finalizeFrame();
entities.clear();
terrainRenderer->prepare(camera, light);
terrainRenderer->prepare(camera, light, projectionMatrix);
terrainRenderer->renderTerrainTiles(terrainTiles);
terrainRenderer->finalizeFrame();
terrainTiles.clear();
worldSpriteRenderer->prepare(camera);
worldSpriteRenderer->prepare(camera, projectionMatrix);
worldSpriteRenderer->render(worldSprites, camera);
worldSpriteRenderer->finalize();
worldSprites.clear();
@ -68,8 +68,9 @@ void MasterRenderer::submitWorldSprite(
}
glm::mat4 MasterRenderer::createProjectionMatrix() {
float aspectRatio = static_cast<float>(Application::getInstance().getWindow().GetWidth()) / static_cast<float>(Application::getInstance().getWindow().GetHeight());
glm::mat4 MasterRenderer::createProjectionMatrix(const float width, const float height) {
std::cout << "Creating projection matrix with width: " << width << " and height: " << height << std::endl;
const float aspectRatio = width / height;
glm::mat4 projection = glm::perspective(
glm::radians(FOV),

View File

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

View File

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

View File

@ -21,7 +21,7 @@ public:
staticShader.loadProjectionMatrix(projectionMatrix);
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 finalizeFrame();

View File

@ -6,11 +6,12 @@
#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);
terrainShader.start();
terrainShader.loadLight(light.getPosition(), light.getColor());
terrainShader.loadViewMatrix(viewMatrix);
terrainShader.loadProjectionMatrix(projectionMatrix);
}
void TerrainRenderer::renderTerrainTiles(const std::unordered_map<TexturedModel *, std::vector<std::unique_ptr<TerrainRenderingData>>> &terrainTiles) {

View File

@ -22,7 +22,7 @@ public:
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 finalizeFrame();
private:

View File

@ -9,32 +9,32 @@
#include "../core/gui/text/Font.h"
#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.loadProjectionMatrix(calculateOrthographicProjectionMatrix());
shader.loadProjectionMatrix(calculateOrthographicProjectionMatrix(width, height));
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
for (const auto &text : texts) {
renderGuiText(*text);
renderGuiText(*text, width, height);
}
glEnable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
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 textSize = text.getSize();
const Font& font = text.getFont();
const std::string& string = text.getText();
float screenX = textPos.x * Application::getInstance().getWindow().GetWidth();
float screenY = (1.f - textPos.y) * Application::getInstance().getWindow().GetHeight();
float screenX = textPos.x * width;
float screenY = (1.f - textPos.y) * height;
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 y = screenY - textHeight - font.getDescent();
@ -82,10 +82,7 @@ void TextRenderer::renderGuiText(const GUIText &text) {
glm::mat4 TextRenderer::calculateOrthographicProjectionMatrix() {
const auto screenWidth = static_cast<float>(Application::getInstance().getWindow().GetWidth());
const auto screenHeight = static_cast<float>(Application::getInstance().getWindow().GetHeight());
const glm::mat4 projectionMat = glm::ortho(0.f, screenWidth, 0.0f, screenHeight);
glm::mat4 TextRenderer::calculateOrthographicProjectionMatrix(float width, float height) {
const glm::mat4 projectionMat = glm::ortho(0.f, width, 0.0f, height);
return projectionMat;
}

View File

@ -16,13 +16,13 @@
class TextRenderer {
public:
TextRenderer() : textModel(Loader::instance().loadTextModel()) {};
void renderGuiTexts(const std::vector<std::shared_ptr<GUIText>> & texts);
void renderGuiText(const GUIText& text);
void renderGuiTexts(const std::vector<std::shared_ptr<GUIText>> & texts, float width, float height);
void renderGuiText(const GUIText& text, float width, float height);
private:
TextShader shader;
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 "loader/Loader.h"
WorldSpriteRenderer::WorldSpriteRenderer(const glm::mat4& projectionMatrix) : projectionMatrix(projectionMatrix) {
WorldSpriteRenderer::WorldSpriteRenderer() {
std::vector<float> positions = {
-0.5f, 0.5f, 0.0f, // oben 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));
}
void WorldSpriteRenderer::prepare(const Camera &camera) {
void WorldSpriteRenderer::prepare(const Camera &camera, glm::mat4& projectionMatrix) {
glm::mat4 viewMatrix = MathUtils::createViewMatrix(camera);
glm::mat4 projectionViewMatrix = projectionMatrix * viewMatrix;
worldSpriteShader.start();

View File

@ -18,14 +18,13 @@ class Camera;
class WorldSpriteRenderer {
public:
WorldSpriteRenderer(const glm::mat4& projectionMatrix);
void prepare(const Camera &camera);
WorldSpriteRenderer();
void prepare(const Camera &camera, glm::mat4& projectionMatrix);
void render(std::unordered_map<ModelTexture *, std::vector<WorldSpriteRenderingData>> worldSprites, const Camera &camera);
void finalize();
private:
std::unique_ptr<RawModel> quadModel;
WorldSpriteShader worldSpriteShader;
glm::mat4 projectionMatrix;
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<Light> light;
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> hexModelWood;

View File

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