Better scene management.

This commit is contained in:
2025-01-02 12:57:21 -05:00
parent b9afc57e6e
commit 30bdd66086
13 changed files with 78 additions and 47 deletions

View File

@@ -1,6 +1,6 @@
#pragma once #pragma once
#include "JGL/types/Texture.h" #include <JGL/types/Texture.h>
using JGL::Texture; using JGL::Texture;

View File

@@ -1,5 +1,5 @@
#pragma once #pragma once
#include "J3ML/LinearAlgebra/Vector2.hpp" #include <J3ML/LinearAlgebra/Vector2.hpp>
#include <vector> #include <vector>
class Entity { class Entity {

View File

@@ -1,7 +1,7 @@
#pragma once #pragma once
#include "Entity.h" #include <Engine/Entity/Entity.h>
#include "JGL/JGL.h" #include <JGL/JGL.h>
class Renderable : public Entity { class Renderable : public Entity {
public: public:

View File

@@ -4,10 +4,22 @@
#include <Engine/GameWindow.h> #include <Engine/GameWindow.h>
namespace Globals { namespace Globals {
inline std::vector<Scene*> SceneList{};
inline Scene* CurrentScene = nullptr; inline Scene* CurrentScene = nullptr;
inline DemoGameWindow* Window = nullptr; inline DemoGameWindow* Window = nullptr;
inline float DeltaTime() { return Window->GetDeltaTime(); } inline float DeltaTime() { return Window->GetDeltaTime(); }
inline void RemoveScene() { delete CurrentScene; CurrentScene = nullptr; } inline void ChangeScene(Scene* scene) { if (CurrentScene != nullptr) CurrentScene->Unload(); CurrentScene = scene; if (CurrentScene != nullptr) CurrentScene->Init(); }
inline void ChangeScene(Scene* scene) { delete CurrentScene; CurrentScene = scene; CurrentScene->Init(); } inline bool ChangeScene (const std::string& scene_name)
{
Scene* scene = nullptr;
for (auto* s : SceneList)
if (s->GetName() == scene_name)
scene = s;
if (scene)
ChangeScene(scene);
return scene;
}
} }

View File

@@ -7,7 +7,7 @@
class Scene { class Scene {
protected: protected:
bool Paused = false; std::string name;
Hud* HeadsUpDisplay = nullptr; Hud* HeadsUpDisplay = nullptr;
std::vector<Fixed*> FixedList{}; std::vector<Fixed*> FixedList{};
std::vector<Entity*> EntityList{}; std::vector<Entity*> EntityList{};
@@ -16,6 +16,7 @@ public:
[[nodiscard]] bool FixedListContains(const Fixed* fixed) const; [[nodiscard]] bool FixedListContains(const Fixed* fixed) const;
[[nodiscard]] size_t FixedCount() const; [[nodiscard]] size_t FixedCount() const;
[[nodiscard]] size_t EntityCount() const; [[nodiscard]] size_t EntityCount() const;
[[nodiscard]] std::string GetName() const;
public: public:
void AppendEntity(Entity* entity); void AppendEntity(Entity* entity);
void AppendFixed(Fixed* fixed); void AppendFixed(Fixed* fixed);
@@ -28,10 +29,11 @@ public:
void RemoveEntity(Entity* entity); void RemoveEntity(Entity* entity);
void RemoveFixed(Fixed* fixed); void RemoveFixed(Fixed* fixed);
virtual void Unload();
virtual void Init() {} virtual void Init() {}
virtual void Update(); virtual void Update();
virtual void Render(); virtual void Render();
public: public:
Scene() = default; explicit Scene(const std::string& name) : name(name) {}
virtual ~Scene(); virtual ~Scene();
}; };

View File

@@ -0,0 +1,11 @@
#pragma once
#include <Engine/Level/Scene.h>
class ControllableBox final : public Scene {
public:
void Init() final;
public:
explicit ControllableBox(const std::string& name) : Scene(name) {}
};

View File

@@ -1,12 +0,0 @@
#pragma once
#include "Engine/Level/Scene.h"
class DemoGameScene final : public Scene {
public:
void Init() final;
public:
DemoGameScene() = default;
};

View File

@@ -1,16 +1,16 @@
#pragma once #pragma once
#include "Engine/Level/Scene.h" #include <Engine/Level/Scene.h>
class DemoGameSplash final : public Scene { class LoadingScreen final : public Scene {
protected: protected:
float elapsed = 0.0f; float elapsed = 0.0f;
float angle = 0.0f; float angle = 0.0f;
Texture* RedactedSoftware = nullptr; Texture* RedactedSoftware = nullptr;
public: public:
DemoGameSplash() : Scene() {} explicit LoadingScreen(const std::string& name) : Scene(name) {}
void Init() final; void Init() final;
void Update() final; void Update() final;
void Render() final; void Render() final;
~DemoGameSplash() final; ~LoadingScreen() final;
}; };

View File

@@ -1,12 +1,18 @@
#include "Engine/Globals.h" #include <Engine/Globals.h>
#include <Engine/Level/Scene.h>
#include <rewindow/logger/logger.h> #include <rewindow/logger/logger.h>
#include "Game/Scene/Splash.h" #include <Game/Scene/ControllableBox.h>
#include <Game/Scene/Loading.h>
Scene* scene = nullptr;
using namespace JGL; using namespace JGL;
void CreateScenes() {
auto ld = new LoadingScreen("LoadingScreen");
Globals::SceneList.push_back(ld);
auto cb = new ControllableBox("Scene0");
Globals::SceneList.push_back(cb);
};
int main() { int main() {
Globals::Window = new DemoGameWindow("Demo Game", 1024, 896); Globals::Window = new DemoGameWindow("Demo Game", 1024, 896);
Globals::Window->SetRenderer(RenderingAPI::OPENGL); Globals::Window->SetRenderer(RenderingAPI::OPENGL);
@@ -18,8 +24,8 @@ int main() {
ReWindow::Logger::Warning.EnableConsole(false); ReWindow::Logger::Warning.EnableConsole(false);
ReWindow::Logger::Debug.EnableConsole(false); ReWindow::Logger::Debug.EnableConsole(false);
auto* splash = new DemoGameSplash(); CreateScenes();
Globals::ChangeScene(splash); Globals::ChangeScene("LoadingScreen");
while (Globals::Window->IsAlive()) while (Globals::Window->IsAlive())
Globals::Window->ManagedRefresh(); Globals::Window->ManagedRefresh();

View File

@@ -1,4 +1,4 @@
#include "Engine/Animation.h" #include "Engine/Entity/Animation.h"
#include "jlog/Logger.hpp" #include "jlog/Logger.hpp"
float Animation::GetMsBetweenFrames() const { float Animation::GetMsBetweenFrames() const {

View File

@@ -1,4 +1,4 @@
#include "Engine/Level/Scene.h" #include <Engine/Level/Scene.h>
bool Scene::EntityListContains(const Entity* entity) const { bool Scene::EntityListContains(const Entity* entity) const {
for (auto* e : EntityList) for (auto* e : EntityList)
@@ -47,6 +47,18 @@ Scene::~Scene() {
for (auto* e : EntityList) for (auto* e : EntityList)
delete e; delete e;
delete HeadsUpDisplay;
}
void Scene::Unload() {
for (auto* f : FixedList)
delete f;
for (auto* e : EntityList)
delete e;
delete HeadsUpDisplay;
} }
void Scene::AppendEntity(Entity* entity) { void Scene::AppendEntity(Entity* entity) {
@@ -84,3 +96,7 @@ void Scene::RemoveFixed(Fixed* fixed) {
FixedList.erase(it); FixedList.erase(it);
} }
std::string Scene::GetName() const {
return name;
}

View File

@@ -1,11 +1,10 @@
#include <Game/Scene/DemoGameScene.h> #include <Game/Scene/ControllableBox.h>
#include <Game/Entity/DemoGameHud.h> #include <Game/Entity/DemoGameHud.h>
#include <Game/Entity/Box.h> #include <Game/Entity/Box.h>
void DemoGameScene::Init() { void ControllableBox::Init() {
auto* hud = new DemoGameHud(); auto* hud = new DemoGameHud();
auto* b = new Box({0, 0}); auto* b = new Box({0, 0});
HeadsUpDisplay = hud; HeadsUpDisplay = hud;
AppendEntity(b); AppendEntity(b);
} }

View File

@@ -1,23 +1,20 @@
#include "Game/Scene/Splash.h" #include <Game/Scene/Loading.h>
#include "Engine/Globals.h" #include <Engine/Globals.h>
#include "Game/Scene/DemoGameScene.h"
void DemoGameSplash::Init() { void LoadingScreen::Init() {
RedactedSoftware = new JGL::Texture("assets/sprites/Re3D.png"); RedactedSoftware = new JGL::Texture("assets/sprites/Re3D.png");
} }
void DemoGameSplash::Update() { void LoadingScreen::Update() {
angle += (elapsed * 4) * Globals::DeltaTime(); angle += (elapsed * 4) * Globals::DeltaTime();
elapsed += Globals::DeltaTime(); elapsed += Globals::DeltaTime();
// Pretend we're actually loading something idk. // Pretend we're actually loading something idk.
if (elapsed >= 2.75) { if (elapsed >= 2.75)
auto* dgs = new DemoGameScene(); Globals::ChangeScene("Scene0");
Globals::ChangeScene(dgs);
}
} }
void DemoGameSplash::Render() { void LoadingScreen::Render() {
auto text_length = JGL::Fonts::Jupiteroid.MeasureString("Loading ....", 24); auto text_length = JGL::Fonts::Jupiteroid.MeasureString("Loading ....", 24);
std::string text = "Loading"; std::string text = "Loading";
int dots = static_cast<int>(floor(elapsed / 0.35)) % 4; int dots = static_cast<int>(floor(elapsed / 0.35)) % 4;
@@ -31,7 +28,7 @@ void DemoGameSplash::Render() {
} }
DemoGameSplash::~DemoGameSplash() { LoadingScreen::~LoadingScreen() {
delete RedactedSoftware; delete RedactedSoftware;
} }