sdlterm/src/sdlterm.h
changeset 51 dce7325109c1
parent 50 c5b8b9d2da95
child 52 50a1857557da
equal deleted inserted replaced
50:c5b8b9d2da95 51:dce7325109c1
     5 #include <vector>
     5 #include <vector>
     6 
     6 
     7 
     7 
     8 enum class Style: Uint16
     8 enum class Style: Uint16
     9 {
     9 {
    10 	BOLD      = 1 << 0,  // bold font
    10     BOLD      = 1 << 0,  // bold font
    11 	UNDERLINE = 1 << 1,  // underline text
    11     UNDERLINE = 1 << 1,  // underline text
    12 	STANDOUT  = 1 << 2,  // inverse bg/fg
    12     STANDOUT  = 1 << 2,  // inverse bg/fg
    13 	BLINK     = 1 << 3,  // blinking
    13     BLINK     = 1 << 3,  // blinking
    14 };
    14 };
    15 
    15 
    16 
    16 
    17 class ColorMap
    17 class ColorMap
    18 {
    18 {
    19 private:
    19 private:
    20 	Uint8 _map[16][3] = {
    20     Uint8 _map[16][3] = {
    21 		{0,0,0},		// 0 - black
    21         {0,0,0},        // 0 - black
    22         {23,23,178},	// 1 - blue
    22         {23,23,178},    // 1 - blue
    23         {23,178,23},    // 2 - green
    23         {23,178,23},    // 2 - green
    24         {23,178,178},   // 3 - cyan
    24         {23,178,178},   // 3 - cyan
    25         {178,23,23},	// 4 - red
    25         {178,23,23},    // 4 - red
    26         {178,23,178},	// 5 - magenta
    26         {178,23,178},   // 5 - magenta
    27         {178,103,23},	// 6 - brown
    27         {178,103,23},   // 6 - brown
    28         {178,178,178},	// 7 - light gray
    28         {178,178,178},  // 7 - light gray
    29         {104,104,104},	// 8 - gray
    29         {104,104,104},  // 8 - gray
    30         {84,84,255},	// 9 - light blue
    30         {84,84,255},    // 9 - light blue
    31         {84,255,84},	// 10 - light green
    31         {84,255,84},    // 10 - light green
    32         {84,255,255},	// 11 - light cyan
    32         {84,255,255},   // 11 - light cyan
    33         {255,84,84},	// 12 - light red
    33         {255,84,84},    // 12 - light red
    34         {255,84,255},	// 13 - light magenta
    34         {255,84,255},   // 13 - light magenta
    35         {255,255,84},   // 14 - yellow
    35         {255,255,84},   // 14 - yellow
    36         {255,255,255},  // 15 - white
    36         {255,255,255},  // 15 - white
    37 	};
    37     };
    38 
    38 
    39 public:
    39 public:
    40     void index_to_rgb(int index, SDL_Color &color);
    40     void index_to_rgb(int index, SDL_Color &color);
    41 };
    41 };
    42 
    42 
    43 
    43 
    44 class GlyphCache
    44 class GlyphCache
    45 {
    45 {
    46 public:
    46 public:
    47 	SDL_Surface *lookup_glyph(Uint16 ch);
    47     SDL_Surface *lookup_glyph(Uint16 ch);
    48 	void put_glyph(Uint16 ch, SDL_Surface *srf);
    48     void put_glyph(Uint16 ch, SDL_Surface *srf);
    49 	void flush();
    49     void flush();
    50 
    50 
    51 private:
    51 private:
    52 	std::map<Uint16, SDL_Surface*> _glyph_map;
    52     std::map<Uint16, SDL_Surface*> _glyph_map;
    53 };
    53 };
    54 
    54 
    55 
    55 
    56 class GlyphRenderer
    56 class GlyphRenderer
    57 {
    57 {
    58 public:
    58 public:
    59 	GlyphRenderer();
    59     GlyphRenderer();
    60 	~GlyphRenderer();
    60     ~GlyphRenderer();
    61 
    61 
    62 	void open_font(const char *fname_regular, const char *fname_bold, int ptsize);
    62     void open_font(const char *fname_regular, const char *fname_bold, int ptsize);
    63 	void close_font();
    63     void close_font();
    64 
    64 
    65 	// do not free surface returned!
    65     // do not free surface returned!
    66 	SDL_Surface *render_cell(Uint16 ch, Uint16 attr);
    66     SDL_Surface *render_cell(Uint16 ch, Uint16 attr);
    67 
    67 
    68 	int get_cell_width() { return _cell_width; };
    68     int get_cell_width() { return _cell_width; };
    69 	int get_cell_height() { return _cell_height; };
    69     int get_cell_height() { return _cell_height; };
    70 
    70 
    71 private:
    71 private:
    72 	TTF_Font *_font_regular;
    72     TTF_Font *_font_regular;
    73 	TTF_Font *_font_bold;
    73     TTF_Font *_font_bold;
    74 	int _cell_width;
    74     int _cell_width;
    75 	int _cell_height;
    75     int _cell_height;
    76 	GlyphCache _cache;
    76     GlyphCache _cache;
    77 	ColorMap _colormap;
    77     ColorMap _colormap;
    78 };
    78 };
    79 
    79 
    80 
    80 
    81 struct TerminalCell
    81 struct TerminalCell
    82 {
    82 {
    83 	Uint16 ch;
    83     Uint16 ch;
    84 	Uint16 attr;
    84     Uint16 attr;
    85 	bool operator !=(const TerminalCell &rhs) const { return ch != rhs.ch || attr != rhs.attr; };
    85     bool operator !=(const TerminalCell &rhs) const { return ch != rhs.ch || attr != rhs.attr; };
    86 };
    86 };
    87 
    87 
    88 
    88 
    89 class TerminalScreen
    89 class TerminalScreen
    90 {
    90 {
    91 public:
    91 public:
    92 	TerminalScreen():
    92     TerminalScreen():
    93 		_screen_surface(NULL), _cells_front(0), _cells_back(0), _render(),
    93         _screen_surface(NULL), _cells_front(0), _cells_back(0), _render(),
    94 		_pixel_width(0), _pixel_height(0) {};
    94         _pixel_width(0), _pixel_height(0), _width(0), _height(0),
    95 	~TerminalScreen() {};
    95         _cell_width(0), _cell_height(0) {};
       
    96     ~TerminalScreen() {};
    96 
    97 
    97 	void select_font(const char *fname_regular, const char *fname_bold, int ptsize);
    98     void select_font(const char *fname_regular, const char *fname_bold, int ptsize);
    98 	void resize(int pxwidth, int pxheight);
    99     void resize(int pxwidth, int pxheight);
    99 
   100 
   100 	void erase();
   101     void erase();
   101 	void putch(int x, int y, Uint16 ch, Uint16 attr);
   102     void putch(int x, int y, Uint16 ch, Uint16 attr);
   102 	void commit();
   103     void commit();
   103 
   104 
   104 	int get_width() { return _width; };
   105     int get_width() { return _width; };
   105 	int get_height() { return _height; };
   106     int get_height() { return _height; };
   106 	int get_cell_width() { return _cell_width; };
   107     int get_cell_width() { return _cell_width; };
   107 	int get_cell_height() { return _cell_height; };
   108     int get_cell_height() { return _cell_height; };
   108 
   109 
   109 private:
   110 private:
   110 	SDL_Surface *_screen_surface;
   111     SDL_Surface *_screen_surface;
   111 	std::vector<TerminalCell> _cells_front;
   112     std::vector<TerminalCell> _cells_front;
   112 	std::vector<TerminalCell> _cells_back;
   113     std::vector<TerminalCell> _cells_back;
   113 	GlyphRenderer _render;
   114     GlyphRenderer _render;
   114 
   115 
   115 	int _pixel_width;  // terminal window width in pixels
   116     int _pixel_width;  // terminal window width in pixels
   116 	int _pixel_height;
   117     int _pixel_height;
   117 	int _width;  // width in characters
   118     int _width;  // width in characters
   118 	int _height; // height in characters
   119     int _height; // height in characters
   119 	int _cell_width;  // character cell width in pixels
   120     int _cell_width;  // character cell width in pixels
   120 	int _cell_height;
   121     int _cell_height;
   121 
   122 
   122 	void _reset_cells();
   123     void _reset_cells();
   123 };
   124 };
   124 
   125 
   125 
   126 
   126 struct Event
   127 struct Event
   127 {
   128 {
   128 	enum { QUIT, RESIZE, KEYPRESS, MOUSEDOWN, MOUSEUP, MOUSEMOVE, MOUSEWHEEL };
   129     enum { QUIT, RESIZE, KEYPRESS, MOUSEDOWN, MOUSEUP, MOUSEMOVE, MOUSEWHEEL };
   129 	int type;
   130     int type;
   130 
   131 
   131 	union
   132     union
   132 	{
   133     {
   133 		struct
   134         struct
   134 		{
   135         {
   135 			char keyname[10];
   136             char keyname[10];
   136 			Uint16 unicode;
   137             Uint16 unicode;
   137 		} key;
   138         } key;
   138 		struct
   139         struct
   139 		{
   140         {
   140 			int x, y;
   141             int x, y;
   141 			int button;
   142             int button;
   142 		} mouse;
   143         } mouse;
   143 	};
   144     };
   144 };
   145 };
   145 
   146 
   146 
   147 
   147 class Terminal
   148 class Terminal
   148 {
   149 {
   149 public:
   150 public:
   150 	Terminal();
   151     Terminal();
   151 	~Terminal();
   152     ~Terminal();
   152 
   153 
   153 	void select_font(const char *fname_regular, const char *fname_bold, int ptsize)
   154     void select_font(const char *fname_regular, const char *fname_bold, int ptsize)
   154 		{ _screen.select_font(fname_regular, fname_bold, ptsize); };
   155         { _screen.select_font(fname_regular, fname_bold, ptsize); };
   155 	void resize(int pxwidth, int pxheight) { _screen.resize(pxwidth, pxheight); };
   156     void resize(int pxwidth, int pxheight) { _screen.resize(pxwidth, pxheight); };
   156 
   157 
   157 	void erase() { _screen.erase(); };
   158     void erase() { _screen.erase(); };
   158 	void putch(int x, int y, Uint16 ch) { _screen.putch(x, y, ch, _attr); };
   159     void putch(int x, int y, Uint16 ch) { _screen.putch(x, y, ch, _attr); };
   159 	void commit() { _screen.commit(); };
   160     void commit() { _screen.commit(); };
   160 
   161 
   161 	Uint16 prepare_attr(Uint8 fg, Uint8 bg, Uint8 style) { return fg | bg << 4 | style << 8; };
   162     Uint16 prepare_attr(Uint8 fg, Uint8 bg, Uint8 style) { return fg | bg << 4 | style << 8; };
   162 	void set_attr(Uint16 value) { _attr = value; };
   163     void set_attr(Uint16 value) { _attr = value; };
   163 
   164 
   164 	void set_cursor(int x, int y) { _cursor_x = x; _cursor_y = y; };
   165     void set_cursor(int x, int y) { _cursor_x = x; _cursor_y = y; };
   165 	void show_cursor(bool visible) { _cursor_visible = visible; };
   166     void show_cursor(bool visible) { _cursor_visible = visible; };
   166 
   167 
   167 	void get_next_event(Event &event);
   168     void get_next_event(Event &event);
   168 
   169 
   169 	int get_width() { return _screen.get_width(); };
   170     int get_width() { return _screen.get_width(); };
   170 	int get_height() { return _screen.get_height(); };
   171     int get_height() { return _screen.get_height(); };
   171 
   172 
   172 private:
   173 private:
   173 	TerminalScreen _screen;
   174     TerminalScreen _screen;
   174 	Uint16 _attr;
   175     Uint16 _attr;
   175 	int _cursor_x;
   176     int _cursor_x;
   176 	int _cursor_y;
   177     int _cursor_y;
   177 	bool _cursor_visible;
   178     bool _cursor_visible;
   178 
   179 
   179 	int _mousemove_last_x;
   180     int _mousemove_last_x;
   180 	int _mousemove_last_y;
   181     int _mousemove_last_y;
   181 
   182 
   182 	const char *_translate_keyname(SDLKey sym);
   183     const char *_translate_keyname(SDLKey sym);
   183 };
   184 };
   184 
   185