sdlterm: Cleanup exceptions.
authorRadek Brich <radek.brich@devl.cz>
Tue, 08 Jan 2013 23:36:01 +0100
changeset 58 50308ed5e4f9
parent 57 911927edbdde
child 59 729fcdfe6b57
sdlterm: Cleanup exceptions.
sdlterm/src/sdlterm.cc
sdlterm/src/sdlterm.h
--- a/sdlterm/src/sdlterm.cc	Tue Jan 08 01:12:07 2013 +0100
+++ b/sdlterm/src/sdlterm.cc	Tue Jan 08 23:36:01 2013 +0100
@@ -1,6 +1,5 @@
 #include "sdlterm.h"
 
-#include <exception>
 #include <algorithm>
 
 
@@ -44,8 +43,7 @@
 {
     if (TTF_Init() == -1)
     {
-        printf("TTF_Init: %s\n", TTF_GetError());
-        throw std::exception();
+        throw SDLTermError(std::string("TTF_Init: ") + TTF_GetError());
     }
 }
 
@@ -66,23 +64,21 @@
     _font_regular = TTF_OpenFont(fname_regular, ptsize);
     if (!_font_regular)
     {
-        printf("TTF_OpenFont: %s\n", TTF_GetError());
-        throw std::exception();
+        throw SDLTermError(std::string("TTF_OpenFont: ") + TTF_GetError());
     }
 
     // open bold font
     _font_bold = TTF_OpenFont(fname_bold, ptsize);
     if (!_font_bold)
     {
-        printf("TTF_OpenFont: %s\n", TTF_GetError());
-        throw std::exception();
+        throw SDLTermError(std::string("TTF_OpenFont: ") + TTF_GetError());
     }
 
     // update metrics for regular font
     int advance;
     if (TTF_GlyphMetrics(_font_regular, 'M', NULL, NULL, NULL, NULL, &advance) == -1)
     {
-        printf("TTF_GlyphMetrics: %s\n", TTF_GetError());
+        throw SDLTermError(std::string("TTF_GlyphMetrics: ") + TTF_GetError());
     }
     _cell_width = advance;
     _cell_height = TTF_FontHeight(_font_regular);
@@ -90,7 +86,7 @@
     // read metrics for bold font
     if (TTF_GlyphMetrics(_font_bold, 'M', NULL, NULL, NULL, NULL, &advance) == -1)
     {
-        printf("TTF_GlyphMetrics: %s\n", TTF_GetError());
+        throw SDLTermError(std::string("TTF_GlyphMetrics: ") + TTF_GetError());
     }
     if (advance > _cell_width)
     {
@@ -221,8 +217,7 @@
 
     if (_screen_surface == NULL)
     {
-        fprintf(stderr, "Unable to set video: %s\n", SDL_GetError());
-        throw std::exception();
+        throw SDLTermError(std::string("SDL_SetVideoMode: ") + SDL_GetError());
     }
 
     _pixel_width = pxwidth;
@@ -312,8 +307,7 @@
 {
     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1)
     {
-        fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError());
-        throw std::exception();
+        throw SDLTermError(std::string("SDL_Init: ") + SDL_GetError());
     }
     SDL_EnableUNICODE(1);
     SDL_EnableKeyRepeat(250, SDL_DEFAULT_REPEAT_INTERVAL);
@@ -436,8 +430,7 @@
                 break; // continue loop
         }
     }
-    fprintf(stderr, "SDL_WaitEvent error: %s\n", SDL_GetError());
-    throw std::exception();
+    throw SDLTermError(std::string("SDL_WaitEvent: ") + SDL_GetError());
 }
 
 
--- a/sdlterm/src/sdlterm.h	Tue Jan 08 01:12:07 2013 +0100
+++ b/sdlterm/src/sdlterm.h	Tue Jan 08 23:36:01 2013 +0100
@@ -2,7 +2,9 @@
 #include "SDL_ttf.h"
 
 #include <map>
+#include <string>
 #include <vector>
+#include <exception>
 
 
 namespace Style
@@ -223,3 +225,17 @@
     static Uint32 _wait_event_callback(Uint32 interval, void *param);
 };
 
+
+class SDLTermError: public std::exception
+{
+public:
+    SDLTermError(const std::string &a_msg)
+        : std::exception(), _msg(a_msg) {};
+    virtual ~SDLTermError() throw() {};
+
+    virtual const char *what() const throw() { return _msg.c_str(); };
+
+private:
+    std::string _msg;
+};
+