diff --git a/CMakeLists.txt b/CMakeLists.txt index bf53e81..3d8a1b1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/src/engine/core/Application.cpp b/src/engine/core/Application.cpp index a54ecc4..da4f728 100644 --- a/src/engine/core/Application.cpp +++ b/src/engine/core/Application.cpp @@ -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(); } } diff --git a/src/engine/core/Application.h b/src/engine/core/Application.h index f9b75fe..02bca29 100644 --- a/src/engine/core/Application.h +++ b/src/engine/core/Application.h @@ -5,10 +5,13 @@ #ifndef DICEWARS_SIEDLER_APPLICATION_H #define DICEWARS_SIEDLER_APPLICATION_H #include +#include #include "Window.h" +class Layer; + class Application { public: @@ -24,6 +27,11 @@ private: std::unique_ptr window; static Application* instance; + + std::vector layers; + +protected: + void pushLayer(Layer* layer); }; diff --git a/src/engine/layer/GameLayer.cpp b/src/engine/layer/GameLayer.cpp new file mode 100644 index 0000000..e27dc16 --- /dev/null +++ b/src/engine/layer/GameLayer.cpp @@ -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 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); +} + + diff --git a/src/engine/layer/GameLayer.h b/src/engine/layer/GameLayer.h new file mode 100644 index 0000000..7b592b1 --- /dev/null +++ b/src/engine/layer/GameLayer.h @@ -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 \ No newline at end of file diff --git a/src/engine/layer/Layer.cpp b/src/engine/layer/Layer.cpp new file mode 100644 index 0000000..814eb4e --- /dev/null +++ b/src/engine/layer/Layer.cpp @@ -0,0 +1,5 @@ +// +// Created by sebastian on 24.12.25. +// + +#include "Layer.h" \ No newline at end of file diff --git a/src/engine/layer/Layer.h b/src/engine/layer/Layer.h new file mode 100644 index 0000000..ee671b9 --- /dev/null +++ b/src/engine/layer/Layer.h @@ -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 \ No newline at end of file diff --git a/src/engine/platform/glfw/GLFWWindow.cpp b/src/engine/platform/glfw/GLFWWindow.cpp index 5f7bf50..246034d 100644 --- a/src/engine/platform/glfw/GLFWWindow.cpp +++ b/src/engine/platform/glfw/GLFWWindow.cpp @@ -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(m_Data.Width), static_cast(m_Data.Height), m_Data.Title.c_str(), nullptr, nullptr); diff --git a/src/engine/renderer/Renderer.cpp b/src/engine/renderer/Renderer.cpp new file mode 100644 index 0000000..6e2033d --- /dev/null +++ b/src/engine/renderer/Renderer.cpp @@ -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); + +} diff --git a/src/engine/renderer/Renderer.h b/src/engine/renderer/Renderer.h new file mode 100644 index 0000000..4f77fe1 --- /dev/null +++ b/src/engine/renderer/Renderer.h @@ -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 \ No newline at end of file diff --git a/src/engine/renderer/model/Loader.cpp b/src/engine/renderer/model/Loader.cpp new file mode 100644 index 0000000..34821ca --- /dev/null +++ b/src/engine/renderer/model/Loader.cpp @@ -0,0 +1,49 @@ +// +// Created by sebastian on 24.12.25. +// + +#include "Loader.h" + +RawModel Loader::loadToVAO(std::vector vertices){ + GLuint vaoID = createVAO(); + storeDataInAttributeList(0, vertices); + unbindVAO(); + return {vaoID, static_cast(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 &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); +} diff --git a/src/engine/renderer/model/Loader.h b/src/engine/renderer/model/Loader.h new file mode 100644 index 0000000..3e9c452 --- /dev/null +++ b/src/engine/renderer/model/Loader.h @@ -0,0 +1,30 @@ +// +// Created by sebastian on 24.12.25. +// + +#ifndef DICEWARS_SIEDLER_LOADER_H +#define DICEWARS_SIEDLER_LOADER_H +#include + +#include "RawModel.h" +#include "glad/glad.h" + + +class Loader +{ +public: + RawModel loadToVAO(std::vector vertices); + ~Loader(); + void cleanUp(); + +private: + GLuint createVAO(); + void storeDataInAttributeList(int attributeNumber, const std::vector &data); + void unbindVAO(); + + std::vector vaoIDs; + std::vector vboIDs; +}; + + +#endif //DICEWARS_SIEDLER_LOADER_H \ No newline at end of file diff --git a/src/engine/renderer/model/RawModel.cpp b/src/engine/renderer/model/RawModel.cpp new file mode 100644 index 0000000..bac9c36 --- /dev/null +++ b/src/engine/renderer/model/RawModel.cpp @@ -0,0 +1,7 @@ +// +// Created by sebastian on 24.12.25. +// + +#include "RawModel.h" + +RawModel::RawModel(GLuint vaoID, int vertexCount) : vaoID(vaoID), vertexCount(vertexCount){} diff --git a/src/engine/renderer/model/RawModel.h b/src/engine/renderer/model/RawModel.h new file mode 100644 index 0000000..1929445 --- /dev/null +++ b/src/engine/renderer/model/RawModel.h @@ -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 \ No newline at end of file diff --git a/src/game/DicewarsApp.cpp b/src/game/DicewarsApp.cpp index 8652171..a15d92d 100644 --- a/src/game/DicewarsApp.cpp +++ b/src/game/DicewarsApp.cpp @@ -2,4 +2,10 @@ // Created by sebastian on 23.12.25. // -#include "DicewarsApp.h" \ No newline at end of file +#include "DicewarsApp.h" + +#include "../engine/layer/GameLayer.h" + +DicewarsApp::DicewarsApp() { + pushLayer(new GameLayer()); +} diff --git a/src/main.cpp b/src/main.cpp index 8a69b3e..a8f447a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,11 +1,12 @@ #include "engine/core/Application.h" +#include "game/DicewarsApp.h" // TIP To Run code, press or click the icon in the gutter. Application* CreateApplication() { - return new Application(); + return new DicewarsApp(); } int main() { auto app = CreateApplication();