From: Charles McGarvey Date: Tue, 1 Sep 2009 05:23:25 +0000 (-0600) Subject: better logging (maybe) and exception handling X-Git-Url: https://git.dogcows.com/gitweb?a=commitdiff_plain;h=5fa5f117f28922a7e539a432367960c1a61f837d;p=chaz%2Fyoink better logging (maybe) and exception handling --- diff --git a/configure.ac b/configure.ac index e8fd379..731e421 100644 --- a/configure.ac +++ b/configure.ac @@ -155,7 +155,11 @@ AC_CHECK_FUNCS([nanosleep strchr strcspn strrchr strstr]) # DATA_FILES=$(echo $(cd data; \ - find . -name "*.png" -o -name "*.json" -o -name yoinkrc)) + find . -name "*.json" \ + -o -name "*.ogg" \ + -o -name "*.png" \ + -o -name "*.xm" \ + -o -name "yoinkrc")) AC_SUBST([DATA_FILES]) diff --git a/extra/yoink.ebuild b/extra/yoink.ebuild index 932251e..9ce2464 100644 --- a/extra/yoink.ebuild +++ b/extra/yoink.ebuild @@ -19,7 +19,7 @@ IUSE="debug profile" RDEPEND="media-libs/libsdl[opengl] media-libs/sdl-image[png] virtual/opengl - media-libs/sdl-sound[mikmod, vorbis] + media-libs/sdl-sound[mikmod,vorbis] media-libs/openal media-libs/freealut" DEPEND="${RDEPEND} diff --git a/src/Character.cc b/src/Character.cc index 97a10b2..873f9e1 100644 --- a/src/Character.cc +++ b/src/Character.cc @@ -27,8 +27,8 @@ *******************************************************************************/ #include "Character.hh" +#include "Log.hh" -#include Character::Character(const std::string& name) : tilemap_(name), @@ -125,7 +125,7 @@ void Character::handleEvent(const Mf::Event& event) break; } - std::cout << "current force: " << current.force << std::endl; + Mf::logInfo("current force [%f %f]", current.force[0], current.force[1]); } diff --git a/src/Makefile.am b/src/Makefile.am index 3e3bd99..8b3900c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -20,11 +20,14 @@ libmoof_la_SOURCES = \ Moof/Engine.hh \ Moof/Entity.hh \ Moof/Event.hh \ + Moof/Exception.hh \ Moof/Frustum.cc \ Moof/Frustum.hh \ Moof/Hash.cc \ Moof/Hash.hh \ Moof/Interpolator.hh \ + Moof/Log.cc \ + Moof/Log.hh \ Moof/Math.hh \ Moof/Mippleton.hh \ Moof/Octree.cc \ diff --git a/src/Moof/Animation.cc b/src/Moof/Animation.cc index 30e3fbb..39dcb5a 100644 --- a/src/Moof/Animation.cc +++ b/src/Moof/Animation.cc @@ -225,7 +225,7 @@ struct Animation::Impl */ Impl(const std::string& name) : - data(GlobalData::retain(name), &GlobalData::release), + data(GlobalData::getInstance(name)), currentSequence(0), frameCounter(0), frameIndex(0), diff --git a/src/Moof/Dispatcher.cc b/src/Moof/Dispatcher.cc index 7e33c27..fd93ccf 100644 --- a/src/Moof/Dispatcher.cc +++ b/src/Moof/Dispatcher.cc @@ -30,8 +30,6 @@ #include "Dispatcher.hh" -#include - namespace Mf { diff --git a/src/Moof/Engine.cc b/src/Moof/Engine.cc index d7d04a8..c74ace8 100644 --- a/src/Moof/Engine.cc +++ b/src/Moof/Engine.cc @@ -55,16 +55,19 @@ public: { if (SDL_Init(SDL_INIT_EVERYTHING | SDL_INIT_EVENTTHREAD) != 0) { - throw Exception(SDL_GetError()); + std::cerr << "sdl is complaining: " << SDL_GetError() << std::endl; + throw Exception(Exception::SDL_ERROR); } if (FE_Init() != 0) { - throw Exception(FE_GetError()); + std::cerr << "fast events error: " << FE_GetError() << std::endl; + throw Exception(Exception::SDL_ERROR); } - if (Sound_Init() != 0) + if (Sound_Init() == 0) { - //throw Exception(Sound_GetError()); - std::cerr << Sound_GetError() << std::endl; + std::cerr << "sound initialization failed: " << Sound_GetError() + << std::endl; + throw Exception(Exception::SDL_ERROR); } alutInit(&argc, argv); diff --git a/src/Moof/Engine.hh b/src/Moof/Engine.hh index aa75135..f088dc5 100644 --- a/src/Moof/Engine.hh +++ b/src/Moof/Engine.hh @@ -35,6 +35,7 @@ #include #include +#include #include @@ -71,10 +72,15 @@ public: virtual void draw(Scalar alpha); virtual void handleEvent(const Event& event); - struct Exception : std::runtime_error + struct Exception : public Mf::Exception { - explicit Exception(const std::string& what_arg) : - std::runtime_error(what_arg) {} + explicit Exception(unsigned error) : + Mf::Exception(error) {} + + void raise() + { + throw *this; + } }; private: diff --git a/src/Moof/Exception.hh b/src/Moof/Exception.hh new file mode 100644 index 0000000..2478ca1 --- /dev/null +++ b/src/Moof/Exception.hh @@ -0,0 +1,83 @@ + +/******************************************************************************* + + Copyright (c) 2009, Charles McGarvey + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +*******************************************************************************/ + +#ifndef _MOOF_EXCEPTION_HH_ +#define _MOOF_EXCEPTION_HH_ + +#include +#include + + +namespace Mf { + + +struct Exception : public std::exception +{ + enum + { + FILE_NOT_FOUND = 1, + OPENGL_ERROR = 2, + OPENAL_ERROR = 3, + SDL_ERROR = 4 + }; + + explicit Exception(unsigned error) : + code(error) {} + virtual ~Exception() throw() {} + + virtual void raise() + { + throw *this; + } + + virtual const char* what() const throw() + { + switch (code) + { + case FILE_NOT_FOUND: + return "file not found"; + case OPENGL_ERROR: + return "opengl error"; + case OPENAL_ERROR: + return "openal error"; + case SDL_ERROR: + return "sdl error"; + } + return "unknown error"; + } + + unsigned code; +}; + + +} // namespace Mf + +#endif // _MOOF_EXCEPTION_HH_ + +/** vim: set ts=4 sw=4 tw=80: *************************************************/ + diff --git a/src/Moof/Hash.cc b/src/Moof/Hash.cc index 457c353..eb024ad 100644 --- a/src/Moof/Hash.cc +++ b/src/Moof/Hash.cc @@ -32,8 +32,10 @@ namespace Mf { -//----------------------------------------------------------------------------- // MurmurHash2, by Austin Appleby +// http://murmurhash.googlepages.com/ + +// This function is in the public domain. // Note - This code makes a few assumptions about how your machine behaves - @@ -46,7 +48,7 @@ namespace Mf { // 2. It will not produce the same results on little-endian and big-endian // machines. -unsigned int MurmurHash2_(const void* key, int len, unsigned int seed) +unsigned getHash::operator()(const void* key, int len, unsigned int seed) const { // 'm' and 'r' are mixing constants generated offline. // They're not really 'magic', they just happen to work well. diff --git a/src/Moof/Hash.hh b/src/Moof/Hash.hh index c069544..f0fab50 100644 --- a/src/Moof/Hash.hh +++ b/src/Moof/Hash.hh @@ -37,13 +37,19 @@ namespace Mf { -unsigned MurmurHash2_(const void* key, int len, unsigned seed); - -struct hash_string +struct getHash { + // generic hash function + unsigned operator()(const void* key, int len, unsigned seed = -1) const; + inline unsigned operator()(const std::string& val) const { - return MurmurHash2_(val.data(), val.length(), -1); + return (*this)(val.data(), val.length()); + } + + inline unsigned operator()(int val) const + { + return val; } }; diff --git a/src/Moof/Log.cc b/src/Moof/Log.cc new file mode 100644 index 0000000..dcdb5ec --- /dev/null +++ b/src/Moof/Log.cc @@ -0,0 +1,121 @@ + +/******************************************************************************* + + Copyright (c) 2009, Charles McGarvey + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +*******************************************************************************/ + +#include +#include // snprintf +#include // strcpy + +#include "Log.hh" + + +namespace Mf { + + +static LogLevel logLevel_ = WARNING; + +static void printLog_(int logLevel, const char* fmt, va_list args) +{ + if (logLevel_ < logLevel) return; + + switch (logLevel) + { + case ERROR: + fprintf(stderr, " error: "); + break; + case WARNING: + fprintf(stderr, "warning: "); + break; + case INFO: + fprintf(stderr, " info: "); + break; + case DEBUGGING: + fprintf(stderr, " debug: "); + break; + } + + vfprintf(stderr, fmt, args); + fprintf(stderr, "\n"); +} + + +LogLevel setLogLevel(LogLevel level) +{ + if (level != 0) + logLevel_ = level; + + return logLevel_; +} + + +void +logError(const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + + printLog_(ERROR, fmt, args); + + va_end(args); +} + +void +logWarning(const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + + printLog_(WARNING, fmt, args); + + va_end(args); +} + +void +logInfo(const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + + printLog_(INFO, fmt, args); + + va_end(args); +} + +void +logDebug(const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + + printLog_(DEBUGGING, fmt, args); + + va_end(args); +} + + +} // namespace Mf + diff --git a/src/Moof/Log.hh b/src/Moof/Log.hh new file mode 100644 index 0000000..d3e4009 --- /dev/null +++ b/src/Moof/Log.hh @@ -0,0 +1,119 @@ + +/******************************************************************************* + + Copyright (c) 2009, Charles McGarvey + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +*******************************************************************************/ + +#ifndef _MOOF_LOG_H_ +#define _MOOF_LOG_H_ + +/** + * @file log.h + * Functions related to logging the process. + * The logging functions are logError(), logWarning(), logInfo(), and + * logDebug(), listed from most critical to least critical. + */ + +#include // exit +#include // errno +#include // strerror + + +namespace Mf { + + +/** + * Macro which tests an assertion and issues an logError() and exits if false. + * This differs from the standard assert() in that this is a runtime assertion + * test and will always be compiled in. + * @param X test to perform + */ + +#define ASSERT(X) if (!(X)) logError("false assertion at %s:%d, " #X \ + " (errno: %d, %s)", __FILE__, __LINE__, errno, strerror(errno)), exit(1) + + +/** + * Possible values used for setting the log level using LogLevel(). Log + * messages of lesser importance than the level specified are ignored. + * @see LogLevel() + */ + +typedef enum { + NONE = -1, ///< Disable all logging. + ERROR = 1, ///< Log only errors. + WARNING = 2, ///< Log warnings and errors. + INFO = 3, ///< Log info, warnings, errors. + DEBUGGING = 4 ///< Log all messages. +} LogLevel; + + +/** + * Set and/or get the level of logs which will be logged. If not called, + * defaults to WARNING + * @param level LOG_LEVEL_* constant or 0 for no change. + * @return The currently set log level. + */ + +LogLevel setLogLevel(LogLevel level); + + +/** + * Log an error. + * @param fmt Log format string. + * @param ... Extra format arguments. + */ + +void logError(const char* fmt, ...); + +/** + * Log a warning. + * @param fmt Log format string. + * @param ... Extra format arguments. + */ + +void logWarning(const char* fmt, ...); + +/** + * Log a message. + * @param fmt Log format string. + * @param ... Extra format arguments. + */ + +void logInfo(const char* fmt, ...); + +/** + * Log a debug message. + * @param fmt Log format string. + * @param ... Extra format arguments. + */ + +void logDebug(const char* fmt, ...); + + +} // namespace Mf + +#endif // _MOOF_LOG_H_ + diff --git a/src/Moof/Mippleton.hh b/src/Moof/Mippleton.hh index 9ef9602..e9b9732 100644 --- a/src/Moof/Mippleton.hh +++ b/src/Moof/Mippleton.hh @@ -40,6 +40,8 @@ #include +#include + #include @@ -49,26 +51,15 @@ namespace Mf { template class Mippleton { - typedef std::pair ptr_value_t; - typedef std::pair ptr_map_pair_t; - typedef stlplus::hash ptr_map_t; - //typedef std::map ptr_map_t; - - static ptr_map_t ptrs_; - std::string name_; - -public: - explicit Mippleton(const std::string& name) : - name_(name) {} + typedef std::pair PtrValue; + typedef stlplus::hash PtrMap; - inline const std::string& getName() const - { - return name_; - } + static PtrMap ptrs_; + std::string name_; inline static T* retain(const std::string& name) { - typename ptr_map_t::iterator it = ptrs_.find(name); + typename PtrMap::iterator it = ptrs_.find(name); if (it != ptrs_.end()) { @@ -78,14 +69,19 @@ public: else { T* newObj = new T(name); - ptrs_.insert(ptr_map_pair_t(name, ptr_value_t(1, newObj))); + ptrs_.insert(std::make_pair(name, std::make_pair(1, newObj))); return newObj; } } + inline static void release(T* obj) + { + releaseByName(obj->name_); + } + inline static void releaseByName(const std::string& name) { - typename ptr_map_t::iterator it; + typename PtrMap::iterator it; if ((it = ptrs_.find(name)) != ptrs_.end() && --(*it).second.first == 0) { @@ -94,15 +90,23 @@ public: } } - inline static void release(T* obj) +public: + explicit Mippleton(const std::string& name) : + name_(name) {} + + inline const std::string& getName() const + { + return name_; + } + + inline static boost::shared_ptr getInstance(const std::string& name) { - releaseByName(obj->getName()); + return boost::shared_ptr(retain(name), &release); } }; template -stlplus::hash< std::string,std::pair,hash_string > -//std::map< std::string,std::pair > +stlplus::hash< std::string,std::pair,getHash > Mippleton::ptrs_; diff --git a/src/Moof/Scene.cc b/src/Moof/Scene.cc index 5e44221..1f76570 100644 --- a/src/Moof/Scene.cc +++ b/src/Moof/Scene.cc @@ -541,7 +541,7 @@ public: Scene::Scene(const std::string& name) : // pass through - impl_(Scene::Impl::retain(name), &Scene::Impl::release) {} + impl_(Scene::Impl::getInstance(name)) {} void Scene::draw(Scalar alpha, const Camera& cam) const diff --git a/src/Moof/Sound.cc b/src/Moof/Sound.cc index 59acfaa..368e938 100644 --- a/src/Moof/Sound.cc +++ b/src/Moof/Sound.cc @@ -26,13 +26,13 @@ *******************************************************************************/ -#include #include #include #include #include +#include "Log.hh" #include "Mippleton.hh" #include "Sound.hh" @@ -84,8 +84,8 @@ struct Sound::Impl if (!sound) { - std::cerr << "could not load sound from file" << std::endl; - exit(1); + logWarning("audio not found: %s", getName().c_str()); + throw Exception(Exception::FILE_NOT_FOUND); } if (!stream) @@ -93,27 +93,27 @@ struct Sound::Impl unsigned decoded = Sound_DecodeAll(sound); if (decoded == 0) { - std::cout << "decoded no bytes" << std::endl; - exit(1); + logWarning("decoded not bytes from %s", getName().c_str()); + throw Exception(Exception::FILE_NOT_FOUND); } alGenBuffers(2, objects); alBufferData(objects[0], getAudioFormat(sound->actual), sound->buffer, sound->buffer_size, sound->actual.rate); - std::cerr << "buffer size: " << sound->buffer_size << std::endl; - std::cerr << "channels: " << (int)sound->actual.channels << std::endl; - std::cerr << "format: " << sound->actual.format << std::endl; - std::cerr << "frequency: " << sound->actual.rate << std::endl; + logDebug("buffer size: %d", sound->buffer_size); + logDebug(" channels: %d", sound->actual.channels); + logDebug(" format: %d", sound->actual.format); + logDebug(" frequency: %d", sound->actual.rate); Sound_FreeSample(sound); sound = 0; } else { - std::cerr << "buffer size: " << sound->buffer_size << std::endl; - std::cerr << "channels: " << (int)sound->actual.channels << std::endl; - std::cerr << "format: " << sound->actual.format << std::endl; - std::cerr << "frequency: " << sound->actual.rate << std::endl; + logDebug("buffer size: %d", sound->buffer_size); + logDebug(" channels: %d", sound->actual.channels); + logDebug(" format: %d", sound->actual.format); + logDebug(" frequency: %d", sound->actual.rate); alGenBuffers(2, objects); } } @@ -143,7 +143,7 @@ struct Sound::Impl }; Impl(const std::string& name, bool stream = false) : - buffer_(Buffer::retain(name), Buffer::release) + buffer_(Buffer::getInstance(name)) { if (!stream) buffer_->loadFromFile(Sound::getPath(name), stream); else buffer_->loadFromFile(SoundStream::getPath(name), stream); diff --git a/src/Moof/Sound.hh b/src/Moof/Sound.hh index 23979db..bc669bc 100644 --- a/src/Moof/Sound.hh +++ b/src/Moof/Sound.hh @@ -38,6 +38,7 @@ #include +#include #include #include @@ -58,10 +59,30 @@ public: static std::string getPath(const std::string& name); - struct Exception : std::runtime_error + struct Exception : public Mf::Exception { - explicit Exception(const std::string& what_arg) : - std::runtime_error(what_arg) {} + enum + { + BAD_AUDIO_FORMAT = 1024 + }; + + explicit Exception(unsigned error) : + Mf::Exception(error) {} + + void raise() + { + throw *this; + } + + const char* what() const throw() + { + switch (code) + { + case BAD_AUDIO_FORMAT: + return "unknown audio format"; + } + return Mf::Exception::what(); + } }; protected: diff --git a/src/Moof/Texture.cc b/src/Moof/Texture.cc index 46dbb48..b778c1c 100644 --- a/src/Moof/Texture.cc +++ b/src/Moof/Texture.cc @@ -34,11 +34,11 @@ #include #include "Dispatcher.hh" +#include "Log.hh" #include "Mippleton.hh" #include "OpenGL.hh" #include "Texture.hh" -#include namespace Mf { @@ -81,7 +81,6 @@ class Texture::Impl : public Mippleton void contextRecreated(const Notification* note) { - std::cout << "context recrated!" << std::endl; object_ = globalObject_ = 0; uploadToGL(); } @@ -245,7 +244,8 @@ public: if (!surface) { - throw Texture::Exception("loading from file failed"); + logWarning("texture not found: %s", getName().c_str()); + throw Exception(Exception::FILE_NOT_FOUND); } SDL_Surface* temp = prepareImageForGL(surface); @@ -253,7 +253,7 @@ public: if (!temp) { - throw Texture::Exception("uploading to opengl failed"); + throw Exception(Exception::OPENGL_ERROR); } if (temp->format->BytesPerPixel == 3) @@ -267,7 +267,7 @@ public: else { SDL_FreeSurface(temp); - throw Texture::Exception("incompatible color mode"); + throw Exception(Exception::BAD_IMAGE_FORMAT); } width_ = temp->w; @@ -390,7 +390,7 @@ GLuint Texture::Impl::globalObject_ = 0; Texture::Texture(const std::string& name) : // pass through - impl_(Texture::Impl::retain(name), &Texture::Impl::release) {} + impl_(Texture::Impl::getInstance(name)) {} /** diff --git a/src/Moof/Texture.hh b/src/Moof/Texture.hh index cdb6059..d55f6f7 100644 --- a/src/Moof/Texture.hh +++ b/src/Moof/Texture.hh @@ -38,6 +38,7 @@ #include +#include #include #include @@ -65,10 +66,30 @@ public: static std::string getPath(const std::string& name); - struct Exception : std::runtime_error + struct Exception : public Mf::Exception { - explicit Exception(const std::string& what_arg) : - std::runtime_error(what_arg) {} + enum + { + BAD_IMAGE_FORMAT = 1024 + }; + + explicit Exception(unsigned error) : + Mf::Exception(error) {} + + void raise() + { + throw *this; + } + + const char* what() const throw() + { + switch (code) + { + case BAD_IMAGE_FORMAT: + return "inappropriate image format"; + } + return Mf::Exception::what(); + } }; private: diff --git a/src/Moof/Tilemap.cc b/src/Moof/Tilemap.cc index 17642c7..85c117e 100644 --- a/src/Moof/Tilemap.cc +++ b/src/Moof/Tilemap.cc @@ -164,7 +164,7 @@ public: Tilemap::Tilemap(const std::string& name) : Texture(name), - impl_(Tilemap::Impl::retain(name), &Tilemap::Impl::release) + impl_(Tilemap::Impl::getInstance(name)) { setMinFilter(impl_->minFilter_); setMagFilter(impl_->magFilter_); diff --git a/src/YoinkApp.cc b/src/YoinkApp.cc index 626fe1a..a17bf88 100644 --- a/src/YoinkApp.cc +++ b/src/YoinkApp.cc @@ -32,6 +32,8 @@ #include +#include +#include #include #include #include @@ -138,7 +140,6 @@ YoinkApp::YoinkApp(int argc, char* argv[]) : YoinkApp::~YoinkApp() { - std::cerr << "yoinkapp destructor" << std::endl; //delete heroine; delete font; delete testScene; @@ -457,26 +458,45 @@ void YoinkApp::handleEvent(const Mf::Event& event) int main(int argc, char* argv[]) { - std::cout << PACKAGE_STRING << std::endl + std::cout << std::endl << PACKAGE_STRING << std::endl << "Compiled " << __TIME__ " " __DATE__ << std::endl << "Send patches and bug reports to <" PACKAGE_BUGREPORT << ">." << std::endl << std::endl; +#if ! NDEBUG + Mf::setLogLevel(Mf::DEBUGGING); +#endif + int status = 0; +//start: try { - YoinkApp* app = new YoinkApp(argc, argv); - status = app->run(); - delete app; + YoinkApp app(argc, argv); + status = app.run(); } - catch (Mf::Engine::Exception e) + //catch (Mf::Texture::Exception e) + //{ + //std::cout << "Unhandled exception: " << e.what() << std::endl; + //status = 1; + //} + catch (Mf::Exception e) { - std::cerr << "Unhandled exception: " << e.what() << std::endl; + //std::cout << "Unhandled exception: " << e.what() << std::endl; + Mf::logError("unhandled exception: <<%s>>", e.what()); + Mf::logInfo("it's time to crash now ;-("); status = 1; + + //std::cout << "Yoink stopped. Do you want to run it again? [yes/No]" + //<< std::endl; + + //char answer; + //std::cin >> answer; + + //if (answer == 'y' || answer == 'Y') goto start; } - std::cout << "Goodbye..." << std::endl; + std::cout << std::endl << "Goodbye..." << std::endl << std::endl; return status; }