Merge branch 'SDL2_exception'

Improves the exception handling for the SDL2 code.
This commit is contained in:
Mark de Wever 2014-03-24 20:01:06 +01:00
commit 4b298611fb
7 changed files with 96 additions and 26 deletions

View file

@ -359,6 +359,7 @@ endif()
set(wesnoth-sdl_SRC
sdl/alpha.cpp
sdl/exception.cpp
sdl/texture.cpp
sdl/window.cpp
)

View file

@ -151,6 +151,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

View file

@ -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;

37
src/sdl/exception.cpp Normal file
View file

@ -0,0 +1,37 @@
/*
Copyright (C) 2014 by Mark de Wever <koraq@xs4all.nl>
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 <SDL_error.h>
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

44
src/sdl/exception.hpp Normal file
View file

@ -0,0 +1,44 @@
/*
Copyright (C) 2014 by Mark de Wever <koraq@xs4all.nl>
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

View file

@ -16,13 +16,10 @@
#if SDL_VERSION_ATLEAST(2, 0, 0)
#include "log.hpp"
#include "sdl/exception.hpp"
#include <cassert>
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);
}
}

View file

@ -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 <SDL_render.h>
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];