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
} catch (SDL2pp::Exception& e) {
// Exception stores SDL_GetError() result
std::cerr << "Exception: " << e.what() << std::endl;
std::cerr << "SDL Error: " << e.GetSDLError() << std::endl;
// Exception stores SDL_GetError() result and name of function which failed
std::cerr << "Error in: " << e.GetSDLFunction() << 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 ##

View File

@ -40,7 +40,7 @@ AudioDevice::AudioDevice(const Optional<std::string>& device, bool iscapture, co
SDL_AudioSpec obtained;
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);
}
@ -54,7 +54,7 @@ AudioDevice::AudioDevice(const Optional<std::string>& device, bool iscapture, Au
SDL_AudioSpec obtained;
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);
@ -113,7 +113,7 @@ AudioDevice::LockHandle AudioDevice::Lock() {
#ifdef SDL2PP_WITH_2_0_4
AudioDevice& AudioDevice::QueueAudio(const void* data, Uint32 len) {
if (SDL_QueueAudio(device_id_, data, len) == 0)
throw Exception("SDL_QueueAudio failed");
throw Exception("SDL_QueueAudio");
return *this;
}

View File

@ -1,6 +1,6 @@
/*
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
warranty. In no event will the authors be held liable for any damages
@ -25,18 +25,25 @@
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 {
}
const char* Exception::what() const noexcept {
return what_;
return what_.c_str();
}
const char* Exception::GetSDLError() const noexcept {
return sdl_error_.c_str();
std::string Exception::GetSDLFunction() const {
return sdl_function_;
}
std::string Exception::GetSDLError() const {
return sdl_error_;
}
} // namespace SDL2pp

View File

@ -68,17 +68,18 @@ namespace SDL2pp {
////////////////////////////////////////////////////////////
class Exception : public std::exception {
private:
const char* what_; ///< User-specified message
std::string sdl_error_; ///< SDL error string
std::string sdl_function_; ///< SDL function which caused an error
std::string sdl_error_; ///< SDL error string
std::string what_; ///< User-readable message
public:
////////////////////////////////////////////////////////////
/// \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
@ -94,6 +95,14 @@ public:
////////////////////////////////////////////////////////////
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
///
@ -102,7 +111,7 @@ public:
/// \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) {
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) {
if ((font_ = TTF_OpenFontIndexRW(rwops.Get(), 0, ptsize, index)) == nullptr)
throw Exception("TTF_OpenFontIndexRW failed");
throw Exception("TTF_OpenFontIndexRW");
}
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 {
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 {
int minx, maxx, miny, maxy;
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);
}
int Font::GetGlyphAdvance(Uint16 ch) const {
int advance;
if (TTF_GlyphMetrics(font_, ch, nullptr, nullptr, nullptr, nullptr, &advance) != 0)
throw Exception("TTF_GlyphMetrics failed");
throw Exception("TTF_GlyphMetrics");
return advance;
}
Point Font::GetSizeText(const std::string& text) const {
int w, h;
if (TTF_SizeText(font_, text.c_str(), &w, &h) != 0)
throw Exception("TTF_SizeText failed");
throw Exception("TTF_SizeText");
return Point(w, h);
}
Point Font::GetSizeUTF8(const std::string& text) const {
int w, h;
if (TTF_SizeUTF8(font_, text.c_str(), &w, &h) != 0)
throw Exception("TTF_SizeUTF8 failed");
throw Exception("TTF_SizeUTF8");
return Point(w, h);
}
Point Font::GetSizeUNICODE(const Uint16* text) const {
int w, h;
if (TTF_SizeUNICODE(font_, text, &w, &h) != 0)
throw Exception("TTF_SizeUNICODE failed");
throw Exception("TTF_SizeUNICODE");
return Point(w, h);
}
Surface Font::RenderText_Solid(const std::string& text, SDL_Color fg) {
SDL_Surface* surface = TTF_RenderText_Solid(font_, text.c_str(), fg);
if (surface == nullptr)
throw Exception("TTF_RenderText_Solid failed");
throw Exception("TTF_RenderText_Solid");
return Surface(surface);
}
Surface Font::RenderUTF8_Solid(const std::string& text, SDL_Color fg) {
SDL_Surface* surface = TTF_RenderUTF8_Solid(font_, text.c_str(), fg);
if (surface == nullptr)
throw Exception("TTF_RenderUTF8_Solid failed");
throw Exception("TTF_RenderUTF8_Solid");
return Surface(surface);
}
Surface Font::RenderUNICODE_Solid(const Uint16* text, SDL_Color fg) {
SDL_Surface* surface = TTF_RenderUNICODE_Solid(font_, text, fg);
if (surface == nullptr)
throw Exception("TTF_RenderUNICODE_Solid failed");
throw Exception("TTF_RenderUNICODE_Solid");
return Surface(surface);
}
Surface Font::RenderGlyph_Solid(Uint16 ch, SDL_Color fg) {
SDL_Surface* surface = TTF_RenderGlyph_Solid(font_, ch, fg);
if (surface == nullptr)
throw Exception("TTF_RenderGlyph_Solid failed");
throw Exception("TTF_RenderGlyph_Solid");
return Surface(surface);
}
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);
if (surface == nullptr)
throw Exception("TTF_RenderText_Shaded failed");
throw Exception("TTF_RenderText_Shaded");
return Surface(surface);
}
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);
if (surface == nullptr)
throw Exception("TTF_RenderUTF8_Shaded failed");
throw Exception("TTF_RenderUTF8_Shaded");
return Surface(surface);
}
Surface Font::RenderUNICODE_Shaded(const Uint16* text, SDL_Color fg, SDL_Color bg) {
SDL_Surface* surface = TTF_RenderUNICODE_Shaded(font_, text, fg, bg);
if (surface == nullptr)
throw Exception("TTF_RenderUNICODE_Shaded failed");
throw Exception("TTF_RenderUNICODE_Shaded");
return Surface(surface);
}
Surface Font::RenderGlyph_Shaded(Uint16 ch, SDL_Color fg, SDL_Color bg) {
SDL_Surface* surface = TTF_RenderGlyph_Shaded(font_, ch, fg, bg);
if (surface == nullptr)
throw Exception("TTF_RenderGlyph_Shaded failed");
throw Exception("TTF_RenderGlyph_Shaded");
return Surface(surface);
}
Surface Font::RenderText_Blended(const std::string& text, SDL_Color fg) {
SDL_Surface* surface = TTF_RenderText_Blended(font_, text.c_str(), fg);
if (surface == nullptr)
throw Exception("TTF_RenderText_Blended failed");
throw Exception("TTF_RenderText_Blended");
return Surface(surface);
}
Surface Font::RenderUTF8_Blended(const std::string& text, SDL_Color fg) {
SDL_Surface* surface = TTF_RenderUTF8_Blended(font_, text.c_str(), fg);
if (surface == nullptr)
throw Exception("TTF_RenderUTF8_Blended failed");
throw Exception("TTF_RenderUTF8_Blended");
return Surface(surface);
}
Surface Font::RenderUNICODE_Blended(const Uint16* text, SDL_Color fg) {
SDL_Surface* surface = TTF_RenderUNICODE_Blended(font_, text, fg);
if (surface == nullptr)
throw Exception("TTF_RenderUNICODE_Blended failed");
throw Exception("TTF_RenderUNICODE_Blended");
return Surface(surface);
}
Surface Font::RenderGlyph_Blended(Uint16 ch, SDL_Color fg) {
SDL_Surface* surface = TTF_RenderGlyph_Blended(font_, ch, fg);
if (surface == nullptr)
throw Exception("TTF_RenderGlyph_Blended failed");
throw Exception("TTF_RenderGlyph_Blended");
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) {
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) {
return CheckedCreateStandardRWops(SDL_RWFromConstMem(mem, size), "SDL_RWFromConstMem failed");
return CheckedCreateStandardRWops(SDL_RWFromConstMem(mem, size), "SDL_RWFromConstMem");
}
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) {
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_ = SDL_AllocRW();
if (rwops_ == nullptr)
throw Exception("SDL_AllocRW failed");
throw Exception("SDL_AllocRW");
rwops_->seek = StdSeekFuncWrapper;
rwops_->read = StdReadFuncWrapper;

View File

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

View File

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

View File

@ -1,6 +1,6 @@
/*
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
warranty. In no event will the authors be held liable for any damages
@ -28,7 +28,7 @@ namespace SDL2pp {
SDL::SDL(Uint32 flags) {
if (SDL_Init(flags) != 0)
throw Exception("SDL_Init failed");
throw Exception("SDL_Init");
}
SDL::~SDL() {
@ -41,7 +41,7 @@ Uint32 WasInit(Uint32 flags) {
void InitSubsystem(Uint32 flags) {
if (SDL_InitSubSystem(flags) != 0)
throw Exception("SDL_InitSubsystem failed");
throw Exception("SDL_InitSubsystem");
}
void QuitSubSystem(Uint32 flags) {

View File

@ -1,6 +1,6 @@
/*
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
warranty. In no event will the authors be held liable for any damages
@ -28,7 +28,7 @@ namespace SDL2pp {
SDLImage::SDLImage(int flags) {
if ((IMG_Init(flags) & flags) != flags)
throw Exception("IMG_Init failed");
throw Exception("IMG_Init");
}
SDLImage::~SDLImage() {
@ -38,7 +38,7 @@ SDLImage::~SDLImage() {
int SDLImage::InitMore(int flags) {
int ret;
if (((ret = IMG_Init(flags)) & flags) != flags)
throw Exception("IMG_Init failed");
throw Exception("IMG_Init");
return ret;
}

View File

@ -1,6 +1,6 @@
/*
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
warranty. In no event will the authors be held liable for any damages
@ -28,7 +28,7 @@ namespace SDL2pp {
SDLTTF::SDLTTF() {
if (TTF_Init() != 0)
throw Exception("TTF_Init failed");
throw Exception("TTF_Init");
}
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) {
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) {
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
Surface::Surface(RWops& rwops) {
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) {
if ((surface_ = IMG_Load(path.c_str())) == nullptr)
throw Exception("IMG_Load failed");
throw Exception("IMG_Load");
}
#endif
@ -87,21 +87,21 @@ SDL_Surface* Surface::Get() const {
Surface Surface::Convert(const SDL_PixelFormat& format) {
SDL_Surface* surface = SDL_ConvertSurface(surface_, &format, 0);
if (surface == nullptr)
throw Exception("SDL_ConvertPixels failed");
throw Exception("SDL_ConvertSurface");
return surface;
}
Surface Surface::Convert(Uint32 pixel_format) {
SDL_Surface* surface = SDL_ConvertSurfaceFormat(surface_, pixel_format, 0);
if (surface == nullptr)
throw Exception("SDL_ConvertPixels failed");
throw Exception("SDL_ConvertSurfaceFormat");
return surface;
}
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?
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) {
@ -109,7 +109,7 @@ void Surface::BlitScaled(const Optional<Rect>& srcrect, Surface& dst, const Opti
if (dstrect)
tmpdstrect = *dstrect;
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() {
@ -125,68 +125,68 @@ Rect Surface::GetClipRect() const {
Uint32 Surface::GetColorKey() const {
Uint32 key;
if (SDL_GetColorKey(surface_, &key) != 0)
throw Exception("SDL_GetColorKey failed");
throw Exception("SDL_GetColorKey");
return key;
}
Uint8 Surface::GetAlphaMod() const {
Uint8 alpha;
if (SDL_GetSurfaceAlphaMod(surface_, &alpha) != 0)
throw Exception("SDL_GetSurfaceAlphaMod failed");
throw Exception("SDL_GetSurfaceAlphaMod");
return alpha;
}
SDL_BlendMode Surface::GetBlendMode() const {
SDL_BlendMode blendMode;
if (SDL_GetSurfaceBlendMode(surface_, &blendMode) != 0)
throw Exception("SDL_GetSurfaceBlendMode failed");
throw Exception("SDL_GetSurfaceBlendMode");
return blendMode;
}
void Surface::GetColorMod(Uint8& r, Uint8& g, Uint8& b) const {
if (SDL_GetSurfaceColorMod(surface_, &r, &g, &b) != 0)
throw Exception("SDL_GetSurfaceColorMod failed");
throw Exception("SDL_GetSurfaceColorMod");
}
Surface& Surface::SetClipRect(const Optional<Rect>& rect) {
if (SDL_SetClipRect(surface_, rect ? &*rect : nullptr) != 0)
throw Exception("SDL_SetClipRect failed");
throw Exception("SDL_SetClipRect");
return *this;
}
Surface& Surface::SetColorKey(bool flag, Uint32 key) {
if (SDL_SetColorKey(surface_, flag, key) != 0)
throw Exception("SDL_SetColorKey failed");
throw Exception("SDL_SetColorKey");
return *this;
}
Surface& Surface::SetAlphaMod(Uint8 alpha) {
if (SDL_SetSurfaceAlphaMod(surface_, alpha) != 0)
throw Exception("SDL_SetSurfaceAlphaMod failed");
throw Exception("SDL_SetSurfaceAlphaMod");
return *this;
}
Surface& Surface::SetBlendMode(SDL_BlendMode blendMode) {
if (SDL_SetSurfaceBlendMode(surface_, blendMode) != 0)
throw Exception("SDL_SetSurfaceBlendMode failed");
throw Exception("SDL_SetSurfaceBlendMode");
return *this;
}
Surface& Surface::SetColorMod(Uint8 r, Uint8 g, Uint8 b) {
if (SDL_SetSurfaceColorMod(surface_, r, g, b) != 0)
throw Exception("SDL_SetSurfaceColorMod failed");
throw Exception("SDL_SetSurfaceColorMod");
return *this;
}
Surface& Surface::SetRLE(bool flag) {
if (SDL_SetSurfaceRLE(surface_, flag ? 1 : 0) != 0)
throw Exception("SDL_SetSurfaceRLE failed");
throw Exception("SDL_SetSurfaceRLE");
return *this;
}
Surface& Surface::FillRect(const Optional<Rect>& rect, Uint32 color) {
if (SDL_FillRect(surface_, rect ? &*rect : nullptr, color) != 0)
throw Exception("SDL_FillRect failed");
throw Exception("SDL_FillRect");
return *this;
}
@ -197,7 +197,7 @@ Surface& Surface::FillRects(const Rect* rects, int count, Uint32 color) {
sdl_rects.emplace_back(*r);
if (SDL_FillRects(surface_, sdl_rects.data(), sdl_rects.size(), color) != 0)
throw Exception("SDL_FillRects failed");
throw Exception("SDL_FillRects");
return *this;
}

View File

@ -1,6 +1,6 @@
/*
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
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) {
if (SDL_MUSTLOCK(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) {
if ((texture_ = SDL_CreateTexture(renderer.Get(), format, access, w, h)) == nullptr)
throw Exception("SDL_CreateTexture failed");
throw Exception("SDL_CreateTexture");
}
#ifdef SDL2PP_WITH_IMAGE
Texture::Texture(Renderer& renderer, RWops& rwops) {
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) {
if ((texture_ = IMG_LoadTexture(renderer.Get(), path.c_str())) == nullptr)
throw Exception("IMG_LoadTexture failed");
throw Exception("IMG_LoadTexture");
}
#endif
Texture::Texture(Renderer& renderer, const Surface& surface) {
if ((texture_ = SDL_CreateTextureFromSurface(renderer.Get(), surface.Get())) == nullptr)
throw Exception("SDL_CreateTextureFromSurface failed");
throw Exception("SDL_CreateTextureFromSurface");
}
Texture::~Texture() {
@ -90,31 +90,31 @@ SDL_Texture* Texture::Get() const {
Texture& Texture::Update(const Optional<Rect>& rect, const void* pixels, int pitch) {
if (SDL_UpdateTexture(texture_, rect ? &*rect : nullptr, pixels, pitch) != 0)
throw Exception("SDL_UpdateTexture failed");
throw Exception("SDL_UpdateTexture");
return *this;
}
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)
throw Exception("SDL_UpdateYUVTexture failed");
throw Exception("SDL_UpdateYUVTexture");
return *this;
}
Texture& Texture::SetBlendMode(SDL_BlendMode blendMode) {
if (SDL_SetTextureBlendMode(texture_, blendMode) != 0)
throw Exception("SDL_SetTextureBlendMode failed");
throw Exception("SDL_SetTextureBlendMode");
return *this;
}
Texture& Texture::SetAlphaMod(Uint8 alpha) {
if (SDL_SetTextureAlphaMod(texture_, alpha) != 0)
throw Exception("SDL_SetTextureAlphaMod failed");
throw Exception("SDL_SetTextureAlphaMod");
return *this;
}
Texture& Texture::SetColorMod(Uint8 r, Uint8 g, Uint8 b) {
if (SDL_SetTextureColorMod(texture_, r, g, b) != 0)
throw Exception("SDL_SetTextureColorMod failed");
throw Exception("SDL_SetTextureColorMod");
return *this;
}
@ -125,55 +125,55 @@ Texture::LockHandle Texture::Lock(const Optional<Rect>& rect) {
Uint32 Texture::GetFormat() const {
Uint32 format;
if (SDL_QueryTexture(texture_, &format, nullptr, nullptr, nullptr) != 0)
throw Exception("SDL_QueryTexture failed");
throw Exception("SDL_QueryTexture");
return format;
}
int Texture::GetAccess() const {
int access;
if (SDL_QueryTexture(texture_, nullptr, &access, nullptr, nullptr) != 0)
throw Exception("SDL_QueryTexture failed");
throw Exception("SDL_QueryTexture");
return access;
}
int Texture::GetWidth() const {
int w;
if (SDL_QueryTexture(texture_, nullptr, nullptr, &w, nullptr) != 0)
throw Exception("SDL_QueryTexture failed");
throw Exception("SDL_QueryTexture");
return w;
}
int Texture::GetHeight() const {
int h;
if (SDL_QueryTexture(texture_, nullptr, nullptr, nullptr, &h) != 0)
throw Exception("SDL_QueryTexture failed");
throw Exception("SDL_QueryTexture");
return h;
}
Point Texture::GetSize() const {
int w, h;
if (SDL_QueryTexture(texture_, nullptr, nullptr, &w, &h) != 0)
throw Exception("SDL_QueryTexture failed");
throw Exception("SDL_QueryTexture");
return Point(w, h);
}
Uint8 Texture::GetAlphaMod() const {
Uint8 alpha;
if (SDL_GetTextureAlphaMod(texture_, &alpha) != 0)
throw Exception("SDL_GetTextureAlphaMod failed");
throw Exception("SDL_GetTextureAlphaMod");
return alpha;
}
SDL_BlendMode Texture::GetBlendMode() const {
SDL_BlendMode mode;
if (SDL_GetTextureBlendMode(texture_, &mode) != 0)
throw Exception("SDL_GetTextureBlendMode failed");
throw Exception("SDL_GetTextureBlendMode");
return mode;
}
void Texture::GetColorMod(Uint8& r, Uint8& g, Uint8& b) const {
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
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
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) {
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_) {

View File

@ -1,6 +1,6 @@
/*
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
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) {
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) {
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() {

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

View File

@ -1,6 +1,6 @@
/*
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
warranty. In no event will the authors be held liable for any damages
@ -26,7 +26,6 @@
#include <SDL2pp/SDL.hh>
#include <SDL2pp/AudioDevice.hh>
#include <SDL2pp/AudioSpec.hh>
#include <SDL2pp/Exception.hh>
using namespace SDL2pp;
@ -60,8 +59,6 @@ int Run() {
int main() {
try {
return Run();
} catch (Exception& e) {
std::cerr << "Error: " << e.what() << " (" << e.GetSDLError() << ")" << std::endl;
} catch (std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}

View File

@ -1,6 +1,6 @@
/*
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
warranty. In no event will the authors be held liable for any damages
@ -28,7 +28,6 @@
#include <SDL2pp/AudioDevice.hh>
#include <SDL2pp/AudioSpec.hh>
#include <SDL2pp/Wav.hh>
#include <SDL2pp/Exception.hh>
using namespace SDL2pp;
@ -70,8 +69,6 @@ int Run() {
int main() {
try {
return Run();
} catch (Exception& e) {
std::cerr << "Error: " << e.what() << " (" << e.GetSDLError() << ")" << std::endl;
} catch (std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}

View File

@ -1,6 +1,6 @@
/*
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
warranty. In no event will the authors be held liable for any damages
@ -30,7 +30,6 @@
#include <SDL2pp/Renderer.hh>
#include <SDL2pp/Texture.hh>
#include <SDL2pp/Surface.hh>
#include <SDL2pp/Exception.hh>
using namespace SDL2pp;
@ -82,8 +81,6 @@ int Run() {
int main() {
try {
return Run();
} catch (Exception& e) {
std::cerr << "Error: " << e.what() << " (" << e.GetSDLError() << ")" << std::endl;
} catch (std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}

View File

@ -1,6 +1,6 @@
/*
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
warranty. In no event will the authors be held liable for any damages
@ -26,7 +26,6 @@
#include <SDL2pp/SDL.hh>
#include <SDL2pp/Window.hh>
#include <SDL2pp/Renderer.hh>
#include <SDL2pp/Exception.hh>
using namespace SDL2pp;
@ -94,8 +93,6 @@ int Run() {
int main() {
try {
return Run();
} catch (Exception& e) {
std::cerr << "Error: " << e.what() << " (" << e.GetSDLError() << ")" << std::endl;
} catch (std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}

View File

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

View File

@ -1,6 +1,6 @@
/*
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
warranty. In no event will the authors be held liable for any damages
@ -27,7 +27,6 @@
#include <SDL2pp/Window.hh>
#include <SDL2pp/Renderer.hh>
#include <SDL2pp/Texture.hh>
#include <SDL2pp/Exception.hh>
using namespace SDL2pp;
@ -93,8 +92,6 @@ int Run() {
int main() {
try {
return Run();
} catch (Exception& e) {
std::cerr << "Error: " << e.what() << " (" << e.GetSDLError() << ")" << std::endl;
} catch (std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}

View File

@ -1,6 +1,6 @@
/*
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
warranty. In no event will the authors be held liable for any damages
@ -21,15 +21,12 @@
#include <iostream>
//#include <SDL2/SDL.h>
#include <SDL2pp/SDL.hh>
#include <SDL2pp/SDLTTF.hh>
#include <SDL2pp/Font.hh>
#include <SDL2pp/Window.hh>
#include <SDL2pp/Renderer.hh>
#include <SDL2pp/Texture.hh>
#include <SDL2pp/Exception.hh>
using namespace SDL2pp;
@ -89,8 +86,6 @@ int Run() {
int main() {
try {
return Run();
} catch (Exception& e) {
std::cerr << "Error: " << e.what() << " (" << e.GetSDLError() << ")" << std::endl;
} catch (std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}