diff options
author | JAROWMR <jarorutjes07@gmail.com> | 2024-12-01 23:05:27 +0100 |
---|---|---|
committer | JAROWMR <jarorutjes07@gmail.com> | 2024-12-01 23:05:27 +0100 |
commit | b6fdd1d644368226054094a282f5db24b31b5ba2 (patch) | |
tree | f995c792d6b8330a7880dada88d470bd357279e7 | |
parent | cbd4b97d348c46f4f43fe59683a5e3d1bdbc500f (diff) | |
parent | 647eb8e318f1ed1e3ec18505ea4df57025e6ffd5 (diff) |
merge with master
39 files changed, 624 insertions, 281 deletions
diff --git a/asset/texture/test_ap43.png b/asset/texture/test_ap43.png Binary files differnew file mode 100644 index 0000000..e758ed7 --- /dev/null +++ b/asset/texture/test_ap43.png diff --git a/contributing.md b/contributing.md index 0a90e86..b0f623b 100644 --- a/contributing.md +++ b/contributing.md @@ -17,6 +17,8 @@ that you can click on to open them. working/compiling version of the project - Pull requests for new code include either automated tests for the new code or an explanation as to why the code can not (reliably) be tested +- Non-bugfix pull requests must be approved by at least 2 reviewers before being + merged <!-- - TODO: tagging / versions diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index e4de027..5b73009 100644 --- a/src/crepe/ComponentManager.cpp +++ b/src/crepe/ComponentManager.cpp @@ -2,6 +2,7 @@ #include "util/Log.h" #include "ComponentManager.h" +#include "types.h" using namespace crepe; using namespace std; @@ -10,24 +11,50 @@ ComponentManager::ComponentManager() { dbg_trace(); } ComponentManager::~ComponentManager() { dbg_trace(); } void ComponentManager::delete_all_components_of_id(game_object_id_t id) { + // Do not delete persistent objects + if (this->persistent[id]) { + return; + } + // Loop through all the types (in the unordered_map<>) - for (auto & [type, componentArray] : this->components) { + for (auto & [type, component_array] : this->components) { // Make sure that the id (that we are looking for) is within the boundaries of the vector<> - if (id < componentArray.size()) { + if (id < component_array.size()) { // Clear the components at this specific id - componentArray[id].clear(); + component_array[id].clear(); } } } void ComponentManager::delete_all_components() { - this->components.clear(); + // Loop through all the types (in the unordered_map<>) + for (auto & [type, component_array] : this->components) { + // Loop through all the ids (in the vector<>) + for (game_object_id_t id = 0; id < component_array.size(); id++) { + // Do not delete persistent objects + if (!this->persistent[id]) { + // Clear the components at this specific id + component_array[id].clear(); + } + } + } + this->next_id = 0; } GameObject ComponentManager::new_object(const string & name, const string & tag, const vec2 & position, double rotation, double scale) { + // Find the first available id (taking persistent objects into account) + while (this->persistent[this->next_id]) { + this->next_id++; + } + GameObject object{*this, this->next_id, name, tag, position, rotation, scale}; this->next_id++; + return object; } + +void ComponentManager::set_persistent(game_object_id_t id, bool persistent) { + this->persistent[id] = persistent; +} diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h index 1cb0b5f..480124f 100644 --- a/src/crepe/ComponentManager.h +++ b/src/crepe/ComponentManager.h @@ -99,6 +99,16 @@ protected: * This method deletes all components. */ void delete_all_components(); + /** + * \brief Set a GameObject as persistent + * + * This method sets a GameObject as persistent. If a GameObject is persistent, its + * components will not be deleted. + * + * \param id The id of the GameObject to set as persistent + * \param persistent The persistent flag + */ + void set_persistent(game_object_id_t id, bool persistent); public: /** @@ -137,6 +147,9 @@ private: std::unordered_map<std::type_index, std::vector<std::vector<std::unique_ptr<Component>>>> components; + //! Persistent flag for each GameObject + std::unordered_map<game_object_id_t, bool> persistent; + //! ID of next GameObject allocated by \c ComponentManager::new_object game_object_id_t next_id = 0; }; diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index 4d5eaf4..ffb38ec 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -54,6 +54,11 @@ template <typename T> void ComponentManager::delete_components_by_id(game_object_id_t id) { using namespace std; + // Do not delete persistent objects + if (this->persistent[id]) { + return; + } + // Determine the type of T (this is used as the key of the unordered_map<>) type_index type = typeid(T); @@ -77,7 +82,13 @@ void ComponentManager::delete_components() { if (this->components.find(type) == this->components.end()) return; - this->components[type].clear(); + // Loop through the whole vector<> of this specific type + for (game_object_id_t i = 0; i < this->components[type].size(); ++i) { + // Do not delete persistent objects + if (!this->persistent[i]) { + this->components[type][i].clear(); + } + } } template <typename T> diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 464b0fd..45f67f6 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -14,11 +14,14 @@ Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_a col(col) { dbg_trace(); - animator_rect = spritesheet.sprite_rect; - animator_rect.h /= col; - animator_rect.w /= row; - animator_rect.x = 0; - animator_rect.y = col_animator * animator_rect.h; + this->spritesheet.mask.h /= col; + this->spritesheet.mask.w /= row; + this->spritesheet.mask.x = 0; + this->spritesheet.mask.y = col_animator * this->spritesheet.mask.h; this->active = false; + + // need to do this for to get the aspect ratio for a single clipping in the spritesheet + this->spritesheet.aspect_ratio + = static_cast<double>(this->spritesheet.mask.w) / this->spritesheet.mask.h; } Animator::~Animator() { dbg_trace(); } diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 53f4b91..6c506aa 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -40,10 +40,6 @@ public: Animator(uint32_t id, Sprite & spritesheet, int row, int col, int col_animate); ~Animator(); // dbg_trace - Animator(const Animator &) = delete; - Animator(Animator &&) = delete; - Animator & operator=(const Animator &) = delete; - Animator & operator=(Animator &&) = delete; private: //! A reference to the Sprite sheet containing the animation frames. @@ -61,8 +57,6 @@ private: //! The current row being animated. int curr_row = 0; - Rect animator_rect; - //TODO: Is this necessary? //int fps; diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 057f88c..d8215b9 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -12,7 +12,6 @@ target_sources(crepe PUBLIC SaveManager.cpp Config.cpp Metadata.cpp - Scene.cpp SceneManager.cpp Camera.cpp Animator.cpp diff --git a/src/crepe/api/Camera.cpp b/src/crepe/api/Camera.cpp index 5835bdd..39d8ab0 100644 --- a/src/crepe/api/Camera.cpp +++ b/src/crepe/api/Camera.cpp @@ -1,3 +1,4 @@ +#include "types.h" #include "util/Log.h" #include "Camera.h" @@ -6,9 +7,14 @@ using namespace crepe; -Camera::Camera(game_object_id_t id, const Color & bg_color) +Camera::Camera(game_object_id_t id, const Color & bg_color, const ivec2 & screen, + const vec2 & viewport_size, const double & zoom, const vec2 & offset) : Component(id), - bg_color(bg_color) { + bg_color(bg_color), + offset(offset), + screen(screen), + viewport_size(viewport_size), + zoom(zoom) { dbg_trace(); } diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index e0cda34..2d8fa48 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -2,6 +2,7 @@ #include "Color.h" #include "Component.h" +#include "types.h" namespace crepe { @@ -20,33 +21,31 @@ public: * \param id Unique identifier for the camera component. * \param bg_color Background color for the camera view. */ - Camera(game_object_id_t id, const Color & bg_color); + Camera(game_object_id_t id, const Color & bg_color, const ivec2 & screen, + const vec2 & viewport_size, const double & zoom, const vec2 & offset = {0, 0}); ~Camera(); // dbg_trace only public: //! Background color of the camera view. - Color bg_color; + const Color bg_color; - //! Aspect ratio height for the camera. - double aspect_height = 480; + //! offset postion from the game object transform component + vec2 offset; - //! Aspect ratio width for the camera. - double aspect_width = 640; + //! screen the display size in pixels ( output resolution ) + const ivec2 screen; - //! X-coordinate of the camera position. - double x = 0.0; - - //! Y-coordinate of the camera position. - double y = 0.0; + //! viewport is the area of the world visible through the camera (in world units) + const vec2 viewport_size; //! Zoom level of the camera view. - double zoom = 1.0; + const double zoom; public: /** * \brief Gets the maximum number of camera instances allowed. * \return Maximum instance count as an integer. */ - virtual int get_instances_max() const { return 10; } + virtual int get_instances_max() const { return 1; } }; } // namespace crepe diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 13eabd1..225e9b9 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -1,6 +1,8 @@ #pragma once #include "../util/Log.h" +#include "types.h" +#include <string> namespace crepe { @@ -11,19 +13,18 @@ namespace crepe { * modified *before* execution is handed over from the game programmer to the engine (i.e. the * main loop is started). */ -class Config { +class Config final { public: //! Retrieve handle to global Config instance static Config & get_instance(); private: Config() = default; - - // singleton - Config(const Config &) = delete; - Config(Config &&) = delete; - Config & operator=(const Config &) = delete; - Config & operator=(Config &&) = delete; + ~Config() = default; + Config(const Config &) = default; + Config(Config &&) = default; + Config & operator=(const Config &) = default; + Config & operator=(Config &&) = default; public: //! Logging-related settings @@ -63,6 +64,14 @@ public: double gravity = 1; } physics; + //! default window settings + struct { + //TODO make this constexpr because this will never change + ivec2 default_size = {1080, 720}; + std::string window_title = "Jetpack joyride clone"; + + } window_settings; + //! Asset loading options struct { /** diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp index 3c36a21..9ef4682 100644 --- a/src/crepe/api/GameObject.cpp +++ b/src/crepe/api/GameObject.cpp @@ -30,3 +30,9 @@ void GameObject::set_parent(const GameObject & parent) { RefVector<Metadata> parent_metadata = mgr.get_components_by_id<Metadata>(parent.id); parent_metadata.at(0).get().children.push_back(this->id); } + +void GameObject::set_persistent(bool persistent) { + ComponentManager & mgr = this->component_manager; + + mgr.set_persistent(this->id, persistent); +} diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h index fcb8d9a..4cd2bc0 100644 --- a/src/crepe/api/GameObject.h +++ b/src/crepe/api/GameObject.h @@ -58,6 +58,15 @@ public: */ template <typename T, typename... Args> T & add_component(Args &&... args); + /** + * \brief Components will not be deleted if this method is called + * + * This method sets the persistent flag of the GameObject to true. If the persistent + * flag is set to true, the GameObject will not be deleted when the scene is changed. + * + * \param persistent The persistent flag + */ + void set_persistent(bool persistent = true); public: //! The id of the GameObject diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 35919cc..feb1338 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -63,7 +63,7 @@ void LoopManager::loop() { void LoopManager::setup() { this->game_running = true; LoopTimer::get_instance().start(); - LoopTimer::get_instance().set_fps(60); + LoopTimer::get_instance().set_fps(200); this->scene_manager.load_next_scene(); } diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp index a9800b7..15a0e3a 100644 --- a/src/crepe/api/LoopTimer.cpp +++ b/src/crepe/api/LoopTimer.cpp @@ -47,7 +47,7 @@ double LoopTimer::get_fixed_delta_time() const { return this->fixed_delta_time.c void LoopTimer::set_fps(int fps) { this->fps = fps; // target time per frame in seconds - this->frame_target_time = std::chrono::seconds(1) / fps; + this->frame_target_time = std::chrono::duration<double>(1.0) / fps; } int LoopTimer::get_fps() const { return this->fps; } diff --git a/src/crepe/api/LoopTimer.h b/src/crepe/api/LoopTimer.h index f277d7b..9393439 100644 --- a/src/crepe/api/LoopTimer.h +++ b/src/crepe/api/LoopTimer.h @@ -130,9 +130,9 @@ private: //! Delta time for the current frame in seconds std::chrono::duration<double> delta_time{0.0}; //! Target time per frame in seconds - std::chrono::duration<double> frame_target_time = std::chrono::seconds(1) / fps; + std::chrono::duration<double> frame_target_time = std::chrono::duration<double>(1.0) / fps; //! Fixed delta time for fixed updates in seconds - std::chrono::duration<double> fixed_delta_time = std::chrono::seconds(1) / 50; + std::chrono::duration<double> fixed_delta_time = std::chrono::duration<double>(1.0) / 50.0; //! Total elapsed game time in seconds std::chrono::duration<double> elapsed_time{0.0}; //! Total elapsed time for fixed updates in seconds diff --git a/src/crepe/api/Scene.cpp b/src/crepe/api/Scene.cpp deleted file mode 100644 index 849945e..0000000 --- a/src/crepe/api/Scene.cpp +++ /dev/null @@ -1,5 +0,0 @@ -#include "Scene.h" - -using namespace crepe; - -Scene::Scene(ComponentManager & mgr) : component_manager(mgr) {} diff --git a/src/crepe/api/Scene.h b/src/crepe/api/Scene.h index 869bf6f..f6fdb2a 100644 --- a/src/crepe/api/Scene.h +++ b/src/crepe/api/Scene.h @@ -2,6 +2,8 @@ #include <string> +#include "../util/OptionalRef.h" + namespace crepe { class SceneManager; @@ -15,11 +17,8 @@ class ComponentManager; */ class Scene { protected: - //TODO: Use Loek's custom reference class to set ComponentManger via SceneManager instead of via constructor - /** - * \param mgr Reference to the ComponentManager - */ - Scene(ComponentManager & mgr); + // NOTE: This must be the only constructor on Scene, see "Late references" below + Scene() = default; //! SceneManager instances Scene friend class SceneManager; @@ -36,8 +35,20 @@ public: virtual std::string get_name() const = 0; protected: + /** + * \name Late references + * + * These references are set by SceneManager immediately after calling the constructor of Scene. + * + * \note Scene must have a constructor without arguments so the game programmer doesn't need to + * manually add `using Scene::Scene` to their concrete scene class, if they want to add a + * constructor with arguments (e.g. for passing references to their own concrete Scene classes). + * + * \{ + */ //! Reference to the ComponentManager - ComponentManager & component_manager; + OptionalRef<ComponentManager> component_manager; + //! \} }; } // namespace crepe diff --git a/src/crepe/api/SceneManager.h b/src/crepe/api/SceneManager.h index 45ba668..f6f62cd 100644 --- a/src/crepe/api/SceneManager.h +++ b/src/crepe/api/SceneManager.h @@ -26,8 +26,8 @@ public: * * \tparam T Type of concrete scene */ - template <typename T> - void add_scene(); + template <typename T, typename... Args> + void add_scene(Args &&... args); /** * \brief Set the next scene * diff --git a/src/crepe/api/SceneManager.hpp b/src/crepe/api/SceneManager.hpp index 94e5946..5c8e417 100644 --- a/src/crepe/api/SceneManager.hpp +++ b/src/crepe/api/SceneManager.hpp @@ -4,13 +4,17 @@ namespace crepe { -template <typename T> -void SceneManager::add_scene() { +template <typename T, typename... Args> +void SceneManager::add_scene(Args &&... args) { using namespace std; static_assert(is_base_of<Scene, T>::value, "T must be derived from Scene"); - Scene * scene = new T(this->component_manager); - this->scenes.emplace_back(unique_ptr<Scene>(scene)); + Scene * scene = new T(std::forward<Args>(args)...); + unique_ptr<Scene> unique_scene(scene); + + unique_scene->component_manager = this->component_manager; + + this->scenes.emplace_back(std::move(unique_scene)); // The first scene added, is the one that will be loaded at the beginning if (next_scene.empty()) { diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index bd2d5cf..8647794 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -1,7 +1,7 @@ -#include <memory> +#include <cmath> +#include <utility> #include "../util/Log.h" -#include "facade/SDLContext.h" #include "Component.h" #include "Sprite.h" @@ -10,16 +10,21 @@ using namespace std; using namespace crepe; -Sprite::Sprite(game_object_id_t id, const shared_ptr<Texture> image, const Color & color, - const FlipSettings & flip) +Sprite::Sprite(game_object_id_t id, Texture & image, const Color & color, + const FlipSettings & flip, int sort_layer, int order_layer, int height) : Component(id), color(color), flip(flip), - sprite_image(image) { + sprite_image(std::move(image)), + sorting_in_layer(sort_layer), + order_in_layer(order_layer), + height(height) { + dbg_trace(); - this->sprite_rect.w = sprite_image->get_width(); - this->sprite_rect.h = sprite_image->get_height(); + this->mask.w = sprite_image.get_width(); + this->mask.h = sprite_image.get_height(); + this->aspect_ratio = static_cast<double>(this->mask.w) / this->mask.h; } Sprite::~Sprite() { dbg_trace(); } diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 74a55d4..a0e90a0 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -1,6 +1,6 @@ #pragma once -#include <memory> +#include <cstdint> #include "../Component.h" @@ -9,18 +9,6 @@ namespace crepe { -struct Rect { - int w = 0; - int h = 0; - int x = 0; - int y = 0; -}; - -struct FlipSettings { - bool flip_x = false; - bool flip_y = false; -}; - class SDLContext; class Animator; class AnimatorSystem; @@ -34,6 +22,12 @@ class AnimatorSystem; class Sprite : public Component { public: + struct FlipSettings { + bool flip_x = false; + bool flip_y = false; + }; + +public: // TODO: Loek comment in github #27 will be looked another time // about shared_ptr Texture /** @@ -42,9 +36,12 @@ public: * \param image Shared pointer to the texture for this sprite. * \param color Color tint applied to the sprite. * \param flip Flip settings for horizontal and vertical orientation. + * \param order_layer decides the sorting in layer of the sprite. + * \param sort_layer decides the order in layer of the sprite. + * \param height the height of the image in game units */ - Sprite(game_object_id_t id, const std::shared_ptr<Texture> image, const Color & color, - const FlipSettings & flip); + Sprite(game_object_id_t id, Texture & image, const Color & color, + const FlipSettings & flip, int sort_layer, int order_layer, int height); /** * \brief Destroys the Sprite instance. @@ -52,38 +49,49 @@ public: ~Sprite(); //! Texture used for the sprite - const std::shared_ptr<Texture> sprite_image; + const Texture sprite_image; + //! Color tint of the sprite Color color; + //! Flip settings for the sprite FlipSettings flip; + //! Layer sorting level of the sprite - uint8_t sorting_in_layer = 0; + const int sorting_in_layer; //! Order within the sorting layer - uint8_t order_in_layer = 0; + const int order_in_layer; + + //! height in world units + const int height; -public: /** - * \brief Gets the maximum number of instances allowed for this sprite. - * \return Maximum instance count as an integer. + * \aspect_ratio ratio of the img so that scaling will not become weird * - * For now is this number randomly picked. I think it will eventually be 1. + * cannot be const because if Animator component is addded then ratio becomes scuffed and + * does it need to be calculated again in the Animator */ - virtual int get_instances_max() const { return 10; } + double aspect_ratio; private: - //! Reads the sprite_rect of sprite + //! Reads the mask of sprite friend class SDLContext; - //! Reads the all the variables plus the sprite_rect + //! Reads the all the variables plus the mask friend class Animator; - //! Reads the all the variables plus the sprite_rect + //! Reads the all the variables plus the mask friend class AnimatorSystem; + struct Rect { + int w = 0; + int h = 0; + int x = 0; + int y = 0; + }; //! Render area of the sprite this will also be adjusted by the AnimatorSystem if an Animator - // object is present in GameObject - Rect sprite_rect; + // object is present in GameObject. this is in sprite pixels + Rect mask; }; } // namespace crepe diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp index 9be9421..e43bdaa 100644 --- a/src/crepe/api/Texture.cpp +++ b/src/crepe/api/Texture.cpp @@ -1,5 +1,3 @@ -#include <SDL2/SDL_render.h> - #include "../facade/SDLContext.h" #include "../util/Log.h" @@ -9,14 +7,9 @@ using namespace crepe; using namespace std; -Texture::Texture(unique_ptr<Asset> res) { - dbg_trace(); - this->load(std::move(res)); -} - -Texture::Texture(const char * src) { +Texture::Texture(const Asset & src) { dbg_trace(); - this->load(make_unique<Asset>(src)); + this->load(src); } Texture::~Texture() { @@ -24,9 +17,18 @@ Texture::~Texture() { this->texture.reset(); } -void Texture::load(unique_ptr<Asset> res) { +Texture::Texture(Texture && other) noexcept : texture(std::move(other.texture)) {} + +Texture & Texture::operator=(Texture && other) noexcept { + if (this != &other) { + texture = std::move(other.texture); + } + return *this; +} + +void Texture::load(const Asset & res) { SDLContext & ctx = SDLContext::get_instance(); - this->texture = std::move(ctx.texture_from_path(res->get_path())); + this->texture = ctx.texture_from_path(res.get_path()); } int Texture::get_width() const { diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h index 6965223..7206a66 100644 --- a/src/crepe/api/Texture.h +++ b/src/crepe/api/Texture.h @@ -25,16 +25,10 @@ class Texture { public: /** - * \brief Constructs a Texture from a file path. - * \param src Path to the image file to be loaded as a texture. - */ - Texture(const char * src); - - /** * \brief Constructs a Texture from an Asset resource. - * \param res Unique pointer to an Asset resource containing texture data. + * \param src Asset with texture data to load. */ - Texture(std::unique_ptr<Asset> res); + Texture(const Asset & src); /** * \brief Destroys the Texture instance, freeing associated resources. @@ -42,6 +36,11 @@ public: ~Texture(); // FIXME: this constructor shouldn't be necessary because this class doesn't manage memory + Texture(Texture && other) noexcept; + Texture & operator=(Texture && other) noexcept; + Texture(const Texture &) = delete; + Texture & operator=(const Texture &) = delete; + /** * \brief Gets the width of the texture. * \return Width of the texture in pixels. @@ -59,7 +58,7 @@ private: * \brief Loads the texture from an Asset resource. * \param res Unique pointer to an Asset resource to load the texture from. */ - void load(std::unique_ptr<Asset> res); + void load(const Asset & res); private: //! The texture of the class from the library diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index b3298a7..9f60285 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -10,16 +10,15 @@ #include <functional> #include <memory> #include <stdexcept> -#include <string> #include "../api/Camera.h" +#include "../api/Config.h" #include "../api/Sprite.h" #include "../api/Texture.h" -#include "../api/Transform.h" -#include "../api/Vector2.h" #include "../util/Log.h" #include "SDLContext.h" +#include "types.h" using namespace crepe; using namespace std; @@ -31,14 +30,15 @@ SDLContext & SDLContext::get_instance() { SDLContext::SDLContext() { dbg_trace(); - // FIXME: read window defaults from config manager if (SDL_Init(SDL_INIT_VIDEO) != 0) { throw runtime_error(format("SDLContext: SDL_Init error: {}", SDL_GetError())); } + + auto & cfg = Config::get_instance().window_settings; SDL_Window * tmp_window - = SDL_CreateWindow("Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, - this->viewport.w, this->viewport.h, 0); + = SDL_CreateWindow(cfg.window_title.c_str(), SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, cfg.default_size.x, cfg.default_size.y, 0); if (!tmp_window) { throw runtime_error(format("SDLContext: SDL_Window error: {}", SDL_GetError())); } @@ -93,68 +93,96 @@ void SDLContext::handle_events(bool & running) { */ } -void SDLContext::clear_screen() { SDL_RenderClear(this->game_renderer.get()); } +void SDLContext::clear_screen() { + SDL_SetRenderDrawColor(this->game_renderer.get(), 0, 0, 0, 255); + SDL_RenderClear(this->game_renderer.get()); +} void SDLContext::present_screen() { SDL_RenderPresent(this->game_renderer.get()); } SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { return SDL_Rect{ - .x = sprite.sprite_rect.x, - .y = sprite.sprite_rect.y, - .w = sprite.sprite_rect.w, - .h = sprite.sprite_rect.h, + .x = sprite.mask.x, + .y = sprite.mask.y, + .w = sprite.mask.w, + .h = sprite.mask.h, }; } -SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const vec2 & pos, - const double & scale, const Camera & cam) const { +SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, + const vec2 & cam_pos, const double & img_scale) const { + + int width = sprite.height * sprite.aspect_ratio; + int height = sprite.height; - double adjusted_w = sprite.sprite_rect.w * scale * cam.zoom; - double adjusted_h = sprite.sprite_rect.h * scale * cam.zoom; - double adjusted_x = (pos.x - cam.x) * cam.zoom - adjusted_w / 2; - double adjusted_y = (pos.y - cam.y) * cam.zoom - adjusted_h / 2; + width *= img_scale * cam.zoom; + height *= img_scale * cam.zoom; return SDL_Rect{ - .x = static_cast<int>(adjusted_x), - .y = static_cast<int>(adjusted_y), - .w = static_cast<int>(adjusted_w), - .h = static_cast<int>(adjusted_h), + .x = static_cast<int>((pos.x - cam_pos.x + (cam.viewport_size.x / 2) - width / 2)), + .y = static_cast<int>((pos.y - cam_pos.y + (cam.viewport_size.y / 2) - height / 2)), + .w = width, + .h = height, }; } -void SDLContext::draw_particle(const Sprite & sprite, const vec2 & pos, const double & angle, - const double & scale, const Camera & camera) { +void SDLContext::draw(const RenderContext & ctx) { SDL_RendererFlip render_flip - = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) - | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); + = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * ctx.sprite.flip.flip_x) + | (SDL_FLIP_VERTICAL * ctx.sprite.flip.flip_y)); - SDL_Rect srcrect = this->get_src_rect(sprite); - SDL_Rect dstrect = this->get_dst_rect(sprite, pos, scale, camera); + SDL_Rect srcrect = this->get_src_rect(ctx.sprite); + SDL_Rect dstrect + = this->get_dst_rect(ctx.sprite, ctx.pos, ctx.cam, ctx.cam_pos, ctx.scale); - SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect, - &dstrect, angle, NULL, render_flip); + SDL_RenderCopyEx(this->game_renderer.get(), ctx.sprite.sprite_image.texture.get(), + &srcrect, &dstrect, ctx.angle, NULL, render_flip); } -void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Camera & cam) { - - SDL_RendererFlip render_flip - = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) - | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); +void SDLContext::set_camera(const Camera & cam) { - SDL_Rect srcrect = this->get_src_rect(sprite); - SDL_Rect dstrect = this->get_dst_rect(sprite, transform.position, transform.scale, cam); + // resize window + int w, h; + SDL_GetWindowSize(this->game_window.get(), &w, &h); + if (w != cam.screen.x || h != cam.screen.y) { + SDL_SetWindowSize(this->game_window.get(), cam.screen.x, cam.screen.y); + } - SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect, - &dstrect, transform.rotation, NULL, render_flip); -} + double screen_aspect = cam.screen.x / cam.screen.y; + double viewport_aspect = cam.viewport_size.x / cam.viewport_size.y; + + SDL_Rect view; + // calculate black bars + if (screen_aspect > viewport_aspect) { + // letterboxing + view.h = cam.screen.y / cam.zoom; + view.w = cam.screen.y * viewport_aspect; + view.x = (cam.screen.x - view.w) / 2; + view.y = 0; + } else { + // pillarboxing + view.h = cam.screen.x / viewport_aspect; + view.w = cam.screen.x / cam.zoom; + view.x = 0; + view.y = (cam.screen.y - view.h) / 2; + } + // set drawing area + SDL_RenderSetViewport(this->game_renderer.get(), &view); -void SDLContext::set_camera(const Camera & cam) { - this->viewport.w = static_cast<int>(cam.aspect_width); - this->viewport.h = static_cast<int>(cam.aspect_height); - this->viewport.x = static_cast<int>(cam.x) - (this->viewport.w / 2); - this->viewport.y = static_cast<int>(cam.y) - (this->viewport.h / 2); + SDL_RenderSetLogicalSize(this->game_renderer.get(), static_cast<int>(cam.viewport_size.x), + static_cast<int>(cam.viewport_size.y)); + // set bg color SDL_SetRenderDrawColor(this->game_renderer.get(), cam.bg_color.r, cam.bg_color.g, cam.bg_color.b, cam.bg_color.a); + + SDL_Rect bg = { + .x = 0, + .y = 0, + .w = static_cast<int>(cam.viewport_size.x), + .h = static_cast<int>(cam.viewport_size.y), + }; + // fill bg color + SDL_RenderFillRect(this->game_renderer.get(), &bg); } uint64_t SDLContext::get_ticks() const { return SDL_GetTicks64(); } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 20e30b3..6030a6e 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -9,9 +9,8 @@ #include <memory> #include <string> +#include "../api/Camera.h" #include "../api/Sprite.h" -#include "../api/Transform.h" -#include "api/Camera.h" #include "types.h" @@ -29,6 +28,15 @@ typedef SDL_Keycode CREPE_KEYCODES; * event handling, and rendering to the screen. It is never used directly by the user */ class SDLContext { +public: + struct RenderContext { + const Sprite & sprite; + const Camera & cam; + const vec2 & cam_pos; + const vec2 & pos; + const double & angle; + const double & scale; + }; public: /** @@ -100,14 +108,14 @@ private: * \param texture Reference to the Texture object. * \return Width of the texture as an integer. */ - int get_width(const Texture &) const; + int get_width(const Texture & texture) const; /** * \brief Gets the height of a texture. * \param texture Reference to the Texture object. * \return Height of the texture as an integer. */ - int get_height(const Texture &) const; + int get_height(const Texture & texture) const; private: //! Will use draw,clear_screen, present_screen, camera. @@ -115,14 +123,9 @@ private: /** * \brief Draws a sprite to the screen using the specified transform and camera. - * \param sprite Reference to the Sprite to draw. - * \param transform Reference to the Transform for positioning. - * \param camera Reference to the Camera for view adjustments. + * \param RenderCtx Reference to rendering data to draw */ - void draw(const Sprite & sprite, const Transform & transform, const Camera & camera); - - void draw_particle(const Sprite & sprite, const vec2 & pos, const double & angle, - const double & scale, const Camera & camera); + void draw(const RenderContext & ctx); //! Clears the screen, preparing for a new frame. void clear_screen(); @@ -144,18 +147,19 @@ private: * \return sdl rectangle to draw a src image */ SDL_Rect get_src_rect(const Sprite & sprite) const; + /** - * \brief calculates the sqaure size of the image for an destination + * \brief calculates the sqaure size of the image for destination * - * \param sprite Reference to the sprite to calculate the rectangle - * \param pos the pos in pixel positions - * \param scale the multiplier to increase of decrease for the specified sprite - * \param cam Reference to the current camera in the scene to calculate the position based - * on the camera + * \param sprite Reference to the sprite to calculate rectangle + * \param pos the pos in world units + * \param cam the camera of the current scene + * \param cam_pos the current postion of the camera + * \param img_scale the image multiplier for increasing img size * \return sdl rectangle to draw a dst image to draw on the screen */ - SDL_Rect get_dst_rect(const Sprite & sprite, const vec2 & pos, const double & scale, - const Camera & cam) const; + SDL_Rect get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, + const vec2 & cam_pos, const double & img_scale) const; private: //! sdl Window @@ -163,9 +167,6 @@ private: //! renderer for the crepe engine std::unique_ptr<SDL_Renderer, std::function<void(SDL_Renderer *)>> game_renderer; - - //! viewport for the camera window - SDL_Rect viewport = {0, 0, 640, 480}; }; } // namespace crepe diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index 676e485..4c40940 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -15,10 +15,10 @@ void AnimatorSystem::update() { uint64_t tick = SDLContext::get_instance().get_ticks(); for (Animator & a : animations) { - if (a.active) { - a.curr_row = (tick / 100) % a.row; - a.animator_rect.x = (a.curr_row * a.animator_rect.w) + a.curr_col; - a.spritesheet.sprite_rect = a.animator_rect; - } + if (!a.active) continue; + // (10 frames per second) + a.curr_row = (tick / 100) % a.row; + a.spritesheet.mask.x = (a.curr_row * a.spritesheet.mask.w) + a.curr_col; + a.spritesheet.mask = a.spritesheet.mask; } } diff --git a/src/crepe/system/AnimatorSystem.h b/src/crepe/system/AnimatorSystem.h index 56cc7b3..f8179a9 100644 --- a/src/crepe/system/AnimatorSystem.h +++ b/src/crepe/system/AnimatorSystem.h @@ -21,12 +21,11 @@ public: /** * \brief Updates the Animator components. * - * This method is called periodically (likely every frame) to update the state of all + * This method is called to update the state of all * Animator components, moving the animations forward and managing their behavior (e.g., * looping). */ void update() override; - // FIXME: never say "likely" in the documentation lmao }; } // namespace crepe diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index ad510f5..c196bb1 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -2,15 +2,14 @@ #include <cassert> #include <cmath> #include <functional> -#include <iostream> #include <stdexcept> #include <vector> #include "../ComponentManager.h" +#include "../api/Camera.h" #include "../api/ParticleEmitter.h" #include "../api/Sprite.h" #include "../api/Transform.h" -#include "../api/Vector2.h" #include "../facade/SDLContext.h" #include "RenderSystem.h" @@ -21,7 +20,8 @@ using namespace std; void RenderSystem::clear_screen() { this->context.clear_screen(); } void RenderSystem::present_screen() { this->context.present_screen(); } -void RenderSystem::update_camera() { + +const Camera & RenderSystem::update_camera() { ComponentManager & mgr = this->component_manager; RefVector<Camera> cameras = mgr.get_components_by_type<Camera>(); @@ -30,9 +30,13 @@ void RenderSystem::update_camera() { for (Camera & cam : cameras) { if (!cam.active) continue; + const Transform & transform + = mgr.get_components_by_id<Transform>(cam.game_object_id).front().get(); this->context.set_camera(cam); - this->curr_cam_ref = &cam; + this->cam_pos = transform.position + cam.offset; + return cam; } + throw std::runtime_error("No active cameras in current scene"); } bool sorting_comparison(const Sprite & a, const Sprite & b) { @@ -51,12 +55,12 @@ RefVector<Sprite> RenderSystem::sort(RefVector<Sprite> & objs) const { void RenderSystem::update() { this->clear_screen(); - this->update_camera(); this->render(); this->present_screen(); } -bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) { +bool RenderSystem::render_particle(const Sprite & sprite, const Camera & cam, + const double & scale) { ComponentManager & mgr = this->component_manager; @@ -72,19 +76,35 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) for (const Particle & p : em.data.particles) { if (!p.active) continue; - this->context.draw_particle(sprite, p.position, p.angle, scale, - *this->curr_cam_ref); + + this->context.draw(SDLContext::RenderContext{ + .sprite = sprite, + .cam = cam, + .cam_pos = this->cam_pos, + .pos = p.position, + .angle = p.angle, + .scale = scale, + }); } } return rendering_particles; } -void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) { - this->context.draw(sprite, tm, *this->curr_cam_ref); +void RenderSystem::render_normal(const Sprite & sprite, const Camera & cam, + const Transform & tm) { + this->context.draw(SDLContext::RenderContext{ + .sprite = sprite, + .cam = cam, + .cam_pos = this->cam_pos, + .pos = tm.position, + .angle = tm.rotation, + .scale = tm.scale, + }); } void RenderSystem::render() { - ComponentManager & mgr = this->component_manager; + const Camera & cam = this->update_camera(); + RefVector<Sprite> sprites = mgr.get_components_by_type<Sprite>(); RefVector<Sprite> sorted_sprites = this->sort(sprites); @@ -93,10 +113,10 @@ void RenderSystem::render() { const Transform & transform = mgr.get_components_by_id<Transform>(sprite.game_object_id).front().get(); - bool rendered_particles = this->render_particle(sprite, transform.scale); + bool rendered_particles = this->render_particle(sprite, cam, transform.scale); if (rendered_particles) continue; - this->render_normal(sprite, transform); + this->render_normal(sprite, cam, transform); } } diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 30b41cf..e70831e 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -1,17 +1,17 @@ #pragma once -#include <functional> -#include <vector> +#include <cmath> #include "facade/SDLContext.h" #include "System.h" -#include <cmath> +#include "types.h" namespace crepe { class Camera; class Sprite; +class Transform; /** * \class RenderSystem @@ -37,7 +37,7 @@ private: void present_screen(); //! Updates the active camera used for rendering. - void update_camera(); + const Camera & update_camera(); //! Renders the whole screen void render(); @@ -49,7 +49,7 @@ private: * \param tm the Transform component for scale * \return true if particles have been rendered */ - bool render_particle(const Sprite & sprite, const double & scale); + bool render_particle(const Sprite & sprite, const Camera & cam, const double & scale); /** * \brief renders a sprite with a Transform component on the screen @@ -57,7 +57,7 @@ private: * \param sprite the sprite component that holds all the data * \param tm the Transform component that holds the position,rotation and scale */ - void render_normal(const Sprite & sprite, const Transform & tm); + void render_normal(const Sprite & sprite, const Camera & cam, const Transform & tm); /** * \brief sort a vector sprite objects with @@ -71,17 +71,14 @@ private: * \todo Include color handling for sprites. * \todo Add text rendering using SDL_ttf for text components. * \todo Implement a text component and a button component. - * \todo Ensure each sprite is checked for active status before rendering. - * \todo Sort all layers by order before rendering. * \todo Consider adding text input functionality. */ private: - //! Pointer to the current active camera for rendering - Camera * curr_cam_ref = nullptr; - // TODO: needs a better solution - SDLContext & context = SDLContext::get_instance(); + + //! camera postion in the current scene + vec2 cam_pos; }; } // namespace crepe diff --git a/src/crepe/types.h b/src/crepe/types.h index 17f1619..69cc526 100644 --- a/src/crepe/types.h +++ b/src/crepe/types.h @@ -27,4 +27,4 @@ typedef Vector2<float> vec2; //! Default Vector2<double> type typedef Vector2<double> dvec2; -} // namespace crepe +}; // namespace crepe diff --git a/src/doc/feature/scene.dox b/src/doc/feature/scene.dox index eedc69a..d81df4c 100644 --- a/src/doc/feature/scene.dox +++ b/src/doc/feature/scene.dox @@ -36,16 +36,14 @@ concrete scene to be added. #include <crepe/api/LoopManager.h> #include <crepe/api/GameObject.h> #include <crepe/api/Scene.h> -#include <crepe/api/Vector2.h> +#include <crepe/types.h> using namespace crepe; class MyScene : public Scene { public: - using Scene::Scene; - void load_scene() { - auto & mgr = this->component_manager; + ComponentManager & mgr = this->component_manager; GameObject object1 = mgr.new_object("object1", "tag_my_scene", vec2{0, 0}, 0, 1); GameObject object2 = mgr.new_object("object2", "tag_my_scene", vec2{1, 0}, 0, 1); } diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index b38e860..3a12144 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -1,6 +1,7 @@ +#include "api/Animator.h" #include "api/Camera.h" +#include "system/AnimatorSystem.h" #include "system/ParticleSystem.h" -#include "types.h" #include <SDL2/SDL_timer.h> #include <crepe/ComponentManager.h> @@ -12,28 +13,31 @@ #include <crepe/api/Sprite.h> #include <crepe/api/Texture.h> #include <crepe/api/Transform.h> -#include <crepe/api/Vector2.h> #include <crepe/system/RenderSystem.h> +#include <crepe/types.h> #include <chrono> -#include <iostream> -#include <memory> using namespace crepe; using namespace std; int main(int argc, char * argv[]) { ComponentManager mgr; - GameObject game_object = mgr.new_object("", "", vec2{0, 0}, 0, 0.1); + GameObject game_object = mgr.new_object("", "", vec2{0, 0}, 0, 1); RenderSystem sys{mgr}; ParticleSystem psys{mgr}; + AnimatorSystem asys{mgr}; Color color(255, 255, 255, 255); + auto img = Texture("asset/texture/test_ap43.png"); Sprite & test_sprite = game_object.add_component<Sprite>( - make_shared<Texture>("asset/texture/img.png"), color, FlipSettings{false, false}); - test_sprite.order_in_layer = 5; + img, color, Sprite::FlipSettings{true, true}, 1, 1, 500); + //game_object.add_component<Animator>(test_sprite, 4, 1, 0).active = true; + game_object.add_component<Animator>(test_sprite, 1, 1, 0).active = true; + + /* auto & test = game_object.add_component<ParticleEmitter>(ParticleEmitter::Data{ .position = {0, 0}, .max_particles = 10, @@ -53,17 +57,24 @@ int main(int argc, char * argv[]) { }, .sprite = test_sprite, }); - game_object.add_component<Camera>(Color::WHITE); + */ + + auto & cam = game_object.add_component<Camera>(Color::WHITE, ivec2{1080, 720}, + vec2{2000, 2000}, 1.0f); + /* game_object .add_component<Sprite>(make_shared<Texture>("asset/texture/img.png"), color, + .add_component<Sprite>(make_shared<Texture>("asset/texture/img.png"), color, FlipSettings{false, false}) .order_in_layer = 6; + */ auto start = std::chrono::steady_clock::now(); while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) { psys.update(); + asys.update(); sys.update(); SDL_Delay(10); } diff --git a/src/test/AssetTest.cpp b/src/test/AssetTest.cpp index 8aa7629..93fd6a9 100644 --- a/src/test/AssetTest.cpp +++ b/src/test/AssetTest.cpp @@ -7,17 +7,12 @@ using namespace std; using namespace crepe; using namespace testing; -class AssetTest : public Test { -public: - Config & cfg = Config::get_instance(); - void SetUp() override { this->cfg.asset.root_pattern = ".crepe-root"; } -}; - -TEST_F(AssetTest, Existant) { ASSERT_NO_THROW(Asset{"asset/texture/img.png"}); } +TEST(AssetTest, Existant) { ASSERT_NO_THROW(Asset{"asset/texture/img.png"}); } -TEST_F(AssetTest, Nonexistant) { ASSERT_ANY_THROW(Asset{"asset/nonexistant"}); } +TEST(AssetTest, Nonexistant) { ASSERT_ANY_THROW(Asset{"asset/nonexistant"}); } -TEST_F(AssetTest, Rootless) { +TEST(AssetTest, Rootless) { + Config & cfg = Config::get_instance(); cfg.asset.root_pattern.clear(); string arbitrary = "\\/this is / /../passed through as-is"; diff --git a/src/test/ECSTest.cpp b/src/test/ECSTest.cpp index 80b936b..af9d6f2 100644 --- a/src/test/ECSTest.cpp +++ b/src/test/ECSTest.cpp @@ -136,6 +136,53 @@ TEST_F(ECSTest, manyGameObjects) { EXPECT_EQ(metadata.size(), 10000 - 5000); EXPECT_EQ(transform.size(), 10000); + + for (int i = 0; i < 10000 - 5000; i++) { + EXPECT_EQ(metadata[i].get().game_object_id, i + 5000); + EXPECT_EQ(metadata[i].get().name, "body"); + EXPECT_EQ(metadata[i].get().tag, "person" + to_string(i)); + EXPECT_EQ(metadata[i].get().parent, -1); + EXPECT_EQ(metadata[i].get().children.size(), 0); + + EXPECT_EQ(transform[i].get().game_object_id, i); + EXPECT_EQ(transform[i].get().position.x, 0); + EXPECT_EQ(transform[i].get().position.y, 0); + EXPECT_EQ(transform[i].get().rotation, 0); + EXPECT_EQ(transform[i].get().scale, i); + } + + mgr.delete_all_components(); + + metadata = mgr.get_components_by_type<Metadata>(); + transform = mgr.get_components_by_type<Transform>(); + + EXPECT_EQ(metadata.size(), 0); + EXPECT_EQ(transform.size(), 0); + + for (int i = 0; i < 10000; i++) { + string name = "body" + to_string(i); + GameObject obj = mgr.new_object(name, "person", vec2{0, 0}, 0, 0); + } + + metadata = mgr.get_components_by_type<Metadata>(); + transform = mgr.get_components_by_type<Transform>(); + + EXPECT_EQ(metadata.size(), 10000); + EXPECT_EQ(transform.size(), 10000); + + for (int i = 0; i < 10000; i++) { + EXPECT_EQ(metadata[i].get().game_object_id, i); + EXPECT_EQ(metadata[i].get().name, "body" + to_string(i)); + EXPECT_EQ(metadata[i].get().tag, "person"); + EXPECT_EQ(metadata[i].get().parent, -1); + EXPECT_EQ(metadata[i].get().children.size(), 0); + + EXPECT_EQ(transform[i].get().game_object_id, i); + EXPECT_EQ(transform[i].get().position.x, 0); + EXPECT_EQ(transform[i].get().position.y, 0); + EXPECT_EQ(transform[i].get().rotation, 0); + EXPECT_EQ(transform[i].get().scale, 0); + } } TEST_F(ECSTest, getComponentsByID) { @@ -234,3 +281,107 @@ TEST_F(ECSTest, partentChild) { EXPECT_EQ(metadata[1].get().children[0], 3); EXPECT_EQ(metadata[2].get().children[0], 4); } + +TEST_F(ECSTest, persistent) { + GameObject obj0 = mgr.new_object("obj0", "obj0", vec2{0, 0}, 0, 1); + GameObject obj1 = mgr.new_object("obj1", "obj1", vec2{0, 0}, 0, 1); + obj1.set_persistent(); + GameObject obj2 = mgr.new_object("obj2", "obj2", vec2{0, 0}, 0, 1); + + vector<reference_wrapper<Metadata>> metadata = mgr.get_components_by_type<Metadata>(); + vector<reference_wrapper<Transform>> transform = mgr.get_components_by_type<Transform>(); + + EXPECT_EQ(metadata.size(), 3); + EXPECT_EQ(transform.size(), 3); + + mgr.delete_components_by_id<Metadata>(1); + mgr.delete_components<Metadata>(); + mgr.delete_all_components_of_id(1); + + metadata = mgr.get_components_by_type<Metadata>(); + transform = mgr.get_components_by_type<Transform>(); + + EXPECT_EQ(metadata.size(), 1); + EXPECT_EQ(transform.size(), 3); + + mgr.delete_all_components(); + + metadata = mgr.get_components_by_type<Metadata>(); + transform = mgr.get_components_by_type<Transform>(); + + EXPECT_EQ(metadata.size(), 1); + EXPECT_EQ(transform.size(), 1); + + EXPECT_EQ(metadata[0].get().game_object_id, 1); + EXPECT_EQ(metadata[0].get().name, "obj1"); + EXPECT_EQ(metadata[0].get().tag, "obj1"); + EXPECT_EQ(metadata[0].get().parent, -1); + EXPECT_EQ(metadata[0].get().children.size(), 0); + + EXPECT_EQ(transform[0].get().game_object_id, 1); + EXPECT_EQ(transform[0].get().position.x, 0); + EXPECT_EQ(transform[0].get().position.y, 0); + + GameObject obj3 = mgr.new_object("obj3", "obj3", vec2{0, 0}, 0, 5); + GameObject obj4 = mgr.new_object("obj4", "obj4", vec2{0, 0}, 0, 5); + + metadata = mgr.get_components_by_type<Metadata>(); + transform = mgr.get_components_by_type<Transform>(); + + EXPECT_EQ(metadata.size(), 3); + EXPECT_EQ(transform.size(), 3); + + EXPECT_EQ(metadata[0].get().game_object_id, 0); + EXPECT_EQ(metadata[0].get().name, "obj3"); + + EXPECT_EQ(metadata[1].get().game_object_id, 1); + EXPECT_EQ(metadata[1].get().name, "obj1"); + + EXPECT_EQ(metadata[2].get().game_object_id, 2); + EXPECT_EQ(metadata[2].get().name, "obj4"); + + EXPECT_EQ(transform[0].get().game_object_id, 0); + EXPECT_EQ(transform[0].get().scale, 5); + + EXPECT_EQ(transform[1].get().game_object_id, 1); + EXPECT_EQ(transform[1].get().scale, 1); + + EXPECT_EQ(transform[2].get().game_object_id, 2); + EXPECT_EQ(transform[2].get().scale, 5); +} + +TEST_F(ECSTest, resetPersistent) { + GameObject obj0 = mgr.new_object("obj0", "obj0", vec2{0, 0}, 0, 1); + GameObject obj1 = mgr.new_object("obj1", "obj1", vec2{0, 0}, 0, 1); + obj1.set_persistent(); + GameObject obj2 = mgr.new_object("obj2", "obj2", vec2{0, 0}, 0, 1); + + vector<reference_wrapper<Metadata>> metadata = mgr.get_components_by_type<Metadata>(); + vector<reference_wrapper<Transform>> transform = mgr.get_components_by_type<Transform>(); + + EXPECT_EQ(metadata.size(), 3); + EXPECT_EQ(transform.size(), 3); + + mgr.delete_all_components(); + + metadata = mgr.get_components_by_type<Metadata>(); + transform = mgr.get_components_by_type<Transform>(); + + EXPECT_EQ(metadata.size(), 1); + EXPECT_EQ(transform.size(), 1); + + vector<reference_wrapper<Metadata>> metadata_id = mgr.get_components_by_id<Metadata>(1); + + EXPECT_EQ(metadata_id.size(), 1); + EXPECT_EQ(metadata_id[0].get().game_object_id, 1); + EXPECT_EQ(metadata_id[0].get().name, "obj1"); + + mgr.set_persistent(1, false); + mgr.delete_all_components(); + + metadata = mgr.get_components_by_type<Metadata>(); + transform = mgr.get_components_by_type<Transform>(); + + EXPECT_EQ(metadata.size(), 0); + EXPECT_EQ(transform.size(), 0); +} diff --git a/src/test/ParticleTest.cpp b/src/test/ParticleTest.cpp index 8b81e74..976f9a1 100644 --- a/src/test/ParticleTest.cpp +++ b/src/test/ParticleTest.cpp @@ -1,4 +1,4 @@ -#include "api/Vector2.h" +#include "api/Texture.h" #include <crepe/ComponentManager.h> #include <crepe/Particle.h> #include <crepe/api/Config.h> @@ -28,9 +28,9 @@ public: GameObject game_object = mgr.new_object("", "", vec2{0, 0}, 0, 0); Color color(0, 0, 0, 0); + auto s1 = Texture("asset/texture/img.png"); Sprite & test_sprite = game_object.add_component<Sprite>( - make_shared<Texture>("asset/texture/img.png"), color, - FlipSettings{true, true}); + s1, color, Sprite::FlipSettings{true, true}, 1, 1, 100); game_object.add_component<ParticleEmitter>(ParticleEmitter::Data{ .position = {0, 0}, diff --git a/src/test/RenderSystemTest.cpp b/src/test/RenderSystemTest.cpp index ac479d3..bb5b81a 100644 --- a/src/test/RenderSystemTest.cpp +++ b/src/test/RenderSystemTest.cpp @@ -1,4 +1,4 @@ -#include "api/Camera.h" +#include "types.h" #include <functional> #include <gtest/gtest.h> #include <memory> @@ -7,6 +7,7 @@ #define private public #define protected public +#include "crepe/api/Camera.h" #include <crepe/ComponentManager.h> #include <crepe/api/Color.h> #include <crepe/api/GameObject.h> @@ -29,40 +30,30 @@ public: GameObject entity4 = this->mgr.new_object("name"); void SetUp() override { - auto & sprite1 - = entity1.add_component<Sprite>(make_shared<Texture>("../asset/texture/img.png"), - Color(0, 0, 0, 0), FlipSettings{false, false}); - ASSERT_NE(sprite1.sprite_image.get(), nullptr); - sprite1.order_in_layer = 5; - sprite1.sorting_in_layer = 5; + auto s1 = Texture("asset/texture/img.png"); + auto s2 = Texture("asset/texture/img.png"); + auto s3 = Texture("asset/texture/img.png"); + auto s4 = Texture("asset/texture/img.png"); + auto & sprite1 = entity1.add_component<Sprite>( + s1, Color(0, 0, 0, 0), Sprite::FlipSettings{false, false}, 5, 5, 100); + ASSERT_NE(sprite1.sprite_image.texture.get(), nullptr); EXPECT_EQ(sprite1.order_in_layer, 5); EXPECT_EQ(sprite1.sorting_in_layer, 5); - auto & sprite2 - = entity2.add_component<Sprite>(make_shared<Texture>("../asset/texture/img.png"), - Color(0, 0, 0, 0), FlipSettings{false, false}); - ASSERT_NE(sprite2.sprite_image.get(), nullptr); - sprite2.sorting_in_layer = 2; - sprite2.order_in_layer = 1; - + auto & sprite2 = entity2.add_component<Sprite>( + s2, Color(0, 0, 0, 0), Sprite::FlipSettings{false, false}, 2, 1, 100); + ASSERT_NE(sprite2.sprite_image.texture.get(), nullptr); EXPECT_EQ(sprite2.sorting_in_layer, 2); EXPECT_EQ(sprite2.order_in_layer, 1); - auto & sprite3 - = entity3.add_component<Sprite>(make_shared<Texture>("../asset/texture/img.png"), - Color(0, 0, 0, 0), FlipSettings{false, false}); - ASSERT_NE(sprite3.sprite_image.get(), nullptr); - sprite3.sorting_in_layer = 1; - sprite3.order_in_layer = 2; - + auto & sprite3 = entity3.add_component<Sprite>( + s3, Color(0, 0, 0, 0), Sprite::FlipSettings{false, false}, 1, 2, 100); + ASSERT_NE(sprite3.sprite_image.texture.get(), nullptr); EXPECT_EQ(sprite3.sorting_in_layer, 1); EXPECT_EQ(sprite3.order_in_layer, 2); - auto & sprite4 - = entity4.add_component<Sprite>(make_shared<Texture>("../asset/texture/img.png"), - Color(0, 0, 0, 0), FlipSettings{false, false}); - ASSERT_NE(sprite4.sprite_image.get(), nullptr); - sprite4.sorting_in_layer = 1; - sprite4.order_in_layer = 1; + auto & sprite4 = entity4.add_component<Sprite>( + s4, Color(0, 0, 0, 0), Sprite::FlipSettings{false, false}, 1, 1, 100); + ASSERT_NE(sprite4.sprite_image.texture.get(), nullptr); EXPECT_EQ(sprite4.sorting_in_layer, 1); EXPECT_EQ(sprite4.order_in_layer, 1); } @@ -73,8 +64,9 @@ TEST_F(RenderSystemTest, expected_throws) { // no texture img EXPECT_ANY_THROW({ - entity1.add_component<Sprite>(make_shared<Texture>("NO_IMAGE"), Color(0, 0, 0, 0), - FlipSettings{false, false}); + auto test = Texture(""); + entity1.add_component<Sprite>(test, Color(0, 0, 0, 0), + Sprite::FlipSettings{false, false}, 1, 1, 100); }); // No camera @@ -121,7 +113,7 @@ TEST_F(RenderSystemTest, sorting_sprites) { } TEST_F(RenderSystemTest, Update) { - entity1.add_component<Camera>(Color::WHITE); + entity1.add_component<Camera>(Color::WHITE, ivec2{1080, 720}, vec2{2000, 2000}, 1.0f); { vector<reference_wrapper<Sprite>> sprites = this->mgr.get_components_by_type<Sprite>(); ASSERT_EQ(sprites.size(), 4); @@ -149,7 +141,7 @@ TEST_F(RenderSystemTest, Camera) { EXPECT_NE(cameras.size(), 1); } { - entity1.add_component<Camera>(Color::WHITE); + entity1.add_component<Camera>(Color::WHITE, ivec2{1080, 720}, vec2{2000, 2000}, 1.0f); auto cameras = this->mgr.get_components_by_type<Camera>(); EXPECT_EQ(cameras.size(), 1); } @@ -157,9 +149,9 @@ TEST_F(RenderSystemTest, Camera) { //TODO improve with newer version } TEST_F(RenderSystemTest, Color) { - entity1.add_component<Camera>(Color::WHITE); + entity1.add_component<Camera>(Color::WHITE, ivec2{1080, 720}, vec2{2000, 2000}, 1.0f); auto & sprite = this->mgr.get_components_by_id<Sprite>(entity1.id).front().get(); - ASSERT_NE(sprite.sprite_image.get(), nullptr); + ASSERT_NE(sprite.sprite_image.texture.get(), nullptr); sprite.color = Color::GREEN; EXPECT_EQ(sprite.color.r, Color::GREEN.r); diff --git a/src/test/SceneManagerTest.cpp b/src/test/SceneManagerTest.cpp index f3d2387..62b7d33 100644 --- a/src/test/SceneManagerTest.cpp +++ b/src/test/SceneManagerTest.cpp @@ -1,3 +1,4 @@ +#include "types.h" #include <crepe/ComponentManager.h> #include <crepe/api/GameObject.h> #include <crepe/api/Metadata.h> @@ -12,10 +13,8 @@ using namespace crepe; class ConcreteScene1 : public Scene { public: - using Scene::Scene; - void load_scene() { - auto & mgr = this->component_manager; + ComponentManager & mgr = this->component_manager; GameObject object1 = mgr.new_object("scene_1", "tag_scene_1", vec2{0, 0}, 0, 1); GameObject object2 = mgr.new_object("scene_1", "tag_scene_1", vec2{1, 0}, 0, 1); GameObject object3 = mgr.new_object("scene_1", "tag_scene_1", vec2{2, 0}, 0, 1); @@ -26,10 +25,8 @@ public: class ConcreteScene2 : public Scene { public: - using Scene::Scene; - void load_scene() { - auto & mgr = this->component_manager; + ComponentManager & mgr = this->component_manager; GameObject object1 = mgr.new_object("scene_2", "tag_scene_2", vec2{0, 0}, 0, 1); GameObject object2 = mgr.new_object("scene_2", "tag_scene_2", vec2{0, 1}, 0, 1); GameObject object3 = mgr.new_object("scene_2", "tag_scene_2", vec2{0, 2}, 0, 1); @@ -39,6 +36,21 @@ public: string get_name() const { return "scene2"; } }; +class ConcreteScene3 : public Scene { +public: + ConcreteScene3(const string & name) : name(name) {} + + void load_scene() { + ComponentManager & mgr = this->component_manager; + GameObject object1 = mgr.new_object("scene_3", "tag_scene_3", vec2{0, 0}, 0, 1); + } + + string get_name() const { return name; } + +private: + const string name; +}; + class SceneManagerTest : public ::testing::Test { public: ComponentManager component_mgr{}; @@ -124,3 +136,25 @@ TEST_F(SceneManagerTest, loadScene) { EXPECT_EQ(transform[3].get().position.x, 0); EXPECT_EQ(transform[3].get().position.y, 3); } + +TEST_F(SceneManagerTest, perfectForwarding) { + scene_mgr.add_scene<ConcreteScene3>("scene3"); + + scene_mgr.load_next_scene(); + + vector<reference_wrapper<Metadata>> metadata + = component_mgr.get_components_by_type<Metadata>(); + vector<reference_wrapper<Transform>> transform + = component_mgr.get_components_by_type<Transform>(); + + EXPECT_EQ(metadata.size(), 1); + EXPECT_EQ(transform.size(), 1); + + EXPECT_EQ(metadata[0].get().game_object_id, 0); + EXPECT_EQ(metadata[0].get().name, "scene_3"); + EXPECT_EQ(metadata[0].get().tag, "tag_scene_3"); + EXPECT_EQ(metadata[0].get().parent, -1); + EXPECT_EQ(metadata[0].get().children.size(), 0); + EXPECT_EQ(transform[0].get().position.x, 0); + EXPECT_EQ(transform[0].get().position.y, 0); +} diff --git a/src/test/main.cpp b/src/test/main.cpp index ec293a6..aece72d 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -1,15 +1,30 @@ -#include <crepe/api/Config.h> - #include <gtest/gtest.h> +#define protected public +#define private public + +#include <crepe/api/Config.h> + using namespace crepe; using namespace testing; +class GlobalConfigReset : public EmptyTestEventListener { +public: + Config & cfg = Config::get_instance(); + Config cfg_default = Config(); + + // This function is called before each test + void OnTestStart(const TestInfo &) override { + cfg = cfg_default; + cfg.log.level = Log::Level::WARNING; + } +}; + int main(int argc, char ** argv) { InitGoogleTest(&argc, argv); - auto & cfg = Config::get_instance(); - cfg.log.level = Log::Level::DEBUG; + UnitTest & ut = *UnitTest::GetInstance(); + ut.listeners().Append(new GlobalConfigReset); return RUN_ALL_TESTS(); } |