From f6b52f338cce591efd57b22c2a77c49793697171 Mon Sep 17 00:00:00 2001 From: Mark de Wever Date: Sun, 23 Mar 2014 13:09:08 +0100 Subject: [PATCH 1/3] Add the SDL exception class. --- src/CMakeLists.txt | 1 + src/SConscript | 1 + src/sdl/exception.cpp | 37 ++++++++++++++++++++++++++++++++++++ src/sdl/exception.hpp | 44 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+) create mode 100644 src/sdl/exception.cpp create mode 100644 src/sdl/exception.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 86e41f94849b..27e8fe0ec3b8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -358,6 +358,7 @@ endif() set(wesnoth-sdl_SRC sdl/alpha.cpp + sdl/exception.cpp sdl/texture.cpp sdl/window.cpp ) diff --git a/src/SConscript b/src/SConscript index 75354b636dfb..eefe0591bd03 100644 --- a/src/SConscript +++ b/src/SConscript @@ -150,6 +150,7 @@ libcampaignd = env.Library("campaignd", libcampaignd_sources, OBJPREFIX = "campa libwesnoth_sdl_sources = Split(""" sdl_utils.cpp sdl/alpha.cpp + sdl/exception.cpp sdl/texture.cpp sdl/window.cpp tracer.cpp diff --git a/src/sdl/exception.cpp b/src/sdl/exception.cpp new file mode 100644 index 000000000000..4120ae99ac70 --- /dev/null +++ b/src/sdl/exception.cpp @@ -0,0 +1,37 @@ +/* + Copyright (C) 2014 by Mark de Wever + Part of the Battle for Wesnoth Project http://www.wesnoth.org/ + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY. + + See the COPYING file for more details. +*/ + +#include "sdl/exception.hpp" + +#include + +namespace sdl +{ + +static std::string create_error(const std::string& operation, + const bool use_sdl_error) +{ + if(use_sdl_error) { + return operation + " Error »" + SDL_GetError() + "«.\n"; + } else { + return operation; + } +} + +texception::texception(const std::string& operation, const bool use_sdl_error) + : game::error(create_error(operation, use_sdl_error)) +{ +} + +} // namespace sdl diff --git a/src/sdl/exception.hpp b/src/sdl/exception.hpp new file mode 100644 index 000000000000..96fa622f7bbc --- /dev/null +++ b/src/sdl/exception.hpp @@ -0,0 +1,44 @@ +/* + Copyright (C) 2014 by Mark de Wever + Part of the Battle for Wesnoth Project http://www.wesnoth.org/ + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY. + + See the COPYING file for more details. +*/ + +#ifndef SDL_EXCEPTION_HPP_INCLUDED +#define SDL_EXCEPTION_HPP_INCLUDED + +/** + * @file + * Contains a basic exception class for SDL operations. + */ + +#include "exceptions.hpp" + +namespace sdl +{ + +struct texception : public game::error +{ + /** + * Constructor. + * + * @param operation The operation that has failed. + * @param use_sdl_error If set to @c true the @p operation is + * appended with the SDL error message. Else the + * @p operation is the error message for the + * exception. + */ + texception(const std::string& operation, const bool use_sdl_error); +}; + +} // namespace sdl + +#endif From 982a84447043c4e544977e37d8f8e4356579b899 Mon Sep 17 00:00:00 2001 From: Mark de Wever Date: Sun, 23 Mar 2014 13:17:29 +0100 Subject: [PATCH 2/3] Catch the new SDL exception in the main game. --- src/game.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/game.cpp b/src/game.cpp index ccf35d9c33dd..b55e9c78938a 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -30,6 +30,7 @@ #include "playcampaign.hpp" #include "preferences_display.hpp" #include "replay.hpp" +#include "sdl/exception.hpp" #include "serialization/binary_or_text.hpp" #include "serialization/parser.hpp" #include "serialization/validator.hpp" @@ -721,6 +722,9 @@ int main(int argc, char** argv) std::cerr << e.what() << "\n\nGame will be aborted.\n"; return 1; + } catch(const sdl::texception& e) { + std::cerr << e.what(); + return 1; } catch(game::error &) { // A message has already been displayed. return 1; From f1793ab3e8313bbb4cea0c54e051d6a8d2fd58ff Mon Sep 17 00:00:00 2001 From: Mark de Wever Date: Sun, 23 Mar 2014 13:20:07 +0100 Subject: [PATCH 3/3] Use the new SDL exception in the SDL code. Also: * Fix a copy paste error in an error message. * Make creation failures of a texture object throwing an exception. --- src/sdl/texture.cpp | 8 ++------ src/sdl/window.cpp | 27 +++++++-------------------- 2 files changed, 9 insertions(+), 26 deletions(-) diff --git a/src/sdl/texture.cpp b/src/sdl/texture.cpp index eab50c32aa59..f17a4258d137 100644 --- a/src/sdl/texture.cpp +++ b/src/sdl/texture.cpp @@ -16,13 +16,10 @@ #if SDL_VERSION_ATLEAST(2, 0, 0) -#include "log.hpp" +#include "sdl/exception.hpp" #include -static lg::log_domain log_display("display"); -#define ERR_DP LOG_STREAM(err, log_display) - namespace sdl { @@ -35,8 +32,7 @@ ttexture::ttexture(SDL_Renderer& renderer, , texture_(SDL_CreateTexture(&renderer, format, access, w, h)) { if(!texture_) { - ERR_DP << "Failed to create a SDL_Texture object with error »" - << SDL_GetError() << "«.\n"; + throw texception("Failed to create a SDL_Texture object.", true); } } diff --git a/src/sdl/window.cpp b/src/sdl/window.cpp index 836901b581c1..7e6d97bd5f84 100644 --- a/src/sdl/window.cpp +++ b/src/sdl/window.cpp @@ -16,15 +16,11 @@ #if SDL_VERSION_ATLEAST(2, 0, 0) -#include "exceptions.hpp" -#include "log.hpp" +#include "sdl/exception.hpp" #include "sdl/texture.hpp" #include -static lg::log_domain log_display("display"); -#define ERR_DP LOG_STREAM(err, log_display) - namespace sdl { @@ -39,31 +35,22 @@ twindow::twindow(const std::string& title, , pixel_format_(SDL_PIXELFORMAT_UNKNOWN) { if(!window_) { - ERR_DP << "Failed to create a SDL_Window object with error »" - << SDL_GetError() << "«.\n"; - - throw game::error(""); + throw texception("Failed to create a SDL_Window object.", true); } if(!SDL_CreateRenderer(window_, -1, render_flags)) { - ERR_DP << "Failed to create a SDL_Window object with error »" - << SDL_GetError() << "«.\n"; - - throw game::error(""); + throw texception("Failed to create a SDL_Renderer object.", true); } SDL_RendererInfo info; if(SDL_GetRendererInfo(*this, &info) != 0) { - ERR_DP << "Failed to retrieve the information of the renderer, error »" - << SDL_GetError() << "«.\n"; - - throw game::error(""); + throw texception("Failed to retrieve the information of the renderer.", + true); } if(info.num_texture_formats == 0) { - ERR_DP << "The renderer has no texture information available.\n"; - - throw game::error(""); + throw texception("The renderer has no texture information available.\n", + false); } pixel_format_ = info.texture_formats[0];