From f9ef31c4b23fa66210d9ddcbdb918273db4ce72a Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Sun, 18 Jan 2015 03:52:19 +0300 Subject: [PATCH] Make setters return reference to self: Renderer --- SDL2pp/Renderer.cc | 82 +++++++++++++++++++++++------------ SDL2pp/Renderer.hh | 104 +++++++++++++++++++++++++++++++++------------ 2 files changed, 134 insertions(+), 52 deletions(-) diff --git a/SDL2pp/Renderer.cc b/SDL2pp/Renderer.cc index ff11176..3ae0e96 100644 --- a/SDL2pp/Renderer.cc +++ b/SDL2pp/Renderer.cc @@ -61,13 +61,15 @@ SDL_Renderer* Renderer::Get() const { return renderer_; } -void Renderer::Present() { +Renderer& Renderer::Present() { SDL_RenderPresent(renderer_); + return *this; } -void Renderer::Clear() { +Renderer& Renderer::Clear() { if (SDL_RenderClear(renderer_) != 0) throw Exception("SDL_RenderClear failed"); + return *this; } void Renderer::GetInfo(SDL_RendererInfo* info) { @@ -80,46 +82,54 @@ void Renderer::GetInfo(SDL_RendererInfo& info) { throw Exception("SDL_GetRendererInfo failed"); } -void Renderer::Copy(Texture& texture, const Optional& srcrect, const Optional& dstrect) { +Renderer& Renderer::Copy(Texture& texture, const Optional& srcrect, const Optional& dstrect) { if (SDL_RenderCopy(renderer_, texture.Get(), srcrect ? &*srcrect : nullptr, dstrect ? &*dstrect : nullptr) != 0) throw Exception("SDL_RenderCopy failed"); + return *this; } -void Renderer::Copy(Texture& texture, const Optional& srcrect, const Optional& dstrect, double angle, const Optional& center, int flip) { +Renderer& Renderer::Copy(Texture& texture, const Optional& srcrect, const Optional& dstrect, double angle, const Optional& center, int flip) { if (SDL_RenderCopyEx(renderer_, texture.Get(), srcrect ? &*srcrect : nullptr, dstrect ? &*dstrect : nullptr, angle, center ? &*center : nullptr, static_cast(flip)) != 0) throw Exception("SDL_RenderCopyEx failed"); + return *this; } -void Renderer::SetDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a) { +Renderer& Renderer::SetDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a) { if (SDL_SetRenderDrawColor(renderer_, r, g, b, a) != 0) throw Exception("SDL_SetRenderDrawColor failed"); + return *this; } -void Renderer::SetTarget() { +Renderer& Renderer::SetTarget() { if (SDL_SetRenderTarget(renderer_, nullptr) != 0) throw Exception("SDL_SetRenderTarget failed"); + return *this; } -void Renderer::SetTarget(Texture& texture) { +Renderer& Renderer::SetTarget(Texture& texture) { if (SDL_SetRenderTarget(renderer_, texture.Get()) != 0) throw Exception("SDL_SetRenderTarget failed"); + return *this; } -void Renderer::SetDrawBlendMode(SDL_BlendMode blendMode) { +Renderer& Renderer::SetDrawBlendMode(SDL_BlendMode blendMode) { if (SDL_SetRenderDrawBlendMode(renderer_, blendMode) != 0) throw Exception("SDL_SetRenderDrawBlendMode failed"); + return *this; } -void Renderer::DrawPoint(int x, int y) { +Renderer& Renderer::DrawPoint(int x, int y) { if (SDL_RenderDrawPoint(renderer_, x, y) != 0) throw Exception("SDL_RenderDrawPoint failed"); + return *this; } -void Renderer::DrawPoint(const Point& p) { +Renderer& Renderer::DrawPoint(const Point& p) { DrawPoint(p.x, p.y); + return *this; } -void Renderer::DrawPoints(const Point* points, int count) { +Renderer& Renderer::DrawPoints(const Point* points, int count) { std::vector sdl_points; sdl_points.reserve(count); for (const Point* p = points; p != points + count; ++p) @@ -127,18 +137,22 @@ void Renderer::DrawPoints(const Point* points, int count) { if (SDL_RenderDrawPoints(renderer_, sdl_points.data(), sdl_points.size()) != 0) throw Exception("SDL_RenderDrawPoints failed"); + + return *this; } -void Renderer::DrawLine(int x1, int y1, int x2, int y2) { +Renderer& Renderer::DrawLine(int x1, int y1, int x2, int y2) { if (SDL_RenderDrawLine(renderer_, x1, y1, x2, y2) != 0) throw Exception("SDL_RenderDrawLine failed"); + return *this; } -void Renderer::DrawLine(const Point& p1, const Point& p2) { +Renderer& Renderer::DrawLine(const Point& p1, const Point& p2) { DrawLine(p1.x, p1.y, p2.x, p2.y); + return *this; } -void Renderer::DrawLines(const Point* points, int count) { +Renderer& Renderer::DrawLines(const Point* points, int count) { std::vector sdl_points; sdl_points.reserve(count); for (const Point* p = points; p != points + count; ++p) @@ -146,24 +160,29 @@ void Renderer::DrawLines(const Point* points, int count) { if (SDL_RenderDrawLines(renderer_, sdl_points.data(), sdl_points.size()) != 0) throw Exception("SDL_RenderDrawLines failed"); + + return *this; } -void Renderer::DrawRect(int x1, int y1, int x2, int y2) { +Renderer& Renderer::DrawRect(int x1, int y1, int x2, int y2) { SDL_Rect rect = {x1, y1, x2 - x1 + 1, y2 - y1 + 1}; if (SDL_RenderDrawRect(renderer_, &rect) != 0) throw Exception("SDL_RenderDrawRect failed"); + return *this; } -void Renderer::DrawRect(const Point& p1, const Point& p2) { +Renderer& Renderer::DrawRect(const Point& p1, const Point& p2) { DrawRect(p1.x, p1.y, p2.x, p2.y); + return *this; } -void Renderer::DrawRect(const Rect& r) { +Renderer& Renderer::DrawRect(const Rect& r) { if (SDL_RenderDrawRect(renderer_, &r) != 0) throw Exception("SDL_RenderDrawRect failed"); + return *this; } -void Renderer::DrawRects(const Rect* rects, int count) { +Renderer& Renderer::DrawRects(const Rect* rects, int count) { std::vector sdl_rects; sdl_rects.reserve(count); for (const Rect* r = rects; r != rects + count; ++r) @@ -171,24 +190,29 @@ void Renderer::DrawRects(const Rect* rects, int count) { if (SDL_RenderDrawRects(renderer_, sdl_rects.data(), sdl_rects.size()) != 0) throw Exception("SDL_RenderDrawRects failed"); + + return *this; } -void Renderer::FillRect(int x1, int y1, int x2, int y2) { +Renderer& Renderer::FillRect(int x1, int y1, int x2, int y2) { SDL_Rect rect = {x1, y1, x2 - x1 + 1, y2 - y1 + 1}; if (SDL_RenderFillRect(renderer_, &rect) != 0) throw Exception("SDL_RenderFillRect failed"); + return *this; } -void Renderer::FillRect(const Point& p1, const Point& p2) { +Renderer& Renderer::FillRect(const Point& p1, const Point& p2) { FillRect(p1.x, p1.y, p2.x, p2.y); + return *this; } -void Renderer::FillRect(const Rect& r) { +Renderer& Renderer::FillRect(const Rect& r) { if (SDL_RenderFillRect(renderer_, &r) != 0) throw Exception("SDL_RenderFillRect failed"); + return *this; } -void Renderer::FillRects(const Rect* rects, int count) { +Renderer& Renderer::FillRects(const Rect* rects, int count) { std::vector sdl_rects; sdl_rects.reserve(count); for (const Rect* r = rects; r != rects + count; ++r) @@ -196,6 +220,8 @@ void Renderer::FillRects(const Rect* rects, int count) { if (SDL_RenderFillRects(renderer_, sdl_rects.data(), sdl_rects.size()) != 0) throw Exception("SDL_RenderFillRects failed"); + + return *this; } void Renderer::ReadPixels(const Optional& rect, Uint32 format, void* pixels, int pitch) { @@ -203,24 +229,28 @@ void Renderer::ReadPixels(const Optional& rect, Uint32 format, void* pixel throw Exception("SDL_RenderReadPixels failed"); } -void Renderer::SetClipRect(const Optional& rect) { +Renderer& Renderer::SetClipRect(const Optional& rect) { if (SDL_RenderSetClipRect(renderer_, rect ? &*rect : nullptr) != 0) throw Exception("SDL_RenderSetClipRect failed"); + return *this; } -void Renderer::SetLogicalSize(int w, int h) { +Renderer& Renderer::SetLogicalSize(int w, int h) { if (SDL_RenderSetLogicalSize(renderer_, w, h) != 0) throw Exception("SDL_RenderSetLogicalSize failed"); + return *this; } -void Renderer::SetScale(float scaleX, float scaleY) { +Renderer& Renderer::SetScale(float scaleX, float scaleY) { if (SDL_RenderSetScale(renderer_, scaleX, scaleY) != 0) throw Exception("SDL_RenderSetScale failed"); + return *this; } -void Renderer::SetViewport(const Optional& rect) { +Renderer& Renderer::SetViewport(const Optional& rect) { if (SDL_RenderSetViewport(renderer_, rect ? &*rect : nullptr) != 0) throw Exception("SDL_RenderSetViewport failed"); + return *this; } bool Renderer::TargetSupported() const { diff --git a/SDL2pp/Renderer.hh b/SDL2pp/Renderer.hh index 2950216..d27297b 100644 --- a/SDL2pp/Renderer.hh +++ b/SDL2pp/Renderer.hh @@ -141,20 +141,24 @@ public: /// drawing intended for the frame, and then calls this function /// once per frame to present the final drawing to the user. /// + /// \returns Reference to self + /// /// \see http://wiki.libsdl.org/SDL_RenderPresent /// //////////////////////////////////////////////////////////// - void Present(); + Renderer& Present(); //////////////////////////////////////////////////////////// /// \brief Clear the current rendering target with the drawing color /// + /// \returns Reference to self + /// /// \throws SDL2pp::Exception /// /// \see http://wiki.libsdl.org/SDL_RenderClear /// //////////////////////////////////////////////////////////// - void Clear(); + Renderer& Clear(); //////////////////////////////////////////////////////////// /// \brief Get information about a rendering context @@ -191,12 +195,14 @@ public: /// \param[in] dstrect Destination rectangle, NullOpt for the entire /// rendering target /// + /// \returns Reference to self + /// /// \throws SDL2pp::Exception /// /// \see http://wiki.libsdl.org/SDL_RenderCopy /// //////////////////////////////////////////////////////////// - void Copy(Texture& texture, const Optional& srcrect = NullOpt, const Optional& dstrect = NullOpt); + Renderer& Copy(Texture& texture, const Optional& srcrect = NullOpt, const Optional& dstrect = NullOpt); //////////////////////////////////////////////////////////// /// \brief Copy a portion of the texture to the current rendering @@ -214,13 +220,15 @@ public: /// \param[in] flip SDL_RendererFlip value stating which flipping /// actions should be performed on the texture /// + /// \returns Reference to self + /// /// \throws SDL2pp::Exception /// /// \see http://wiki.libsdl.org/SDL_RendererFlip /// \see http://wiki.libsdl.org/SDL_RenderCopyEx /// //////////////////////////////////////////////////////////// - void Copy(Texture& texture, const Optional& srcrect, const Optional& dstrect, double angle, const Optional& center = NullOpt, int flip = 0); + Renderer& Copy(Texture& texture, const Optional& srcrect, const Optional& dstrect, double angle, const Optional& center = NullOpt, int flip = 0); //////////////////////////////////////////////////////////// /// \brief Set color user for drawing operations @@ -230,22 +238,26 @@ public: /// \param[in] b Blue value used to draw on the rendering target /// \param[in] a Alpha value used to draw on the rendering target /// + /// \returns Reference to self + /// /// \throws SDL2pp::Exception /// /// \see http://wiki.libsdl.org/SDL_SetRenderDrawColor /// //////////////////////////////////////////////////////////// - void SetDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a = 255); + Renderer& SetDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a = 255); //////////////////////////////////////////////////////////// /// \brief Set current render target to default /// + /// \returns Reference to self + /// /// \throws SDL2pp::Exception /// /// \see http://wiki.libsdl.org/SDL_SetRenderTarget /// //////////////////////////////////////////////////////////// - void SetTarget(); + Renderer& SetTarget(); //////////////////////////////////////////////////////////// /// \brief Set current render target to specified texture @@ -253,25 +265,29 @@ public: /// \param[in] texture Target texture, SDL2pp::Texture created with /// SDL_TEXTUREACCESS_TARGET /// + /// \returns Reference to self + /// /// \throws SDL2pp::Exception /// /// \see http://wiki.libsdl.org/SDL_SetRenderTarget /// //////////////////////////////////////////////////////////// - void SetTarget(Texture& texture); + Renderer& SetTarget(Texture& texture); //////////////////////////////////////////////////////////// /// \brief Set the blend mode used for drawing operations /// /// \param[in] blendMode SDL_BlendMode to use for blending /// + /// \returns Reference to self + /// /// \throws SDL2pp::Exception /// /// \see http://wiki.libsdl.org/SDL_BlendMode /// \see http://wiki.libsdl.org/SDL_SetRenderDrawBlendMode /// //////////////////////////////////////////////////////////// - void SetDrawBlendMode(SDL_BlendMode blendMode); + Renderer& SetDrawBlendMode(SDL_BlendMode blendMode); //////////////////////////////////////////////////////////// /// \brief Draw a point on the current rendering target @@ -279,24 +295,28 @@ public: /// \param[in] x X coordinate of the point /// \param[in] y Y coordinate of the point /// + /// \returns Reference to self + /// /// \throws SDL2pp::Exception /// /// \see http://wiki.libsdl.org/SDL_RenderDrawPoint /// //////////////////////////////////////////////////////////// - void DrawPoint(int x, int y); + Renderer& DrawPoint(int x, int y); //////////////////////////////////////////////////////////// /// \brief Draw a point on the current rendering target /// /// \param[in] p Coordinates of the point /// + /// \returns Reference to self + /// /// \throws SDL2pp::Exception /// /// \see http://wiki.libsdl.org/SDL_RenderDrawPoint /// //////////////////////////////////////////////////////////// - void DrawPoint(const Point& p); + Renderer& DrawPoint(const Point& p); //////////////////////////////////////////////////////////// /// \brief Draw multiple points on the current rendering target @@ -304,12 +324,14 @@ public: /// \param[in] points Array of coordinates of points to draw /// \param[in] count Number of points to draw /// + /// \returns Reference to self + /// /// \throws SDL2pp::Exception /// /// \see http://wiki.libsdl.org/SDL_RenderDrawPoints /// //////////////////////////////////////////////////////////// - void DrawPoints(const Point* points, int count); + Renderer& DrawPoints(const Point* points, int count); //////////////////////////////////////////////////////////// /// \brief Draw a line on the current rendering target @@ -319,12 +341,14 @@ public: /// \param[in] x2 X coordinate of the end point /// \param[in] y2 Y coordinate of the end point /// + /// \returns Reference to self + /// /// \throws SDL2pp::Exception /// /// \see http://wiki.libsdl.org/SDL_RenderDrawLine /// //////////////////////////////////////////////////////////// - void DrawLine(int x1, int y1, int x2, int y2); + Renderer& DrawLine(int x1, int y1, int x2, int y2); //////////////////////////////////////////////////////////// /// \brief Draw a line on the current rendering target @@ -332,12 +356,14 @@ public: /// \param[in] p1 Coordinates of the start point /// \param[in] p2 Coordinates of the end point /// + /// \returns Reference to self + /// /// \throws SDL2pp::Exception /// /// \see http://wiki.libsdl.org/SDL_RenderDrawLine /// //////////////////////////////////////////////////////////// - void DrawLine(const Point& p1, const Point& p2); + Renderer& DrawLine(const Point& p1, const Point& p2); //////////////////////////////////////////////////////////// /// \brief Draw a polyline on the current rendering target @@ -345,12 +371,14 @@ public: /// \param[in] points Array of coordinates of points along the polyline /// \param[in] count Number of points to draw count-1 polyline segments /// + /// \returns Reference to self + /// /// \throws SDL2pp::Exception /// /// \see http://wiki.libsdl.org/SDL_RenderDrawLines /// //////////////////////////////////////////////////////////// - void DrawLines(const Point* points, int count); + Renderer& DrawLines(const Point* points, int count); //////////////////////////////////////////////////////////// /// \brief Draw a rectangle on the current rendering target @@ -360,12 +388,14 @@ public: /// \param[in] x2 X coordinate of the end corner /// \param[in] y2 Y coordinate of the end corner /// + /// \returns Reference to self + /// /// \throws SDL2pp::Exception /// /// \see http://wiki.libsdl.org/SDL_RenderDrawRect /// //////////////////////////////////////////////////////////// - void DrawRect(int x1, int y1, int x2, int y2); + Renderer& DrawRect(int x1, int y1, int x2, int y2); //////////////////////////////////////////////////////////// /// \brief Draw a rectangle on the current rendering target @@ -373,24 +403,28 @@ public: /// \param[in] p1 Coordinates of the start corner /// \param[in] p2 Coordinates of the end corner /// + /// \returns Reference to self + /// /// \throws SDL2pp::Exception /// /// \see http://wiki.libsdl.org/SDL_RenderDrawRect /// //////////////////////////////////////////////////////////// - void DrawRect(const Point& p1, const Point& p2); + Renderer& DrawRect(const Point& p1, const Point& p2); //////////////////////////////////////////////////////////// /// \brief Draw a rectangle on the current rendering target /// /// \param[in] r Rectangle to draw /// + /// \returns Reference to self + /// /// \throws SDL2pp::Exception /// /// \see http://wiki.libsdl.org/SDL_RenderDrawRect /// //////////////////////////////////////////////////////////// - void DrawRect(const Rect& r); + Renderer& DrawRect(const Rect& r); //////////////////////////////////////////////////////////// /// \brief Draw multiple rectangles on the current rendering target @@ -398,12 +432,14 @@ public: /// \param[in] rects Array of rectangles to draw /// \param[in] count Number of rectangles /// + /// \returns Reference to self + /// /// \throws SDL2pp::Exception /// /// \see http://wiki.libsdl.org/SDL_RenderDrawRects /// //////////////////////////////////////////////////////////// - void DrawRects(const Rect* rects, int count); + Renderer& DrawRects(const Rect* rects, int count); //////////////////////////////////////////////////////////// /// \brief Fill a rectangle on the current rendering target @@ -413,12 +449,14 @@ public: /// \param[in] x2 X coordinate of the end corner /// \param[in] y2 Y coordinate of the end corner /// + /// \returns Reference to self + /// /// \throws SDL2pp::Exception /// /// \see http://wiki.libsdl.org/SDL_RenderFillRect /// //////////////////////////////////////////////////////////// - void FillRect(int x1, int y1, int x2, int y2); + Renderer& FillRect(int x1, int y1, int x2, int y2); //////////////////////////////////////////////////////////// /// \brief Fill a rectangle on the current rendering target @@ -426,24 +464,28 @@ public: /// \param[in] p1 Coordinates of the start corner /// \param[in] p2 Coordinates of the end corner /// + /// \returns Reference to self + /// /// \throws SDL2pp::Exception /// /// \see http://wiki.libsdl.org/SDL_RenderFillRect /// //////////////////////////////////////////////////////////// - void FillRect(const Point& p1, const Point& p2); + Renderer& FillRect(const Point& p1, const Point& p2); //////////////////////////////////////////////////////////// /// \brief Fill a rectangle on the current rendering target /// /// \param[in] r Rectangle to draw /// + /// \returns Reference to self + /// /// \throws SDL2pp::Exception /// /// \see http://wiki.libsdl.org/SDL_RenderFillRect /// //////////////////////////////////////////////////////////// - void FillRect(const Rect& r); + Renderer& FillRect(const Rect& r); //////////////////////////////////////////////////////////// /// \brief Fill multiple rectangles on the current rendering target @@ -451,12 +493,14 @@ public: /// \param[in] rects Array of rectangles to draw /// \param[in] count Number of rectangles /// + /// \returns Reference to self + /// /// \throws SDL2pp::Exception /// /// \see http://wiki.libsdl.org/SDL_RenderFillRects /// //////////////////////////////////////////////////////////// - void FillRects(const Rect* rects, int count); + Renderer& FillRects(const Rect* rects, int count); //////////////////////////////////////////////////////////// /// \brief Read pixels from the current rendering target @@ -483,12 +527,14 @@ public: /// \param[in] rect New clipping rectangle or NullOpt to disable /// clipping /// + /// \returns Reference to self + /// /// \throws SDL2pp::Exception /// /// \see http://wiki.libsdl.org/SDL_RenderSetClipRect /// //////////////////////////////////////////////////////////// - void SetClipRect(const Optional& rect = NullOpt); + Renderer& SetClipRect(const Optional& rect = NullOpt); //////////////////////////////////////////////////////////// /// \brief Set a device independent resolution for rendering @@ -496,12 +542,14 @@ public: /// \param[in] w Width of the logical resolution /// \param[in] h Height of the logical resolution /// + /// \returns Reference to self + /// /// \throws SDL2pp::Exception /// /// \see http://wiki.libsdl.org/SDL_RenderSetLogicalSize /// //////////////////////////////////////////////////////////// - void SetLogicalSize(int w, int h); + Renderer& SetLogicalSize(int w, int h); //////////////////////////////////////////////////////////// /// \brief Set the drawing scale for rendering on the current target @@ -509,12 +557,14 @@ public: /// \param[in] scaleX Horizontal scaling factor /// \param[in] scaleY Vertical scaling factor /// + /// \returns Reference to self + /// /// \throws SDL2pp::Exception /// /// \see http://wiki.libsdl.org/SDL_RenderSetScale /// //////////////////////////////////////////////////////////// - void SetScale(float scaleX, float scaleY); + Renderer& SetScale(float scaleX, float scaleY); //////////////////////////////////////////////////////////// /// \brief Set the drawing area for rendering on the current target @@ -522,12 +572,14 @@ public: /// \param[in] rect Rectangle representing the drawing area or /// NullOpt to set the viewport to the entire target /// + /// \returns Reference to self + /// /// \throws SDL2pp::Exception /// /// \see http://wiki.libsdl.org/SDL_RenderSetViewport /// //////////////////////////////////////////////////////////// - void SetViewport(const Optional& rect = NullOpt); + Renderer& SetViewport(const Optional& rect = NullOpt); //////////////////////////////////////////////////////////// /// \brief Determine whether a window supports the use of