Skip to content
This repository has been archived by the owner on Jun 7, 2018. It is now read-only.

Commit

Permalink
Replace the DIE macro by better code
Browse files Browse the repository at this point in the history
git-svn-id: svn://solarus-engine.org/solarus/trunk@1438 08e91b48-8f19-0410-8fc9-9db6b7bce602
  • Loading branch information
christopho authored and Christophe THIÉRY committed Feb 10, 2012
1 parent 41d591a commit 97e22be
Show file tree
Hide file tree
Showing 71 changed files with 505 additions and 447 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
This ChangeLog describes the important changes of each svn commit.

r1437 (2010-09-21):

Replace the DIE macro by better code.

r1436 (2010-09-18):

Fix shop items.
Expand Down
16 changes: 0 additions & 16 deletions include/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@
#ifndef SOLARUS_COMMON_H
#define SOLARUS_COMMON_H

#include <string>
#include <sstream>
#include <stdexcept>
#include <iostream>
#include "Types.h"

/**
Expand All @@ -38,17 +34,5 @@
*/
#define SOLARUS_DEBUG_LEVEL 2

/**
* @brief Stops the program on an error message.
*
* This macro should be used to exit the program properly on an error message.
* The message parameter can contain several elements separated by the '<<' operator.
*/
//#if SOLARUS_DEBUG_LEVEL == 0
//#define DIE(message) do { throw std::logic_error(""); } while (false)
//#else
#define DIE(message) do { std::cerr << message << std::endl; std::ostringstream oss; oss << message; throw std::logic_error(oss.str()); } while (false)
//#endif

#endif

2 changes: 1 addition & 1 deletion include/GameControls.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class GameControls {
// controls
const std::string & get_key_name(GameKey game_key);
const std::string get_keyboard_string(GameKey game_key);
const std::string & get_joypad_string(GameKey game_key);
const std::string get_joypad_string(GameKey game_key);
void notify_event(InputEvent &event);
bool is_key_pressed(GameKey game_key);
int get_wanted_direction8(void);
Expand Down
12 changes: 12 additions & 0 deletions include/Map.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,17 @@ class Map {
void display_sprite(Sprite *sprite, int x, int y);
};

/**
* @brief Tests whether a point is outside the map area.
* @param x x of the point to check
* @param y y of the point to check
* @return true if this point is outside the map area
*/
inline bool Map::test_collision_with_border(int x, int y) {

return (x < 0 || y < 0 || x >= location.get_width() || y >= location.get_height());
}


#endif

10 changes: 5 additions & 5 deletions include/Solarus.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ class Solarus {

private:

Screen *current_screen; /**< the screen currently displayed (may be the title screen,
* the selection menu, the game, etc.) */
Surface *root_surface; /**< the surface where everything is drawn (always 320*240) */
DebugKeys *debug_keys; /**< special keys to debug the game, e.g. to traverse walls (disabled in release mode) */
bool exiting; /**< indicates that the program is about to stop */
Screen *current_screen; /**< the screen currently displayed (may be the title screen,
* the selection menu, the game, etc.) */
Surface *root_surface; /**< the surface where everything is drawn (always 320*240) */
DebugKeys *debug_keys; /**< special keys to debug the game, e.g. to traverse walls (disabled in release mode) */
bool exiting; /**< indicates that the program is about to stop */

void launch_adventure_mode(Savegame *savegame);

Expand Down
3 changes: 3 additions & 0 deletions include/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#define SOLARUS_TYPES_H

#include <stdint.h>
#include <string>

/*
* Types of the id of each resource.
Expand Down Expand Up @@ -69,6 +70,8 @@ class Rectangle;
class PixelBits;
class IniFile;
class InputEvent;
class Debug;
class StringConcat;

// menus
class LanguageScreen;
Expand Down
90 changes: 90 additions & 0 deletions include/lowlevel/Debug.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (C) 2009 Christopho, Solarus - http://www.solarus-engine.org
*
* Solarus 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 3 of the License, or
* (at your option) any later version.
*
* Solarus is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SOLARUS_DEBUG_H
#define SOLARUS_DEBUG_H

#include "Common.h"
#include <stdexcept>
#include <iostream>

/**
* @brief Provides functionalities for printing debug messages
* or making runtime verifications, especially when the code is compiled in debugging mode.
*/
class Debug {

private:

Debug(); // don't instanciate this class

public:

static void print(const std::string &message, std::ostream &os = std::cout);
static void assert(bool assertion, const std::string &error_message = "");
static void die(const std::string &error_message = "");
};


/**
* @brief Prints a message if the program is in debug mode.
*
* This function does nothing if SOLARUS_DEBUG_LEVEL is 0.
*
* @param message the message to print.
* @param the output stream to write (default is std::cout)
*/
inline void Debug::print(const std::string &message, std::ostream &os) {

#if SOLARUS_DEBUG_LEVEL > 0
os << message << std::endl;
#endif
}

/**
* @brief Throws an exception if the specified assertion fails.
*
* If the assertion fails, an std::logic_error with the specified error message is thrown.
* If SOLARUS_DEBUG_LEVEL is greater than 0, the error message is
* printed on stderr first.
* This function should be used to detect fatal errors only, that is,
* errors in your code or in the quest (the data files) that require to stop the program.
*
* @param assertion the boolean condition to check
* @param error_message the error message to attach to the exception when the assertion fails
*/
inline void Debug::assert(bool assertion, const std::string &error_message) {

if (!assertion) {
print(error_message, std::cerr);
throw std::logic_error(error_message);
}
}

/**
* @brief Throws an exception to stop the program.
*
* This function is equivalent to assert(false, error_message).
*
* @param error_message the error message to attach to the exception
*/
inline void Debug::die(const std::string &error_message) {

assert(false, error_message);
}

#endif

63 changes: 63 additions & 0 deletions include/lowlevel/StringConcat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2009 Christopho, Solarus - http://www.solarus-engine.org
*
* Solarus 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 3 of the License, or
* (at your option) any later version.
*
* Solarus is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SOLARUS_STRING_CONCAT_H
#define SOLARUS_STRING_CONCAT_H

#include <sstream>

/**
* @brief A subclass of std::string that add operators
* to directly concatenate elements.
*/
class StringConcat: public std::string {

private:

std::ostringstream oss; /**< used to concatenate non string objects to this string */

public:

StringConcat& operator<<(const std::string& element);
template <typename T> StringConcat& operator<<(const T &element);

};

/**
* @brief Appends a string to this string.
* @param element the string to append
*/
inline StringConcat& StringConcat::operator<<(const std::string& element) {

append(element);
return *this;
}

/**
* @brief Appends an object to this string.
* @param element the object to append
*/
template <typename T>
inline StringConcat& StringConcat::operator<<(const T &element) {

oss.str("");
oss << element;
append(oss.str());
return *this;
}

#endif

14 changes: 5 additions & 9 deletions src/DialogBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "lowlevel/Color.h"
#include "lowlevel/FileTools.h"
#include "lowlevel/Surface.h"
#include "lowlevel/Debug.h"
#include "lowlevel/StringConcat.h"

/**
* @brief Creates a new dialog box.
Expand Down Expand Up @@ -220,9 +222,7 @@ const std::string& DialogBox::get_variable(void) {

const std::string &value = variables[first_message_id];

if (value == "") {
DIE("Missing variable in message '" << current_message_id << "'");
}
Debug::assert(value.size() > 0, StringConcat() << "Missing variable in message '" << current_message_id << "'");

return value;
}
Expand All @@ -243,9 +243,7 @@ int DialogBox::get_last_answer(void) {
*/
void DialogBox::set_last_answer(int answer) {

if (answer < -1 || answer > 1) {
DIE("Invalid value of answer: " << answer);
}
Debug::assert(answer >= -1 && answer <= 1, StringConcat() << "Invalid value of answer: " << answer);
this->last_answer = answer;
}

Expand All @@ -257,9 +255,7 @@ void DialogBox::set_last_answer(int answer) {
*/
void DialogBox::start_dialog(const MessageId &first_message_id, VerticalPosition vertical_position) {

if (is_enabled()) {
DIE("Cannot start message sequence '" << first_message_id << ": the dialog box is already enabled");
}
Debug::assert(!is_enabled(), StringConcat() << "Cannot start message sequence '" << first_message_id << ": the dialog box is already enabled");

// save the action and sword keys
KeysEffect *keys_effect = game->get_keys_effect();
Expand Down
1 change: 1 addition & 0 deletions src/Dungeon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "lowlevel/FileTools.h"
#include "lowlevel/Rectangle.h"
#include "lowlevel/IniFile.h"
#include <sstream>

/**
* @brief Creates the specified dungeon.
Expand Down
Loading

0 comments on commit 97e22be

Please sign in to comment.