From 31adb6cbc95dceaff3d7947dce5f40320f856d46 Mon Sep 17 00:00:00 2001 From: sillysagiri Date: Wed, 6 Nov 2024 16:19:36 +0700 Subject: [PATCH] move scene manager into App class --- src/Panda.hpp | 78 ++++++++++++++++++++++++++++++++++++++--------- src/core/App.cpp | 51 +++++++++++++++++++++++++++++++ src/core/Core.cpp | 51 ------------------------------- 3 files changed, 115 insertions(+), 65 deletions(-) create mode 100755 src/core/App.cpp delete mode 100755 src/core/Core.cpp diff --git a/src/Panda.hpp b/src/Panda.hpp index 89e0649..ab33b3f 100755 --- a/src/Panda.hpp +++ b/src/Panda.hpp @@ -1,17 +1,59 @@ #pragma once -#include #include -#include -#include #include "utils/Random.hpp" +struct Vector2; +struct Color; +struct Rectangle; struct Texture; struct RenderTexture; -struct Color; -struct Vector2; -struct Rectangle; +struct Camera2D; + +#ifdef PANDA_INCLUDE_TYPE + +struct Vector2 { + float x; + float y; +}; + +struct Color { + unsigned char r; + unsigned char g; + unsigned char b; + unsigned char a; +}; + +struct Rectangle { + float x; + float y; + float width; + float height; +} + +struct Texture { + unsigned int id; + int width; + int height; + int mipmaps; + int format; +} + +struct RenderTexture { + unsigned int id; + Texture texture; + Texture depth; +} + +struct Camera2D { + Vector2 offset; + Vector2 target; + float rotation; + float zoom; +} + +#endif namespace panda { @@ -29,7 +71,10 @@ namespace panda public: Scene() {} virtual ~Scene() {} - virtual void update() {} + virtual void Preload() {} + virtual void Update() {} + + bool preloaded = false; }; struct Sprite { @@ -73,15 +118,20 @@ namespace panda void concat(Matrix m); }; + class App { + public: + void CreateWindow(int w, int h, const char *str); + + void AppUpdate(); + void SetScene(Scene* scene); + Scene* GetScene(); + + private: + Scene* _scene_current = nullptr; + }; + // ############################################################################################ - // Core - void CreateWindow(int w, int h, const char *str); - - void AppUpdate(); - void SetScene(Scene* scene); - Scene* GetScene(); - // # Graphic // ### GUI diff --git a/src/core/App.cpp b/src/core/App.cpp new file mode 100755 index 0000000..7f54449 --- /dev/null +++ b/src/core/App.cpp @@ -0,0 +1,51 @@ +#include "Panda.hpp" + + +#ifdef PANDA_BACKEND_RAYLIB +#include + +void panda::App::CreateWindow(int w, int h, const char *str) +{ + SetConfigFlags( + FLAG_MSAA_4X_HINT | FLAG_WINDOW_RESIZABLE); + + #if PLATFORM_ANDROID + InitWindow(0, 0, str); + #elif PLATFORM_DESKTOP + InitWindow(w, h, str); + SetWindowMinSize(w*0.8f, h*0.8f); + #endif + + SetExitKey(-1); +} +#endif + +void panda::App::AppUpdate() +{ + if (_scene_current != nullptr) + { + while (!_scene_current->preloaded) + { + _scene_current->preloaded = true; + _scene_current->Preload(); + } + + _scene_current->Update(); + } +} + +void panda::App::SetScene(Scene* scene) +{ + if (_scene_current != nullptr) + { + delete _scene_current; + _scene_current = nullptr; + } + + _scene_current = scene; +} + +panda::Scene* panda::App::GetScene() +{ + return _scene_current; +} \ No newline at end of file diff --git a/src/core/Core.cpp b/src/core/Core.cpp deleted file mode 100755 index 7cf9f9f..0000000 --- a/src/core/Core.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include - -#include "Panda.hpp" - -static panda::Scene* currentScene = nullptr; - -// CORE -void panda::CreateWindow(int w, int h, const char *str) -{ - Random::getInstance().setSeedFromTime(); - - SetConfigFlags( - FLAG_MSAA_4X_HINT | FLAG_WINDOW_RESIZABLE); - - #if PLATFORM_ANDROID - InitWindow(0, 0, str); - #elif PLATFORM_DESKTOP - InitWindow(w, h, str); - SetWindowMinSize(w*0.8f, h*0.8f); - #endif - - SetExitKey(-1); -} - -void panda::AppUpdate() -{ - if (currentScene != nullptr) - currentScene->update(); -} - -void panda::SetScene(Scene* scene) -{ - if (currentScene != nullptr) - { - delete currentScene; - currentScene = nullptr; - } - - currentScene = scene; -} - -panda::Scene* panda::GetScene() -{ - return currentScene; -} - -// UTILS -panda::Random &panda::GetDefaultRandom() -{ - return Random::getInstance(); -} \ No newline at end of file