diff options
Diffstat (limited to 'src/crepe/api')
-rw-r--r-- | src/crepe/api/AssetManager.cpp | 25 | ||||
-rw-r--r-- | src/crepe/api/AssetManager.h | 57 | ||||
-rw-r--r-- | src/crepe/api/AudioSource.cpp | 2 | ||||
-rw-r--r-- | src/crepe/api/CMakeLists.txt | 1 | ||||
-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.h | 28 | ||||
-rw-r--r-- | src/crepe/api/Texture.cpp | 39 | ||||
-rw-r--r-- | src/crepe/api/Texture.h | 31 | ||||
-rw-r--r-- | src/crepe/api/Transform.h | 13 |
11 files changed, 295 insertions, 2 deletions
diff --git a/src/crepe/api/AssetManager.cpp b/src/crepe/api/AssetManager.cpp new file mode 100644 index 0000000..0ecdae5 --- /dev/null +++ b/src/crepe/api/AssetManager.cpp @@ -0,0 +1,25 @@ + + +#include "resource_manager.h" +#include <string> +#include <unordered_map> + +using namespace crepe::api; + +ResourceManager& ResourceManager::get_instance(){ + static ResourceManager instance; + return instance; +} + + +ResourceManager::~ResourceManager(){ + m_resources.clear(); +} + + +void ResourceManager::Unload(const std::string& file_path){ + if(m_resources.find(file_path) != m_resources.end()){ + m_resources.erase(file_path); + } +} + diff --git a/src/crepe/api/AssetManager.h b/src/crepe/api/AssetManager.h new file mode 100644 index 0000000..a646d95 --- /dev/null +++ b/src/crepe/api/AssetManager.h @@ -0,0 +1,57 @@ +#pragma once + + + +#include <memory> +#include <string> +#include <unordered_map> +#include <utility> + +#include "api/baseResource.h" + + +namespace crepe::api{ + +class ResourceManager{ + + +private: + + std::unordered_map< std::string, std::unique_ptr<BaseResource>> m_resources; + + +protected: + ResourceManager() = default; + ~ResourceManager(); + +public: + ResourceManager(const ResourceManager &) = delete; + ResourceManager(ResourceManager &&) = delete; + ResourceManager &operator=(const ResourceManager &) = delete; + ResourceManager &operator=(ResourceManager &&) = delete; + + static ResourceManager& get_instance(); + + + +public: + template<typename T> + T* Load(const std::string& file_path){ + + if (m_resources.find(file_path) != m_resources.end()) { + return static_cast<T*>(m_resources[file_path].get()); + } + + auto resource = std::make_unique<T>(file_path.c_str()); + if (resource) { + m_resources[file_path] = std::move(resource); + return static_cast<T*>(m_resources[file_path].get() ); + } + + return nullptr; + } + + void Unload(const std::string& file_path); + +}; +} diff --git a/src/crepe/api/AudioSource.cpp b/src/crepe/api/AudioSource.cpp index b512d27..a5b6d6a 100644 --- a/src/crepe/api/AudioSource.cpp +++ b/src/crepe/api/AudioSource.cpp @@ -1,6 +1,6 @@ #include "AudioSource.h" -#include "../Sound.h" +#include "facade/Sound.h" #include <memory> using namespace crepe::api; diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 6b337be..9a02580 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -9,4 +9,3 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES BehaviorScript.h Script.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.h b/src/crepe/api/Sprite.h new file mode 100644 index 0000000..84eeb83 --- /dev/null +++ b/src/crepe/api/Sprite.h @@ -0,0 +1,28 @@ +#pragma once + +#include "Component.h" +#include "api/Color.h" +#include "facade/Texture.h" +#include <cstdint> + + +namespace crepe::api { + +struct flip_settings{ + bool flipX : 1; + bool flipY : 1; +}; +class Sprite : public Component { + +public: + Sprite(crepe::Texture& image, const Color& color, const flip_settings& flip ) : sprite_image(&image), color(color), flip(flip){} + crepe::Texture* sprite_image; + Color color; + flip_settings flip; + uint8_t sortingLayer; + uint8_t orderInLayer; + + +}; + +} diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp new file mode 100644 index 0000000..b4e3aa8 --- /dev/null +++ b/src/crepe/api/Texture.cpp @@ -0,0 +1,39 @@ + + +#include "util/log.h" + +#include "Texture.h" +#include "SdlContext.h" +#include <SDL2/SDL_render.h> + +using namespace crepe; + +Texture::Texture(std::unique_ptr<api::Resource> res) { + dbg_trace(); + this->load(std::move(res)); +} + +Texture::Texture(const char * src) { + dbg_trace(); + this->load(std::make_unique<api::Resource>(src)); +} + +Texture::~Texture(){ + dbg_trace(); + if(this->m_texture){ + SDL_DestroyTexture(m_texture); + } +} +void Texture::load(std::unique_ptr<api::Resource> res) { + dbg_trace(); + SdlContext& ctx = SdlContext::get_instance(); + m_texture = ctx.setTextureFromPath(res->canonical(), srcrect, 1, 1); +} + +SDL_Texture* Texture::get_texture() const{ + return m_texture; +} + +SDL_Rect& Texture::get_rect() { + return srcrect; +} diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h new file mode 100644 index 0000000..db2f1f9 --- /dev/null +++ b/src/crepe/api/Texture.h @@ -0,0 +1,31 @@ +#pragma once + +#include "SDL_rect.h" +#include "api/baseResource.h" +#include "api/Resource.h" +#include <SDL2/SDL_render.h> +#include <memory> + +namespace crepe { + + + +class Texture : public api::BaseResource{ + +public: + Texture(const char * src); + Texture(std::unique_ptr<api::Resource> res); + ~Texture(); + + SDL_Texture* get_texture() const; + SDL_Rect& get_rect() ; +private: + void load(std::unique_ptr<api::Resource> res); + +private: + SDL_Texture* m_texture; + SDL_Rect srcrect; +}; + +} // namespace crepe + diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h new file mode 100644 index 0000000..d4dfafc --- /dev/null +++ b/src/crepe/api/Transform.h @@ -0,0 +1,13 @@ +#pragma once + +#include "api/Component.h" +#include "api/Point.h" +namespace crepe::api { + +class Transform : public Component { +public: + Point position; // Translation (shift) + double rotation; // Rotation, in radians + double scale; // Multiplication factoh +}; +} // namespace crepe::api |