diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-12-04 14:06:17 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-12-04 14:06:17 +0100 |
commit | ac7cc7d5c5815ee75b58a3b5ceff747ab99015ed (patch) | |
tree | a402c3a3a514f70d8008e75e0cd67d14df94ce49 /src/crepe/facade | |
parent | 04998545f0c43e76cec9819d1a2993f58e551cab (diff) | |
parent | 210800c5fa71460d9cbcfff808a62cc07e0fdb7a (diff) |
Merge branch 'master' into loek/scripts
Diffstat (limited to 'src/crepe/facade')
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 26 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.h | 22 |
2 files changed, 29 insertions, 19 deletions
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 9f60285..e8be7ca 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -1,4 +1,5 @@ #include <SDL2/SDL.h> +#include <SDL2/SDL_blendmode.h> #include <SDL2/SDL_image.h> #include <SDL2/SDL_keycode.h> #include <SDL2/SDL_rect.h> @@ -7,7 +8,9 @@ #include <SDL2/SDL_video.h> #include <cmath> #include <cstddef> +#include <cstdint> #include <functional> +#include <iostream> #include <memory> #include <stdexcept> @@ -18,6 +21,7 @@ #include "../util/Log.h" #include "SDLContext.h" +#include "api/Color.h" #include "types.h" using namespace crepe; @@ -134,6 +138,7 @@ void SDLContext::draw(const RenderContext & ctx) { SDL_Rect dstrect = this->get_dst_rect(ctx.sprite, ctx.pos, ctx.cam, ctx.cam_pos, ctx.scale); + 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); } @@ -207,16 +212,19 @@ SDLContext::texture_from_path(const std::string & path) { std::unique_ptr<SDL_Texture, std::function<void(SDL_Texture *)>> 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 { - 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; + +ivec2 SDLContext::get_size(const Texture & ctx) { + ivec2 size; + SDL_QueryTexture(ctx.texture.get(), NULL, NULL, &size.x, &size.y); + return size; } + 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 6030a6e..e49ca78 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -12,6 +12,8 @@ #include "../api/Camera.h" #include "../api/Sprite.h" +#include "api/Color.h" +#include "api/Texture.h" #include "types.h" namespace crepe { @@ -104,18 +106,11 @@ private: std::unique_ptr<SDL_Texture, std::function<void(SDL_Texture *)>> 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. @@ -160,6 +155,13 @@ 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 Set an additional color value multiplied into render copy operations. + * + * \param texture the given texture to adjust + * \param color the color data for the texture + */ + void set_color_texture(const Texture & texture, const Color & color); private: //! sdl Window |