diff options
| m--------- | lib/soloud | 0 | ||||
| -rw-r--r-- | readme.md | 2 | ||||
| -rw-r--r-- | src/crepe/api/Animator.cpp | 23 | ||||
| -rw-r--r-- | src/crepe/api/Animator.h | 76 | ||||
| -rw-r--r-- | src/crepe/api/CMakeLists.txt | 4 | ||||
| -rw-r--r-- | src/crepe/api/Camera.cpp | 17 | ||||
| -rw-r--r-- | src/crepe/api/Camera.h | 55 | ||||
| -rw-r--r-- | src/crepe/api/Color.cpp | 3 | ||||
| -rw-r--r-- | src/crepe/api/Color.h | 17 | ||||
| -rw-r--r-- | src/crepe/api/Sprite.cpp | 7 | ||||
| -rw-r--r-- | src/crepe/api/Sprite.h | 76 | ||||
| -rw-r--r-- | src/crepe/api/Texture.cpp | 15 | ||||
| -rw-r--r-- | src/crepe/api/Texture.h | 54 | ||||
| -rw-r--r-- | src/crepe/api/Transform.h | 2 | ||||
| -rw-r--r-- | src/crepe/system/AnimatorSystem.cpp | 37 | ||||
| -rw-r--r-- | src/crepe/system/AnimatorSystem.h | 44 | ||||
| -rw-r--r-- | src/crepe/system/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/crepe/system/RenderSystem.cpp | 36 | ||||
| -rw-r--r-- | src/crepe/system/RenderSystem.h | 49 | 
19 files changed, 480 insertions, 39 deletions
| diff --git a/lib/soloud b/lib/soloud -Subproject c8e339fdce5c7107bdb3e64bbf707c8fd3449be +Subproject e82fd32c1f62183922f08c14c814a02b58db187 @@ -29,7 +29,7 @@ This project uses the following libraries  |`SDL2`|2.30.9|  |`SDL2_image`|2.8.2|  |`SDL_ttf`|2.22.0| -|`SoLoud`|20200207| +|`SoLoud`|(latest git `master` version)|  |Google Test (`GTest`)|1.15.2|  |Berkeley DB (`libdb`)|5.3.21| diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp new file mode 100644 index 0000000..8b396af --- /dev/null +++ b/src/crepe/api/Animator.cpp @@ -0,0 +1,23 @@ + +#include <cstdint> + +#include "util/log.h" + +#include "Animator.h" +#include "Component.h" +#include "Sprite.h" + +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..def0240 --- /dev/null +++ b/src/crepe/api/Animator.h @@ -0,0 +1,76 @@ +#pragma once + +#include <cstdint> + +#include "Component.h" +#include "Sprite.h" + +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 id The unique identifier for the component, typically assigned automatically. +	 * \param spritesheet A reference to the Sprite object which holds the sprite sheet for animation. +	 * \param row The maximum number of rows in the sprite sheet. +	 * \param col The maximum number of columns in the sprite sheet. +	 * \param 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); + +	~Animator(); // dbg_trace +	Animator(const Animator &) = delete; +	Animator(Animator &&) = delete; +	Animator & operator=(const Animator &) = delete; +	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: +	//! AnimatorSystem adjust the private member parameters of Animator; +	friend class AnimatorSystem; + +	//! SDLContext reads the Animator member var's +	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..820a6a8 --- /dev/null +++ b/src/crepe/api/Camera.cpp @@ -0,0 +1,17 @@ + +#include <cstdint> + +#include "util/log.h" + +#include "Camera.h" +#include "Color.h" +#include "Component.h" + +using namespace crepe; + +Camera::Camera(uint32_t id, const Color & bg_color) +	: Component(id), bg_color(bg_color) { +	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..ba3a9ef --- /dev/null +++ b/src/crepe/api/Camera.h @@ -0,0 +1,55 @@ +#pragma once + +#include <cstdint> + +#include "Color.h" +#include "Component.h" + +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); +	~Camera(); // dbg_trace only + +public: +	//! Background color of the camera view. +	Color bg_color; + +	//! Aspect ratio height for the camera. +	double aspect_height = 480; + +	//! Aspect ratio width for the camera. +	double aspect_width = 640; + +	//! X-coordinate of the camera position. +	double x = 0.0; + +	//! Y-coordinate of the camera position. +	double y = 0.0; + +	//! Zoom level of the camera view. +	double zoom = 1.0; + +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..aa47bf4 100644 --- a/src/crepe/api/Color.h +++ b/src/crepe/api/Color.h @@ -1,14 +1,17 @@  #pragma once +#include <cstdint> +  namespace crepe { +// TODO: make Color a struct w/o constructors/destructors  class Color {  	// FIXME: can't these colors be defined as a `static constexpr const 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 +22,11 @@ public:  	static const Color & get_black();  private: -	double r; -	double g; -	double b; -	double a; +	// TODO: why are these private!? +	uint8_t r; +	uint8_t g; +	uint8_t b; +	uint8_t a;  	static Color white;  	static Color red; @@ -32,6 +36,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..f9cd761 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -1,7 +1,7 @@ -#include <cstdint>  #include <memory>  #include "../util/log.h" +#include "facade/SDLContext.h"  #include "Component.h"  #include "Sprite.h" @@ -10,10 +10,13 @@  using namespace std;  using namespace crepe; -Sprite::Sprite(game_object_id_t id, shared_ptr<Texture> image, +Sprite::Sprite(game_object_id_t id, const 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..deb3f93 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -1,32 +1,88 @@  #pragma once -#include <SDL2/SDL_rect.h>  #include <cstdint>  #include <memory> -#include "api/Color.h" -#include "api/Texture.h" - +#include "Color.h"  #include "Component.h" +#include "Texture.h"  namespace crepe { +struct Rect { +	int w = 0; +	int h = 0; +	int x = 0; +	int y = 0; +}; +  struct FlipSettings { -	bool flip_x = true; -	bool flip_y = true; +	bool flip_x = false; +	bool flip_y = false;  }; +class SDLContext; +class Animator; +class AnimatorSystem; + +/** + * \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: -	Sprite(game_object_id_t id, std::shared_ptr<Texture> image, +	// TODO: Loek comment in github #27 will be looked another time +	// about shared_ptr Texture +	/** +	 * \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, const std::shared_ptr<Texture> image,  		   const Color & color, const FlipSettings & flip); + +	/** +	 * \brief Destroys the Sprite instance. +	 */  	~Sprite(); -	std::shared_ptr<Texture> sprite_image; + +	//! Texture used for the sprite +	const std::shared_ptr<Texture> sprite_image; +	//! Color tint of the sprite  	Color color; +	//! Flip settings for the sprite  	FlipSettings flip; -	uint8_t sorting_in_layer; -	uint8_t order_in_layer; +	//! Layer sorting level of the sprite +	uint8_t sorting_in_layer = 0; +	//! Order within the sorting layer +	uint8_t order_in_layer = 0; + +public: +	/** +	 * \brief Gets the maximum number of instances allowed for this sprite. +	 * \return Maximum instance count as an integer. +	 * +	 * For now is this number randomly picked. I think it will eventually be 1.  +	 */ +	virtual int get_instances_max() const { return 10; } + +private: +	//! Reads the sprite_rect of sprite +	friend class SDLContext; + +	//! Reads the all the variables plus the  sprite_rect +	friend class Animator; + +	//! Reads the all the variables plus the  sprite_rect +	friend class AnimatorSystem; + +	//! Render area of the sprite this will also be adjusted by the AnimatorSystem if an Animator object is present in GameObject +	Rect sprite_rect;  };  } // namespace crepe diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp index 8fc5c13..5ebd23d 100644 --- a/src/crepe/api/Texture.cpp +++ b/src/crepe/api/Texture.cpp @@ -21,12 +21,19 @@ Texture::Texture(const char * src) {  Texture::~Texture() {  	dbg_trace(); -	if (this->texture != nullptr) { -		SDL_DestroyTexture(this->texture); -	} +	this->texture.reset();  }  void Texture::load(unique_ptr<Asset> res) {  	SDLContext & ctx = SDLContext::get_instance(); -	this->texture = ctx.texture_from_path(res->canonical()); +	this->texture = std::move(ctx.texture_from_path(res->canonical())); +} + +int Texture::get_width() const { +	if (this->texture == nullptr) return 0; +	return SDLContext::get_instance().get_width(*this); +} +int Texture::get_height() const { +	if (this->texture == nullptr) return 0; +	return SDLContext::get_instance().get_width(*this);  } diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h index 9a86f6f..b89bc17 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 <functional>  #include <memory>  #include "Asset.h"  namespace crepe { -class SDLContext; -} -namespace crepe { +class SDLContext; +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: +	/** +	 * \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(); +	// FIXME: this constructor shouldn't be necessary because this class doesn't +	// manage memory + +	/** +	 * \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: +	/** +	 * \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 +	std::unique_ptr<SDL_Texture, std::function<void(SDL_Texture *)>> texture; + +	//! Grants SDLContext access to private members. +	friend class SDLContext; -	friend class crepe::SDLContext; +	//! Grants Animator access to private members. +	friend class Animator;  };  } // namespace crepe diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h index d7a5b8a..756e45b 100644 --- a/src/crepe/api/Transform.h +++ b/src/crepe/api/Transform.h @@ -32,7 +32,7 @@ public:  public:  	//! Translation (shift)  	Vector2 position; -	//! Rotation, in radians +	//! Rotation, in degrees  	double rotation;  	//! Multiplication factor  	double scale; diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp new file mode 100644 index 0000000..bf45362 --- /dev/null +++ b/src/crepe/system/AnimatorSystem.cpp @@ -0,0 +1,37 @@ + +#include <cstdint> +#include <functional> +#include <vector> + +#include "api/Animator.h" +#include "facade/SDLContext.h" +#include "util/log.h" + +#include "AnimatorSystem.h" +#include "ComponentManager.h" + +using namespace crepe; + +AnimatorSystem::AnimatorSystem() { dbg_trace(); } +AnimatorSystem::~AnimatorSystem() { dbg_trace(); } + +AnimatorSystem & AnimatorSystem::get_instance() { +	static AnimatorSystem instance; +	return instance; +} + +void AnimatorSystem::update() { +	ComponentManager & mgr = ComponentManager::get_instance(); + +	std::vector<std::reference_wrapper<Animator>> animations +		= mgr.get_components_by_type<Animator>(); + +	uint64_t tick = SDLContext::get_instance().get_ticks(); +	for (Animator & a : animations) { +		if (a.active) { +			a.curr_row = (tick / 100) % a.row; +			a.animator_rect.x = (a.curr_row * a.animator_rect.w) + a.curr_col; +			a.spritesheet.sprite_rect = a.animator_rect; +		} +	} +} diff --git a/src/crepe/system/AnimatorSystem.h b/src/crepe/system/AnimatorSystem.h new file mode 100644 index 0000000..969e9d1 --- /dev/null +++ b/src/crepe/system/AnimatorSystem.h @@ -0,0 +1,44 @@ +#pragma once + +#include "System.h" + +//TODO: +// control if flip works with animation system + +namespace crepe { + +/** + * \brief The AnimatorSystem is responsible for managing and updating all Animator components. + * + * This system is responsible for controlling the behavior of the animations for all entities + * that have the Animator component attached. It updates the animations by controlling their + * frame changes, looping behavior, and overall animation state. + */ +class AnimatorSystem : public System { + +public: +	/** +	 * \brief Retrieves the singleton instance of the AnimatorSystem. +	 * +	 * \return A reference to the single instance of the AnimatorSystem. +	 * +	 * This method ensures that there is only one instance of the AnimatorSystem, following the +	 * singleton design pattern. It can be used to access the system globally. +	 */ +	static AnimatorSystem & get_instance(); + +	/** +	 * \brief Updates the Animator components. +	 * +	 * This method is called periodically (likely every frame) to update the state of all +	 * Animator components, moving the animations forward and managing their behavior (e.g., looping). +	 */ +	void update() override; + +private: +	// private because singleton +	AnimatorSystem(); // dbg_trace +	~AnimatorSystem(); // dbg_trace +}; + +} // namespace crepe diff --git a/src/crepe/system/CMakeLists.txt b/src/crepe/system/CMakeLists.txt index ff6f66f..4c18b87 100644 --- a/src/crepe/system/CMakeLists.txt +++ b/src/crepe/system/CMakeLists.txt @@ -4,6 +4,7 @@ target_sources(crepe PUBLIC  	PhysicsSystem.cpp  	CollisionSystem.cpp  	RenderSystem.cpp +	AnimatorSystem.cpp  )  target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -12,4 +13,5 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES  	PhysicsSystem.h  	CollisionSystem.h  	RenderSystem.h +	AnimatorSystem.h  ) diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 5a07cc2..10211a3 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -20,7 +20,25 @@ RenderSystem & RenderSystem::get_instance() {  	return instance;  } -void RenderSystem::update() { +void RenderSystem::clear_screen() const { +	SDLContext::get_instance().clear_screen(); +} + +void RenderSystem::present_screen() const { +	SDLContext::get_instance().present_screen(); +} +void RenderSystem::update_camera() { +	ComponentManager & mgr = ComponentManager::get_instance(); + +	std::vector<std::reference_wrapper<Camera>> cameras +		= mgr.get_components_by_type<Camera>(); + +	for (Camera & cam : cameras) { +		SDLContext::get_instance().camera(cam); +		this->curr_cam = &cam; +	} +} +void RenderSystem::render_sprites() const {  	ComponentManager & mgr = ComponentManager::get_instance(); @@ -28,14 +46,16 @@ void RenderSystem::update() {  		= mgr.get_components_by_type<Sprite>();  	SDLContext & render = SDLContext::get_instance(); -	render.clear_screen(); -  	for (const Sprite & sprite : sprites) { -		std::vector<std::reference_wrapper<Transform>> transforms +		auto transforms  			= mgr.get_components_by_id<Transform>(sprite.game_object_id); -		for (const Transform & transform : transforms) { -			render.draw(sprite, transform); -		} +		render.draw(sprite, transforms[0], *curr_cam);  	} -	render.present_screen(); +} + +void RenderSystem::update() { +	this->clear_screen(); +	this->update_camera(); +	this->render_sprites(); +	this->present_screen();  } diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 4b910a4..70db21a 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -1,17 +1,64 @@  #pragma once +#include "api/Camera.h" +  #include "System.h"  namespace crepe { +/** + * \class RenderSystem + * \brief Manages rendering operations for all game objects. + * + * RenderSystem is responsible for rendering sprites, clearing and presenting the screen,  + * and managing the active camera. It functions as a singleton, providing centralized  + * rendering services for the application. + */  class RenderSystem : public System {  public: +	/** +	 * \brief Gets the singleton instance of RenderSystem. +	 * \return Reference to the RenderSystem instance. +	 */  	static RenderSystem & get_instance(); -	void update(); + +	/** +	 * \brief Updates the RenderSystem for the current frame. +	 * This method is called to perform all rendering operations for the current game frame. +	 */ +	void update() override;  private: +	// Private constructor to enforce singleton pattern.  	RenderSystem();  	~RenderSystem(); + +	//! Clears the screen in preparation for rendering. +	void clear_screen() const; + +	//! Presents the rendered frame to the display. +	void present_screen() const; + +	//! Updates the active camera used for rendering. +	void update_camera(); + +	//! Renders all active sprites to the screen. +	void render_sprites() const; + +	/** +	 * \todo Include color handling for sprites. +	 * \todo Implement particle emitter rendering with sprites. +	 * \todo Add text rendering using SDL_ttf for text components. +	 * \todo Implement a text component and a button component. +	 * \todo Ensure each sprite is checked for active status before rendering. +	 * \todo Sort all layers by order before rendering. +	 * \todo Consider adding text input functionality. +	 */ + +private: +	//! Pointer to the current active camera for rendering +	Camera * curr_cam = nullptr; +	// TODO: needs a better solution  };  } // namespace crepe |