sdlterm/src/sdlterm.h
changeset 52 50a1857557da
parent 51 dce7325109c1
child 53 c4263588b716
equal deleted inserted replaced
51:dce7325109c1 52:50a1857557da
     3 
     3 
     4 #include <map>
     4 #include <map>
     5 #include <vector>
     5 #include <vector>
     6 
     6 
     7 
     7 
     8 enum class Style: Uint16
     8 namespace Style
     9 {
     9 {
    10     BOLD      = 1 << 0,  // bold font
    10     const int BOLD      = 1 << 0;  // bold font
    11     UNDERLINE = 1 << 1,  // underline text
    11     const int UNDERLINE = 1 << 1;  // underline text
    12     STANDOUT  = 1 << 2,  // inverse bg/fg
    12     const int STANDOUT  = 1 << 2;  // inverse bg/fg
    13     BLINK     = 1 << 3,  // blinking
    13     const int BLINK     = 1 << 3;  // blinking
    14 };
    14 };
    15 
    15 
    16 
    16 
    17 class ColorMap
    17 class ColorMap
    18 {
    18 {
    42 
    42 
    43 
    43 
    44 class GlyphCache
    44 class GlyphCache
    45 {
    45 {
    46 public:
    46 public:
    47     SDL_Surface *lookup_glyph(Uint16 ch);
    47     // lookup glyph in cache, id is combined char and attr
    48     void put_glyph(Uint16 ch, SDL_Surface *srf);
    48     SDL_Surface *lookup_glyph(Uint64 id);
       
    49     void put_glyph(Uint64 id, SDL_Surface *srf);
    49     void flush();
    50     void flush();
    50 
    51 
    51 private:
    52 private:
    52     std::map<Uint16, SDL_Surface*> _glyph_map;
    53     std::map<Uint64, SDL_Surface*> _glyph_map;
    53 };
    54 };
    54 
    55 
    55 
    56 
    56 class GlyphRenderer
    57 class GlyphRenderer
    57 {
    58 {
    61 
    62 
    62     void open_font(const char *fname_regular, const char *fname_bold, int ptsize);
    63     void open_font(const char *fname_regular, const char *fname_bold, int ptsize);
    63     void close_font();
    64     void close_font();
    64 
    65 
    65     // do not free surface returned!
    66     // do not free surface returned!
    66     SDL_Surface *render_cell(Uint16 ch, Uint16 attr);
    67     SDL_Surface *render_cell(Uint32 ch, Uint32 attr);
    67 
    68 
    68     int get_cell_width() { return _cell_width; };
    69     int get_cell_width() { return _cell_width; };
    69     int get_cell_height() { return _cell_height; };
    70     int get_cell_height() { return _cell_height; };
    70 
    71 
    71 private:
    72 private:
    73     TTF_Font *_font_bold;
    74     TTF_Font *_font_bold;
    74     int _cell_width;
    75     int _cell_width;
    75     int _cell_height;
    76     int _cell_height;
    76     GlyphCache _cache;
    77     GlyphCache _cache;
    77     ColorMap _colormap;
    78     ColorMap _colormap;
    78 };
    79 
    79 
    80     void _render_glyph(SDL_Surface *cell_surface, TTF_Font *font, Uint32 ch,
    80 
    81             SDL_Color fgcolor, SDL_Color bgcolor);
       
    82 };
       
    83 
       
    84 
       
    85 /* One cell of terminal window
       
    86  *
       
    87  * Each cell contains one character, its color and attributes.
       
    88  * Character is encoded in 32bit unicode.
       
    89  * Other 32 bits are attributes:
       
    90  *   0-7   (8b) - foreground color index
       
    91  *   8-15  (8b) - background color index
       
    92  *   16-23 (8b) - RESERVED for alpha channel
       
    93  *   24    (1b) - bold font
       
    94  *   25    (1b) - underline
       
    95  *   26    (1b) - standout (swap fg/bg color)
       
    96  *   27    (1b) - blink
       
    97  *   28-31 (4b) - RESERVED
       
    98  */
    81 struct TerminalCell
    99 struct TerminalCell
    82 {
   100 {
    83     Uint16 ch;
   101     Uint32 ch;
    84     Uint16 attr;
   102     Uint32 attr;
    85     bool operator !=(const TerminalCell &rhs) const { return ch != rhs.ch || attr != rhs.attr; };
   103     bool operator !=(const TerminalCell &rhs) const { return ch != rhs.ch || attr != rhs.attr; };
    86 };
   104 };
    87 
   105 
    88 
   106 
    89 class TerminalScreen
   107 class TerminalScreen
    97 
   115 
    98     void select_font(const char *fname_regular, const char *fname_bold, int ptsize);
   116     void select_font(const char *fname_regular, const char *fname_bold, int ptsize);
    99     void resize(int pxwidth, int pxheight);
   117     void resize(int pxwidth, int pxheight);
   100 
   118 
   101     void erase();
   119     void erase();
   102     void putch(int x, int y, Uint16 ch, Uint16 attr);
   120     void putch(int x, int y, Uint32 ch, Uint32 attr);
   103     void commit();
   121     void commit();
   104 
   122 
   105     int get_width() { return _width; };
   123     int get_width() { return _width; };
   106     int get_height() { return _height; };
   124     int get_height() { return _height; };
   107     int get_cell_width() { return _cell_width; };
   125     int get_cell_width() { return _cell_width; };
   132     union
   150     union
   133     {
   151     {
   134         struct
   152         struct
   135         {
   153         {
   136             char keyname[10];
   154             char keyname[10];
   137             Uint16 unicode;
   155             Uint32 unicode;
   138         } key;
   156         } key;
   139         struct
   157         struct
   140         {
   158         {
   141             int x, y;
   159             int x, y;
   142             int button;
   160             int button;
   154     void select_font(const char *fname_regular, const char *fname_bold, int ptsize)
   172     void select_font(const char *fname_regular, const char *fname_bold, int ptsize)
   155         { _screen.select_font(fname_regular, fname_bold, ptsize); };
   173         { _screen.select_font(fname_regular, fname_bold, ptsize); };
   156     void resize(int pxwidth, int pxheight) { _screen.resize(pxwidth, pxheight); };
   174     void resize(int pxwidth, int pxheight) { _screen.resize(pxwidth, pxheight); };
   157 
   175 
   158     void erase() { _screen.erase(); };
   176     void erase() { _screen.erase(); };
   159     void putch(int x, int y, Uint16 ch) { _screen.putch(x, y, ch, _attr); };
   177     void putch(int x, int y, Uint32 ch) { _screen.putch(x, y, ch, _attr); };
   160     void commit() { _screen.commit(); };
   178     void commit() { _screen.commit(); };
   161 
   179 
   162     Uint16 prepare_attr(Uint8 fg, Uint8 bg, Uint8 style) { return fg | bg << 4 | style << 8; };
   180     Uint32 prepare_attr(Uint8 fg, Uint8 bg, Uint8 style) { return (Uint32)fg | (Uint32)bg << 8 | (Uint32)style << 24; };
   163     void set_attr(Uint16 value) { _attr = value; };
   181     void set_attr(Uint32 value) { _attr = value; };
   164 
   182 
   165     void set_cursor(int x, int y) { _cursor_x = x; _cursor_y = y; };
   183     void set_cursor(int x, int y) { _cursor_x = x; _cursor_y = y; };
   166     void show_cursor(bool visible) { _cursor_visible = visible; };
   184     void show_cursor(bool visible) { _cursor_visible = visible; };
   167 
   185 
   168     void get_next_event(Event &event);
   186     void get_next_event(Event &event);
   170     int get_width() { return _screen.get_width(); };
   188     int get_width() { return _screen.get_width(); };
   171     int get_height() { return _screen.get_height(); };
   189     int get_height() { return _screen.get_height(); };
   172 
   190 
   173 private:
   191 private:
   174     TerminalScreen _screen;
   192     TerminalScreen _screen;
   175     Uint16 _attr;
   193     Uint32 _attr;
   176     int _cursor_x;
   194     int _cursor_x;
   177     int _cursor_y;
   195     int _cursor_y;
   178     bool _cursor_visible;
   196     bool _cursor_visible;
   179 
   197 
   180     int _mousemove_last_x;
   198     int _mousemove_last_x;