Core Rendering Logic
This commit is contained in:
parent
cbfeea220e
commit
017bf6057a
@ -23,7 +23,17 @@ add_executable(Dicewars_Siedler src/main.cpp
|
|||||||
src/engine/core/Application.cpp
|
src/engine/core/Application.cpp
|
||||||
src/engine/core/Application.h
|
src/engine/core/Application.h
|
||||||
src/game/DicewarsApp.cpp
|
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
|
target_include_directories(Dicewars_Siedler PRIVATE
|
||||||
lib/glfw/include
|
lib/glfw/include
|
||||||
|
|||||||
@ -4,8 +4,15 @@
|
|||||||
|
|
||||||
#include "Application.h"
|
#include "Application.h"
|
||||||
|
|
||||||
|
#include "../layer/Layer.h"
|
||||||
|
|
||||||
Application* Application::instance = nullptr;
|
Application* Application::instance = nullptr;
|
||||||
|
|
||||||
|
void Application::pushLayer(Layer* layer) {
|
||||||
|
layers.push_back(layer);
|
||||||
|
layer->onAttach();
|
||||||
|
}
|
||||||
|
|
||||||
Application::Application()
|
Application::Application()
|
||||||
{
|
{
|
||||||
instance = this;
|
instance = this;
|
||||||
@ -26,7 +33,10 @@ Application::~Application()
|
|||||||
void Application::run() {
|
void Application::run() {
|
||||||
while (!window->shouldClose())
|
while (!window->shouldClose())
|
||||||
{
|
{
|
||||||
//Rendering
|
for (Layer* layer : layers)
|
||||||
|
{
|
||||||
|
layer->onUpdate();
|
||||||
|
}
|
||||||
window->OnUpdate();
|
window->OnUpdate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,10 +5,13 @@
|
|||||||
#ifndef DICEWARS_SIEDLER_APPLICATION_H
|
#ifndef DICEWARS_SIEDLER_APPLICATION_H
|
||||||
#define DICEWARS_SIEDLER_APPLICATION_H
|
#define DICEWARS_SIEDLER_APPLICATION_H
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "Window.h"
|
#include "Window.h"
|
||||||
|
|
||||||
|
|
||||||
|
class Layer;
|
||||||
|
|
||||||
class Application
|
class Application
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -24,6 +27,11 @@ private:
|
|||||||
std::unique_ptr<Window> window;
|
std::unique_ptr<Window> window;
|
||||||
|
|
||||||
static Application* instance;
|
static Application* instance;
|
||||||
|
|
||||||
|
std::vector<Layer*> layers;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void pushLayer(Layer* layer);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
37
src/engine/layer/GameLayer.cpp
Normal file
37
src/engine/layer/GameLayer.cpp
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
28
src/engine/layer/GameLayer.h
Normal file
28
src/engine/layer/GameLayer.h
Normal 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
|
||||||
5
src/engine/layer/Layer.cpp
Normal file
5
src/engine/layer/Layer.cpp
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
//
|
||||||
|
// Created by sebastian on 24.12.25.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "Layer.h"
|
||||||
18
src/engine/layer/Layer.h
Normal file
18
src/engine/layer/Layer.h
Normal 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
|
||||||
@ -42,7 +42,7 @@ void GLFWWindow::Init(const WindowProps& props)
|
|||||||
|
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
|
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(
|
m_Window = glfwCreateWindow(
|
||||||
static_cast<int>(m_Data.Width), static_cast<int>(m_Data.Height), m_Data.Title.c_str(), nullptr, nullptr);
|
static_cast<int>(m_Data.Width), static_cast<int>(m_Data.Height), m_Data.Title.c_str(), nullptr, nullptr);
|
||||||
|
|||||||
26
src/engine/renderer/Renderer.cpp
Normal file
26
src/engine/renderer/Renderer.cpp
Normal 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);
|
||||||
|
|
||||||
|
}
|
||||||
19
src/engine/renderer/Renderer.h
Normal file
19
src/engine/renderer/Renderer.h
Normal 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
|
||||||
49
src/engine/renderer/model/Loader.cpp
Normal file
49
src/engine/renderer/model/Loader.cpp
Normal 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);
|
||||||
|
}
|
||||||
30
src/engine/renderer/model/Loader.h
Normal file
30
src/engine/renderer/model/Loader.h
Normal 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
|
||||||
7
src/engine/renderer/model/RawModel.cpp
Normal file
7
src/engine/renderer/model/RawModel.cpp
Normal 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){}
|
||||||
20
src/engine/renderer/model/RawModel.h
Normal file
20
src/engine/renderer/model/RawModel.h
Normal 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
|
||||||
@ -3,3 +3,9 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
#include "DicewarsApp.h"
|
#include "DicewarsApp.h"
|
||||||
|
|
||||||
|
#include "../engine/layer/GameLayer.h"
|
||||||
|
|
||||||
|
DicewarsApp::DicewarsApp() {
|
||||||
|
pushLayer(new GameLayer());
|
||||||
|
}
|
||||||
|
|||||||
@ -1,11 +1,12 @@
|
|||||||
|
|
||||||
|
|
||||||
#include "engine/core/Application.h"
|
#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.
|
// TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
|
||||||
|
|
||||||
Application* CreateApplication()
|
Application* CreateApplication()
|
||||||
{
|
{
|
||||||
return new Application();
|
return new DicewarsApp();
|
||||||
}
|
}
|
||||||
int main() {
|
int main() {
|
||||||
auto app = CreateApplication();
|
auto app = CreateApplication();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user