11 #include "Configuration.h" 17 #include <Utilities.h> 40 SDL_DestroyWindow(sdlWindow);
44 using SdlWindow = std::unique_ptr<SDL_Window, SdlWindowDestroy>;
63 SDL_RendererFlip mFlip;
69 constexpr
explicit RenderFlip(SDL_RendererFlip flip) noexcept: mFlip(flip) {}
90 SDL_DestroyRenderer(sdlRenderer);
94 using RendererPtr = std::unique_ptr<SDL_Renderer, RendererDestroy>;
97 SDL_Texture *mCurrentRenderTarget{
nullptr};
112 { mRenderer.reset(SDL_CreateRenderer(window.get(), index, flags)); }
115 explicit operator bool() const noexcept {
return mRenderer.operator bool(); }
118 [[nodiscard]]
auto get()
const {
return mRenderer.get(); }
122 SDL_SetRenderDrawBlendMode(mRenderer.get(), blendMode);
147 int renderCopy(
const Texture &texture);
193 return setDrawColor(color.
toRGBA());
249 SDL_Texture *mLastTexture{
nullptr};
267 explicit operator bool() const noexcept {
return status == 0; }
284 int setRenderTarget(
Texture &texture);
295 SDL_Color mOldColor{};
309 if (SDL_SetRenderDrawColor(mContext.
get(), mOldColor.r, mOldColor.g, mOldColor.b, mOldColor.a)) {
333 explicit operator bool() const noexcept {
return mStatus == 0; }
340 int setDrawColor(SDL_Color color);
367 explicit operator bool ()
const {
return mStatus == 0; }
373 if (mOldClip.w == 0 && mOldClip.y == 0)
374 mStatus = SDL_RenderSetClipRect(mContext.
get(),
nullptr);
376 mStatus = SDL_RenderSetClipRect(mContext.
get(), &mOldClip);
386 SDL_RenderGetClipRect(mContext.
get(), &mOldClip);
395 SDL_RenderGetClipRect(mContext.
get(), &mOldClip);
396 mStatus = SDL_RenderSetClipRect(mContext.
get(), &clip);
418 SDL_RenderGetClipRect(mContext.
get(), &mOldClip);
419 SDL_Rect rect{clip.x, clip.y, clip.w, clip.h};
420 mStatus = SDL_RenderSetClipRect(mContext.
get(), &rect);
429 mStatus = SDL_RenderSetClipRect(mContext.
get(), &clip);
440 SDL_Rect rect{clip.x, clip.y, clip.w, clip.h};
441 mStatus = SDL_RenderSetClipRect(mContext.
get(), &rect);
447 SDL_RenderGetClipRect(mContext.
get(), ¤t);
448 if (SDL_RectEmpty(¤t)) {
452 Rectangle r{current.x, current.y, current.w, current.h};
453 r = r.intersection(clip);
454 current = SDL_Rect{r.x, r.y, r.w, r.h};
455 mStatus = SDL_RenderSetClipRect(mContext.
get(), ¤t);
467 bool mRunEventLoop{
true};
469 bool mRedrawBackground{
true};
475 std::vector<Rectangle> mDisplayBounds{};
480 bool initialize(
const std::string &title,
Size initialSize,
const Position<int>& initialPosition, uint32_t extraFlags);
482 void eventLoop(std::shared_ptr<Screen> &screen);
495 void drawAll(std::shared_ptr<Screen> &screen);
497 std::function<void(SDL_Event)> eventCallback{};
499 Context& context() {
return mContext; }
501 [[nodiscard]]
int currentDisplayIndex()
const {
return SDL_GetWindowDisplayIndex(mSdlWindow.get()); }
503 Rectangle displayBounds(
int displayIndex = -1) {
504 if (displayIndex < 0)
505 displayIndex = currentDisplayIndex();
507 if (displayIndex < 0 || displayIndex >= mDisplayBounds.size())
510 return mDisplayBounds.at(displayIndex);
515 SDL_GetWindowSize(mSdlWindow.get(), &screenRectangle.w, &screenRectangle.h);
516 return screenRectangle;
519 void redrawBackground() { mRedrawBackground =
true; }
521 [[nodiscard]]
Padding windowBorders()
const noexcept {
523 SDL_GetWindowBordersSize(mSdlWindow.get(), &p.t, &p.l, &p.b, &p.r);
Definition: GraphicsModel.cpp:20
ClipRectangleGuard & operator=(Rectangle &clip)
Assign a new clip rectangle through the ClipRectangleGuard.
Definition: GraphicsModel.h:439
Context & mContext
The renderer to which the draw colors are set.
Definition: GraphicsModel.h:294
std::string StringCompositor(Arg &&arg, Args &&... args)
Composite a pack of arguments that are streamable to a string.
Definition: Utilities.h:114
ClipRectangleGuard(Context &context, const Rectangle &clip)
Conditional constructor.
Definition: GraphicsModel.h:417
std::unique_ptr< SDL_Renderer, RendererDestroy > RendererPtr
An SDL_Renderer unique pointer.
Definition: GraphicsModel.h:94
Red Green Blue Alpha representation of a color.
Definition: Color.h:64
Thrown by RenderTargetGuard on errors.
Definition: GraphicsModel.h:218
A functor to destroy an SDL_Renderer.
Definition: GraphicsModel.h:83
~ClipRectangleGuard()
Set the old clip rectangle back on the renderer when destroyed.
Definition: GraphicsModel.h:372
int renderClear()
Prepare for the start of a rendering iteration.
Definition: GraphicsModel.h:136
ClipRectangleGuard(Context &context, int x, int y, int w, int h)
Constructor.
Definition: GraphicsModel.h:407
A functor to destroy an SDL_Window in a std::unique_ptr (rose::sdl::Window).
Definition: GraphicsModel.h:33
Hue Saturation Value (or Brightness) representation of a color.
Definition: Color.h:184
Abstraction of space consumed around an object for space, borders, etc.
Definition: Types.h:454
Abstraction of SDL_Texture.
Definition: Texture.h:46
Context
Definition: GraphicsModel.h:76
Context & mContext
The renderer to which the clip rectangles are set.
Definition: GraphicsModel.h:359
int mStatus
The status of the last SDL operation.
Definition: GraphicsModel.h:296
void setDrawBlendMode(SDL_BlendMode blendMode)
Set the draw blend mode.
Definition: GraphicsModel.h:121
constexpr RenderFlip() noexcept
Default constructor – No flipping.
Definition: GraphicsModel.h:66
~DrawColorGuard() noexcept(false)
Set the old clip rectangle back on the renderer when destroyed.
Definition: GraphicsModel.h:308
Thrown by DrawColorGuard on errors.
Definition: GraphicsModel.h:233
RoseErrorCode
Rose object error codes.
Definition: GraphicsModel.h:49
int setDrawColor(color::HSVA color)
Set the drawing color used for drawing Rectangles, lines and clearing.
Definition: GraphicsModel.h:192
A composite of a Position and a Size.
Definition: Types.h:307
Store the current draw color replacing it with a new draw color.
Definition: GraphicsModel.h:292
Store the current render target replacing it with a new render target.
Definition: GraphicsModel.h:245
int setDrawColor(color::RGBA color)
Set the draw Color on the renderer without pushing the old color on the stack.
Definition: GraphicsModel.h:347
ClipRectangleGuard(Context &context)
Speculative constructor.
Definition: GraphicsModel.h:385
std::unique_ptr< SDL_Window, SdlWindowDestroy > SdlWindow
An SDL_Window unique pointer.
Definition: GraphicsModel.h:44
uint32_t mapRGBA(SDL_PixelFormat *format, const color::RGBA &color)
Map a color::RGBA to a uint32_t.
Definition: GraphicsModel.cpp:304
int pixel(SDL_Renderer *renderer, Sint16 x, Sint16 y)
Draw pixel in currently set color.
Definition: GfxPrimitives.cpp:20
void renderPresent()
Complete a rendering iteration.
Definition: GraphicsModel.h:139
void operator()(SDL_Renderer *sdlRenderer)
Call the SDL API to destroy the renderer.
Definition: GraphicsModel.h:89
A size in integer dimensions.
Definition: Types.h:230
constexpr RGBA toRGBA() const noexcept
Convert to RGBA in an array of floats.
Definition: Color.h:262
void operator()(SDL_Window *sdlWindow)
Call the SDL API to destroy an SDL_Window.
Definition: GraphicsModel.h:39
A Widget manipulator to indicate if and how rendering a texture should be flipped.
Definition: GraphicsModel.h:62
ToDo: There is an issue that the initial scroll interaction is lost if the click/press lands on a Wid...
Definition: CelestialOverlay.cpp:13
Store the current clip rectangle replacing it with a new clip rectangle.
Definition: GraphicsModel.h:357
constexpr RenderFlip(SDL_RendererFlip flip) noexcept
Constructor – user specified flipping.
Definition: GraphicsModel.h:69
ClipRectangleGuard & operator=(SDL_Rect &clip)
Assign a new clip rectangle through the ClipRectangleGuard.
Definition: GraphicsModel.h:428
ClipRectangleGuard(Context &context, const SDL_Rect &clip)
Constructor.
Definition: GraphicsModel.h:394
auto get() const
Get an opaque pointer for API calls.
Definition: GraphicsModel.h:118
Definition: GraphicsModel.h:461
RenderTargetGuardException(const std::string &what_arg)
Create a RenderTargetGuardException.
Definition: GraphicsModel.h:224
constexpr SDL_Color toSdlColor() const noexcept
Convert this colour to an SDL_Color.
Definition: Color.h:97
User Interface Visual types.
Context & mContext
The Context being guarded.
Definition: GraphicsModel.h:247