ADD: Introduce Scenes

This commit is contained in:
sebastian 2026-07-30 07:42:56 +02:00
parent c1732e01fa
commit 6e37fda356
12 changed files with 213 additions and 17 deletions

View File

@ -101,6 +101,8 @@ add_library(Engine STATIC
engine/src/utils/openglWrapper/shader/Uniform.h
engine/src/utils/openglWrapper/shader/UniformValue.cpp
engine/src/utils/openglWrapper/shader/UniformValue.h
engine/src/layer/Scene.cpp
engine/src/layer/Scene.h
)
target_include_directories(Engine PUBLIC engine/src)
target_link_libraries(Engine PUBLIC OpenGL::GL glfw glad glm::glm imgui)
@ -140,6 +142,10 @@ add_executable(ColorRace
engine/src/layer/Layer.h
game/src/GameLayer.cpp
game/src/GameLayer.h
engine/src/layer/SceneManager.cpp
engine/src/layer/SceneManager.h
game/src/GameScene.cpp
game/src/GameScene.h
)

View File

@ -0,0 +1,26 @@
//
// Created by sebastian on 30.07.26.
//
#include "Scene.h"
#include <iostream>
#include <ostream>
void engine::Scene::onUpdate() {
for (const auto& layer : m_layers) {
layer->onUpdate();
}
}
void engine::Scene::onRender() {
for (const auto& layer : m_layers) {
layer->onRender();
}
}
void engine::Scene::addLayer(std::unique_ptr<Layer> layer) {
layer->onAttach();
m_layers.push_back(std::move(layer));
}

38
engine/src/layer/Scene.h Normal file
View File

@ -0,0 +1,38 @@
//
// Created by sebastian on 30.07.26.
//
#ifndef COLORRACE_SCENE_H
#define COLORRACE_SCENE_H
#include <memory>
#include <vector>
#include "Layer.h"
#include "renderer/entities/Camera.h"
namespace engine {
class Scene {
public:
explicit Scene() {}
virtual ~Scene() = default;
virtual void onEnter() {}
virtual void onExit() {
for (const auto& layer : m_layers) {
layer->onDetach();
}
}
void onUpdate();
void onRender();
protected:
std::vector<std::unique_ptr<Layer>> m_layers;
void addLayer(std::unique_ptr<Layer> layer);
engine::EntityRegistry entityRegistry;
};
}
#endif //COLORRACE_SCENE_H

View File

@ -0,0 +1,51 @@
//
// Created by sebastian on 30.07.26.
//
#include "SceneManager.h"
#include <iostream>
#include <ostream>
void engine::SceneManager::switchTo(std::unique_ptr<Scene> scene, std::function<void()> onSceneLoaded) {
onSceneLoaded = std::move(onSceneLoaded);
nextScene = std::move(scene);
//Todo: Integrate Asset Loader
}
void engine::SceneManager::update() {
if (nextScene) {
//Todo: AssetLoader should process AssetQueue
if (getTransitionProgress() == 1.0) { // Todo: Replace with AssetLoader.getProgress.done()
if (currentScene) {
currentScene->onExit();
}
currentScene = std::move(nextScene);
nextScene = nullptr;
currentScene->onEnter();
// Todo: Stop AssetLoader
if (onSceneLoaded) {
onSceneLoaded();
onSceneLoaded = nullptr;
}
}
}
if (currentScene) {
currentScene->onUpdate();
}
}
void engine::SceneManager::render() const {
if (currentScene) {
currentScene->onRender();
}
}
float engine::SceneManager::getTransitionProgress() const {
return 1.0f;
}

View File

@ -0,0 +1,31 @@
//
// Created by sebastian on 30.07.26.
//
#ifndef COLORRACE_SCENEMANAGER_H
#define COLORRACE_SCENEMANAGER_H
#include <memory>
#include "Scene.h"
namespace engine {
class SceneManager {
public:
SceneManager() = default;
void switchTo(std::unique_ptr<Scene> scene, std::function<void()> onSceneLoaded = nullptr);
void update();
void render() const;
~SceneManager() = default;
[[nodiscard]] float getTransitionProgress() const;
private:
std::unique_ptr<Scene> currentScene;
std::unique_ptr<Scene> nextScene;
bool isTransitioning = false;
std::function<void()> onSceneLoaded;
};
}
#endif //COLORRACE_SCENEMANAGER_H

View File

@ -4,7 +4,9 @@
#ifndef COLORRACE_RENDERQUEUE_H
#define COLORRACE_RENDERQUEUE_H
#include <iostream>
#include <memory>
#include <ostream>
#include "glm/glm.hpp"
#include "model/RawModel.h"

View File

@ -12,9 +12,9 @@
void ColorRaceApp::onUpdate(float deltaTime) {
gameLayer->onUpdate();
sceneManager->update();
}
void ColorRaceApp::onRender() {
gameLayer->onRender();
sceneManager->render();
}

View File

@ -5,10 +5,12 @@
#ifndef COLORRACE_COLORRACEAPP_H
#define COLORRACE_COLORRACEAPP_H
#include "GameLayer.h"
#include "GameScene.h"
#include "core/Application.h"
#include "ecs/EntityRegistry.h"
#include "ecs/standardComponents/MeshComponent.h"
#include "ecs/standardComponents/TransformComponent.h"
#include "layer/SceneManager.h"
#include "renderer/MeshRenderer.h"
#include "renderer/entities/Camera.h"
#include "renderer/primitives/CubeFactory.h"
@ -31,13 +33,11 @@ public:
config.debugContext = true;
return config;
}
ColorRaceApp() : Application(makeConfig()) {
gameLayer = std::make_unique<GameLayer>();
gameLayer->onAttach();
ColorRaceApp() : Application(makeConfig()), sceneManager(std::make_unique<engine::SceneManager>()) {
sceneManager->switchTo(std::make_unique<GameScene>());
}
private:
std::unique_ptr<GameLayer> gameLayer;
std::unique_ptr<engine::SceneManager> sceneManager;
protected:
void onUpdate(float deltaTime) override;
void onRender() override;

View File

@ -4,6 +4,9 @@
#include "GameLayer.h"
#include <iostream>
#include <ostream>
#include "ecs/standardComponents/MeshComponent.h"
#include "ecs/standardComponents/TransformComponent.h"
#include "renderer/MeshRenderer.h"
@ -14,11 +17,6 @@ void GameLayer::onAttach() {
m_renderState.depthTesting = false;
m_renderState.backfaceCulling = false;
m_camera = std::make_unique<engine::Camera>();
m_camera->setPosition({0.0f, 0.0f, 5.0f});
m_camera->setYaw(0.0f);
m_camera->setPitch(0.0f);
m_renderer = std::make_unique<engine::MeshRenderer>();
Entity entity = m_entityRegistry.createEntity();
@ -45,7 +43,5 @@ void GameLayer::onRender() {
MeshRenderSystem meshRenderSystem;
meshRenderSystem.update(m_entityRegistry, m_renderQueue);
MeshRenderer renderer;
renderer.render(m_renderQueue, *m_camera);
m_renderer->render(m_renderQueue, *m_camera);
}

View File

@ -4,6 +4,8 @@
#ifndef COLORRACE_GAMELAYER_H
#define COLORRACE_GAMELAYER_H
#include <utility>
#include "ecs/EntityRegistry.h"
#include "layer/Layer.h"
#include "renderer/MeshRenderer.h"
@ -13,6 +15,8 @@
class GameLayer: public engine::Layer {
public:
GameLayer(engine::EntityRegistry& entityRegistry, std::shared_ptr<engine::Camera> cam)
: m_entityRegistry(entityRegistry), m_camera(std::move(cam)), m_renderer(std::make_unique<engine::MeshRenderer>()) {}
void onAttach() override;
void onDetach() override;
void onUpdate() override;
@ -20,9 +24,9 @@ public:
private:
engine::GLRenderState m_renderState;
engine::RenderQueue m_renderQueue;
engine::EntityRegistry m_entityRegistry;
engine::EntityRegistry& m_entityRegistry;
std::shared_ptr<engine::Camera> m_camera;
std::unique_ptr<engine::MeshRenderer> m_renderer;
std::unique_ptr<engine::Camera> m_camera;
};
#endif //COLORRACE_GAMELAYER_H

21
game/src/GameScene.cpp Normal file
View File

@ -0,0 +1,21 @@
//
// Created by sebastian on 30.07.26.
//
#include "GameScene.h"
#include "GameLayer.h"
GameScene::GameScene() : Scene(), m_camera(std::make_unique<engine::Camera>()) {}
void GameScene::onEnter() {
addLayer(std::make_unique<GameLayer>(entityRegistry, m_camera));
m_camera->setPosition({0.0f, 0.0f, 5.0f});
m_camera->setYaw(0.0f);
m_camera->setPitch(0.0f);
}
void GameScene::onExit() {
Scene::onExit();
}

21
game/src/GameScene.h Normal file
View File

@ -0,0 +1,21 @@
//
// Created by sebastian on 30.07.26.
//
#ifndef COLORRACE_GAMESCENE_H
#define COLORRACE_GAMESCENE_H
#include "layer/Scene.h"
class GameScene : public engine::Scene {
public:
GameScene();
void onEnter() override;
void onExit() override;
private:
std::shared_ptr<engine::Camera> m_camera;
};
#endif //COLORRACE_GAMESCENE_H