Exception rework

Now it explicitely stores name of SDL function which caused an error
and generates complete user-readable error message which contains both
function name and SDL error message. Users can now handle SDL2pp
exceptions along with all others in `catch (std::exception&)' and
get complete error info.

While here, fixed incorrect function names in some throw's
This commit is contained in:
Dmitry Marakasov 2015-01-19 01:41:37 +03:00
parent c22b50e62e
commit 689f57b864
24 changed files with 152 additions and 156 deletions

View File

@ -43,9 +43,12 @@ This library provides C++11 bindings/wrapper for SDL2 and satellite libraries.
// All SDL objects are released at this point or if an error occurs // All SDL objects are released at this point or if an error occurs
} catch (SDL2pp::Exception& e) { } catch (SDL2pp::Exception& e) {
// Exception stores SDL_GetError() result // Exception stores SDL_GetError() result and name of function which failed
std::cerr << "Exception: " << e.what() << std::endl; std::cerr << "Error in: " << e.GetSDLFunction() << std::endl;
std::cerr << "SDL Error: " << e.GetSDLError() << std::endl; std::cerr << " Reason: " << e.GetSDLError() << std::endl;
} catch (std::exception& e) {
// This also works (e.g. "SDL_Init failed: No available video device")
std::cerr << e.what() << std::endl;
} }
## Features ## ## Features ##

View File

@ -40,7 +40,7 @@ AudioDevice::AudioDevice(const Optional<std::string>& device, bool iscapture, co
SDL_AudioSpec obtained; SDL_AudioSpec obtained;
if ((device_id_ = SDL_OpenAudioDevice(device ? device->c_str() : nullptr, iscapture ? 1 : 0, &spec_with_callback, &obtained, 0)) == 0) if ((device_id_ = SDL_OpenAudioDevice(device ? device->c_str() : nullptr, iscapture ? 1 : 0, &spec_with_callback, &obtained, 0)) == 0)
throw Exception("SDL_OpenAudioDevice failed"); throw Exception("SDL_OpenAudioDevice");
callback_ = std::move(callback); callback_ = std::move(callback);
} }
@ -54,7 +54,7 @@ AudioDevice::AudioDevice(const Optional<std::string>& device, bool iscapture, Au
SDL_AudioSpec obtained; SDL_AudioSpec obtained;
if ((device_id_ = SDL_OpenAudioDevice(device ? device->c_str() : nullptr, iscapture ? 1 : 0, &spec_with_callback, &obtained, allowed_changes)) == 0) if ((device_id_ = SDL_OpenAudioDevice(device ? device->c_str() : nullptr, iscapture ? 1 : 0, &spec_with_callback, &obtained, allowed_changes)) == 0)
throw Exception("SDL_OpenAudioDevice failed"); throw Exception("SDL_OpenAudioDevice");
spec.MergeChanges(obtained); spec.MergeChanges(obtained);
@ -113,7 +113,7 @@ AudioDevice::LockHandle AudioDevice::Lock() {
#ifdef SDL2PP_WITH_2_0_4 #ifdef SDL2PP_WITH_2_0_4
AudioDevice& AudioDevice::QueueAudio(const void* data, Uint32 len) { AudioDevice& AudioDevice::QueueAudio(const void* data, Uint32 len) {
if (SDL_QueueAudio(device_id_, data, len) == 0) if (SDL_QueueAudio(device_id_, data, len) == 0)
throw Exception("SDL_QueueAudio failed"); throw Exception("SDL_QueueAudio");
return *this; return *this;
} }

View File

@ -1,6 +1,6 @@
/* /*
libSDL2pp - C++11 bindings/wrapper for SDL2 libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 Dmitry Marakasov <amdmi3@amdmi3.ru> Copyright (C) 2013-2015 Dmitry Marakasov <amdmi3@amdmi3.ru>
This software is provided 'as-is', without any express or implied This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages
@ -25,18 +25,25 @@
namespace SDL2pp { namespace SDL2pp {
Exception::Exception(const char* what) : what_(what), sdl_error_(SDL_GetError()) { Exception::Exception(const char* function)
: sdl_function_(function),
sdl_error_(SDL_GetError()),
what_(sdl_function_ + " failed: " + sdl_error_) {
} }
Exception::~Exception() noexcept { Exception::~Exception() noexcept {
} }
const char* Exception::what() const noexcept { const char* Exception::what() const noexcept {
return what_; return what_.c_str();
} }
const char* Exception::GetSDLError() const noexcept { std::string Exception::GetSDLFunction() const {
return sdl_error_.c_str(); return sdl_function_;
}
std::string Exception::GetSDLError() const {
return sdl_error_;
} }
} // namespace SDL2pp } // namespace SDL2pp

View File

@ -68,17 +68,18 @@ namespace SDL2pp {
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
class Exception : public std::exception { class Exception : public std::exception {
private: private:
const char* what_; ///< User-specified message std::string sdl_function_; ///< SDL function which caused an error
std::string sdl_error_; ///< SDL error string std::string sdl_error_; ///< SDL error string
std::string what_; ///< User-readable message
public: public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Construct exception, storing result of SDL_GetError() /// \brief Construct exception, storing result of SDL_GetError()
/// ///
/// \param[in] what User-specified explanatory string /// \param[in] function Name of SDL function which generated an error
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Exception(const char* what = ""); Exception(const char* function);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Destructor /// \brief Destructor
@ -94,6 +95,14 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
const char* what() const noexcept; const char* what() const noexcept;
////////////////////////////////////////////////////////////
/// \brief Get name of SDL function which caused an error
///
/// \returns Name of function which caused an error
///
////////////////////////////////////////////////////////////
std::string GetSDLFunction() const;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Get SDL2 error text /// \brief Get SDL2 error text
/// ///
@ -102,7 +111,7 @@ public:
/// \see http://wiki.libsdl.org/SDL_GetError /// \see http://wiki.libsdl.org/SDL_GetError
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
const char* GetSDLError() const noexcept; std::string GetSDLError() const;
}; };
} }

View File

@ -33,12 +33,12 @@ Font::Font(TTF_Font* font) {
Font::Font(const std::string& file, int ptsize, long index) { Font::Font(const std::string& file, int ptsize, long index) {
if ((font_ = TTF_OpenFontIndex(file.c_str(), ptsize, index)) == nullptr) if ((font_ = TTF_OpenFontIndex(file.c_str(), ptsize, index)) == nullptr)
throw Exception("TTF_OpenFontIndex failed"); throw Exception("TTF_OpenFontIndex");
} }
Font::Font(RWops& rwops, int ptsize, long index) { Font::Font(RWops& rwops, int ptsize, long index) {
if ((font_ = TTF_OpenFontIndexRW(rwops.Get(), 0, ptsize, index)) == nullptr) if ((font_ = TTF_OpenFontIndexRW(rwops.Get(), 0, ptsize, index)) == nullptr)
throw Exception("TTF_OpenFontIndexRW failed"); throw Exception("TTF_OpenFontIndexRW");
} }
Font::~Font() { Font::~Font() {
@ -140,125 +140,125 @@ Optional<std::string> Font::GetStyleName() const {
void Font::GetGlyphMetrics(Uint16 ch, int& minx, int& maxx, int& miny, int& maxy, int& advance) const { void Font::GetGlyphMetrics(Uint16 ch, int& minx, int& maxx, int& miny, int& maxy, int& advance) const {
if (TTF_GlyphMetrics(font_, ch, &minx, &maxx, &miny, &maxy, &advance) != 0) if (TTF_GlyphMetrics(font_, ch, &minx, &maxx, &miny, &maxy, &advance) != 0)
throw Exception("TTF_GlyphMetrics failed"); throw Exception("TTF_GlyphMetrics");
} }
Rect Font::GetGlyphRect(Uint16 ch) const { Rect Font::GetGlyphRect(Uint16 ch) const {
int minx, maxx, miny, maxy; int minx, maxx, miny, maxy;
if (TTF_GlyphMetrics(font_, ch, &minx, &maxx, &miny, &maxy, nullptr) != 0) if (TTF_GlyphMetrics(font_, ch, &minx, &maxx, &miny, &maxy, nullptr) != 0)
throw Exception("TTF_GlyphMetrics failed"); throw Exception("TTF_GlyphMetrics");
return Rect(minx, miny, maxx - minx + 1, maxy - miny + 1); return Rect(minx, miny, maxx - minx + 1, maxy - miny + 1);
} }
int Font::GetGlyphAdvance(Uint16 ch) const { int Font::GetGlyphAdvance(Uint16 ch) const {
int advance; int advance;
if (TTF_GlyphMetrics(font_, ch, nullptr, nullptr, nullptr, nullptr, &advance) != 0) if (TTF_GlyphMetrics(font_, ch, nullptr, nullptr, nullptr, nullptr, &advance) != 0)
throw Exception("TTF_GlyphMetrics failed"); throw Exception("TTF_GlyphMetrics");
return advance; return advance;
} }
Point Font::GetSizeText(const std::string& text) const { Point Font::GetSizeText(const std::string& text) const {
int w, h; int w, h;
if (TTF_SizeText(font_, text.c_str(), &w, &h) != 0) if (TTF_SizeText(font_, text.c_str(), &w, &h) != 0)
throw Exception("TTF_SizeText failed"); throw Exception("TTF_SizeText");
return Point(w, h); return Point(w, h);
} }
Point Font::GetSizeUTF8(const std::string& text) const { Point Font::GetSizeUTF8(const std::string& text) const {
int w, h; int w, h;
if (TTF_SizeUTF8(font_, text.c_str(), &w, &h) != 0) if (TTF_SizeUTF8(font_, text.c_str(), &w, &h) != 0)
throw Exception("TTF_SizeUTF8 failed"); throw Exception("TTF_SizeUTF8");
return Point(w, h); return Point(w, h);
} }
Point Font::GetSizeUNICODE(const Uint16* text) const { Point Font::GetSizeUNICODE(const Uint16* text) const {
int w, h; int w, h;
if (TTF_SizeUNICODE(font_, text, &w, &h) != 0) if (TTF_SizeUNICODE(font_, text, &w, &h) != 0)
throw Exception("TTF_SizeUNICODE failed"); throw Exception("TTF_SizeUNICODE");
return Point(w, h); return Point(w, h);
} }
Surface Font::RenderText_Solid(const std::string& text, SDL_Color fg) { Surface Font::RenderText_Solid(const std::string& text, SDL_Color fg) {
SDL_Surface* surface = TTF_RenderText_Solid(font_, text.c_str(), fg); SDL_Surface* surface = TTF_RenderText_Solid(font_, text.c_str(), fg);
if (surface == nullptr) if (surface == nullptr)
throw Exception("TTF_RenderText_Solid failed"); throw Exception("TTF_RenderText_Solid");
return Surface(surface); return Surface(surface);
} }
Surface Font::RenderUTF8_Solid(const std::string& text, SDL_Color fg) { Surface Font::RenderUTF8_Solid(const std::string& text, SDL_Color fg) {
SDL_Surface* surface = TTF_RenderUTF8_Solid(font_, text.c_str(), fg); SDL_Surface* surface = TTF_RenderUTF8_Solid(font_, text.c_str(), fg);
if (surface == nullptr) if (surface == nullptr)
throw Exception("TTF_RenderUTF8_Solid failed"); throw Exception("TTF_RenderUTF8_Solid");
return Surface(surface); return Surface(surface);
} }
Surface Font::RenderUNICODE_Solid(const Uint16* text, SDL_Color fg) { Surface Font::RenderUNICODE_Solid(const Uint16* text, SDL_Color fg) {
SDL_Surface* surface = TTF_RenderUNICODE_Solid(font_, text, fg); SDL_Surface* surface = TTF_RenderUNICODE_Solid(font_, text, fg);
if (surface == nullptr) if (surface == nullptr)
throw Exception("TTF_RenderUNICODE_Solid failed"); throw Exception("TTF_RenderUNICODE_Solid");
return Surface(surface); return Surface(surface);
} }
Surface Font::RenderGlyph_Solid(Uint16 ch, SDL_Color fg) { Surface Font::RenderGlyph_Solid(Uint16 ch, SDL_Color fg) {
SDL_Surface* surface = TTF_RenderGlyph_Solid(font_, ch, fg); SDL_Surface* surface = TTF_RenderGlyph_Solid(font_, ch, fg);
if (surface == nullptr) if (surface == nullptr)
throw Exception("TTF_RenderGlyph_Solid failed"); throw Exception("TTF_RenderGlyph_Solid");
return Surface(surface); return Surface(surface);
} }
Surface Font::RenderText_Shaded(const std::string& text, SDL_Color fg, SDL_Color bg) { Surface Font::RenderText_Shaded(const std::string& text, SDL_Color fg, SDL_Color bg) {
SDL_Surface* surface = TTF_RenderText_Shaded(font_, text.c_str(), fg, bg); SDL_Surface* surface = TTF_RenderText_Shaded(font_, text.c_str(), fg, bg);
if (surface == nullptr) if (surface == nullptr)
throw Exception("TTF_RenderText_Shaded failed"); throw Exception("TTF_RenderText_Shaded");
return Surface(surface); return Surface(surface);
} }
Surface Font::RenderUTF8_Shaded(const std::string& text, SDL_Color fg, SDL_Color bg) { Surface Font::RenderUTF8_Shaded(const std::string& text, SDL_Color fg, SDL_Color bg) {
SDL_Surface* surface = TTF_RenderUTF8_Shaded(font_, text.c_str(), fg, bg); SDL_Surface* surface = TTF_RenderUTF8_Shaded(font_, text.c_str(), fg, bg);
if (surface == nullptr) if (surface == nullptr)
throw Exception("TTF_RenderUTF8_Shaded failed"); throw Exception("TTF_RenderUTF8_Shaded");
return Surface(surface); return Surface(surface);
} }
Surface Font::RenderUNICODE_Shaded(const Uint16* text, SDL_Color fg, SDL_Color bg) { Surface Font::RenderUNICODE_Shaded(const Uint16* text, SDL_Color fg, SDL_Color bg) {
SDL_Surface* surface = TTF_RenderUNICODE_Shaded(font_, text, fg, bg); SDL_Surface* surface = TTF_RenderUNICODE_Shaded(font_, text, fg, bg);
if (surface == nullptr) if (surface == nullptr)
throw Exception("TTF_RenderUNICODE_Shaded failed"); throw Exception("TTF_RenderUNICODE_Shaded");
return Surface(surface); return Surface(surface);
} }
Surface Font::RenderGlyph_Shaded(Uint16 ch, SDL_Color fg, SDL_Color bg) { Surface Font::RenderGlyph_Shaded(Uint16 ch, SDL_Color fg, SDL_Color bg) {
SDL_Surface* surface = TTF_RenderGlyph_Shaded(font_, ch, fg, bg); SDL_Surface* surface = TTF_RenderGlyph_Shaded(font_, ch, fg, bg);
if (surface == nullptr) if (surface == nullptr)
throw Exception("TTF_RenderGlyph_Shaded failed"); throw Exception("TTF_RenderGlyph_Shaded");
return Surface(surface); return Surface(surface);
} }
Surface Font::RenderText_Blended(const std::string& text, SDL_Color fg) { Surface Font::RenderText_Blended(const std::string& text, SDL_Color fg) {
SDL_Surface* surface = TTF_RenderText_Blended(font_, text.c_str(), fg); SDL_Surface* surface = TTF_RenderText_Blended(font_, text.c_str(), fg);
if (surface == nullptr) if (surface == nullptr)
throw Exception("TTF_RenderText_Blended failed"); throw Exception("TTF_RenderText_Blended");
return Surface(surface); return Surface(surface);
} }
Surface Font::RenderUTF8_Blended(const std::string& text, SDL_Color fg) { Surface Font::RenderUTF8_Blended(const std::string& text, SDL_Color fg) {
SDL_Surface* surface = TTF_RenderUTF8_Blended(font_, text.c_str(), fg); SDL_Surface* surface = TTF_RenderUTF8_Blended(font_, text.c_str(), fg);
if (surface == nullptr) if (surface == nullptr)
throw Exception("TTF_RenderUTF8_Blended failed"); throw Exception("TTF_RenderUTF8_Blended");
return Surface(surface); return Surface(surface);
} }
Surface Font::RenderUNICODE_Blended(const Uint16* text, SDL_Color fg) { Surface Font::RenderUNICODE_Blended(const Uint16* text, SDL_Color fg) {
SDL_Surface* surface = TTF_RenderUNICODE_Blended(font_, text, fg); SDL_Surface* surface = TTF_RenderUNICODE_Blended(font_, text, fg);
if (surface == nullptr) if (surface == nullptr)
throw Exception("TTF_RenderUNICODE_Blended failed"); throw Exception("TTF_RenderUNICODE_Blended");
return Surface(surface); return Surface(surface);
} }
Surface Font::RenderGlyph_Blended(Uint16 ch, SDL_Color fg) { Surface Font::RenderGlyph_Blended(Uint16 ch, SDL_Color fg) {
SDL_Surface* surface = TTF_RenderGlyph_Blended(font_, ch, fg); SDL_Surface* surface = TTF_RenderGlyph_Blended(font_, ch, fg);
if (surface == nullptr) if (surface == nullptr)
throw Exception("TTF_RenderGlyph_Blended failed"); throw Exception("TTF_RenderGlyph_Blended");
return Surface(surface); return Surface(surface);
} }

View File

@ -114,25 +114,25 @@ RWops RWops::CheckedCreateStandardRWops(SDL_RWops* sdl_rwops, const char* errmsg
} }
RWops RWops::FromFP(FILE* file, bool autoclose) { RWops RWops::FromFP(FILE* file, bool autoclose) {
return CheckedCreateStandardRWops(SDL_RWFromFP(file, autoclose ? SDL_TRUE : SDL_FALSE), "SDL_RWFromFP failed"); return CheckedCreateStandardRWops(SDL_RWFromFP(file, autoclose ? SDL_TRUE : SDL_FALSE), "SDL_RWFromFP");
} }
RWops RWops::FromConstMem(const void* mem, int size) { RWops RWops::FromConstMem(const void* mem, int size) {
return CheckedCreateStandardRWops(SDL_RWFromConstMem(mem, size), "SDL_RWFromConstMem failed"); return CheckedCreateStandardRWops(SDL_RWFromConstMem(mem, size), "SDL_RWFromConstMem");
} }
RWops RWops::FromMem(void* mem, int size) { RWops RWops::FromMem(void* mem, int size) {
return CheckedCreateStandardRWops(SDL_RWFromMem(mem, size), "SDL_RWFromMem failed"); return CheckedCreateStandardRWops(SDL_RWFromMem(mem, size), "SDL_RWFromMem");
} }
RWops RWops::FromFile(const std::string& file, const std::string& mode) { RWops RWops::FromFile(const std::string& file, const std::string& mode) {
return CheckedCreateStandardRWops(SDL_RWFromFile(file.c_str(), mode.c_str()), "SDL_RWFromFile failed"); return CheckedCreateStandardRWops(SDL_RWFromFile(file.c_str(), mode.c_str()), "SDL_RWFromFile");
} }
RWops::RWops(SDL_RWops* rwops) { RWops::RWops(SDL_RWops* rwops) {
rwops_ = SDL_AllocRW(); rwops_ = SDL_AllocRW();
if (rwops_ == nullptr) if (rwops_ == nullptr)
throw Exception("SDL_AllocRW failed"); throw Exception("SDL_AllocRW");
rwops_->seek = StdSeekFuncWrapper; rwops_->seek = StdSeekFuncWrapper;
rwops_->read = StdReadFuncWrapper; rwops_->read = StdReadFuncWrapper;

View File

@ -269,7 +269,7 @@ public:
RWops(C&& custom_rwops) { RWops(C&& custom_rwops) {
rwops_ = SDL_AllocRW(); rwops_ = SDL_AllocRW();
if (rwops_ == nullptr) if (rwops_ == nullptr)
throw Exception("SDL_AllocRW failed"); throw Exception("SDL_AllocRW");
rwops_->seek = CustomSeekFuncWrapper; rwops_->seek = CustomSeekFuncWrapper;
rwops_->read = CustomReadFuncWrapper; rwops_->read = CustomReadFuncWrapper;

View File

@ -35,7 +35,7 @@ Renderer::Renderer(SDL_Renderer* renderer) : renderer_(renderer) {
Renderer::Renderer(Window& window, int index, Uint32 flags) { Renderer::Renderer(Window& window, int index, Uint32 flags) {
if ((renderer_ = SDL_CreateRenderer(window.Get(), index, flags)) == nullptr) if ((renderer_ = SDL_CreateRenderer(window.Get(), index, flags)) == nullptr)
throw Exception("SDL_CreateRenderer failed"); throw Exception("SDL_CreateRenderer");
} }
Renderer::~Renderer() { Renderer::~Renderer() {
@ -68,59 +68,59 @@ Renderer& Renderer::Present() {
Renderer& Renderer::Clear() { Renderer& Renderer::Clear() {
if (SDL_RenderClear(renderer_) != 0) if (SDL_RenderClear(renderer_) != 0)
throw Exception("SDL_RenderClear failed"); throw Exception("SDL_RenderClear");
return *this; return *this;
} }
void Renderer::GetInfo(SDL_RendererInfo* info) { void Renderer::GetInfo(SDL_RendererInfo* info) {
if (SDL_GetRendererInfo(renderer_, info) != 0) if (SDL_GetRendererInfo(renderer_, info) != 0)
throw Exception("SDL_GetRendererInfo failed"); throw Exception("SDL_GetRendererInfo");
} }
void Renderer::GetInfo(SDL_RendererInfo& info) { void Renderer::GetInfo(SDL_RendererInfo& info) {
if (SDL_GetRendererInfo(renderer_, &info) != 0) if (SDL_GetRendererInfo(renderer_, &info) != 0)
throw Exception("SDL_GetRendererInfo failed"); throw Exception("SDL_GetRendererInfo");
} }
Renderer& Renderer::Copy(Texture& texture, const Optional<Rect>& srcrect, const Optional<Rect>& dstrect) { Renderer& Renderer::Copy(Texture& texture, const Optional<Rect>& srcrect, const Optional<Rect>& dstrect) {
if (SDL_RenderCopy(renderer_, texture.Get(), srcrect ? &*srcrect : nullptr, dstrect ? &*dstrect : nullptr) != 0) if (SDL_RenderCopy(renderer_, texture.Get(), srcrect ? &*srcrect : nullptr, dstrect ? &*dstrect : nullptr) != 0)
throw Exception("SDL_RenderCopy failed"); throw Exception("SDL_RenderCopy");
return *this; return *this;
} }
Renderer& Renderer::Copy(Texture& texture, const Optional<Rect>& srcrect, const Optional<Rect>& dstrect, double angle, const Optional<Point>& center, int flip) { Renderer& Renderer::Copy(Texture& texture, const Optional<Rect>& srcrect, const Optional<Rect>& dstrect, double angle, const Optional<Point>& center, int flip) {
if (SDL_RenderCopyEx(renderer_, texture.Get(), srcrect ? &*srcrect : nullptr, dstrect ? &*dstrect : nullptr, angle, center ? &*center : nullptr, static_cast<SDL_RendererFlip>(flip)) != 0) if (SDL_RenderCopyEx(renderer_, texture.Get(), srcrect ? &*srcrect : nullptr, dstrect ? &*dstrect : nullptr, angle, center ? &*center : nullptr, static_cast<SDL_RendererFlip>(flip)) != 0)
throw Exception("SDL_RenderCopyEx failed"); throw Exception("SDL_RenderCopyEx");
return *this; return *this;
} }
Renderer& 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) if (SDL_SetRenderDrawColor(renderer_, r, g, b, a) != 0)
throw Exception("SDL_SetRenderDrawColor failed"); throw Exception("SDL_SetRenderDrawColor");
return *this; return *this;
} }
Renderer& Renderer::SetTarget() { Renderer& Renderer::SetTarget() {
if (SDL_SetRenderTarget(renderer_, nullptr) != 0) if (SDL_SetRenderTarget(renderer_, nullptr) != 0)
throw Exception("SDL_SetRenderTarget failed"); throw Exception("SDL_SetRenderTarget");
return *this; return *this;
} }
Renderer& Renderer::SetTarget(Texture& texture) { Renderer& Renderer::SetTarget(Texture& texture) {
if (SDL_SetRenderTarget(renderer_, texture.Get()) != 0) if (SDL_SetRenderTarget(renderer_, texture.Get()) != 0)
throw Exception("SDL_SetRenderTarget failed"); throw Exception("SDL_SetRenderTarget");
return *this; return *this;
} }
Renderer& Renderer::SetDrawBlendMode(SDL_BlendMode blendMode) { Renderer& Renderer::SetDrawBlendMode(SDL_BlendMode blendMode) {
if (SDL_SetRenderDrawBlendMode(renderer_, blendMode) != 0) if (SDL_SetRenderDrawBlendMode(renderer_, blendMode) != 0)
throw Exception("SDL_SetRenderDrawBlendMode failed"); throw Exception("SDL_SetRenderDrawBlendMode");
return *this; return *this;
} }
Renderer& Renderer::DrawPoint(int x, int y) { Renderer& Renderer::DrawPoint(int x, int y) {
if (SDL_RenderDrawPoint(renderer_, x, y) != 0) if (SDL_RenderDrawPoint(renderer_, x, y) != 0)
throw Exception("SDL_RenderDrawPoint failed"); throw Exception("SDL_RenderDrawPoint");
return *this; return *this;
} }
@ -136,14 +136,14 @@ Renderer& Renderer::DrawPoints(const Point* points, int count) {
sdl_points.emplace_back(*p); sdl_points.emplace_back(*p);
if (SDL_RenderDrawPoints(renderer_, sdl_points.data(), sdl_points.size()) != 0) if (SDL_RenderDrawPoints(renderer_, sdl_points.data(), sdl_points.size()) != 0)
throw Exception("SDL_RenderDrawPoints failed"); throw Exception("SDL_RenderDrawPoints");
return *this; return *this;
} }
Renderer& 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) if (SDL_RenderDrawLine(renderer_, x1, y1, x2, y2) != 0)
throw Exception("SDL_RenderDrawLine failed"); throw Exception("SDL_RenderDrawLine");
return *this; return *this;
} }
@ -159,7 +159,7 @@ Renderer& Renderer::DrawLines(const Point* points, int count) {
sdl_points.emplace_back(*p); sdl_points.emplace_back(*p);
if (SDL_RenderDrawLines(renderer_, sdl_points.data(), sdl_points.size()) != 0) if (SDL_RenderDrawLines(renderer_, sdl_points.data(), sdl_points.size()) != 0)
throw Exception("SDL_RenderDrawLines failed"); throw Exception("SDL_RenderDrawLines");
return *this; return *this;
} }
@ -167,7 +167,7 @@ Renderer& Renderer::DrawLines(const Point* points, int count) {
Renderer& 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}; SDL_Rect rect = {x1, y1, x2 - x1 + 1, y2 - y1 + 1};
if (SDL_RenderDrawRect(renderer_, &rect) != 0) if (SDL_RenderDrawRect(renderer_, &rect) != 0)
throw Exception("SDL_RenderDrawRect failed"); throw Exception("SDL_RenderDrawRect");
return *this; return *this;
} }
@ -178,7 +178,7 @@ Renderer& Renderer::DrawRect(const Point& p1, const Point& p2) {
Renderer& Renderer::DrawRect(const Rect& r) { Renderer& Renderer::DrawRect(const Rect& r) {
if (SDL_RenderDrawRect(renderer_, &r) != 0) if (SDL_RenderDrawRect(renderer_, &r) != 0)
throw Exception("SDL_RenderDrawRect failed"); throw Exception("SDL_RenderDrawRect");
return *this; return *this;
} }
@ -189,7 +189,7 @@ Renderer& Renderer::DrawRects(const Rect* rects, int count) {
sdl_rects.emplace_back(*r); sdl_rects.emplace_back(*r);
if (SDL_RenderDrawRects(renderer_, sdl_rects.data(), sdl_rects.size()) != 0) if (SDL_RenderDrawRects(renderer_, sdl_rects.data(), sdl_rects.size()) != 0)
throw Exception("SDL_RenderDrawRects failed"); throw Exception("SDL_RenderDrawRects");
return *this; return *this;
} }
@ -197,7 +197,7 @@ Renderer& Renderer::DrawRects(const Rect* rects, int count) {
Renderer& 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}; SDL_Rect rect = {x1, y1, x2 - x1 + 1, y2 - y1 + 1};
if (SDL_RenderFillRect(renderer_, &rect) != 0) if (SDL_RenderFillRect(renderer_, &rect) != 0)
throw Exception("SDL_RenderFillRect failed"); throw Exception("SDL_RenderFillRect");
return *this; return *this;
} }
@ -208,7 +208,7 @@ Renderer& Renderer::FillRect(const Point& p1, const Point& p2) {
Renderer& Renderer::FillRect(const Rect& r) { Renderer& Renderer::FillRect(const Rect& r) {
if (SDL_RenderFillRect(renderer_, &r) != 0) if (SDL_RenderFillRect(renderer_, &r) != 0)
throw Exception("SDL_RenderFillRect failed"); throw Exception("SDL_RenderFillRect");
return *this; return *this;
} }
@ -219,37 +219,37 @@ Renderer& Renderer::FillRects(const Rect* rects, int count) {
sdl_rects.emplace_back(*r); sdl_rects.emplace_back(*r);
if (SDL_RenderFillRects(renderer_, sdl_rects.data(), sdl_rects.size()) != 0) if (SDL_RenderFillRects(renderer_, sdl_rects.data(), sdl_rects.size()) != 0)
throw Exception("SDL_RenderFillRects failed"); throw Exception("SDL_RenderFillRects");
return *this; return *this;
} }
void Renderer::ReadPixels(const Optional<Rect>& rect, Uint32 format, void* pixels, int pitch) { void Renderer::ReadPixels(const Optional<Rect>& rect, Uint32 format, void* pixels, int pitch) {
if (SDL_RenderReadPixels(renderer_, rect ? &*rect : nullptr, format, pixels, pitch) != 0) if (SDL_RenderReadPixels(renderer_, rect ? &*rect : nullptr, format, pixels, pitch) != 0)
throw Exception("SDL_RenderReadPixels failed"); throw Exception("SDL_RenderReadPixels");
} }
Renderer& Renderer::SetClipRect(const Optional<Rect>& rect) { Renderer& Renderer::SetClipRect(const Optional<Rect>& rect) {
if (SDL_RenderSetClipRect(renderer_, rect ? &*rect : nullptr) != 0) if (SDL_RenderSetClipRect(renderer_, rect ? &*rect : nullptr) != 0)
throw Exception("SDL_RenderSetClipRect failed"); throw Exception("SDL_RenderSetClipRect");
return *this; return *this;
} }
Renderer& Renderer::SetLogicalSize(int w, int h) { Renderer& Renderer::SetLogicalSize(int w, int h) {
if (SDL_RenderSetLogicalSize(renderer_, w, h) != 0) if (SDL_RenderSetLogicalSize(renderer_, w, h) != 0)
throw Exception("SDL_RenderSetLogicalSize failed"); throw Exception("SDL_RenderSetLogicalSize");
return *this; return *this;
} }
Renderer& Renderer::SetScale(float scaleX, float scaleY) { Renderer& Renderer::SetScale(float scaleX, float scaleY) {
if (SDL_RenderSetScale(renderer_, scaleX, scaleY) != 0) if (SDL_RenderSetScale(renderer_, scaleX, scaleY) != 0)
throw Exception("SDL_RenderSetScale failed"); throw Exception("SDL_RenderSetScale");
return *this; return *this;
} }
Renderer& Renderer::SetViewport(const Optional<Rect>& rect) { Renderer& Renderer::SetViewport(const Optional<Rect>& rect) {
if (SDL_RenderSetViewport(renderer_, rect ? &*rect : nullptr) != 0) if (SDL_RenderSetViewport(renderer_, rect ? &*rect : nullptr) != 0)
throw Exception("SDL_RenderSetViewport failed"); throw Exception("SDL_RenderSetViewport");
return *this; return *this;
} }
@ -306,33 +306,33 @@ Rect Renderer::GetViewport() const {
SDL_BlendMode Renderer::GetDrawBlendMode() const { SDL_BlendMode Renderer::GetDrawBlendMode() const {
SDL_BlendMode mode; SDL_BlendMode mode;
if (SDL_GetRenderDrawBlendMode(renderer_, &mode) != 0) if (SDL_GetRenderDrawBlendMode(renderer_, &mode) != 0)
throw Exception("SDL_GetRenderDrawBlendMode failed"); throw Exception("SDL_GetRenderDrawBlendMode");
return mode; return mode;
} }
void Renderer::GetDrawColor(Uint8& r, Uint8& g, Uint8& b, Uint8& a) const { void Renderer::GetDrawColor(Uint8& r, Uint8& g, Uint8& b, Uint8& a) const {
if (SDL_GetRenderDrawColor(renderer_, &r, &g, &b, &a) != 0) if (SDL_GetRenderDrawColor(renderer_, &r, &g, &b, &a) != 0)
throw Exception("SDL_GetRenderDrawColor failed"); throw Exception("SDL_GetRenderDrawColor");
} }
Point Renderer::GetOutputSize() const { Point Renderer::GetOutputSize() const {
int w, h; int w, h;
if (SDL_GetRendererOutputSize(renderer_, &w, &h) != 0) if (SDL_GetRendererOutputSize(renderer_, &w, &h) != 0)
throw Exception("SDL_GetRendererOutputSize failed"); throw Exception("SDL_GetRendererOutputSize");
return Point(w, h); return Point(w, h);
} }
int Renderer::GetOutputWidth() const { int Renderer::GetOutputWidth() const {
int w; int w;
if (SDL_GetRendererOutputSize(renderer_, &w, nullptr) != 0) if (SDL_GetRendererOutputSize(renderer_, &w, nullptr) != 0)
throw Exception("SDL_GetRendererOutputSize failed"); throw Exception("SDL_GetRendererOutputSize");
return w; return w;
} }
int Renderer::GetOutputHeight() const { int Renderer::GetOutputHeight() const {
int h; int h;
if (SDL_GetRendererOutputSize(renderer_, nullptr, &h) != 0) if (SDL_GetRendererOutputSize(renderer_, nullptr, &h) != 0)
throw Exception("SDL_GetRendererOutputSize failed"); throw Exception("SDL_GetRendererOutputSize");
return h; return h;
} }

View File

@ -1,6 +1,6 @@
/* /*
libSDL2pp - C++11 bindings/wrapper for SDL2 libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 Dmitry Marakasov <amdmi3@amdmi3.ru> Copyright (C) 2013-2015 Dmitry Marakasov <amdmi3@amdmi3.ru>
This software is provided 'as-is', without any express or implied This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages
@ -28,7 +28,7 @@ namespace SDL2pp {
SDL::SDL(Uint32 flags) { SDL::SDL(Uint32 flags) {
if (SDL_Init(flags) != 0) if (SDL_Init(flags) != 0)
throw Exception("SDL_Init failed"); throw Exception("SDL_Init");
} }
SDL::~SDL() { SDL::~SDL() {
@ -41,7 +41,7 @@ Uint32 WasInit(Uint32 flags) {
void InitSubsystem(Uint32 flags) { void InitSubsystem(Uint32 flags) {
if (SDL_InitSubSystem(flags) != 0) if (SDL_InitSubSystem(flags) != 0)
throw Exception("SDL_InitSubsystem failed"); throw Exception("SDL_InitSubsystem");
} }
void QuitSubSystem(Uint32 flags) { void QuitSubSystem(Uint32 flags) {

View File

@ -1,6 +1,6 @@
/* /*
libSDL2pp - C++11 bindings/wrapper for SDL2 libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2014 Dmitry Marakasov <amdmi3@amdmi3.ru> Copyright (C) 2014-2015 Dmitry Marakasov <amdmi3@amdmi3.ru>
This software is provided 'as-is', without any express or implied This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages
@ -28,7 +28,7 @@ namespace SDL2pp {
SDLImage::SDLImage(int flags) { SDLImage::SDLImage(int flags) {
if ((IMG_Init(flags) & flags) != flags) if ((IMG_Init(flags) & flags) != flags)
throw Exception("IMG_Init failed"); throw Exception("IMG_Init");
} }
SDLImage::~SDLImage() { SDLImage::~SDLImage() {
@ -38,7 +38,7 @@ SDLImage::~SDLImage() {
int SDLImage::InitMore(int flags) { int SDLImage::InitMore(int flags) {
int ret; int ret;
if (((ret = IMG_Init(flags)) & flags) != flags) if (((ret = IMG_Init(flags)) & flags) != flags)
throw Exception("IMG_Init failed"); throw Exception("IMG_Init");
return ret; return ret;
} }

View File

@ -1,6 +1,6 @@
/* /*
libSDL2pp - C++11 bindings/wrapper for SDL2 libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2014 Dmitry Marakasov <amdmi3@amdmi3.ru> Copyright (C) 2014-2015 Dmitry Marakasov <amdmi3@amdmi3.ru>
This software is provided 'as-is', without any express or implied This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages
@ -28,7 +28,7 @@ namespace SDL2pp {
SDLTTF::SDLTTF() { SDLTTF::SDLTTF() {
if (TTF_Init() != 0) if (TTF_Init() != 0)
throw Exception("TTF_Init failed"); throw Exception("TTF_Init");
} }
SDLTTF::~SDLTTF() { SDLTTF::~SDLTTF() {

View File

@ -41,23 +41,23 @@ Surface::Surface(SDL_Surface* surface) : surface_(surface) {
Surface::Surface(Uint32 flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask) { Surface::Surface(Uint32 flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask) {
if ((surface_ = SDL_CreateRGBSurface(flags, width, height, depth, Rmask, Gmask, Bmask, Amask)) == nullptr) if ((surface_ = SDL_CreateRGBSurface(flags, width, height, depth, Rmask, Gmask, Bmask, Amask)) == nullptr)
throw Exception("SDL_CreateRGBSurface failed"); throw Exception("SDL_CreateRGBSurface");
} }
Surface::Surface(void* pixels, int width, int height, int depth, int pitch, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask) { Surface::Surface(void* pixels, int width, int height, int depth, int pitch, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask) {
if ((surface_ = SDL_CreateRGBSurfaceFrom(pixels, width, height, depth, pitch, Rmask, Gmask, Bmask, Amask)) == nullptr) if ((surface_ = SDL_CreateRGBSurfaceFrom(pixels, width, height, depth, pitch, Rmask, Gmask, Bmask, Amask)) == nullptr)
throw Exception("SDL_CreateRGBSurfaceFrom failed"); throw Exception("SDL_CreateRGBSurfaceFrom");
} }
#ifdef SDL2PP_WITH_IMAGE #ifdef SDL2PP_WITH_IMAGE
Surface::Surface(RWops& rwops) { Surface::Surface(RWops& rwops) {
if ((surface_ = IMG_Load_RW(rwops.Get(), 0)) == nullptr) if ((surface_ = IMG_Load_RW(rwops.Get(), 0)) == nullptr)
throw Exception("IMG_Load_RW failed"); throw Exception("IMG_Load_RW");
} }
Surface::Surface(const std::string& path) { Surface::Surface(const std::string& path) {
if ((surface_ = IMG_Load(path.c_str())) == nullptr) if ((surface_ = IMG_Load(path.c_str())) == nullptr)
throw Exception("IMG_Load failed"); throw Exception("IMG_Load");
} }
#endif #endif
@ -87,21 +87,21 @@ SDL_Surface* Surface::Get() const {
Surface Surface::Convert(const SDL_PixelFormat& format) { Surface Surface::Convert(const SDL_PixelFormat& format) {
SDL_Surface* surface = SDL_ConvertSurface(surface_, &format, 0); SDL_Surface* surface = SDL_ConvertSurface(surface_, &format, 0);
if (surface == nullptr) if (surface == nullptr)
throw Exception("SDL_ConvertPixels failed"); throw Exception("SDL_ConvertSurface");
return surface; return surface;
} }
Surface Surface::Convert(Uint32 pixel_format) { Surface Surface::Convert(Uint32 pixel_format) {
SDL_Surface* surface = SDL_ConvertSurfaceFormat(surface_, pixel_format, 0); SDL_Surface* surface = SDL_ConvertSurfaceFormat(surface_, pixel_format, 0);
if (surface == nullptr) if (surface == nullptr)
throw Exception("SDL_ConvertPixels failed"); throw Exception("SDL_ConvertSurfaceFormat");
return surface; return surface;
} }
void Surface::Blit(const Optional<Rect>& srcrect, Surface& dst, const Rect& dstrect) { void Surface::Blit(const Optional<Rect>& srcrect, Surface& dst, const Rect& dstrect) {
SDL_Rect tmpdstrect = dstrect; // 4th argument is non-const; does it modify rect? SDL_Rect tmpdstrect = dstrect; // 4th argument is non-const; does it modify rect?
if (SDL_BlitSurface(surface_, srcrect ? &*srcrect : nullptr, dst.Get(), &tmpdstrect) != 0) if (SDL_BlitSurface(surface_, srcrect ? &*srcrect : nullptr, dst.Get(), &tmpdstrect) != 0)
throw Exception("SDL_BlitSurface failed"); throw Exception("SDL_BlitSurface");
} }
void Surface::BlitScaled(const Optional<Rect>& srcrect, Surface& dst, const Optional<Rect>& dstrect) { void Surface::BlitScaled(const Optional<Rect>& srcrect, Surface& dst, const Optional<Rect>& dstrect) {
@ -109,7 +109,7 @@ void Surface::BlitScaled(const Optional<Rect>& srcrect, Surface& dst, const Opti
if (dstrect) if (dstrect)
tmpdstrect = *dstrect; tmpdstrect = *dstrect;
if (SDL_BlitScaled(surface_, srcrect ? &*srcrect : nullptr, dst.Get(), dstrect ? &tmpdstrect : nullptr) != 0) if (SDL_BlitScaled(surface_, srcrect ? &*srcrect : nullptr, dst.Get(), dstrect ? &tmpdstrect : nullptr) != 0)
throw Exception("SDL_BlitScaled failed"); throw Exception("SDL_BlitScaled");
} }
Surface::LockHandle Surface::Lock() { Surface::LockHandle Surface::Lock() {
@ -125,68 +125,68 @@ Rect Surface::GetClipRect() const {
Uint32 Surface::GetColorKey() const { Uint32 Surface::GetColorKey() const {
Uint32 key; Uint32 key;
if (SDL_GetColorKey(surface_, &key) != 0) if (SDL_GetColorKey(surface_, &key) != 0)
throw Exception("SDL_GetColorKey failed"); throw Exception("SDL_GetColorKey");
return key; return key;
} }
Uint8 Surface::GetAlphaMod() const { Uint8 Surface::GetAlphaMod() const {
Uint8 alpha; Uint8 alpha;
if (SDL_GetSurfaceAlphaMod(surface_, &alpha) != 0) if (SDL_GetSurfaceAlphaMod(surface_, &alpha) != 0)
throw Exception("SDL_GetSurfaceAlphaMod failed"); throw Exception("SDL_GetSurfaceAlphaMod");
return alpha; return alpha;
} }
SDL_BlendMode Surface::GetBlendMode() const { SDL_BlendMode Surface::GetBlendMode() const {
SDL_BlendMode blendMode; SDL_BlendMode blendMode;
if (SDL_GetSurfaceBlendMode(surface_, &blendMode) != 0) if (SDL_GetSurfaceBlendMode(surface_, &blendMode) != 0)
throw Exception("SDL_GetSurfaceBlendMode failed"); throw Exception("SDL_GetSurfaceBlendMode");
return blendMode; return blendMode;
} }
void Surface::GetColorMod(Uint8& r, Uint8& g, Uint8& b) const { void Surface::GetColorMod(Uint8& r, Uint8& g, Uint8& b) const {
if (SDL_GetSurfaceColorMod(surface_, &r, &g, &b) != 0) if (SDL_GetSurfaceColorMod(surface_, &r, &g, &b) != 0)
throw Exception("SDL_GetSurfaceColorMod failed"); throw Exception("SDL_GetSurfaceColorMod");
} }
Surface& Surface::SetClipRect(const Optional<Rect>& rect) { Surface& Surface::SetClipRect(const Optional<Rect>& rect) {
if (SDL_SetClipRect(surface_, rect ? &*rect : nullptr) != 0) if (SDL_SetClipRect(surface_, rect ? &*rect : nullptr) != 0)
throw Exception("SDL_SetClipRect failed"); throw Exception("SDL_SetClipRect");
return *this; return *this;
} }
Surface& Surface::SetColorKey(bool flag, Uint32 key) { Surface& Surface::SetColorKey(bool flag, Uint32 key) {
if (SDL_SetColorKey(surface_, flag, key) != 0) if (SDL_SetColorKey(surface_, flag, key) != 0)
throw Exception("SDL_SetColorKey failed"); throw Exception("SDL_SetColorKey");
return *this; return *this;
} }
Surface& Surface::SetAlphaMod(Uint8 alpha) { Surface& Surface::SetAlphaMod(Uint8 alpha) {
if (SDL_SetSurfaceAlphaMod(surface_, alpha) != 0) if (SDL_SetSurfaceAlphaMod(surface_, alpha) != 0)
throw Exception("SDL_SetSurfaceAlphaMod failed"); throw Exception("SDL_SetSurfaceAlphaMod");
return *this; return *this;
} }
Surface& Surface::SetBlendMode(SDL_BlendMode blendMode) { Surface& Surface::SetBlendMode(SDL_BlendMode blendMode) {
if (SDL_SetSurfaceBlendMode(surface_, blendMode) != 0) if (SDL_SetSurfaceBlendMode(surface_, blendMode) != 0)
throw Exception("SDL_SetSurfaceBlendMode failed"); throw Exception("SDL_SetSurfaceBlendMode");
return *this; return *this;
} }
Surface& Surface::SetColorMod(Uint8 r, Uint8 g, Uint8 b) { Surface& Surface::SetColorMod(Uint8 r, Uint8 g, Uint8 b) {
if (SDL_SetSurfaceColorMod(surface_, r, g, b) != 0) if (SDL_SetSurfaceColorMod(surface_, r, g, b) != 0)
throw Exception("SDL_SetSurfaceColorMod failed"); throw Exception("SDL_SetSurfaceColorMod");
return *this; return *this;
} }
Surface& Surface::SetRLE(bool flag) { Surface& Surface::SetRLE(bool flag) {
if (SDL_SetSurfaceRLE(surface_, flag ? 1 : 0) != 0) if (SDL_SetSurfaceRLE(surface_, flag ? 1 : 0) != 0)
throw Exception("SDL_SetSurfaceRLE failed"); throw Exception("SDL_SetSurfaceRLE");
return *this; return *this;
} }
Surface& Surface::FillRect(const Optional<Rect>& rect, Uint32 color) { Surface& Surface::FillRect(const Optional<Rect>& rect, Uint32 color) {
if (SDL_FillRect(surface_, rect ? &*rect : nullptr, color) != 0) if (SDL_FillRect(surface_, rect ? &*rect : nullptr, color) != 0)
throw Exception("SDL_FillRect failed"); throw Exception("SDL_FillRect");
return *this; return *this;
} }
@ -197,7 +197,7 @@ Surface& Surface::FillRects(const Rect* rects, int count, Uint32 color) {
sdl_rects.emplace_back(*r); sdl_rects.emplace_back(*r);
if (SDL_FillRects(surface_, sdl_rects.data(), sdl_rects.size(), color) != 0) if (SDL_FillRects(surface_, sdl_rects.data(), sdl_rects.size(), color) != 0)
throw Exception("SDL_FillRects failed"); throw Exception("SDL_FillRects");
return *this; return *this;
} }

View File

@ -1,6 +1,6 @@
/* /*
libSDL2pp - C++11 bindings/wrapper for SDL2 libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2014 Dmitry Marakasov <amdmi3@amdmi3.ru> Copyright (C) 2014-2015 Dmitry Marakasov <amdmi3@amdmi3.ru>
This software is provided 'as-is', without any express or implied This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages
@ -33,7 +33,7 @@ Surface::LockHandle::LockHandle() : surface_(nullptr) {
Surface::LockHandle::LockHandle(Surface* surface) : surface_(surface) { Surface::LockHandle::LockHandle(Surface* surface) : surface_(surface) {
if (SDL_MUSTLOCK(surface_->Get())) { if (SDL_MUSTLOCK(surface_->Get())) {
if (SDL_LockSurface(surface_->Get())) if (SDL_LockSurface(surface_->Get()))
throw Exception("SDL_LockSurface failed"); throw Exception("SDL_LockSurface");
} }
} }

View File

@ -45,24 +45,24 @@ Texture::Texture(SDL_Texture* texture) : texture_(texture) {
Texture::Texture(Renderer& renderer, Uint32 format, int access, int w, int h) { Texture::Texture(Renderer& renderer, Uint32 format, int access, int w, int h) {
if ((texture_ = SDL_CreateTexture(renderer.Get(), format, access, w, h)) == nullptr) if ((texture_ = SDL_CreateTexture(renderer.Get(), format, access, w, h)) == nullptr)
throw Exception("SDL_CreateTexture failed"); throw Exception("SDL_CreateTexture");
} }
#ifdef SDL2PP_WITH_IMAGE #ifdef SDL2PP_WITH_IMAGE
Texture::Texture(Renderer& renderer, RWops& rwops) { Texture::Texture(Renderer& renderer, RWops& rwops) {
if ((texture_ = IMG_LoadTexture_RW(renderer.Get(), rwops.Get(), 0)) == nullptr) if ((texture_ = IMG_LoadTexture_RW(renderer.Get(), rwops.Get(), 0)) == nullptr)
throw Exception("IMG_LoadTexture_RW failed"); throw Exception("IMG_LoadTexture_RW");
} }
Texture::Texture(Renderer& renderer, const std::string& path) { Texture::Texture(Renderer& renderer, const std::string& path) {
if ((texture_ = IMG_LoadTexture(renderer.Get(), path.c_str())) == nullptr) if ((texture_ = IMG_LoadTexture(renderer.Get(), path.c_str())) == nullptr)
throw Exception("IMG_LoadTexture failed"); throw Exception("IMG_LoadTexture");
} }
#endif #endif
Texture::Texture(Renderer& renderer, const Surface& surface) { Texture::Texture(Renderer& renderer, const Surface& surface) {
if ((texture_ = SDL_CreateTextureFromSurface(renderer.Get(), surface.Get())) == nullptr) if ((texture_ = SDL_CreateTextureFromSurface(renderer.Get(), surface.Get())) == nullptr)
throw Exception("SDL_CreateTextureFromSurface failed"); throw Exception("SDL_CreateTextureFromSurface");
} }
Texture::~Texture() { Texture::~Texture() {
@ -90,31 +90,31 @@ SDL_Texture* Texture::Get() const {
Texture& Texture::Update(const Optional<Rect>& rect, const void* pixels, int pitch) { Texture& Texture::Update(const Optional<Rect>& rect, const void* pixels, int pitch) {
if (SDL_UpdateTexture(texture_, rect ? &*rect : nullptr, pixels, pitch) != 0) if (SDL_UpdateTexture(texture_, rect ? &*rect : nullptr, pixels, pitch) != 0)
throw Exception("SDL_UpdateTexture failed"); throw Exception("SDL_UpdateTexture");
return *this; return *this;
} }
Texture& Texture::UpdateYUV(const Optional<Rect>& rect, const Uint8* yplane, int ypitch, const Uint8* uplane, int upitch, const Uint8* vplane, int vpitch) { Texture& Texture::UpdateYUV(const Optional<Rect>& rect, const Uint8* yplane, int ypitch, const Uint8* uplane, int upitch, const Uint8* vplane, int vpitch) {
if (SDL_UpdateYUVTexture(texture_, rect ? &*rect : nullptr, yplane, ypitch, uplane, upitch, vplane, vpitch) != 0) if (SDL_UpdateYUVTexture(texture_, rect ? &*rect : nullptr, yplane, ypitch, uplane, upitch, vplane, vpitch) != 0)
throw Exception("SDL_UpdateYUVTexture failed"); throw Exception("SDL_UpdateYUVTexture");
return *this; return *this;
} }
Texture& Texture::SetBlendMode(SDL_BlendMode blendMode) { Texture& Texture::SetBlendMode(SDL_BlendMode blendMode) {
if (SDL_SetTextureBlendMode(texture_, blendMode) != 0) if (SDL_SetTextureBlendMode(texture_, blendMode) != 0)
throw Exception("SDL_SetTextureBlendMode failed"); throw Exception("SDL_SetTextureBlendMode");
return *this; return *this;
} }
Texture& Texture::SetAlphaMod(Uint8 alpha) { Texture& Texture::SetAlphaMod(Uint8 alpha) {
if (SDL_SetTextureAlphaMod(texture_, alpha) != 0) if (SDL_SetTextureAlphaMod(texture_, alpha) != 0)
throw Exception("SDL_SetTextureAlphaMod failed"); throw Exception("SDL_SetTextureAlphaMod");
return *this; return *this;
} }
Texture& Texture::SetColorMod(Uint8 r, Uint8 g, Uint8 b) { Texture& Texture::SetColorMod(Uint8 r, Uint8 g, Uint8 b) {
if (SDL_SetTextureColorMod(texture_, r, g, b) != 0) if (SDL_SetTextureColorMod(texture_, r, g, b) != 0)
throw Exception("SDL_SetTextureColorMod failed"); throw Exception("SDL_SetTextureColorMod");
return *this; return *this;
} }
@ -125,55 +125,55 @@ Texture::LockHandle Texture::Lock(const Optional<Rect>& rect) {
Uint32 Texture::GetFormat() const { Uint32 Texture::GetFormat() const {
Uint32 format; Uint32 format;
if (SDL_QueryTexture(texture_, &format, nullptr, nullptr, nullptr) != 0) if (SDL_QueryTexture(texture_, &format, nullptr, nullptr, nullptr) != 0)
throw Exception("SDL_QueryTexture failed"); throw Exception("SDL_QueryTexture");
return format; return format;
} }
int Texture::GetAccess() const { int Texture::GetAccess() const {
int access; int access;
if (SDL_QueryTexture(texture_, nullptr, &access, nullptr, nullptr) != 0) if (SDL_QueryTexture(texture_, nullptr, &access, nullptr, nullptr) != 0)
throw Exception("SDL_QueryTexture failed"); throw Exception("SDL_QueryTexture");
return access; return access;
} }
int Texture::GetWidth() const { int Texture::GetWidth() const {
int w; int w;
if (SDL_QueryTexture(texture_, nullptr, nullptr, &w, nullptr) != 0) if (SDL_QueryTexture(texture_, nullptr, nullptr, &w, nullptr) != 0)
throw Exception("SDL_QueryTexture failed"); throw Exception("SDL_QueryTexture");
return w; return w;
} }
int Texture::GetHeight() const { int Texture::GetHeight() const {
int h; int h;
if (SDL_QueryTexture(texture_, nullptr, nullptr, nullptr, &h) != 0) if (SDL_QueryTexture(texture_, nullptr, nullptr, nullptr, &h) != 0)
throw Exception("SDL_QueryTexture failed"); throw Exception("SDL_QueryTexture");
return h; return h;
} }
Point Texture::GetSize() const { Point Texture::GetSize() const {
int w, h; int w, h;
if (SDL_QueryTexture(texture_, nullptr, nullptr, &w, &h) != 0) if (SDL_QueryTexture(texture_, nullptr, nullptr, &w, &h) != 0)
throw Exception("SDL_QueryTexture failed"); throw Exception("SDL_QueryTexture");
return Point(w, h); return Point(w, h);
} }
Uint8 Texture::GetAlphaMod() const { Uint8 Texture::GetAlphaMod() const {
Uint8 alpha; Uint8 alpha;
if (SDL_GetTextureAlphaMod(texture_, &alpha) != 0) if (SDL_GetTextureAlphaMod(texture_, &alpha) != 0)
throw Exception("SDL_GetTextureAlphaMod failed"); throw Exception("SDL_GetTextureAlphaMod");
return alpha; return alpha;
} }
SDL_BlendMode Texture::GetBlendMode() const { SDL_BlendMode Texture::GetBlendMode() const {
SDL_BlendMode mode; SDL_BlendMode mode;
if (SDL_GetTextureBlendMode(texture_, &mode) != 0) if (SDL_GetTextureBlendMode(texture_, &mode) != 0)
throw Exception("SDL_GetTextureBlendMode failed"); throw Exception("SDL_GetTextureBlendMode");
return mode; return mode;
} }
void Texture::GetColorMod(Uint8& r, Uint8& g, Uint8& b) const { void Texture::GetColorMod(Uint8& r, Uint8& g, Uint8& b) const {
if (SDL_GetTextureColorMod(texture_, &r, &g, &b) != 0) if (SDL_GetTextureColorMod(texture_, &r, &g, &b) != 0)
throw Exception("SDL_GetTextureBlendMode failed"); throw Exception("SDL_GetTextureColorMod");
} }
} }

View File

@ -1,6 +1,6 @@
/* /*
libSDL2pp - C++11 bindings/wrapper for SDL2 libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 Dmitry Marakasov <amdmi3@amdmi3.ru> Copyright (C) 2013-2015 Dmitry Marakasov <amdmi3@amdmi3.ru>
This software is provided 'as-is', without any express or implied This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages
@ -33,7 +33,7 @@ Texture::LockHandle::LockHandle() : texture_(nullptr), pixels_(nullptr), pitch_(
Texture::LockHandle::LockHandle(Texture* texture, const Optional<Rect>& rect) : texture_(texture) { Texture::LockHandle::LockHandle(Texture* texture, const Optional<Rect>& rect) : texture_(texture) {
if (SDL_LockTexture(texture_->Get(), rect ? &*rect : nullptr, &pixels_, &pitch_) != 0) if (SDL_LockTexture(texture_->Get(), rect ? &*rect : nullptr, &pixels_, &pitch_) != 0)
throw Exception("SDL_LockTexture failed"); throw Exception("SDL_LockTexture");
} }
Texture::LockHandle::LockHandle(Texture::LockHandle&& other) noexcept : texture_(other.texture_), pixels_(other.pixels_), pitch_(other.pitch_) { Texture::LockHandle::LockHandle(Texture::LockHandle&& other) noexcept : texture_(other.texture_), pixels_(other.pixels_), pitch_(other.pitch_) {

View File

@ -1,6 +1,6 @@
/* /*
libSDL2pp - C++11 bindings/wrapper for SDL2 libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2014 Dmitry Marakasov <amdmi3@amdmi3.ru> Copyright (C) 2014-2015 Dmitry Marakasov <amdmi3@amdmi3.ru>
This software is provided 'as-is', without any express or implied This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages
@ -28,12 +28,12 @@ namespace SDL2pp {
Wav::Wav(const std::string& file) { Wav::Wav(const std::string& file) {
if (SDL_LoadWAV(file.c_str(), &spec_, &audio_buffer_, &audio_length_) == nullptr) if (SDL_LoadWAV(file.c_str(), &spec_, &audio_buffer_, &audio_length_) == nullptr)
throw Exception("SDL_LoadWAV failed"); throw Exception("SDL_LoadWAV");
} }
Wav::Wav(RWops& rwops) { Wav::Wav(RWops& rwops) {
if (SDL_LoadWAV_RW(rwops.Get(), 0, &spec_, &audio_buffer_, &audio_length_) == nullptr) if (SDL_LoadWAV_RW(rwops.Get(), 0, &spec_, &audio_buffer_, &audio_length_) == nullptr)
throw Exception("SDL_LoadWAV failed"); throw Exception("SDL_LoadWAV_RW");
} }
Wav::~Wav() { Wav::~Wav() {

View File

@ -31,7 +31,7 @@ Window::Window(SDL_Window* window) : window_(window) {
Window::Window(const std::string& title, int x, int y, int w, int h, Uint32 flags) { Window::Window(const std::string& title, int x, int y, int w, int h, Uint32 flags) {
if ((window_ = SDL_CreateWindow(title.c_str(), x, y, w, h, flags)) == nullptr) if ((window_ = SDL_CreateWindow(title.c_str(), x, y, w, h, flags)) == nullptr)
throw Exception("SDL_CreateWindow failed"); throw Exception("SDL_CreateWindow");
} }
Window::~Window() { Window::~Window() {
@ -116,7 +116,7 @@ Window& Window::Show() {
Window& Window::SetFullscreen(int flags) { Window& Window::SetFullscreen(int flags) {
if (SDL_SetWindowFullscreen(window_, flags) != 0) if (SDL_SetWindowFullscreen(window_, flags) != 0)
throw Exception("SDL_SetWindowFullscreen failed"); throw Exception("SDL_SetWindowFullscreen");
return *this; return *this;
} }
@ -136,7 +136,7 @@ float Window::GetBrightness() const {
Window& Window::SetBrightness(float brightness) { Window& Window::SetBrightness(float brightness) {
if (SDL_SetWindowBrightness(window_, brightness) != 0) if (SDL_SetWindowBrightness(window_, brightness) != 0)
throw Exception("SDL_SetWindowBrightness failed"); throw Exception("SDL_SetWindowBrightness");
return *this; return *this;
} }

View File

@ -1,6 +1,6 @@
/* /*
libSDL2pp - C++11 bindings/wrapper for SDL2 libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 Dmitry Marakasov <amdmi3@amdmi3.ru> Copyright (C) 2013-2015 Dmitry Marakasov <amdmi3@amdmi3.ru>
This software is provided 'as-is', without any express or implied This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages
@ -26,7 +26,6 @@
#include <SDL2pp/SDL.hh> #include <SDL2pp/SDL.hh>
#include <SDL2pp/AudioDevice.hh> #include <SDL2pp/AudioDevice.hh>
#include <SDL2pp/AudioSpec.hh> #include <SDL2pp/AudioSpec.hh>
#include <SDL2pp/Exception.hh>
using namespace SDL2pp; using namespace SDL2pp;
@ -60,8 +59,6 @@ int Run() {
int main() { int main() {
try { try {
return Run(); return Run();
} catch (Exception& e) {
std::cerr << "Error: " << e.what() << " (" << e.GetSDLError() << ")" << std::endl;
} catch (std::exception& e) { } catch (std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl; std::cerr << "Error: " << e.what() << std::endl;
} }

View File

@ -1,6 +1,6 @@
/* /*
libSDL2pp - C++11 bindings/wrapper for SDL2 libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 Dmitry Marakasov <amdmi3@amdmi3.ru> Copyright (C) 2013-2015 Dmitry Marakasov <amdmi3@amdmi3.ru>
This software is provided 'as-is', without any express or implied This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages
@ -28,7 +28,6 @@
#include <SDL2pp/AudioDevice.hh> #include <SDL2pp/AudioDevice.hh>
#include <SDL2pp/AudioSpec.hh> #include <SDL2pp/AudioSpec.hh>
#include <SDL2pp/Wav.hh> #include <SDL2pp/Wav.hh>
#include <SDL2pp/Exception.hh>
using namespace SDL2pp; using namespace SDL2pp;
@ -70,8 +69,6 @@ int Run() {
int main() { int main() {
try { try {
return Run(); return Run();
} catch (Exception& e) {
std::cerr << "Error: " << e.what() << " (" << e.GetSDLError() << ")" << std::endl;
} catch (std::exception& e) { } catch (std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl; std::cerr << "Error: " << e.what() << std::endl;
} }

View File

@ -1,6 +1,6 @@
/* /*
libSDL2pp - C++11 bindings/wrapper for SDL2 libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 Dmitry Marakasov <amdmi3@amdmi3.ru> Copyright (C) 2013-2015 Dmitry Marakasov <amdmi3@amdmi3.ru>
This software is provided 'as-is', without any express or implied This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages
@ -30,7 +30,6 @@
#include <SDL2pp/Renderer.hh> #include <SDL2pp/Renderer.hh>
#include <SDL2pp/Texture.hh> #include <SDL2pp/Texture.hh>
#include <SDL2pp/Surface.hh> #include <SDL2pp/Surface.hh>
#include <SDL2pp/Exception.hh>
using namespace SDL2pp; using namespace SDL2pp;
@ -82,8 +81,6 @@ int Run() {
int main() { int main() {
try { try {
return Run(); return Run();
} catch (Exception& e) {
std::cerr << "Error: " << e.what() << " (" << e.GetSDLError() << ")" << std::endl;
} catch (std::exception& e) { } catch (std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl; std::cerr << "Error: " << e.what() << std::endl;
} }

View File

@ -1,6 +1,6 @@
/* /*
libSDL2pp - C++11 bindings/wrapper for SDL2 libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 Dmitry Marakasov <amdmi3@amdmi3.ru> Copyright (C) 2013-2015 Dmitry Marakasov <amdmi3@amdmi3.ru>
This software is provided 'as-is', without any express or implied This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages
@ -26,7 +26,6 @@
#include <SDL2pp/SDL.hh> #include <SDL2pp/SDL.hh>
#include <SDL2pp/Window.hh> #include <SDL2pp/Window.hh>
#include <SDL2pp/Renderer.hh> #include <SDL2pp/Renderer.hh>
#include <SDL2pp/Exception.hh>
using namespace SDL2pp; using namespace SDL2pp;
@ -94,8 +93,6 @@ int Run() {
int main() { int main() {
try { try {
return Run(); return Run();
} catch (Exception& e) {
std::cerr << "Error: " << e.what() << " (" << e.GetSDLError() << ")" << std::endl;
} catch (std::exception& e) { } catch (std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl; std::cerr << "Error: " << e.what() << std::endl;
} }

View File

@ -27,7 +27,6 @@
#include <SDL2pp/Window.hh> #include <SDL2pp/Window.hh>
#include <SDL2pp/Renderer.hh> #include <SDL2pp/Renderer.hh>
#include <SDL2pp/Texture.hh> #include <SDL2pp/Texture.hh>
#include <SDL2pp/Exception.hh>
using namespace SDL2pp; using namespace SDL2pp;
@ -120,8 +119,6 @@ int Run() {
int main() { int main() {
try { try {
return Run(); return Run();
} catch (Exception& e) {
std::cerr << "Error: " << e.what() << " (" << e.GetSDLError() << ")" << std::endl;
} catch (std::exception& e) { } catch (std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl; std::cerr << "Error: " << e.what() << std::endl;
} }

View File

@ -1,6 +1,6 @@
/* /*
libSDL2pp - C++11 bindings/wrapper for SDL2 libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 Dmitry Marakasov <amdmi3@amdmi3.ru> Copyright (C) 2013-2015 Dmitry Marakasov <amdmi3@amdmi3.ru>
This software is provided 'as-is', without any express or implied This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages
@ -27,7 +27,6 @@
#include <SDL2pp/Window.hh> #include <SDL2pp/Window.hh>
#include <SDL2pp/Renderer.hh> #include <SDL2pp/Renderer.hh>
#include <SDL2pp/Texture.hh> #include <SDL2pp/Texture.hh>
#include <SDL2pp/Exception.hh>
using namespace SDL2pp; using namespace SDL2pp;
@ -93,8 +92,6 @@ int Run() {
int main() { int main() {
try { try {
return Run(); return Run();
} catch (Exception& e) {
std::cerr << "Error: " << e.what() << " (" << e.GetSDLError() << ")" << std::endl;
} catch (std::exception& e) { } catch (std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl; std::cerr << "Error: " << e.what() << std::endl;
} }

View File

@ -1,6 +1,6 @@
/* /*
libSDL2pp - C++11 bindings/wrapper for SDL2 libSDL2pp - C++11 bindings/wrapper for SDL2
Copyright (C) 2013-2014 Dmitry Marakasov <amdmi3@amdmi3.ru> Copyright (C) 2013-2015 Dmitry Marakasov <amdmi3@amdmi3.ru>
This software is provided 'as-is', without any express or implied This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages
@ -21,15 +21,12 @@
#include <iostream> #include <iostream>
//#include <SDL2/SDL.h>
#include <SDL2pp/SDL.hh> #include <SDL2pp/SDL.hh>
#include <SDL2pp/SDLTTF.hh> #include <SDL2pp/SDLTTF.hh>
#include <SDL2pp/Font.hh> #include <SDL2pp/Font.hh>
#include <SDL2pp/Window.hh> #include <SDL2pp/Window.hh>
#include <SDL2pp/Renderer.hh> #include <SDL2pp/Renderer.hh>
#include <SDL2pp/Texture.hh> #include <SDL2pp/Texture.hh>
#include <SDL2pp/Exception.hh>
using namespace SDL2pp; using namespace SDL2pp;
@ -89,8 +86,6 @@ int Run() {
int main() { int main() {
try { try {
return Run(); return Run();
} catch (Exception& e) {
std::cerr << "Error: " << e.what() << " (" << e.GetSDLError() << ")" << std::endl;
} catch (std::exception& e) { } catch (std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl; std::cerr << "Error: " << e.what() << std::endl;
} }