From e4bd77197ad4e1dba799c5117b0979b67ae2911b Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Fri, 21 May 2021 12:38:10 +0200 Subject: [PATCH] singleton for window --- .gitignore | 4 +++- lib/Ray/.gitignore | 4 ++-- lib/Ray/sources/Canvas.hpp | 2 +- lib/Ray/sources/Drawables/ADrawable2D.hpp | 2 +- lib/Ray/sources/Drawables/IDrawable.hpp | 2 +- lib/Ray/sources/Drawables/Texture.cpp | 6 ++++++ lib/Ray/sources/Drawables/Texture.hpp | 2 +- lib/Ray/sources/Window.cpp | 9 ++++++++- lib/Ray/sources/Window.hpp | 12 +++++++----- lib/wal/CMakeLists.txt | 4 ++-- sources/main.cpp | 24 ++++++++++++++--------- 11 files changed, 47 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index e27ebc19..42cd6b29 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ .idea cmake-build-debug ./bomberman -.vscode \ No newline at end of file +.vscode +build/* +docs/* \ No newline at end of file diff --git a/lib/Ray/.gitignore b/lib/Ray/.gitignore index c4ec047b..3fcc3d08 100644 --- a/lib/Ray/.gitignore +++ b/lib/Ray/.gitignore @@ -1,2 +1,2 @@ -build/ -docs/ \ No newline at end of file +build/* +docs/* \ No newline at end of file diff --git a/lib/Ray/sources/Canvas.hpp b/lib/Ray/sources/Canvas.hpp index 8fba2b93..81af1418 100644 --- a/lib/Ray/sources/Canvas.hpp +++ b/lib/Ray/sources/Canvas.hpp @@ -18,7 +18,7 @@ namespace RAY { virtual ~Canvas() = default; //! @brief draw drawable - virtual void draw(const Drawables::IDrawable &) = 0; + virtual void draw(Drawables::IDrawable &) = 0; protected: private: diff --git a/lib/Ray/sources/Drawables/ADrawable2D.hpp b/lib/Ray/sources/Drawables/ADrawable2D.hpp index afa873c6..63a5dd72 100644 --- a/lib/Ray/sources/Drawables/ADrawable2D.hpp +++ b/lib/Ray/sources/Drawables/ADrawable2D.hpp @@ -31,7 +31,7 @@ namespace RAY::Drawables::Drawables2D { ADrawable2D(const ADrawable2D &) = default; //! @brief A default destructor - virtual ~ADrawable2D() = 0; + virtual ~ADrawable2D() = default; //! @return the top-left position of the ADrawable const Vector2 &getPosition(void) const; diff --git a/lib/Ray/sources/Drawables/IDrawable.hpp b/lib/Ray/sources/Drawables/IDrawable.hpp index 19382eb1..c0187aff 100644 --- a/lib/Ray/sources/Drawables/IDrawable.hpp +++ b/lib/Ray/sources/Drawables/IDrawable.hpp @@ -18,7 +18,7 @@ namespace RAY namespace Drawables { class IDrawable { public: - virtual ~IDrawable() = 0; + virtual ~IDrawable() = default; virtual void drawOn(RAY::Window &window) = 0; protected: diff --git a/lib/Ray/sources/Drawables/Texture.cpp b/lib/Ray/sources/Drawables/Texture.cpp index c108f51c..01ae2eff 100644 --- a/lib/Ray/sources/Drawables/Texture.cpp +++ b/lib/Ray/sources/Drawables/Texture.cpp @@ -34,6 +34,12 @@ bool RAY::Texture::load(const std::string &filename) return true; } +bool RAY::Texture::unload() +{ + UnloadTexture(this->_texture); + return true; +} + Image RAY::Texture::toImage(void) const { return GetTextureData(_texture); diff --git a/lib/Ray/sources/Drawables/Texture.hpp b/lib/Ray/sources/Drawables/Texture.hpp index 73d940cc..e81b8437 100644 --- a/lib/Ray/sources/Drawables/Texture.hpp +++ b/lib/Ray/sources/Drawables/Texture.hpp @@ -42,7 +42,7 @@ namespace RAY bool load(const std::string &filename); //! @brief unload ressources - bool unload(); + bool unload() override; //! @brief get image ::Image toImage(void) const; diff --git a/lib/Ray/sources/Window.cpp b/lib/Ray/sources/Window.cpp index a3987f35..eb40618d 100644 --- a/lib/Ray/sources/Window.cpp +++ b/lib/Ray/sources/Window.cpp @@ -8,7 +8,14 @@ #include "Window.hpp" #include "Controllers/Mouse.hpp" -RAY::Window::Window(int width, int height, const std::string title, bool openNow): +RAY::Window &RAY::Window::getInstance(int width, int height, const std::string &title, bool openNow) +{ + static RAY::Window window(width, height, title, openNow); + + return window; +} + +RAY::Window::Window(int width, int height, const std::string &title, bool openNow): _dimensions({(float)width, (float)height}), _title(title), _isOpen(openNow) { if (openNow) diff --git a/lib/Ray/sources/Window.hpp b/lib/Ray/sources/Window.hpp index 04874cc8..9e67182e 100644 --- a/lib/Ray/sources/Window.hpp +++ b/lib/Ray/sources/Window.hpp @@ -22,14 +22,14 @@ namespace RAY { class Window: public Canvas { public: - //! @brief Creates window, and opens it if openNow is set to true - Window(int width, int height, const std::string title, bool openNow = false); - + //! @return A widow insta,ce. Only one window can be open at a time + static Window &getInstance(int width, int height, const std::string &title, bool openNow = true); + //! @brief A default copy constructor - Window(const Window &window) = default; + Window(const Window &window) = delete; //! @brief A window is assignable - Window &operator=(const Window &window) = default; + Window &operator=(const Window &window) = delete; //! @brief Closes window if still open ~Window() = default; @@ -108,6 +108,8 @@ namespace RAY { void draw(const Mesh &mesh, const Material &material, const Matrix &transform); private: + //! @brief Creates window, and opens it if openNow is set to true + Window(int width, int height, const std::string &title, bool openNow = true); //! @brief Dimension of window RAY::Vector2 _dimensions; diff --git a/lib/wal/CMakeLists.txt b/lib/wal/CMakeLists.txt index 81d730f2..fb2c1088 100644 --- a/lib/wal/CMakeLists.txt +++ b/lib/wal/CMakeLists.txt @@ -38,5 +38,5 @@ add_executable(wal_tests EXCLUDE_FROM_ALL ) target_link_libraries(wal_tests PRIVATE wal) -find_package(Catch2 REQUIRED) -target_link_libraries(wal_tests PRIVATE Catch2::Catch2) \ No newline at end of file +#find_package(Catch2 REQUIRED) +#target_link_libraries(wal_tests PRIVATE Catch2::Catch2) \ No newline at end of file diff --git a/sources/main.cpp b/sources/main.cpp index 274ba73f..575e420d 100644 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -1,6 +1,7 @@ #include -#include "Wal.hpp" #include "Window.hpp" +#include "Drawables/2D/Text.hpp" +#include "Drawables/2D/Circle.hpp" int main() { @@ -9,13 +10,17 @@ int main() const int screenWidth = 800; const int screenHeight = 450; - InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window"); + RAY::Window &window = RAY::Window::getInstance(screenWidth, screenHeight, "Ta mère en slip", false); + RAY::Drawables::Drawables2D::Text text("Hello World", 10, {190, 200}, RED); + RAY::Drawables::Drawables2D::Circle circle(400, 225, 50, RED); - SetTargetFPS(60); // Set our game to run at 60 frames-per-second + window.open(); + + window.setFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop - while (!WindowShouldClose()) // Detect window close button or ESC key + while (!window.shouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- @@ -24,19 +29,20 @@ int main() // Draw //---------------------------------------------------------------------------------- - BeginDrawing(); + window.beginDrawing(); - ClearBackground(RAYWHITE); + window.clear(); - DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY); + window.draw(circle); + window.draw(text); - EndDrawing(); + window.endDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- - CloseWindow(); // Close window and OpenGL context + window.close(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0;