Core Rendering Logic

This commit is contained in:
Sebastian Böckelmann 2025-12-24 10:55:52 +01:00
parent cbfeea220e
commit 017bf6057a
16 changed files with 279 additions and 5 deletions

View File

@ -23,7 +23,17 @@ add_executable(Dicewars_Siedler src/main.cpp
src/engine/core/Application.cpp
src/engine/core/Application.h
src/game/DicewarsApp.cpp
src/game/DicewarsApp.h)
src/game/DicewarsApp.h
src/engine/renderer/model/RawModel.cpp
src/engine/renderer/model/RawModel.h
src/engine/renderer/model/Loader.cpp
src/engine/renderer/model/Loader.h
src/engine/renderer/Renderer.cpp
src/engine/renderer/Renderer.h
src/engine/layer/Layer.cpp
src/engine/layer/Layer.h
src/engine/layer/GameLayer.cpp
src/engine/layer/GameLayer.h)
target_include_directories(Dicewars_Siedler PRIVATE
lib/glfw/include

View File

@ -4,8 +4,15 @@
#include "Application.h"
#include "../layer/Layer.h"
Application* Application::instance = nullptr;
void Application::pushLayer(Layer* layer) {
layers.push_back(layer);
layer->onAttach();
}
Application::Application()
{
instance = this;
@ -26,7 +33,10 @@ Application::~Application()
void Application::run() {
while (!window->shouldClose())
{
//Rendering
for (Layer* layer : layers)
{
layer->onUpdate();
}
window->OnUpdate();
}
}

View File

@ -5,10 +5,13 @@
#ifndef DICEWARS_SIEDLER_APPLICATION_H
#define DICEWARS_SIEDLER_APPLICATION_H
#include <memory>
#include <vector>
#include "Window.h"
class Layer;
class Application
{
public:
@ -24,6 +27,11 @@ private:
std::unique_ptr<Window> window;
static Application* instance;
std::vector<Layer*> layers;
protected:
void pushLayer(Layer* layer);
};

View File

@ -0,0 +1,37 @@
//
// Created by sebastian on 24.12.25.
//
#include "GameLayer.h"
#include "../renderer/Renderer.h"
GameLayer::GameLayer() :model(0,0) //Platzhalter, echtes Model kommt in onAttach
{
}
void GameLayer::onAttach()
{
std::vector<float> vertices = {
-0.5f, 0.5f, 0.f,
-0.5f, -0.5f, 0.f,
0.5f, -0.5f, 0.f,
0.5f, -0.5f, 0.f,
0.5f, 0.5f, 0.f,
-0.5f, 0.5f, 0.f
};
model = loader.loadToVAO(vertices);
}
void GameLayer::onDetach()
{
Layer::onDetach();
}
void GameLayer::onUpdate()
{
renderer.renderFrame(model);
}

View File

@ -0,0 +1,28 @@
//
// Created by sebastian on 24.12.25.
//
#ifndef DICEWARS_SIEDLER_GAMELAYER_H
#define DICEWARS_SIEDLER_GAMELAYER_H
#include "Layer.h"
#include "../renderer/Renderer.h"
#include "../renderer/model/Loader.h"
class GameLayer: public Layer {
public:
GameLayer();
~GameLayer() override = default;
void onAttach() override;
void onDetach() override;
void onUpdate() override;
private:
Loader loader;
RawModel model;
Renderer renderer;
};
#endif //DICEWARS_SIEDLER_GAMELAYER_H

View File

@ -0,0 +1,5 @@
//
// Created by sebastian on 24.12.25.
//
#include "Layer.h"

18
src/engine/layer/Layer.h Normal file
View File

@ -0,0 +1,18 @@
//
// Created by sebastian on 24.12.25.
//
#ifndef DICEWARS_SIEDLER_LAYER_H
#define DICEWARS_SIEDLER_LAYER_H
class Layer {
public:
virtual ~Layer() = default;
virtual void onAttach() {}
virtual void onDetach() {}
virtual void onUpdate() {}
};
#endif //DICEWARS_SIEDLER_LAYER_H

View File

@ -42,7 +42,7 @@ void GLFWWindow::Init(const WindowProps& props)
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
m_Window = glfwCreateWindow(
static_cast<int>(m_Data.Width), static_cast<int>(m_Data.Height), m_Data.Title.c_str(), nullptr, nullptr);

View File

@ -0,0 +1,26 @@
//
// Created by sebastian on 24.12.25.
//
#include "Renderer.h"
#include "glad/glad.h"
void Renderer::prepare() {
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
}
void Renderer::renderFrame(const RawModel& model) {
prepare();
renderRawModel(model);
}
void Renderer::renderRawModel(const RawModel& model) {
glBindVertexArray(model.vaoID);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES,0 , model.vertexCount);
glDisableVertexAttribArray(0);
glBindVertexArray(0);
}

View File

@ -0,0 +1,19 @@
//
// Created by sebastian on 24.12.25.
//
#ifndef DICEWARS_SIEDLER_RENDERER_H
#define DICEWARS_SIEDLER_RENDERER_H
#include "model/RawModel.h"
class Renderer {
public:
void prepare();
void renderFrame(const RawModel& moddel);
private:
void renderRawModel(const RawModel &model);
};
#endif //DICEWARS_SIEDLER_RENDERER_H

View File

@ -0,0 +1,49 @@
//
// Created by sebastian on 24.12.25.
//
#include "Loader.h"
RawModel Loader::loadToVAO(std::vector<float> vertices){
GLuint vaoID = createVAO();
storeDataInAttributeList(0, vertices);
unbindVAO();
return {vaoID, static_cast<int>(vertices.size() / 3)};
}
Loader::~Loader() {
cleanUp();
}
void Loader::cleanUp() {
for (GLuint vaoID: vaoIDs) {
glDeleteVertexArrays(1, &vaoID);
}
for (GLuint vboID: vboIDs) {
glDeleteBuffers(1, &vboID);
}
}
GLuint Loader::createVAO() {
GLuint vaoID;
glGenVertexArrays(1, &vaoID);
vaoIDs.push_back(vaoID);
glBindVertexArray(vaoID);
return vaoID;
}
void Loader::storeDataInAttributeList(int attributeNumber, const std::vector<float> &data) {
GLuint vboID;
glGenBuffers(1, &vboID);
vboIDs.push_back(vboID);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), data.data(), GL_STATIC_DRAW);
glVertexAttribPointer(attributeNumber, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void Loader::unbindVAO() {
glBindVertexArray(0);
}

View File

@ -0,0 +1,30 @@
//
// Created by sebastian on 24.12.25.
//
#ifndef DICEWARS_SIEDLER_LOADER_H
#define DICEWARS_SIEDLER_LOADER_H
#include <vector>
#include "RawModel.h"
#include "glad/glad.h"
class Loader
{
public:
RawModel loadToVAO(std::vector<float> vertices);
~Loader();
void cleanUp();
private:
GLuint createVAO();
void storeDataInAttributeList(int attributeNumber, const std::vector<float> &data);
void unbindVAO();
std::vector<GLuint> vaoIDs;
std::vector<GLuint> vboIDs;
};
#endif //DICEWARS_SIEDLER_LOADER_H

View File

@ -0,0 +1,7 @@
//
// Created by sebastian on 24.12.25.
//
#include "RawModel.h"
RawModel::RawModel(GLuint vaoID, int vertexCount) : vaoID(vaoID), vertexCount(vertexCount){}

View File

@ -0,0 +1,20 @@
//
// Created by sebastian on 24.12.25.
//
#ifndef DICEWARS_SIEDLER_RAWMODEL_H
#define DICEWARS_SIEDLER_RAWMODEL_H
#include "glad/glad.h"
class RawModel {
public:
RawModel(GLuint vaoID, int vertexCount);
GLuint vaoID;
int vertexCount;
};
#endif //DICEWARS_SIEDLER_RAWMODEL_H

View File

@ -2,4 +2,10 @@
// Created by sebastian on 23.12.25.
//
#include "DicewarsApp.h"
#include "DicewarsApp.h"
#include "../engine/layer/GameLayer.h"
DicewarsApp::DicewarsApp() {
pushLayer(new GameLayer());
}

View File

@ -1,11 +1,12 @@
#include "engine/core/Application.h"
#include "game/DicewarsApp.h"
// TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
Application* CreateApplication()
{
return new Application();
return new DicewarsApp();
}
int main() {
auto app = CreateApplication();