diff options
| -rw-r--r-- | src/crepe/api/Animator.cpp | 13 | ||||
| -rw-r--r-- | src/crepe/api/Animator.h | 6 | ||||
| -rw-r--r-- | src/crepe/api/Sprite.cpp | 6 | ||||
| -rw-r--r-- | src/crepe/api/Sprite.h | 14 | ||||
| -rw-r--r-- | src/crepe/api/Texture.cpp | 18 | ||||
| -rw-r--r-- | src/crepe/api/Texture.h | 34 | ||||
| -rw-r--r-- | src/crepe/facade/SDLContext.cpp | 38 | ||||
| -rw-r--r-- | src/crepe/facade/SDLContext.h | 19 | ||||
| -rw-r--r-- | src/example/rendering_particle.cpp | 21 | 
9 files changed, 91 insertions, 78 deletions
| diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index b8a91dc..ad1778d 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -7,8 +7,9 @@  using namespace crepe; -Animator::Animator(game_object_id_t id, Sprite & spritesheet, unsigned int max_row, -				   unsigned int max_col, const Animator::Data & data) +Animator::Animator(game_object_id_t id, Sprite & spritesheet, unsigned int pixel_frame_x, +				   unsigned int pixel_frame_y, unsigned int max_row, unsigned int max_col, +				   const Animator::Data & data)  	: Component(id),  	  spritesheet(spritesheet),  	  max_rows(max_row), @@ -16,14 +17,10 @@ Animator::Animator(game_object_id_t id, Sprite & spritesheet, unsigned int max_r  	  data(data) {  	dbg_trace(); -	this->spritesheet.mask.h /= this->max_columns; -	this->spritesheet.mask.w /= this->max_rows; +	this->spritesheet.mask.h = this->max_columns * pixel_frame_y; +	this->spritesheet.mask.w /= this->max_rows * pixel_frame_x;  	this->spritesheet.mask.x = this->data.row * this->spritesheet.mask.w;  	this->spritesheet.mask.y = this->data.col * this->spritesheet.mask.h; - -	// need to do this for to get the aspect ratio for a single clipping in the spritesheet -	this->spritesheet.aspect_ratio -		= static_cast<double>(this->spritesheet.mask.w) / this->spritesheet.mask.h;  }  Animator::~Animator() { dbg_trace(); } diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 7c850b8..8ceddad 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -82,8 +82,9 @@ public:  	 * This constructor sets up the Animator with the given parameters, and initializes the  	 * animation system.  	 */ -	Animator(game_object_id_t id, Sprite & spritesheet, unsigned int max_row, -			 unsigned int max_col, const Animator::Data & data); +	Animator(game_object_id_t id, Sprite & spritesheet, unsigned int pixel_frame_x, +			 unsigned int pixel_frame_y, unsigned int max_row, unsigned int max_col, +			 const Animator::Data & data);  	~Animator(); // dbg_trace  public: @@ -96,6 +97,7 @@ public:  private:  	//! A reference to the Sprite sheet containing.  	Sprite & spritesheet; +  	//! Uses the spritesheet  	friend AnimatorSystem;  }; diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index 4cf214c..ba684ba 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -10,16 +10,12 @@  using namespace std;  using namespace crepe; -Sprite::Sprite(game_object_id_t id, const Asset & texture, const ivec2 & size, const Sprite::Data & data) +Sprite::Sprite(game_object_id_t id, const Asset & texture, const Sprite::Data & data)  	: Component(id),  	  source(texture),  	  data(data) {  	dbg_trace(); - -	this->mask.w = size.x; -	this->mask.h = size.y; -	this->aspect_ratio = static_cast<double>(this->mask.w) / this->mask.h;  }  Sprite::~Sprite() { dbg_trace(); } diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 9ef9f03..7e9812d 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -1,10 +1,9 @@  #pragma once  #include "../Component.h" +#include "api/Asset.h"  #include "Color.h" -#include "Texture.h" -#include "api/Asset.h"  #include "types.h"  namespace crepe { @@ -75,7 +74,7 @@ public:  	 * \param texture asset of the image  	 * \param ctx all the sprite data  	 */ -	Sprite(game_object_id_t id, const Asset & texture, const ivec2 & size, const Data & data); +	Sprite(game_object_id_t id, const Asset & texture, const Data & data);  	~Sprite();  	//! Texture used for the sprite @@ -84,15 +83,6 @@ public:  	Data data;  private: -	/** -	 * \brief ratio of the img -	 * -	 * - This will multiply one of \c size variable if it is 0. -	 * - Will be adjusted if \c Animator component is added to an GameObject that is why this -	 *   value cannot be const. -	 */ -	float aspect_ratio; -  	//! Reads the mask of sprite  	friend class SDLContext; diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp index 2ac8606..b0863cb 100644 --- a/src/crepe/api/Texture.cpp +++ b/src/crepe/api/Texture.cpp @@ -1,23 +1,35 @@  #include "../util/Log.h" +#include "manager/Mediator.h" +#include "facade/SDLContext.h"  #include "Asset.h"  #include "Resource.h"  #include "Texture.h" -#include "facade/SDLContext.h" -#include "manager/Mediator.h"  #include "types.h"  using namespace crepe;  using namespace std; -Texture::Texture(const Asset & src, Mediator & mediator) : Resource(src, mediator) { +Texture::Texture(const Asset & src, Mediator & mediator) : Resource(src, mediator){  	dbg_trace();  	SDLContext & ctx = mediator.sdl_context;  	this->texture = ctx.texture_from_path(src.get_path());  	this->size = ctx.get_size(*this); +	this->aspect_ratio = static_cast<float>(this->size.x) / this->size.y;  }  Texture::~Texture() {  	dbg_trace();  	this->texture.reset();  } + +const ivec2 & Texture::get_size() const noexcept{ +	return this->size; +} +const float & Texture::get_ratio() const noexcept{ +	return this->aspect_ratio; +} + +SDL_Texture * Texture::get_img() const noexcept{ +	return this->texture.get(); +} diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h index 4eb1058..c33d9e5 100644 --- a/src/crepe/api/Texture.h +++ b/src/crepe/api/Texture.h @@ -4,7 +4,6 @@  // 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" @@ -13,8 +12,6 @@  namespace crepe { -class SDLContext; -class Animator;  class Mediator;  /** @@ -30,14 +27,36 @@ public:  	/**  	 * \brief Constructs a Texture from an Asset resource.  	 * \param src Asset with texture data to load. +	 * \param mediator use the SDLContext reference to load the image  	 */  	Texture(const Asset & src, Mediator & mediator);  	/** -	 * \brief Destroys the Texture instance, freeing associated resources. +	 * \brief Destroys the Texture instance  	 */  	~Texture(); +	/** +	 * \brief get width and height of image in pixels +	 * \return pixel size width and height +	 * +	 */ +	const ivec2 & get_size() const noexcept; + +	/** +	 * \brief aspect_ratio of image +	 * \return ratio +	 * +	 */ +	const float & get_ratio() const noexcept; + +	/** +	 * \brief get the image texture +	 * \return SDL_Texture +	 * +	 */ +	SDL_Texture * get_img() const noexcept; +  private:  	//! The texture of the class from the library  	std::unique_ptr<SDL_Texture, std::function<void(SDL_Texture *)>> texture; @@ -45,11 +64,8 @@ private:  	// texture size in pixel  	ivec2 size; -	//! Grants SDLContext access to private members. -	friend class SDLContext; - -	//! Grants Animator access to private members. -	friend class Animator; +	//! ratio of image +	float aspect_ratio;  };  } // namespace crepe diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 82c8c50..ac21d15 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -21,10 +21,10 @@  #include "../api/Sprite.h"  #include "../api/Texture.h"  #include "../util/Log.h" - -#include "SDLContext.h"  #include "manager/Manager.h"  #include "manager/Mediator.h" + +#include "SDLContext.h"  #include "types.h"  using namespace crepe; @@ -219,25 +219,26 @@ void SDLContext::present_screen() {  	SDL_RenderPresent(this->game_renderer.get());  } -SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { -	return SDL_Rect{ -		.x = sprite.mask.x, -		.y = sprite.mask.y, -		.w = sprite.mask.w, -		.h = sprite.mask.h, -	}; +SDL_Rect * SDLContext::get_src_rect(const Sprite & sprite) { +	if (sprite.mask.w == 0 && sprite.mask.h == 0) return NULL; + +	this->mask.x = sprite.mask.x; +	this->mask.y = sprite.mask.y; +	this->mask.w = sprite.mask.w; +	this->mask.h = sprite.mask.h; +	return &this->mask;  }  SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const {  	const Sprite::Data & data = ctx.sprite.data; -	vec2 size = {data.size.x , data.size.y}; +	vec2 size = {data.size.x, data.size.y};  	if (data.size.x == 0 && data.size.y != 0) { -		size.x = data.size.y * ctx.sprite.aspect_ratio; +		size.x = data.size.y * ctx.texture.get_ratio();  	}  	if (data.size.y == 0 && data.size.x != 0) { -		size.y = data.size.x / ctx.sprite.aspect_ratio; +		size.y = data.size.x / ctx.texture.get_ratio();  	}  	const CameraValues & cam = ctx.cam; @@ -263,9 +264,10 @@ void SDLContext::draw(const RenderContext & ctx) {  		= (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * data.flip.flip_x)  							  | (SDL_FLIP_VERTICAL * data.flip.flip_y)); -	SDL_Rect srcrect = this->get_src_rect(ctx.sprite); +	SDL_Rect * srcrect = this->get_src_rect(ctx.sprite);  	SDL_FRect dstrect = this->get_dst_rect(SDLContext::DestinationRectangleData{  		.sprite = ctx.sprite, +		.texture = ctx.texture,  		.cam = ctx.cam,  		.pos = ctx.pos,  		.img_scale = ctx.scale, @@ -275,8 +277,8 @@ void SDLContext::draw(const RenderContext & ctx) {  	double angle = ctx.angle + data.angle_offset;  	this->set_color_texture(ctx.texture, ctx.sprite.data.color); -	int error = SDL_RenderCopyExF(this->game_renderer.get(), ctx.texture.texture.get(), -								  &srcrect, &dstrect, angle, NULL, render_flip); +	SDL_RenderCopyExF(this->game_renderer.get(), ctx.texture.get_img(), srcrect, &dstrect, +					  angle, NULL, render_flip);  }  SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) { @@ -366,7 +368,7 @@ SDLContext::texture_from_path(const std::string & path) {  ivec2 SDLContext::get_size(const Texture & ctx) {  	ivec2 size; -	SDL_QueryTexture(ctx.texture.get(), NULL, NULL, &size.x, &size.y); +	SDL_QueryTexture(ctx.get_img(), NULL, NULL, &size.x, &size.y);  	return size;  } @@ -433,6 +435,6 @@ std::vector<SDLContext::EventData> SDLContext::get_events() {  	return event_list;  }  void SDLContext::set_color_texture(const Texture & texture, const Color & color) { -	SDL_SetTextureColorMod(texture.texture.get(), color.r, color.g, color.b); -	SDL_SetTextureAlphaMod(texture.texture.get(), color.a); +	SDL_SetTextureColorMod(texture.get_img(), color.r, color.g, color.b); +	SDL_SetTextureAlphaMod(texture.get_img(), color.a);  } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 9676940..36e6e97 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -15,14 +15,14 @@  #include "api/KeyCodes.h"  #include "api/Sprite.h"  #include "api/Transform.h" -  #include "manager/Manager.h" -#include "manager/Mediator.h" +  #include "types.h"  namespace crepe {  class Texture; +class Mediator;  /**   * \class SDLContext @@ -165,7 +165,6 @@ public:  	 */  	void delay(int ms) const; -  public:  	/**  	 * \brief Loads a texture from a file path. @@ -177,7 +176,7 @@ public:  	/**  	 * \brief Gets the size of a texture.  	 * \param texture Reference to the Texture object. -	 * \return Width and height of the texture as an integer. +	 * \return Width and height of the texture as an integer in pixels.  	 */  	ivec2 get_size(const Texture & ctx); @@ -197,6 +196,7 @@ public:  	/**  	 * \brief sets the background of the camera (will be adjusted in future PR)  	 * \param camera Reference to the Camera object. +	 * \return camera data the component cannot store  	 */  	CameraValues set_camera(const Camera & camera); @@ -204,6 +204,7 @@ public:  	//! the data needed to construct a sdl dst rectangle  	struct DestinationRectangleData {  		const Sprite & sprite; +		const Texture & texture;  		const CameraValues & cam;  		const vec2 & pos;  		const double & img_scale; @@ -214,16 +215,12 @@ public:  	 * \param sprite Reference to the sprite to calculate the rectangle  	 * \return sdl rectangle to draw a src image  	 */ -	SDL_Rect get_src_rect(const Sprite & sprite) const; +	SDL_Rect * get_src_rect(const Sprite & sprite);  	/**  	 * \brief calculates the sqaure size of the image for destination  	 * -	 * \param sprite Reference to the sprite to calculate rectangle -	 * \param pos the pos in world units -	 * \param cam the camera of the current scene -	 * \param cam_pos the current postion of the camera -	 * \param img_scale the image multiplier for increasing img size +	 * \param data needed to calculate a destination rectangle  	 * \return sdl rectangle to draw a dst image to draw on the screen  	 */  	SDL_FRect get_dst_rect(const DestinationRectangleData & data) const; @@ -242,6 +239,8 @@ private:  	//! renderer for the crepe engine  	std::unique_ptr<SDL_Renderer, std::function<void(SDL_Renderer *)>> game_renderer; +	SDL_Rect mask = {}; +  	//! black bars rectangle to draw  	SDL_FRect black_bars[2] = {};  }; diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 87a6eb9..cfc5a84 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -54,17 +54,16 @@ public:  		Asset img{"asset/texture/test_ap43.png"}; -		Sprite & test_sprite -			= game_object.add_component<Sprite>(img, ivec2{259, 195}, -												Sprite::Data{ -													.color = color, -													.flip = Sprite::FlipSettings{false, false}, -													.sorting_in_layer = 2, -													.order_in_layer = 2, -													.size = {0, 100}, -													.angle_offset = 0, -													.position_offset = {0, 0}, -												}); +		Sprite & test_sprite = game_object.add_component<Sprite>( +			img, Sprite::Data{ +					 .color = color, +					 .flip = Sprite::FlipSettings{false, false}, +					 .sorting_in_layer = 2, +					 .order_in_layer = 2, +					 .size = {0, 100}, +					 .angle_offset = 0, +					 .position_offset = {0, 0}, +				 });  		/* |