]> Dogcows Code - chaz/yoink/commitdiff
better logging (maybe) and exception handling
authorCharles McGarvey <chazmcgarvey@brokenzipper.com>
Tue, 1 Sep 2009 05:23:25 +0000 (23:23 -0600)
committerCharles McGarvey <chazmcgarvey@brokenzipper.com>
Tue, 1 Sep 2009 05:23:25 +0000 (23:23 -0600)
21 files changed:
configure.ac
extra/yoink.ebuild
src/Character.cc
src/Makefile.am
src/Moof/Animation.cc
src/Moof/Dispatcher.cc
src/Moof/Engine.cc
src/Moof/Engine.hh
src/Moof/Exception.hh [new file with mode: 0644]
src/Moof/Hash.cc
src/Moof/Hash.hh
src/Moof/Log.cc [new file with mode: 0644]
src/Moof/Log.hh [new file with mode: 0644]
src/Moof/Mippleton.hh
src/Moof/Scene.cc
src/Moof/Sound.cc
src/Moof/Sound.hh
src/Moof/Texture.cc
src/Moof/Texture.hh
src/Moof/Tilemap.cc
src/YoinkApp.cc

index e8fd379c8091dcb2bbc7f704f3732d8446495641..731e421d86f9b67c58f38c347dc29d23d3e915b0 100644 (file)
@@ -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])
 
 
index 932251e561893ffbe8bfb6e2bad401d350f18a6b..9ce24644498f23a908323af78f875f3292f14e66 100644 (file)
@@ -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}
index 97a10b2553d27532cd2631f2a5447ae85ec7f1ca..873f9e13c74db5a2103ddb893d27af2ac010f9b1 100644 (file)
@@ -27,8 +27,8 @@
 *******************************************************************************/
 
 #include "Character.hh"
+#include "Log.hh"
 
-#include <iostream>
        
 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]);
 }
 
 
index 3e3bd99a2c93b9d5b0e094c2ac56bc5434a163b9..8b3900c21ae9abb203f5f5b11599ecede29abf63 100644 (file)
@@ -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 \
index 30e3fbbf4bcebb4d041d02d55b366b9b3e3a936e..39dcb5a40a8b643a7e98958e293ecdcfc7b30ac4 100644 (file)
@@ -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),
index 7e33c270d848050caf7fe43c8a17633106928fb0..fd93ccfb3948c249e543882b47fde522c8330ca6 100644 (file)
@@ -30,8 +30,6 @@
 
 #include "Dispatcher.hh"
 
-#include <iostream>
-
 
 namespace Mf {
 
index d7d04a846af6bc54753551d1135d582b481fab06..c74ace8ef2207428f360dc5ae397d663539aca28 100644 (file)
@@ -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);
 
index aa751356f6a81b6ec1712cf1ee7bdf322384d749..f088dc56e85fcb663766594a2c9b454788e7c519 100644 (file)
@@ -35,6 +35,7 @@
 
 #include <Moof/Dispatcher.hh>
 #include <Moof/Event.hh>
+#include <Moof/Exception.hh>
 #include <Moof/Math.hh>
 
 
@@ -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 (file)
index 0000000..2478ca1
--- /dev/null
@@ -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 <exception>
+#include <boost/shared_ptr.hpp>
+
+
+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: *************************************************/
+
index 457c353d277183bd190b7005f94087110b47db33..eb024ad85c7675176b6894ad323cc12457a2f6d7 100644 (file)
 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.
index c0695447ab303f6438b264fc4b2e9141fb0ad661..f0fab50705aca13e9c554d5eb14c0c161a36699e 100644 (file)
 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 (file)
index 0000000..dcdb5ec
--- /dev/null
@@ -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 <cstdarg>
+#include <cstdio>              // snprintf
+#include <cstring>             // 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 (file)
index 0000000..d3e4009
--- /dev/null
@@ -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 <cstdlib>             // exit
+#include <cerrno>              // errno
+#include <cstring>             // 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_
+
index 9ef960276b34a8d9384557d2b2a8d690c36d7488..e9b97328d351ae9de7e08527a49984f90030231b 100644 (file)
@@ -40,6 +40,8 @@
 
 #include <string>
 
+#include <boost/shared_ptr.hpp>
+
 #include <Moof/Hash.hh>
 
 
@@ -49,26 +51,15 @@ namespace Mf {
 template <class T>
 class Mippleton
 {
-       typedef std::pair<unsigned,T*>                                                          ptr_value_t;
-       typedef std::pair<std::string,ptr_value_t>                                      ptr_map_pair_t;
-       typedef stlplus::hash<std::string,ptr_value_t,hash_string>      ptr_map_t;
-       //typedef std::map<std::string,ptr_value_t>     ptr_map_t;
-
-       static ptr_map_t        ptrs_;
-       std::string                     name_;
-
-public:
-       explicit Mippleton(const std::string& name) :
-               name_(name) {}
+       typedef std::pair<unsigned,T*>                                                          PtrValue;
+       typedef stlplus::hash<std::string,PtrValue,getHash>     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<T> getInstance(const std::string& name)
        {
-               releaseByName(obj->getName());
+               return boost::shared_ptr<T>(retain(name), &release);
        }
 };
 
 template <class T>
-stlplus::hash< std::string,std::pair<unsigned,T*>,hash_string >
-//std::map< std::string,std::pair<unsigned,T*> >
+stlplus::hash< std::string,std::pair<unsigned,T*>,getHash >
        Mippleton<T>::ptrs_;
 
 
index 5e44221a93391a03afacb4b4059fe30ca47ffa37..1f76570f77363f08f81cb65163d6f0556c18f8a8 100644 (file)
@@ -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
index 59acfaa9af269886ec0cc34a8286671751f75505..368e9388bca4aee4f3665ae40b1abf1afa71f6cd 100644 (file)
 
 *******************************************************************************/
 
-#include <iostream>
 #include <string>
 
 #include <SDL/SDL.h>
 #include <SDL/SDL_sound.h>
 #include <AL/al.h>
 
+#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);
index 23979db4aa019423573fbb8e73a59f5a49a6cc89..bc669bcd5df6dcf629ab26a97b564b23e6151b3a 100644 (file)
@@ -38,6 +38,7 @@
 
 #include <boost/shared_ptr.hpp>
 
+#include <Moof/Exception.hh>
 #include <Moof/Math.hh>
 #include <Moof/Resource.hh>
 
@@ -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:
index 46dbb480afc3fc179f06b2db8192ac5b421c0735..b778c1ccde0299b1e306d33724457728c563d65a 100644 (file)
 #include <SDL/SDL_image.h>
 
 #include "Dispatcher.hh"
+#include "Log.hh"
 #include "Mippleton.hh"
 #include "OpenGL.hh"
 #include "Texture.hh"
 
-#include <iostream>
 
 namespace Mf {
 
@@ -81,7 +81,6 @@ class Texture::Impl : public Mippleton<Impl>
 
        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)) {}
 
 
 /**
index cdb6059ceff00d039a41a8b68f6bae45a543f84c..d55f6f7309c71fe05765ac86c02abfbd53599959 100644 (file)
@@ -38,6 +38,7 @@
 
 #include <boost/shared_ptr.hpp>
 
+#include <Moof/Exception.hh>
 #include <Moof/OpenGL.hh>
 #include <Moof/Resource.hh>
 
@@ -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:
index 17642c723c9fccda03ee36325b14ff917f8b9218..85c117e271a4833266c95fcf2554102fced07bb4 100644 (file)
@@ -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_);
index 626fe1aea9ab18e823c49635573a19dad9dfac8c..a17bf88b1c0ffe84c7ef52a2b966773e434f5ea7 100644 (file)
@@ -32,6 +32,8 @@
 
 #include <boost/bind.hpp>
 
+#include <Moof/Exception.hh>
+#include <Moof/Log.hh>
 #include <Moof/Math.hh>
 #include <Moof/OpenGL.hh>
 #include <Moof/Settings.hh>
@@ -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;
 }
 
This page took 0.053888 seconds and 4 git commands to generate.