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
#include "JGL/types/Texture.h"
#include <JGL/types/Texture.h>
using JGL::Texture;

View File

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

View File

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

View File

@@ -4,10 +4,22 @@
#include <Engine/GameWindow.h>
namespace Globals {
inline std::vector<Scene*> SceneList{};
inline Scene* CurrentScene = nullptr;
inline DemoGameWindow* Window = nullptr;
inline float DeltaTime() { return Window->GetDeltaTime(); }
inline void RemoveScene() { delete CurrentScene; CurrentScene = nullptr; }
inline void ChangeScene(Scene* scene) { delete CurrentScene; CurrentScene = scene; CurrentScene->Init(); }
inline void ChangeScene(Scene* scene) { if (CurrentScene != nullptr) CurrentScene->Unload(); CurrentScene = scene; if (CurrentScene != nullptr) 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 {
protected:
bool Paused = false;
std::string name;
Hud* HeadsUpDisplay = nullptr;
std::vector<Fixed*> FixedList{};
std::vector<Entity*> EntityList{};
@@ -16,6 +16,7 @@ public:
[[nodiscard]] bool FixedListContains(const Fixed* fixed) const;
[[nodiscard]] size_t FixedCount() const;
[[nodiscard]] size_t EntityCount() const;
[[nodiscard]] std::string GetName() const;
public:
void AppendEntity(Entity* entity);
void AppendFixed(Fixed* fixed);
@@ -28,10 +29,11 @@ public:
void RemoveEntity(Entity* entity);
void RemoveFixed(Fixed* fixed);
virtual void Unload();
virtual void Init() {}
virtual void Update();
virtual void Render();
public:
Scene() = default;
explicit Scene(const std::string& name) : name(name) {}
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
#include "Engine/Level/Scene.h"
#include <Engine/Level/Scene.h>
class DemoGameSplash final : public Scene {
class LoadingScreen final : public Scene {
protected:
float elapsed = 0.0f;
float angle = 0.0f;
Texture* RedactedSoftware = nullptr;
public:
DemoGameSplash() : Scene() {}
explicit LoadingScreen(const std::string& name) : Scene(name) {}
void Init() final;
void Update() final;
void Render() final;
~DemoGameSplash() final;
~LoadingScreen() final;
};

View File

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

View File

@@ -1,4 +1,4 @@
#include "Engine/Animation.h"
#include "Engine/Entity/Animation.h"
#include "jlog/Logger.hpp"
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 {
for (auto* e : EntityList)
@@ -47,6 +47,18 @@ Scene::~Scene() {
for (auto* e : EntityList)
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) {
@@ -84,3 +96,7 @@ void Scene::RemoveFixed(Fixed* fixed) {
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/Box.h>
void DemoGameScene::Init() {
void ControllableBox::Init() {
auto* hud = new DemoGameHud();
auto* b = new Box({0, 0});
HeadsUpDisplay = hud;
AppendEntity(b);
}

View File

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