/* libSDL2pp - C++11 bindings/wrapper for SDL2 Copyright (C) 2013-2015 Dmitry Marakasov This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. */ #include #include #include #include #include #include namespace SDL2pp { Renderer::Renderer(SDL_Renderer* renderer) : renderer_(renderer) { } Renderer::Renderer(Window& window, int index, Uint32 flags) { if ((renderer_ = SDL_CreateRenderer(window.Get(), index, flags)) == nullptr) throw Exception("SDL_CreateRenderer failed"); } Renderer::~Renderer() { if (renderer_ != nullptr) SDL_DestroyRenderer(renderer_); } Renderer::Renderer(Renderer&& other) noexcept : renderer_(other.renderer_) { other.renderer_ = nullptr; } Renderer& Renderer::operator=(Renderer&& other) noexcept { if (&other == this) return *this; if (renderer_ != nullptr) SDL_DestroyRenderer(renderer_); renderer_ = other.renderer_; other.renderer_ = nullptr; return *this; } SDL_Renderer* Renderer::Get() const { return renderer_; } Renderer& Renderer::Present() { SDL_RenderPresent(renderer_); return *this; } Renderer& Renderer::Clear() { if (SDL_RenderClear(renderer_) != 0) throw Exception("SDL_RenderClear failed"); return *this; } void Renderer::GetInfo(SDL_RendererInfo* info) { if (SDL_GetRendererInfo(renderer_, info) != 0) throw Exception("SDL_GetRendererInfo failed"); } void Renderer::GetInfo(SDL_RendererInfo& info) { if (SDL_GetRendererInfo(renderer_, &info) != 0) throw Exception("SDL_GetRendererInfo failed"); } 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; } 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; } 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; } Renderer& Renderer::SetTarget() { if (SDL_SetRenderTarget(renderer_, nullptr) != 0) throw Exception("SDL_SetRenderTarget failed"); return *this; } Renderer& Renderer::SetTarget(Texture& texture) { if (SDL_SetRenderTarget(renderer_, texture.Get()) != 0) throw Exception("SDL_SetRenderTarget failed"); return *this; } Renderer& Renderer::SetDrawBlendMode(SDL_BlendMode blendMode) { if (SDL_SetRenderDrawBlendMode(renderer_, blendMode) != 0) throw Exception("SDL_SetRenderDrawBlendMode failed"); return *this; } Renderer& Renderer::DrawPoint(int x, int y) { if (SDL_RenderDrawPoint(renderer_, x, y) != 0) throw Exception("SDL_RenderDrawPoint failed"); return *this; } Renderer& Renderer::DrawPoint(const Point& p) { DrawPoint(p.x, p.y); return *this; } 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) sdl_points.emplace_back(*p); if (SDL_RenderDrawPoints(renderer_, sdl_points.data(), sdl_points.size()) != 0) throw Exception("SDL_RenderDrawPoints failed"); return *this; } 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; } Renderer& Renderer::DrawLine(const Point& p1, const Point& p2) { DrawLine(p1.x, p1.y, p2.x, p2.y); return *this; } 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) sdl_points.emplace_back(*p); if (SDL_RenderDrawLines(renderer_, sdl_points.data(), sdl_points.size()) != 0) throw Exception("SDL_RenderDrawLines failed"); return *this; } 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; } Renderer& Renderer::DrawRect(const Point& p1, const Point& p2) { DrawRect(p1.x, p1.y, p2.x, p2.y); return *this; } Renderer& Renderer::DrawRect(const Rect& r) { if (SDL_RenderDrawRect(renderer_, &r) != 0) throw Exception("SDL_RenderDrawRect failed"); return *this; } 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) sdl_rects.emplace_back(*r); if (SDL_RenderDrawRects(renderer_, sdl_rects.data(), sdl_rects.size()) != 0) throw Exception("SDL_RenderDrawRects failed"); return *this; } 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; } Renderer& Renderer::FillRect(const Point& p1, const Point& p2) { FillRect(p1.x, p1.y, p2.x, p2.y); return *this; } Renderer& Renderer::FillRect(const Rect& r) { if (SDL_RenderFillRect(renderer_, &r) != 0) throw Exception("SDL_RenderFillRect failed"); return *this; } 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) sdl_rects.emplace_back(*r); 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) { if (SDL_RenderReadPixels(renderer_, rect ? &*rect : nullptr, format, pixels, pitch) != 0) throw Exception("SDL_RenderReadPixels failed"); } Renderer& Renderer::SetClipRect(const Optional& rect) { if (SDL_RenderSetClipRect(renderer_, rect ? &*rect : nullptr) != 0) throw Exception("SDL_RenderSetClipRect failed"); return *this; } Renderer& Renderer::SetLogicalSize(int w, int h) { if (SDL_RenderSetLogicalSize(renderer_, w, h) != 0) throw Exception("SDL_RenderSetLogicalSize failed"); return *this; } Renderer& Renderer::SetScale(float scaleX, float scaleY) { if (SDL_RenderSetScale(renderer_, scaleX, scaleY) != 0) throw Exception("SDL_RenderSetScale failed"); return *this; } 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 { return SDL_RenderTargetSupported(renderer_) == SDL_TRUE; } Rect Renderer::GetClipRect() const { SDL_Rect rect; SDL_RenderGetClipRect(renderer_, &rect); return rect; } Point Renderer::GetLogicalSize() const { int w, h; SDL_RenderGetLogicalSize(renderer_, &w, &h); return Point(w, h); } int Renderer::GetLogicalWidth() const { int w; SDL_RenderGetLogicalSize(renderer_, &w, nullptr); return w; } int Renderer::GetLogicalHeight() const { int h; SDL_RenderGetLogicalSize(renderer_, nullptr, &h); return h; } void Renderer::GetScale(float& scalex, float& scaley) const { SDL_RenderGetScale(renderer_, &scalex, &scaley); } float Renderer::GetXScale() const { float scalex; SDL_RenderGetScale(renderer_, &scalex, nullptr); return scalex; } float Renderer::GetYScale() const { float scaley; SDL_RenderGetScale(renderer_, nullptr, &scaley); return scaley; } Rect Renderer::GetViewport() const { SDL_Rect rect; SDL_RenderGetViewport(renderer_, &rect); return rect; } SDL_BlendMode Renderer::GetDrawBlendMode() const { SDL_BlendMode mode; if (SDL_GetRenderDrawBlendMode(renderer_, &mode) != 0) throw Exception("SDL_GetRenderDrawBlendMode failed"); return mode; } void Renderer::GetDrawColor(Uint8& r, Uint8& g, Uint8& b, Uint8& a) const { if (SDL_GetRenderDrawColor(renderer_, &r, &g, &b, &a) != 0) throw Exception("SDL_GetRenderDrawColor failed"); } Point Renderer::GetOutputSize() const { int w, h; if (SDL_GetRendererOutputSize(renderer_, &w, &h) != 0) throw Exception("SDL_GetRendererOutputSize failed"); return Point(w, h); } int Renderer::GetOutputWidth() const { int w; if (SDL_GetRendererOutputSize(renderer_, &w, nullptr) != 0) throw Exception("SDL_GetRendererOutputSize failed"); return w; } int Renderer::GetOutputHeight() const { int h; if (SDL_GetRendererOutputSize(renderer_, nullptr, &h) != 0) throw Exception("SDL_GetRendererOutputSize failed"); return h; } }