aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/api')
-rw-r--r--src/crepe/api/AssetManager.cpp23
-rw-r--r--src/crepe/api/AssetManager.h50
-rw-r--r--src/crepe/api/AudioSource.cpp4
-rw-r--r--src/crepe/api/AudioSource.h4
-rw-r--r--src/crepe/api/BehaviorScript.cpp1
-rw-r--r--src/crepe/api/BehaviorScript.h2
-rw-r--r--src/crepe/api/CMakeLists.txt21
-rw-r--r--src/crepe/api/Collider.cpp5
-rw-r--r--src/crepe/api/Collider.h14
-rw-r--r--src/crepe/api/Color.cpp53
-rw-r--r--src/crepe/api/Color.h34
-rw-r--r--src/crepe/api/GameObject.cpp7
-rw-r--r--src/crepe/api/GameObject.h24
-rw-r--r--src/crepe/api/GameObject.hpp15
-rw-r--r--src/crepe/api/Point.h14
-rw-r--r--src/crepe/api/Rigidbody.cpp7
-rw-r--r--src/crepe/api/Rigidbody.h24
-rw-r--r--src/crepe/api/Sprite.cpp18
-rw-r--r--src/crepe/api/Sprite.h30
-rw-r--r--src/crepe/api/Texture.cpp32
-rw-r--r--src/crepe/api/Texture.h32
-rw-r--r--src/crepe/api/Transform.cpp13
-rw-r--r--src/crepe/api/Transform.h15
23 files changed, 436 insertions, 6 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 b512d27..b246cc9 100644
--- a/src/crepe/api/AudioSource.cpp
+++ b/src/crepe/api/AudioSource.cpp
@@ -1,6 +1,8 @@
+#include <memory>
+
#include "AudioSource.h"
-#include "../Sound.h"
+#include "Sound.h"
#include <memory>
using namespace crepe::api;
diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h
index 2d26cda..7980212 100644
--- a/src/crepe/api/AudioSource.h
+++ b/src/crepe/api/AudioSource.h
@@ -2,8 +2,8 @@
#include <memory>
-#include "Asset.h"
-#include "Component.h"
+#include "../Asset.h"
+#include "../Component.h"
namespace crepe {
class Sound;
diff --git a/src/crepe/api/BehaviorScript.cpp b/src/crepe/api/BehaviorScript.cpp
index 063d225..74788ec 100644
--- a/src/crepe/api/BehaviorScript.cpp
+++ b/src/crepe/api/BehaviorScript.cpp
@@ -1,7 +1,6 @@
#include "../util/log.h"
#include "BehaviorScript.h"
-#include "Script.h"
using namespace crepe::api;
diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h
index 6ce6798..25d3cc5 100644
--- a/src/crepe/api/BehaviorScript.h
+++ b/src/crepe/api/BehaviorScript.h
@@ -2,7 +2,7 @@
#include <memory>
-#include "Component.h"
+#include "../Component.h"
#include "Script.h"
namespace crepe {
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt
index 6b337be..37c194d 100644
--- a/src/crepe/api/CMakeLists.txt
+++ b/src/crepe/api/CMakeLists.txt
@@ -2,11 +2,30 @@ 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
+ Sprite.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
# AudioSource.h
BehaviorScript.h
Script.h
+ GameObject.h
+ GameObject.hpp
+ Collider.h
+ Rigidbody.h
+ Sprite.h
+ Point.h
+ Transform.h
+ Color.h
+ Sprite.h
+ Texture.h
+ AssetManager.h
)
-
diff --git a/src/crepe/api/Collider.cpp b/src/crepe/api/Collider.cpp
new file mode 100644
index 0000000..c3e4929
--- /dev/null
+++ b/src/crepe/api/Collider.cpp
@@ -0,0 +1,5 @@
+#include "Collider.h"
+
+using namespace crepe::api;
+
+Collider::Collider(uint32_t gameObjectId) : Component(gameObjectId){}
diff --git a/src/crepe/api/Collider.h b/src/crepe/api/Collider.h
new file mode 100644
index 0000000..1d540de
--- /dev/null
+++ b/src/crepe/api/Collider.h
@@ -0,0 +1,14 @@
+#pragma once
+
+#include "../Component.h"
+
+namespace crepe::api {
+
+class Collider : public Component {
+public:
+ Collider(uint32_t gameObjectId);
+
+ int size;
+};
+
+} // namespace crepe::api
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/GameObject.cpp b/src/crepe/api/GameObject.cpp
new file mode 100644
index 0000000..b167187
--- /dev/null
+++ b/src/crepe/api/GameObject.cpp
@@ -0,0 +1,7 @@
+#include "GameObject.h"
+
+using namespace crepe::api;
+using namespace std;
+
+GameObject::GameObject(uint32_t id, string name, string tag, int layer)
+ : id(id), name(name), tag(tag), active(true), layer(layer) {}
diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h
new file mode 100644
index 0000000..57508c5
--- /dev/null
+++ b/src/crepe/api/GameObject.h
@@ -0,0 +1,24 @@
+#pragma once
+
+#include <cstdint>
+#include <string>
+
+namespace crepe::api {
+
+class GameObject {
+public:
+ GameObject(uint32_t id, std::string name, std::string tag, int layer);
+
+ template <typename T, typename... Args>
+ T & add_component(Args &&... args);
+
+ uint32_t id;
+ std::string name;
+ std::string tag;
+ bool active;
+ int layer;
+};
+
+} // namespace crepe::api
+
+#include "GameObject.hpp"
diff --git a/src/crepe/api/GameObject.hpp b/src/crepe/api/GameObject.hpp
new file mode 100644
index 0000000..8295ea3
--- /dev/null
+++ b/src/crepe/api/GameObject.hpp
@@ -0,0 +1,15 @@
+#pragma once
+
+#include "../ComponentManager.h"
+
+#include "GameObject.h"
+
+namespace crepe::api {
+
+template <typename T, typename... Args>
+T & GameObject::add_component(Args &&... args) {
+ auto & mgr = ComponentManager::get_instance();
+ return mgr.add_component<T>(id, std::forward<Args>(args)...);
+}
+
+} // 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/Rigidbody.cpp b/src/crepe/api/Rigidbody.cpp
new file mode 100644
index 0000000..30a2cff
--- /dev/null
+++ b/src/crepe/api/Rigidbody.cpp
@@ -0,0 +1,7 @@
+#include "Rigidbody.h"
+
+using namespace crepe::api;
+
+Rigidbody::Rigidbody(uint32_t gameObjectId,int mass, int gravityScale, BodyType bodyType)
+ : Component(gameObjectId), mass(mass), gravity_scale(gravityScale), body_type(bodyType) {}
+
diff --git a/src/crepe/api/Rigidbody.h b/src/crepe/api/Rigidbody.h
new file mode 100644
index 0000000..548650a
--- /dev/null
+++ b/src/crepe/api/Rigidbody.h
@@ -0,0 +1,24 @@
+#pragma once
+
+#include "Component.h"
+#include <cstdint>
+
+namespace crepe {
+
+enum class BodyType {
+ Static, // Does not move (e.g. walls, ground ...)
+ Dynamic, // Moves and responds to forces (e.g. player, physics objects ...)
+ Kinematic // Moves but does not respond to forces (e.g. moving platforms ...)
+};
+
+class Rigidbody : public Component {
+public:
+ Rigidbody(uint32_t gameObjectId,int mass, int gravityScale, BodyType bodyType);
+ int32_t velocity_x;
+ int32_t velocity_y;
+ int mass;
+ int gravity_scale;
+ BodyType body_type;
+};
+
+} // namespace crepe::api
diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp
new file mode 100644
index 0000000..d9e26ab
--- /dev/null
+++ b/src/crepe/api/Sprite.cpp
@@ -0,0 +1,18 @@
+
+
+#include "Sprite.h"
+#include "api/Texture.h"
+#include "util/log.h"
+#include <memory>
+#include <utility>
+
+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() { dbg_trace(); }
diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h
new file mode 100644
index 0000000..920f91e
--- /dev/null
+++ b/src/crepe/api/Sprite.h
@@ -0,0 +1,30 @@
+#pragma once
+
+#include "Component.h"
+#include "api/Color.h"
+#include "api/Texture.h"
+#include <SDL2/SDL_rect.h>
+#include <cstdint>
+#include <memory>
+
+
+namespace crepe::api {
+
+struct flip_settings{
+ bool flip_x: 1;
+ bool flip_y : 1;
+};
+class Sprite : public Component {
+
+public:
+ 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;
+
+};
+
+}
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