diff options
Diffstat (limited to 'src/crepe/facade')
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 30 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.h | 12 | ||||
-rw-r--r-- | src/crepe/facade/Texture.h | 3 |
3 files changed, 15 insertions, 30 deletions
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 1699b53..1dbd6a5 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -11,7 +11,6 @@ #include <cstddef> #include <cstdint> #include <functional> -#include <iostream> #include <memory> #include <stdexcept> @@ -20,7 +19,6 @@ #include "../api/Config.h" #include "../api/Sprite.h" #include "../util/Log.h" -#include "manager/Manager.h" #include "manager/Mediator.h" #include "SDLContext.h" @@ -30,9 +28,8 @@ using namespace crepe; using namespace std; -SDLContext::SDLContext(Mediator & mediator) : Manager(mediator) { +SDLContext::SDLContext(Mediator & mediator) { dbg_trace(); - mediator.sdl_context = *this; if (SDL_Init(SDL_INIT_VIDEO) != 0) { throw runtime_error(format("SDLContext: SDL_Init error: {}", SDL_GetError())); } @@ -60,6 +57,8 @@ SDLContext::SDLContext(Mediator & mediator) : Manager(mediator) { if (!(IMG_Init(img_flags) & img_flags)) { throw runtime_error("SDLContext: SDL_image could not initialize!"); } + + mediator.sdl_context = *this; } SDLContext::~SDLContext() { @@ -219,16 +218,6 @@ void SDLContext::present_screen() { SDL_RenderPresent(this->game_renderer.get()); } -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; @@ -267,7 +256,16 @@ 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; + SDL_Rect * srcrect_ptr = NULL; + if (ctx.sprite.mask.w != 0 || ctx.sprite.mask.h != 0) { + srcrect.w = ctx.sprite.mask.w; + srcrect.h = ctx.sprite.mask.h; + srcrect.x = ctx.sprite.mask.x; + srcrect.y = ctx.sprite.mask.y; + srcrect_ptr = &srcrect; + } + SDL_FRect dstrect = this->get_dst_rect(SDLContext::DestinationRectangleData{ .sprite = ctx.sprite, .texture = ctx.texture, @@ -279,7 +277,7 @@ void SDLContext::draw(const RenderContext & ctx) { double angle = ctx.angle + data.angle_offset; this->set_color_texture(ctx.texture, ctx.sprite.data.color); - SDL_RenderCopyExF(this->game_renderer.get(), ctx.texture.get_img(), srcrect, &dstrect, + SDL_RenderCopyExF(this->game_renderer.get(), ctx.texture.get_img(), srcrect_ptr, &dstrect, angle, NULL, render_flip); } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 36e6e97..46b779f 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -15,7 +15,6 @@ #include "api/KeyCodes.h" #include "api/Sprite.h" #include "api/Transform.h" -#include "manager/Manager.h" #include "types.h" @@ -31,7 +30,7 @@ class Mediator; * SDLContext is a singleton that handles the SDL window and renderer, provides methods for * event handling, and rendering to the screen. It is never used directly by the user */ -class SDLContext : public Manager { +class SDLContext { public: //! data that the camera component cannot hold struct CameraValues { @@ -209,13 +208,6 @@ public: const vec2 & pos; const double & img_scale; }; - /** - * \brief calculates the sqaure size of the image - * - * \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); /** * \brief calculates the sqaure size of the image for destination @@ -239,8 +231,6 @@ 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/crepe/facade/Texture.h b/src/crepe/facade/Texture.h index 255e14b..cdacac4 100644 --- a/src/crepe/facade/Texture.h +++ b/src/crepe/facade/Texture.h @@ -1,8 +1,5 @@ #pragma once -// 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> |