Skip to content

Commit

Permalink
Merge branch 'SDL2_exception'
Browse files Browse the repository at this point in the history
Improves the exception handling for the SDL2 code.
  • Loading branch information
mordante committed Mar 24, 2014
2 parents d79b7d6 + f1793ab commit 4b29861
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 26 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Expand Up @@ -359,6 +359,7 @@ endif()

set(wesnoth-sdl_SRC
sdl/alpha.cpp
sdl/exception.cpp
sdl/texture.cpp
sdl/window.cpp
)
Expand Down
1 change: 1 addition & 0 deletions src/SConscript
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/game.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down
37 changes: 37 additions & 0 deletions src/sdl/exception.cpp
@@ -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 changes: 44 additions & 0 deletions src/sdl/exception.hpp
@@ -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
8 changes: 2 additions & 6 deletions src/sdl/texture.cpp
Expand Up @@ -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
{

Expand All @@ -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);
}
}

Expand Down
27 changes: 7 additions & 20 deletions src/sdl/window.cpp
Expand Up @@ -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
{

Expand All @@ -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];
Expand Down

0 comments on commit 4b29861

Please sign in to comment.