From 767f40a61952892d60d204ecaaffa07e7e396d28 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Thu, 14 Nov 2024 10:05:08 +0100 Subject: color paremeter in sprite works --- src/crepe/facade/SDLContext.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/crepe/facade/SDLContext.h') diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index c4392bd..5b5ee3e 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -145,6 +146,28 @@ private: */ void camera(const Camera & camera); + /** + * \brief changes the texture rbg values with the given parameters + * it sets the allowed color inside a image. So if all the colors are 255 (MAXIMUM) + * it will show the given texture. however if the one of the colors is reduced it will reduce the + * + * + * \param texture the given texture to adjust + * \param r Red color + * \param g Green color + * \param b Blue color + */ + void set_rbg_texture(const std::shared_ptr& texture, const uint8_t& r, const uint8_t& g, const uint8_t& b); + + + /** + * \brief Modifies the transparency of the given texture + * + * \param texture modify the given texture alpha channel + * \param alpha alpha channel + */ + void set_alpha_texture(const std::shared_ptr& texture, const uint8_t& alpha); + private: //! sdl Window std::unique_ptr> game_window; -- cgit v1.2.3 From 9e1d1f0952ed09ee4b0c241fad3d0d66b380b1a3 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 2 Dec 2024 18:59:43 +0100 Subject: fixed the color functionality --- src/crepe/facade/SDLContext.cpp | 20 +++++++++++++++----- src/crepe/facade/SDLContext.h | 10 ++++++---- 2 files changed, 21 insertions(+), 9 deletions(-) (limited to 'src/crepe/facade/SDLContext.h') diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index c01d118..cfe79d2 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -19,6 +19,7 @@ #include "../util/Log.h" #include "SDLContext.h" +#include "api/Color.h" #include "types.h" using namespace crepe; @@ -131,10 +132,16 @@ void SDLContext::draw(const RenderContext & ctx) { = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * ctx.sprite.flip.flip_x) | (SDL_FLIP_VERTICAL * ctx.sprite.flip.flip_y)); + const Color & color = ctx.sprite.color; + SDL_Rect srcrect = this->get_src_rect(ctx.sprite); SDL_Rect dstrect = this->get_dst_rect(ctx.sprite, ctx.pos, ctx.cam, ctx.cam_pos, ctx.scale); + this->set_rbg_texture(ctx.sprite.sprite_image, color.r, color.g, color.b); + this->set_alpha_texture(ctx.sprite.sprite_image, color.a); + + SDL_RenderCopyEx(this->game_renderer.get(), ctx.sprite.sprite_image.texture.get(), &srcrect, &dstrect, ctx.angle, NULL, render_flip); } @@ -221,9 +228,12 @@ int SDLContext::get_height(const Texture & ctx) const { return h; } void SDLContext::delay(int ms) const { SDL_Delay(ms); } -void SDLContext::set_rbg_texture(const std::shared_ptr& texture, const uint8_t& r, const uint8_t& g, const uint8_t& b){ - SDL_SetTextureColorMod(texture->texture.get(), r, g, b); + +void SDLContext::set_rbg_texture(const Texture & texture, const uint8_t & r, + const uint8_t & g, const uint8_t & b) { + SDL_SetTextureColorMod(texture.texture.get(), r, g, b); +} +void SDLContext::set_alpha_texture(const Texture & texture, + const uint8_t & alpha) { + SDL_SetTextureAlphaMod(texture.texture.get(), alpha); } -void SDLContext::set_alpha_texture(const std::shared_ptr& texture, const uint8_t& alpha){ - SDL_SetTextureAlphaMod(texture->texture.get(), alpha ); -} \ No newline at end of file diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 6284a9c..8d63c43 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -12,6 +12,7 @@ #include "../api/Camera.h" #include "../api/Sprite.h" +#include "api/Texture.h" #include "types.h" namespace crepe { @@ -160,7 +161,7 @@ private: */ SDL_Rect get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, const vec2 & cam_pos, const double & img_scale) const; -/** + /** * \brief changes the texture rbg values with the given parameters * it sets the allowed color inside a image. So if all the colors are 255 (MAXIMUM) * it will show the given texture. however if the one of the colors is reduced it will reduce the @@ -171,8 +172,8 @@ private: * \param g Green color * \param b Blue color */ - void set_rbg_texture(const std::shared_ptr& texture, const uint8_t& r, const uint8_t& g, const uint8_t& b); - + void set_rbg_texture(const Texture & texture, const uint8_t & r, + const uint8_t & g, const uint8_t & b); /** * \brief Modifies the transparency of the given texture @@ -180,7 +181,8 @@ private: * \param texture modify the given texture alpha channel * \param alpha alpha channel */ - void set_alpha_texture(const std::shared_ptr& texture, const uint8_t& alpha); + void set_alpha_texture(const Texture & texture, const uint8_t & alpha); + private: //! sdl Window std::unique_ptr> game_window; -- cgit v1.2.3 From cffa9f014c67133dc48569beb53a5bd6cb166faa Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 2 Dec 2024 19:18:41 +0100 Subject: rendering color working --- src/crepe/facade/SDLContext.cpp | 4 ++++ src/crepe/facade/SDLContext.h | 8 +++----- src/example/rendering_particle.cpp | 4 ++-- 3 files changed, 9 insertions(+), 7 deletions(-) (limited to 'src/crepe/facade/SDLContext.h') diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index cfe79d2..aaaec6b 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -9,6 +10,7 @@ #include #include #include +#include #include #include @@ -215,6 +217,7 @@ SDLContext::texture_from_path(const std::string & path) { std::unique_ptr> img_texture; img_texture = {tmp_texture, [](SDL_Texture * texture) { SDL_DestroyTexture(texture); }}; + SDL_SetTextureBlendMode(img_texture.get(), SDL_BLENDMODE_BLEND); return img_texture; } int SDLContext::get_width(const Texture & ctx) const { @@ -235,5 +238,6 @@ void SDLContext::set_rbg_texture(const Texture & texture, const uint8_t & r, } void SDLContext::set_alpha_texture(const Texture & texture, const uint8_t & alpha) { + SDL_SetTextureAlphaMod(texture.texture.get(), alpha); } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 8d63c43..1c67301 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -162,10 +162,7 @@ private: SDL_Rect get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, const vec2 & cam_pos, const double & img_scale) const; /** - * \brief changes the texture rbg values with the given parameters - * it sets the allowed color inside a image. So if all the colors are 255 (MAXIMUM) - * it will show the given texture. however if the one of the colors is reduced it will reduce the - * + * \brief Set an additional color value multiplied into render copy operations. * * \param texture the given texture to adjust * \param r Red color @@ -175,8 +172,9 @@ private: void set_rbg_texture(const Texture & texture, const uint8_t & r, const uint8_t & g, const uint8_t & b); + /** - * \brief Modifies the transparency of the given texture + * \brief Set an additional alpha value multiplied into render copy operations. * * \param texture modify the given texture alpha channel * \param alpha alpha channel diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 3a12144..349d11e 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -28,7 +28,7 @@ int main(int argc, char * argv[]) { ParticleSystem psys{mgr}; AnimatorSystem asys{mgr}; - Color color(255, 255, 255, 255); + Color color(255, 255, 255, 100); auto img = Texture("asset/texture/test_ap43.png"); Sprite & test_sprite = game_object.add_component( @@ -59,7 +59,7 @@ int main(int argc, char * argv[]) { }); */ - auto & cam = game_object.add_component(Color::WHITE, ivec2{1080, 720}, + auto & cam = game_object.add_component(Color::RED, ivec2{1080, 720}, vec2{2000, 2000}, 1.0f); /* -- cgit v1.2.3 From e1a6c367a7d4233ed5f38a68ee4d69c3cef1c4f5 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 2 Dec 2024 19:19:12 +0100 Subject: make format --- src/crepe/facade/SDLContext.cpp | 8 +++----- src/crepe/facade/SDLContext.h | 5 ++--- 2 files changed, 5 insertions(+), 8 deletions(-) (limited to 'src/crepe/facade/SDLContext.h') diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index aaaec6b..fe6aa45 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -143,7 +143,6 @@ void SDLContext::draw(const RenderContext & ctx) { this->set_rbg_texture(ctx.sprite.sprite_image, color.r, color.g, color.b); this->set_alpha_texture(ctx.sprite.sprite_image, color.a); - SDL_RenderCopyEx(this->game_renderer.get(), ctx.sprite.sprite_image.texture.get(), &srcrect, &dstrect, ctx.angle, NULL, render_flip); } @@ -232,12 +231,11 @@ int SDLContext::get_height(const Texture & ctx) const { } void SDLContext::delay(int ms) const { SDL_Delay(ms); } -void SDLContext::set_rbg_texture(const Texture & texture, const uint8_t & r, - const uint8_t & g, const uint8_t & b) { +void SDLContext::set_rbg_texture(const Texture & texture, const uint8_t & r, const uint8_t & g, + const uint8_t & b) { SDL_SetTextureColorMod(texture.texture.get(), r, g, b); } -void SDLContext::set_alpha_texture(const Texture & texture, - const uint8_t & alpha) { +void SDLContext::set_alpha_texture(const Texture & texture, const uint8_t & alpha) { SDL_SetTextureAlphaMod(texture.texture.get(), alpha); } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 1c67301..e128bfd 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -169,9 +169,8 @@ private: * \param g Green color * \param b Blue color */ - void set_rbg_texture(const Texture & texture, const uint8_t & r, - const uint8_t & g, const uint8_t & b); - + void set_rbg_texture(const Texture & texture, const uint8_t & r, const uint8_t & g, + const uint8_t & b); /** * \brief Set an additional alpha value multiplied into render copy operations. -- cgit v1.2.3 From b6609b0fbc263f4fd93791f3897c5f2840b962f2 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 3 Dec 2024 09:28:12 +0100 Subject: implemented feedback removed get_width and height --> get_size for both texture and sdlcontext --- src/crepe/api/Sprite.cpp | 4 ++-- src/crepe/api/Texture.cpp | 11 ++++------- src/crepe/api/Texture.h | 13 ++++--------- src/crepe/facade/SDLContext.cpp | 32 +++++++++++--------------------- src/crepe/facade/SDLContext.h | 29 ++++++----------------------- src/crepe/system/RenderSystem.cpp | 3 +-- src/crepe/system/RenderSystem.h | 1 + 7 files changed, 29 insertions(+), 64 deletions(-) (limited to 'src/crepe/facade/SDLContext.h') diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index 8647794..0a2ad4c 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -22,8 +22,8 @@ Sprite::Sprite(game_object_id_t id, Texture & image, const Color & color, dbg_trace(); - this->mask.w = sprite_image.get_width(); - this->mask.h = sprite_image.get_height(); + this->mask.w = sprite_image.get_size().x; + this->mask.h = sprite_image.get_size().y; this->aspect_ratio = static_cast(this->mask.w) / this->mask.h; } diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp index e43bdaa..c23b91e 100644 --- a/src/crepe/api/Texture.cpp +++ b/src/crepe/api/Texture.cpp @@ -2,6 +2,7 @@ #include "../util/Log.h" #include "Asset.h" +#include "types.h" #include "Texture.h" using namespace crepe; @@ -31,11 +32,7 @@ void Texture::load(const Asset & res) { this->texture = ctx.texture_from_path(res.get_path()); } -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_height(*this); +ivec2 Texture::get_size() const { + if (this->texture == nullptr) return {}; + return SDLContext::get_instance().get_size(*this); } diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h index 7206a66..1817910 100644 --- a/src/crepe/api/Texture.h +++ b/src/crepe/api/Texture.h @@ -8,6 +8,7 @@ #include #include "Asset.h" +#include "types.h" namespace crepe { @@ -42,16 +43,10 @@ public: Texture & operator=(const Texture &) = delete; /** - * \brief Gets the width of the texture. - * \return Width of the texture in pixels. + * \brief Gets the width and height of the texture. + * \return Width and height 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; + ivec2 get_size() const; private: /** diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index fe6aa45..8d8a87d 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -134,15 +134,11 @@ void SDLContext::draw(const RenderContext & ctx) { = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * ctx.sprite.flip.flip_x) | (SDL_FLIP_VERTICAL * ctx.sprite.flip.flip_y)); - const Color & color = ctx.sprite.color; - SDL_Rect srcrect = this->get_src_rect(ctx.sprite); SDL_Rect dstrect = this->get_dst_rect(ctx.sprite, ctx.pos, ctx.cam, ctx.cam_pos, ctx.scale); - this->set_rbg_texture(ctx.sprite.sprite_image, color.r, color.g, color.b); - this->set_alpha_texture(ctx.sprite.sprite_image, color.a); - + this->set_color_texture(ctx.sprite.sprite_image, ctx.sprite.color); SDL_RenderCopyEx(this->game_renderer.get(), ctx.sprite.sprite_image.texture.get(), &srcrect, &dstrect, ctx.angle, NULL, render_flip); } @@ -219,23 +215,17 @@ SDLContext::texture_from_path(const std::string & path) { SDL_SetTextureBlendMode(img_texture.get(), SDL_BLENDMODE_BLEND); return img_texture; } -int SDLContext::get_width(const Texture & ctx) const { - int w; - SDL_QueryTexture(ctx.texture.get(), NULL, NULL, &w, NULL); - return w; -} -int SDLContext::get_height(const Texture & ctx) const { - int h; - SDL_QueryTexture(ctx.texture.get(), NULL, NULL, NULL, &h); - return h; -} -void SDLContext::delay(int ms) const { SDL_Delay(ms); } -void SDLContext::set_rbg_texture(const Texture & texture, const uint8_t & r, const uint8_t & g, - const uint8_t & b) { - SDL_SetTextureColorMod(texture.texture.get(), r, g, b); + +ivec2 SDLContext::get_size(const Texture & ctx){ + ivec2 size; + SDL_QueryTexture(ctx.texture.get(), NULL, NULL, &size.x, &size.y); + return size; } -void SDLContext::set_alpha_texture(const Texture & texture, const uint8_t & alpha) { - SDL_SetTextureAlphaMod(texture.texture.get(), alpha); +void SDLContext::delay(int ms) const { SDL_Delay(ms); } + +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); } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index e128bfd..e49ca78 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -12,6 +12,7 @@ #include "../api/Camera.h" #include "../api/Sprite.h" +#include "api/Color.h" #include "api/Texture.h" #include "types.h" @@ -105,18 +106,11 @@ private: std::unique_ptr> texture_from_path(const std::string & path); /** - * \brief Gets the width of a texture. + * \brief Gets the size of a texture. * \param texture Reference to the Texture object. - * \return Width of the texture as an integer. + * \return Width and height of the texture as an integer. */ - int get_width(const Texture & texture) const; - - /** - * \brief Gets the height of a texture. - * \param texture Reference to the Texture object. - * \return Height of the texture as an integer. - */ - int get_height(const Texture & texture) const; + ivec2 get_size(const Texture & ctx); private: //! Will use draw,clear_screen, present_screen, camera. @@ -165,20 +159,9 @@ private: * \brief Set an additional color value multiplied into render copy operations. * * \param texture the given texture to adjust - * \param r Red color - * \param g Green color - * \param b Blue color - */ - void set_rbg_texture(const Texture & texture, const uint8_t & r, const uint8_t & g, - const uint8_t & b); - - /** - * \brief Set an additional alpha value multiplied into render copy operations. - * - * \param texture modify the given texture alpha channel - * \param alpha alpha channel + * \param color the color data for the texture */ - void set_alpha_texture(const Texture & texture, const uint8_t & alpha); + void set_color_texture(const Texture & texture, const Color & color); private: //! sdl Window diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 1883f8f..11c9669 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -10,7 +10,6 @@ #include "../api/ParticleEmitter.h" #include "../api/Sprite.h" #include "../api/Transform.h" -#include "../api/Vector2.h" #include "../facade/SDLContext.h" #include "RenderSystem.h" @@ -71,7 +70,7 @@ bool RenderSystem::render_particle(const Sprite & sprite, const Camera & cam, bool rendering_particles = false; for (const ParticleEmitter & em : emitters) { - if (!(&em.data.sprite == &sprite)) continue; + if (&em.data.sprite != &sprite) continue; rendering_particles = true; if (!em.active) continue; diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 4d542ec..096d058 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -77,6 +77,7 @@ private: */ private: + // FIXME: retrieve sdlcontext via mediator after #PR57 SDLContext & context = SDLContext::get_instance(); //! camera postion in the current scene -- cgit v1.2.3