aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api
diff options
context:
space:
mode:
authorheavydemon21 <nielsstunnebrink1@gmail.com>2024-11-08 11:44:21 +0100
committerheavydemon21 <nielsstunnebrink1@gmail.com>2024-11-08 11:44:21 +0100
commit9432900158e6a31815345fcf0af8d28ae34c6da9 (patch)
tree36f799da46ba8f5e921349d0f004b08d6d2f29ea /src/crepe/api
parent1c4156ee127b14760ed3b1a0cd16ad12180c7ac6 (diff)
code style
Diffstat (limited to 'src/crepe/api')
-rw-r--r--src/crepe/api/Animator.cpp23
-rw-r--r--src/crepe/api/Animator.h87
-rw-r--r--src/crepe/api/CMakeLists.txt4
-rw-r--r--src/crepe/api/Camera.cpp15
-rw-r--r--src/crepe/api/Camera.h70
-rw-r--r--src/crepe/api/Color.cpp3
-rw-r--r--src/crepe/api/Color.h17
-rw-r--r--src/crepe/api/Sprite.cpp4
-rw-r--r--src/crepe/api/Sprite.h75
-rw-r--r--src/crepe/api/Texture.cpp17
-rw-r--r--src/crepe/api/Texture.h60
11 files changed, 356 insertions, 19 deletions
diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp
new file mode 100644
index 0000000..3834e0b
--- /dev/null
+++ b/src/crepe/api/Animator.cpp
@@ -0,0 +1,23 @@
+
+
+#include "Animator.h"
+#include "Component.h"
+#include "api/Sprite.h"
+
+#include "util/log.h"
+#include <cstdint>
+
+using namespace crepe;
+
+Animator::Animator(uint32_t id, Sprite & ss, int row, int col, int col_animator)
+ : Component(id), spritesheet(ss), row(row), 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->active = false;
+}
+Animator::~Animator() { dbg_trace(); }
diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h
new file mode 100644
index 0000000..3493623
--- /dev/null
+++ b/src/crepe/api/Animator.h
@@ -0,0 +1,87 @@
+#pragma once
+
+#include "Component.h"
+#include "api/Sprite.h"
+#include <cstdint>
+
+namespace crepe {
+class AnimatorSystem;
+class SDLContext;
+
+
+/**
+ * \brief The Animator component is used to animate sprites by managing the movement
+ * and frame changes within a sprite sheet.
+ *
+ * This component allows for controlling sprite animation through rows and columns of a sprite sheet.
+ * It can be used to play animations, loop them, or stop them.
+ */
+class Animator : public Component {
+
+public:
+ //TODO: need to implement this
+ void loop();
+ void stop();
+
+public:
+ /**
+ * \brief Constructs an Animator object that will control animations for a sprite sheet.
+ *
+ * \param[in] id The unique identifier for the component, typically assigned automatically.
+ * \param[in] spritesheet A reference to the Sprite object which holds the sprite sheet for animation.
+ * \param[in] row The maximum number of rows in the sprite sheet.
+ * \param[in] col The maximum number of columns in the sprite sheet.
+ * \param[in] col__animate The specific col index of the sprite sheet to animate. This allows selecting which col to animate from multiple col in the sheet.
+ *
+ * This constructor sets up the Animator with the given parameters, and initializes the animation system.
+ */
+ Animator(uint32_t id, Sprite & spritesheet, int row, int col,
+ int col_animate);
+
+ /**
+ * \brief Destroys the Animator object and cleans up any resources.
+ *
+ * This destructor will handle any necessary cleanup when the Animator component is no longer needed.
+ */
+ ~Animator();
+
+ //! Deleted copy constructor to prevent copying of the Animator instance.
+ Animator(const Animator &) = delete;
+
+ //! Deleted move constructor to prevent moving of the Animator instance.
+ Animator(Animator &&) = delete;
+
+ //! Deleted copy assignment operator to prevent copying the Animator instance.
+ Animator & operator=(const Animator &) = delete;
+
+ //! Deleted move assignment operator to prevent moving the Animator instance.
+ Animator & operator=(Animator &&) = delete;
+
+private:
+ //! A reference to the Sprite sheet containing the animation frames.
+ Sprite & spritesheet;
+
+ //! The maximum number of columns in the sprite sheet.
+ const int col;
+
+ //! The maximum number of rows in the sprite sheet.
+ const int row;
+
+ //! The current col being animated.
+ int curr_col = 0;
+
+ //! The current row being animated.
+ int curr_row = 0;
+
+ Rect animator_rect;
+
+ //TODO: Is this necessary?
+ //int fps;
+
+private:
+ //! Friend class that can directly access the private members of the Animator.
+ friend class AnimatorSystem;
+ friend class SDLContext;
+};
+} // namespace crepe
+//
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt
index 3b20142..87cbb09 100644
--- a/src/crepe/api/CMakeLists.txt
+++ b/src/crepe/api/CMakeLists.txt
@@ -16,6 +16,8 @@ target_sources(crepe PUBLIC
Scene.cpp
SceneManager.cpp
Vector2.cpp
+ Camera.cpp
+ Animator.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
@@ -38,4 +40,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
Metadata.h
SceneManager.h
SceneManager.hpp
+ Camera.h
+ Animator.h
)
diff --git a/src/crepe/api/Camera.cpp b/src/crepe/api/Camera.cpp
new file mode 100644
index 0000000..46a56b2
--- /dev/null
+++ b/src/crepe/api/Camera.cpp
@@ -0,0 +1,15 @@
+
+
+#include "Camera.h"
+#include "Component.h"
+#include "api/Color.h"
+#include "util/log.h"
+#include <cstdint>
+
+using namespace crepe;
+
+Camera::Camera(uint32_t id, const Color& color) : Component(id), bg_color(color), aspect_width(640), aspect_height(480), zoom(1), x(0),y(0){
+ dbg_trace();
+}
+
+Camera::~Camera() { dbg_trace(); }
diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h
new file mode 100644
index 0000000..022496d
--- /dev/null
+++ b/src/crepe/api/Camera.h
@@ -0,0 +1,70 @@
+#pragma once
+
+#include "Component.h"
+#include "api/Color.h"
+#include <cstdint>
+
+namespace crepe {
+
+/**
+ * \class Camera
+ * \brief Represents a camera component for rendering in the game.
+ *
+ * The Camera class defines the view parameters, including background color,
+ * aspect ratio, position, and zoom level. It controls what part of the game
+ * world is visible on the screen.
+ */
+class Camera : public Component {
+
+public:
+ /**
+ * \brief Constructs a Camera with the specified ID and background color.
+ * \param id Unique identifier for the camera component.
+ * \param bg_color Background color for the camera view.
+ */
+ Camera(uint32_t id, const Color & bg_color);
+
+ /**
+ * \brief Destroys the Camera instance.
+ */
+ ~Camera();
+
+public:
+ /**
+ * \brief Background color of the camera view.
+ */
+ Color bg_color;
+
+ /**
+ * \brief Aspect ratio height for the camera.
+ */
+ double aspect_height;
+
+ /**
+ * \brief Aspect ratio width for the camera.
+ */
+ double aspect_width;
+
+ /**
+ * \brief X-coordinate of the camera position.
+ */
+ double x;
+
+ /**
+ * \brief Y-coordinate of the camera position.
+ */
+ double y;
+
+ /**
+ * \brief Zoom level of the camera view.
+ */
+ 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; }
+};
+} // namespace crepe
diff --git a/src/crepe/api/Color.cpp b/src/crepe/api/Color.cpp
index fc6313d..9e5f187 100644
--- a/src/crepe/api/Color.cpp
+++ b/src/crepe/api/Color.cpp
@@ -11,8 +11,7 @@ Color Color::cyan = Color(0, 255, 255, 0);
Color Color::yellow = Color(255, 255, 0, 0);
Color Color::magenta = Color(255, 0, 255, 0);
-// FIXME: do we really need double precision for color values?
-Color::Color(double red, double green, double blue, double alpha) {
+Color::Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha) {
this->a = alpha;
this->r = red;
this->g = green;
diff --git a/src/crepe/api/Color.h b/src/crepe/api/Color.h
index 6b54888..4ebe3a3 100644
--- a/src/crepe/api/Color.h
+++ b/src/crepe/api/Color.h
@@ -1,6 +1,8 @@
#pragma once
-namespace crepe {
+#include <cstdint>
+
+namespace crepe{
class Color {
@@ -8,7 +10,7 @@ class Color {
// instead?
public:
- Color(double red, double green, double blue, double alpha);
+ Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha);
static const Color & get_white();
static const Color & get_red();
static const Color & get_green();
@@ -19,10 +21,10 @@ public:
static const Color & get_black();
private:
- double r;
- double g;
- double b;
- double a;
+ uint8_t r;
+ uint8_t g;
+ uint8_t b;
+ uint8_t a;
static Color white;
static Color red;
@@ -32,6 +34,9 @@ private:
static Color magenta;
static Color yellow;
static Color black;
+
+private:
+ friend class SDLContext;
};
} // namespace crepe
diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp
index d3465c7..3db8f2b 100644
--- a/src/crepe/api/Sprite.cpp
+++ b/src/crepe/api/Sprite.cpp
@@ -6,6 +6,7 @@
#include "Component.h"
#include "Sprite.h"
#include "Texture.h"
+#include "facade/SDLContext.h"
using namespace std;
using namespace crepe;
@@ -14,6 +15,9 @@ Sprite::Sprite(game_object_id_t id, shared_ptr<Texture> image,
const Color & color, const FlipSettings & flip)
: Component(id), color(color), flip(flip), sprite_image(image) {
dbg_trace();
+
+ this->sprite_rect.w = sprite_image->get_width();
+ this->sprite_rect.h = sprite_image->get_height();
}
Sprite::~Sprite() { dbg_trace(); }
diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h
index 00dcb27..2e8b52a 100644
--- a/src/crepe/api/Sprite.h
+++ b/src/crepe/api/Sprite.h
@@ -1,6 +1,5 @@
#pragma once
-#include <SDL2/SDL_rect.h>
#include <cstdint>
#include <memory>
@@ -11,22 +10,92 @@
namespace crepe {
+/**
+ * \struct Rect
+ * \brief Represents a rectangle area for rendering.
+ *
+ * Everything within the defined rectangle will be rendered.
+ * The SDLContext will translate this into the library's rectangle structure.
+ */
+struct Rect {
+ int w = 0;
+ int h = 0;
+ int x = 0;
+ int y = 0;
+};
+
+/**
+ * \struct FlipSettings
+ * \brief Flip settings for the sprite.
+ *
+ * Defines the horizontal and vertical flip settings for a sprite, which the
+ * SDLContext will translate into the corresponding settings for the library.
+ */
struct FlipSettings {
- bool flip_x = true;
- bool flip_y = true;
+ bool flip_x = false;
+ bool flip_y = false;
};
+//! Forward declaration of the SDLContext facade.
+class SDLContext;
+
+//! Forward declaration of the Animator class.
+class Animator;
+
+//! Forward declaration of the AnimatorSystem class.
+class AnimatorSystem;
+
+/**
+ * \class Sprite
+ * \brief Represents a renderable sprite component.
+ *
+ * A renderable sprite that can be displayed in the game. It includes a texture,
+ * color, and flip settings, and is managed in layers with defined sorting orders.
+ */
class Sprite : public Component {
public:
+ /**
+ * \brief Constructs a Sprite with specified parameters.
+ * \param game_id Unique identifier for the game object this sprite belongs to.
+ * \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.
+ */
Sprite(game_object_id_t id, std::shared_ptr<Texture> image,
const Color & color, const FlipSettings & flip);
+
+ /**
+ * \brief Destroys the Sprite instance.
+ */
~Sprite();
+
+
+ //! Texture used for the sprite
std::shared_ptr<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;
+ //! Order within the sorting layer
uint8_t order_in_layer;
+
+public:
+ /**
+ * \brief Gets the maximum number of instances allowed for this sprite.
+ * \return Maximum instance count as an integer.
+ */
+ virtual int get_instances_max() const { return 10; }
+
+private:
+ friend class SDLContext;
+ friend class Animator;
+ friend class AnimatorSystem;
+
+ //! Render area of the sprite
+ Rect sprite_rect;
};
} // namespace crepe
diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp
index 8fc5c13..5519e5e 100644
--- a/src/crepe/api/Texture.cpp
+++ b/src/crepe/api/Texture.cpp
@@ -30,3 +30,20 @@ void Texture::load(unique_ptr<Asset> res) {
SDLContext & ctx = SDLContext::get_instance();
this->texture = ctx.texture_from_path(res->canonical());
}
+
+int Texture::get_width() const{
+ if (this->texture) {
+ return SDLContext::get_instance().get_width(*this);
+ }
+ else {
+ return 0;
+ }
+}
+int Texture::get_height() const{
+ if (this->texture) {
+ return SDLContext::get_instance().get_height(*this);
+ }
+ else {
+ return 0;
+ }
+}
diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h
index 9a86f6f..6d99a93 100644
--- a/src/crepe/api/Texture.h
+++ b/src/crepe/api/Texture.h
@@ -3,31 +3,75 @@
// FIXME: this header can't be included because this is an API header, and SDL2
// development headers won't be bundled with crepe. Why is this facade in the
// API namespace?
+
#include <SDL2/SDL_render.h>
#include <memory>
#include "Asset.h"
namespace crepe {
+
+//! Forward declaration of SDLContext class.
class SDLContext;
-}
-namespace crepe {
+//! Forward declaration of Animator class.
+class Animator;
+/**
+ * \class Texture
+ * \brief Manages texture loading and properties.
+ *
+ * The Texture class is responsible for loading an image from a source
+ * and providing access to its dimensions. Textures can be used for rendering.
+ */
class Texture {
public:
- Texture(const char * src);
- Texture(std::unique_ptr<Asset> res);
- ~Texture();
+ /**
+ * \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.
+ */
+ Texture(std::unique_ptr<Asset> res);
+
+ /**
+ * \brief Destroys the Texture instance, freeing associated resources.
+ */
+ ~Texture();
+
+ /**
+ * \brief Gets the width of the texture.
+ * \return Width of the texture in pixels.
+ */
+ int get_width() const;
+
+ /**
+ * \brief Gets the height of the texture.
+ * \return Height of the texture in pixels.
+ */
+ int get_height() const;
private:
- void load(std::unique_ptr<Asset> res);
+ /**
+ * \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);
private:
- SDL_Texture * texture = nullptr;
+ //! The texture of the class from the library
+ SDL_Texture * texture = nullptr;
+
+ //! Grants SDLContext access to private members.
+ friend class SDLContext;
- friend class crepe::SDLContext;
+ //! Grants Animator access to private members.
+ friend class Animator;
};
} // namespace crepe