sdlterm/src/sdlterm.cc
changeset 58 50308ed5e4f9
parent 57 911927edbdde
child 60 fccca2a60492
equal deleted inserted replaced
57:911927edbdde 58:50308ed5e4f9
     1 #include "sdlterm.h"
     1 #include "sdlterm.h"
     2 
     2 
     3 #include <exception>
       
     4 #include <algorithm>
     3 #include <algorithm>
     5 
     4 
     6 
     5 
     7 void ColorMap::index_to_rgb(int index, SDL_Color &color) const
     6 void ColorMap::index_to_rgb(int index, SDL_Color &color) const
     8 {
     7 {
    42 GlyphRenderer::GlyphRenderer()
    41 GlyphRenderer::GlyphRenderer()
    43  : _font_regular(NULL), _font_bold(NULL), _cell_width(0), _cell_height(0), _cache(), _colormap()
    42  : _font_regular(NULL), _font_bold(NULL), _cell_width(0), _cell_height(0), _cache(), _colormap()
    44 {
    43 {
    45     if (TTF_Init() == -1)
    44     if (TTF_Init() == -1)
    46     {
    45     {
    47         printf("TTF_Init: %s\n", TTF_GetError());
    46         throw SDLTermError(std::string("TTF_Init: ") + TTF_GetError());
    48         throw std::exception();
       
    49     }
    47     }
    50 }
    48 }
    51 
    49 
    52 
    50 
    53 GlyphRenderer::~GlyphRenderer()
    51 GlyphRenderer::~GlyphRenderer()
    64 
    62 
    65     // open regular font
    63     // open regular font
    66     _font_regular = TTF_OpenFont(fname_regular, ptsize);
    64     _font_regular = TTF_OpenFont(fname_regular, ptsize);
    67     if (!_font_regular)
    65     if (!_font_regular)
    68     {
    66     {
    69         printf("TTF_OpenFont: %s\n", TTF_GetError());
    67         throw SDLTermError(std::string("TTF_OpenFont: ") + TTF_GetError());
    70         throw std::exception();
       
    71     }
    68     }
    72 
    69 
    73     // open bold font
    70     // open bold font
    74     _font_bold = TTF_OpenFont(fname_bold, ptsize);
    71     _font_bold = TTF_OpenFont(fname_bold, ptsize);
    75     if (!_font_bold)
    72     if (!_font_bold)
    76     {
    73     {
    77         printf("TTF_OpenFont: %s\n", TTF_GetError());
    74         throw SDLTermError(std::string("TTF_OpenFont: ") + TTF_GetError());
    78         throw std::exception();
       
    79     }
    75     }
    80 
    76 
    81     // update metrics for regular font
    77     // update metrics for regular font
    82     int advance;
    78     int advance;
    83     if (TTF_GlyphMetrics(_font_regular, 'M', NULL, NULL, NULL, NULL, &advance) == -1)
    79     if (TTF_GlyphMetrics(_font_regular, 'M', NULL, NULL, NULL, NULL, &advance) == -1)
    84     {
    80     {
    85         printf("TTF_GlyphMetrics: %s\n", TTF_GetError());
    81         throw SDLTermError(std::string("TTF_GlyphMetrics: ") + TTF_GetError());
    86     }
    82     }
    87     _cell_width = advance;
    83     _cell_width = advance;
    88     _cell_height = TTF_FontHeight(_font_regular);
    84     _cell_height = TTF_FontHeight(_font_regular);
    89 
    85 
    90     // read metrics for bold font
    86     // read metrics for bold font
    91     if (TTF_GlyphMetrics(_font_bold, 'M', NULL, NULL, NULL, NULL, &advance) == -1)
    87     if (TTF_GlyphMetrics(_font_bold, 'M', NULL, NULL, NULL, NULL, &advance) == -1)
    92     {
    88     {
    93         printf("TTF_GlyphMetrics: %s\n", TTF_GetError());
    89         throw SDLTermError(std::string("TTF_GlyphMetrics: ") + TTF_GetError());
    94     }
    90     }
    95     if (advance > _cell_width)
    91     if (advance > _cell_width)
    96     {
    92     {
    97         _cell_width = advance;
    93         _cell_width = advance;
    98     }
    94     }
   219 {
   215 {
   220     _screen_surface = SDL_SetVideoMode(pxwidth, pxheight, 0, SDL_SWSURFACE|SDL_ANYFORMAT|SDL_RESIZABLE);
   216     _screen_surface = SDL_SetVideoMode(pxwidth, pxheight, 0, SDL_SWSURFACE|SDL_ANYFORMAT|SDL_RESIZABLE);
   221 
   217 
   222     if (_screen_surface == NULL)
   218     if (_screen_surface == NULL)
   223     {
   219     {
   224         fprintf(stderr, "Unable to set video: %s\n", SDL_GetError());
   220         throw SDLTermError(std::string("SDL_SetVideoMode: ") + SDL_GetError());
   225         throw std::exception();
       
   226     }
   221     }
   227 
   222 
   228     _pixel_width = pxwidth;
   223     _pixel_width = pxwidth;
   229     _pixel_height = pxheight;
   224     _pixel_height = pxheight;
   230 
   225 
   310  : _screen(), _attr(7), _cursor_x(0), _cursor_y(0), _cursor_visible(false),
   305  : _screen(), _attr(7), _cursor_x(0), _cursor_y(0), _cursor_visible(false),
   311    _mousemove_last_x(-1), _mousemove_last_y(-1)
   306    _mousemove_last_x(-1), _mousemove_last_y(-1)
   312 {
   307 {
   313     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1)
   308     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1)
   314     {
   309     {
   315         fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError());
   310         throw SDLTermError(std::string("SDL_Init: ") + SDL_GetError());
   316         throw std::exception();
       
   317     }
   311     }
   318     SDL_EnableUNICODE(1);
   312     SDL_EnableUNICODE(1);
   319     SDL_EnableKeyRepeat(250, SDL_DEFAULT_REPEAT_INTERVAL);
   313     SDL_EnableKeyRepeat(250, SDL_DEFAULT_REPEAT_INTERVAL);
   320     SDL_WM_SetCaption("terminal", NULL);
   314     SDL_WM_SetCaption("terminal", NULL);
   321 }
   315 }
   434 
   428 
   435             default:
   429             default:
   436                 break; // continue loop
   430                 break; // continue loop
   437         }
   431         }
   438     }
   432     }
   439     fprintf(stderr, "SDL_WaitEvent error: %s\n", SDL_GetError());
   433     throw SDLTermError(std::string("SDL_WaitEvent: ") + SDL_GetError());
   440     throw std::exception();
       
   441 }
   434 }
   442 
   435 
   443 
   436 
   444 const char *Terminal::_translate_keyname(SDLKey sym)
   437 const char *Terminal::_translate_keyname(SDLKey sym)
   445 {
   438 {