diff options
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 | 3 | ||||
-rw-r--r-- | src/crepe/api/CMakeLists.txt | 12 | ||||
-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 | 15 |
13 files changed, 311 insertions, 10 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..b246cc9 100644 --- a/src/crepe/api/AudioSource.cpp +++ b/src/crepe/api/AudioSource.cpp @@ -2,7 +2,8 @@ #include "AudioSource.h" -#include "../Sound.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 ae92f3d..37c194d 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -2,6 +2,11 @@ target_sources(crepe PUBLIC # AudioSource.cpp BehaviorScript.cpp Script.cpp + Color.cpp + Texture.cpp + Sprite.cpp + Transform.cpp + AssetManager.cpp GameObject.cpp Collider.cpp Rigidbody.cpp @@ -17,5 +22,10 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Collider.h Rigidbody.h Sprite.h + Point.h + Transform.h + Color.h + Sprite.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 aa7dbb0..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(string path) : path(path) {} +Sprite::~Sprite() { dbg_trace(); } diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 580f825..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(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 new file mode 100644 index 0000000..c83461f --- /dev/null +++ b/src/crepe/api/Transform.cpp @@ -0,0 +1,13 @@ + + +#include "Transform.h" +#include "api/Point.h" +#include "util/log.h" + +using namespace crepe::api; + +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 new file mode 100644 index 0000000..a34ebb1 --- /dev/null +++ b/src/crepe/api/Transform.h @@ -0,0 +1,15 @@ +#pragma once + +#include "Component.h" +#include "api/Point.h" +namespace crepe::api { + +class Transform : public Component { +public: + Transform(Point&, double, double); + ~Transform(); + Point position; // Translation (shift) + double rotation; // Rotation, in radians + double scale; // Multiplication factoh +}; +} // namespace crepe::api |