diff options
author | jaroWMR <jarorutjes07@gmail.com> | 2024-10-23 19:57:44 +0200 |
---|---|---|
committer | jaroWMR <jarorutjes07@gmail.com> | 2024-10-23 19:57:44 +0200 |
commit | edbb6c892299e3b7f93638abcc9e55b2cfce2358 (patch) | |
tree | d9c306af19636f5c757397ec1c1a157a93c12f0c /src/crepe/api | |
parent | 1b96c6e3c57b9d1dc5fb02cbd24b625d7f7f5b05 (diff) | |
parent | eaa05e7a981b0f581f5393882e4753d9294a3dba (diff) |
merge with niels/rendering
Diffstat (limited to 'src/crepe/api')
-rw-r--r-- | src/crepe/api/AssetManager.cpp | 23 | ||||
-rw-r--r-- | src/crepe/api/AssetManager.h | 50 | ||||
-rw-r--r-- | src/crepe/api/AudioSource.cpp | 1 | ||||
-rw-r--r-- | src/crepe/api/CMakeLists.txt | 8 | ||||
-rw-r--r-- | src/crepe/api/Color.cpp | 53 | ||||
-rw-r--r-- | src/crepe/api/Color.h | 34 | ||||
-rw-r--r-- | src/crepe/api/Point.h | 14 | ||||
-rw-r--r-- | src/crepe/api/Sprite.cpp | 16 | ||||
-rw-r--r-- | src/crepe/api/Sprite.h | 24 | ||||
-rw-r--r-- | src/crepe/api/Texture.cpp | 32 | ||||
-rw-r--r-- | src/crepe/api/Texture.h | 32 | ||||
-rw-r--r-- | src/crepe/api/Transform.cpp | 13 | ||||
-rw-r--r-- | src/crepe/api/Transform.h | 23 |
13 files changed, 296 insertions, 27 deletions
diff --git a/src/crepe/api/AssetManager.cpp b/src/crepe/api/AssetManager.cpp new file mode 100644 index 0000000..f6cc369 --- /dev/null +++ b/src/crepe/api/AssetManager.cpp @@ -0,0 +1,23 @@ + + +#include "AssetManager.h" +#include "util/log.h" + + +using namespace crepe::api; + +AssetManager& AssetManager::get_instance(){ + static AssetManager instance; + return instance; +} + + +AssetManager::~AssetManager(){ + dbg_trace(); + this->asset_cache.clear(); +} + +AssetManager::AssetManager(){ + dbg_trace(); +} + diff --git a/src/crepe/api/AssetManager.h b/src/crepe/api/AssetManager.h new file mode 100644 index 0000000..1b8b86f --- /dev/null +++ b/src/crepe/api/AssetManager.h @@ -0,0 +1,50 @@ +#pragma once + + + +#include <any> +#include <memory> +#include <string> +#include <unordered_map> +#include <utility> + + +namespace crepe::api{ + +class AssetManager{ + + +private: + std::unordered_map< std::string, std::any> asset_cache; + +private: + AssetManager(); + virtual ~AssetManager(); + +public: + AssetManager(const AssetManager &) = delete; + AssetManager(AssetManager &&) = delete; + AssetManager &operator=(const AssetManager &) = delete; + AssetManager &operator=(AssetManager &&) = delete; + + + static AssetManager& get_instance(); + + +public: + template<typename asset> + std::shared_ptr<asset> cache(const std::string& file_path, bool reload = false){ + auto it = asset_cache.find(file_path); + + if (!reload && it != asset_cache.end()) { + return std::any_cast<std::shared_ptr<asset>>(it->second); + } + + std::shared_ptr<asset> new_asset = std::make_shared<asset>(file_path.c_str()); + + asset_cache[file_path] = new_asset; + + return new_asset; + } +}; +} diff --git a/src/crepe/api/AudioSource.cpp b/src/crepe/api/AudioSource.cpp index bb067dc..10b3b49 100644 --- a/src/crepe/api/AudioSource.cpp +++ b/src/crepe/api/AudioSource.cpp @@ -3,6 +3,7 @@ #include "AudioSource.h" #include "../Sound.h" +#include <memory> using namespace crepe::api; diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index ef01594..f6de96e 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -8,6 +8,9 @@ target_sources(crepe PUBLIC Force.cpp ParticleEmitter.cpp Transform.cpp + Color.cpp + Texture.cpp + AssetManager.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -21,5 +24,8 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Force.h ParticleEmitter.h Transform.h + Point.h + Color.h + Texture.h + AssetManager.h ) - diff --git a/src/crepe/api/Color.cpp b/src/crepe/api/Color.cpp new file mode 100644 index 0000000..c73ce6c --- /dev/null +++ b/src/crepe/api/Color.cpp @@ -0,0 +1,53 @@ + + +#include "Color.h" + + +using namespace crepe::api; + +Color Color::white = Color(255,255,255,0); +Color Color::red = Color(255,0,0,0); +Color Color::green = Color(0,255,0,0); +Color Color::blue = Color(0,0,255,0); +Color Color::black = Color(0,0,0,0); +Color Color::cyan = Color(0,255,255,0); +Color Color::yellow = Color(255,255,0,0); +Color Color::magenta= Color(255,0,255,0); + +Color::Color(double red, double green, double blue, double alpha){ + this->a = alpha; + this->r = red; + this->g = green; + this->b = blue; +}; + +const Color& Color::get_white(){ + return Color::white; +}; + +const Color& Color::get_red(){ + return Color::red; +}; +const Color& Color::get_green(){ + return Color::green; +}; +const Color& Color::get_blue(){ + return Color::blue; +}; + +const Color& Color::get_black(){ + return Color::black; +}; + +const Color& Color::get_cyan(){ + return Color::cyan; +}; + +const Color& Color::get_yellow(){ + return Color::yellow; +}; + +const Color& Color::get_magenta(){ + return Color::magenta; +}; + diff --git a/src/crepe/api/Color.h b/src/crepe/api/Color.h new file mode 100644 index 0000000..207434e --- /dev/null +++ b/src/crepe/api/Color.h @@ -0,0 +1,34 @@ +#pragma once + +namespace crepe::api { + +class Color { + +public: + Color(double red, double green, double blue, double alpha); + static const Color & get_white(); + static const Color & get_red(); + static const Color & get_green(); + static const Color & get_blue(); + static const Color & get_cyan(); + static const Color & get_magenta(); + static const Color & get_yellow(); + static const Color & get_black(); + +private: + double r; + double g; + double b; + double a; + + static Color white; + static Color red; + static Color green; + static Color blue; + static Color cyan; + static Color magenta; + static Color yellow; + static Color black; +}; + +} // namespace crepe::api diff --git a/src/crepe/api/Point.h b/src/crepe/api/Point.h new file mode 100644 index 0000000..463aa7c --- /dev/null +++ b/src/crepe/api/Point.h @@ -0,0 +1,14 @@ +#pragma once + + + +namespace crepe::api { + +class Point { +public: + double x; + double y; +}; + + +} diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index 4eda466..d9e26ab 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -1,8 +1,18 @@ -#include <string> + #include "Sprite.h" +#include "api/Texture.h" +#include "util/log.h" +#include <memory> +#include <utility> -using namespace crepe::api; using namespace std; +using namespace crepe; +using namespace crepe::api; + +Sprite::Sprite(shared_ptr<Texture> image, const Color & color, + const flip_settings & flip) : color(color), flip(flip), sprite_image(image) { + dbg_trace(); +} -Sprite::Sprite(uint32_t gameObjectId, string path) : Component(gameObjectId), path(path) {} +Sprite::~Sprite() { dbg_trace(); } diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index c06e76c..920f91e 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -1,16 +1,30 @@ #pragma once -#include <string> +#include "Component.h" +#include "api/Color.h" +#include "api/Texture.h" +#include <SDL2/SDL_rect.h> +#include <cstdint> +#include <memory> -#include "../Component.h" namespace crepe::api { +struct flip_settings{ + bool flip_x: 1; + bool flip_y : 1; +}; class Sprite : public Component { + public: - Sprite(uint32_t gameObjectId,std::string path); + Sprite(std::shared_ptr<Texture> image, const Color& color, const flip_settings& flip ); + ~Sprite(); + std::shared_ptr<Texture> sprite_image; + Color color; + flip_settings flip; + uint8_t sorting_in_layer; + uint8_t order_in_layer; - std::string path; }; -} // namespace crepe::api +} diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp new file mode 100644 index 0000000..ba06c6d --- /dev/null +++ b/src/crepe/api/Texture.cpp @@ -0,0 +1,32 @@ + + +#include "Asset.h" +#include "SdlContext.h" +#include "util/log.h" + +#include "Texture.h" +#include <SDL2/SDL_render.h> + +using namespace crepe::api; + +Texture::Texture(std::unique_ptr<Asset> res) { + dbg_trace(); + this->load(std::move(res)); +} + +Texture::Texture(const char * src) { + dbg_trace(); + this->load(std::make_unique<Asset>(src)); +} + +Texture::~Texture() { + dbg_trace(); + if (this->m_texture != nullptr) { + SDL_DestroyTexture(m_texture); + } +} +void Texture::load(std::unique_ptr<Asset> res) { + SdlContext & ctx = SdlContext::get_instance(); + m_texture = ctx.texture_from_path(res->canonical()); + +} diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h new file mode 100644 index 0000000..b376b44 --- /dev/null +++ b/src/crepe/api/Texture.h @@ -0,0 +1,32 @@ +#pragma once + +#include "Asset.h" +#include <SDL2/SDL_render.h> +#include <memory> + + +namespace crepe { + class SdlContext; +} + +namespace crepe::api { + +class Texture { + +public: + Texture(const char * src); + Texture(std::unique_ptr<Asset> res); + ~Texture(); + + +private: + void load(std::unique_ptr<Asset> res); + + +private: + SDL_Texture * m_texture = nullptr; + + friend class crepe::SdlContext; +}; + +} // namespace crepe diff --git a/src/crepe/api/Transform.cpp b/src/crepe/api/Transform.cpp index 2c39523..c83461f 100644 --- a/src/crepe/api/Transform.cpp +++ b/src/crepe/api/Transform.cpp @@ -1,6 +1,13 @@ + + #include "Transform.h" +#include "api/Point.h" +#include "util/log.h" -using namespace crepe; +using namespace crepe::api; -Transform::Transform(uint32_t gameObjectId,Position position, int rotation, int scale) - : Component(gameObjectId), postion(postion), rotation(rotation), scale(scale) {} +Transform::Transform(Point & point, double rot, double scale) + : position(point), rotation(rot), scale(scale) { + dbg_trace(); +} +Transform::~Transform() { dbg_trace(); } diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h index 3e8d142..a34ebb1 100644 --- a/src/crepe/api/Transform.h +++ b/src/crepe/api/Transform.h @@ -1,22 +1,15 @@ #pragma once #include "Component.h" - -namespace crepe { - -struct Position -{ - int x; - int y; -}; - +#include "api/Point.h" +namespace crepe::api { class Transform : public Component { public: - Transform(uint32_t gameObjectId,Position position, int rotation, int scale); - Position postion; - int rotation; - int scale; + Transform(Point&, double, double); + ~Transform(); + Point position; // Translation (shift) + double rotation; // Rotation, in radians + double scale; // Multiplication factoh }; - -} // namespace crepe +} // namespace crepe::api |