sdlterm/src/sdlterm.h
changeset 50 c5b8b9d2da95
parent 49 1611c462c3e3
child 51 dce7325109c1
equal deleted inserted replaced
49:1611c462c3e3 50:c5b8b9d2da95
     1 #include "SDL.h"
     1 #include "SDL.h"
     2 #include "SDL_ttf.h"
     2 #include "SDL_ttf.h"
     3 
     3 
     4 #include <map>
     4 #include <map>
     5 #include <vector>
     5 #include <vector>
       
     6 
       
     7 
       
     8 enum class Style: Uint16
       
     9 {
       
    10 	BOLD      = 1 << 0,  // bold font
       
    11 	UNDERLINE = 1 << 1,  // underline text
       
    12 	STANDOUT  = 1 << 2,  // inverse bg/fg
       
    13 	BLINK     = 1 << 3,  // blinking
       
    14 };
       
    15 
       
    16 
       
    17 class ColorMap
       
    18 {
       
    19 private:
       
    20 	Uint8 _map[16][3] = {
       
    21 		{0,0,0},		// 0 - black
       
    22         {23,23,178},	// 1 - blue
       
    23         {23,178,23},    // 2 - green
       
    24         {23,178,178},   // 3 - cyan
       
    25         {178,23,23},	// 4 - red
       
    26         {178,23,178},	// 5 - magenta
       
    27         {178,103,23},	// 6 - brown
       
    28         {178,178,178},	// 7 - light gray
       
    29         {104,104,104},	// 8 - gray
       
    30         {84,84,255},	// 9 - light blue
       
    31         {84,255,84},	// 10 - light green
       
    32         {84,255,255},	// 11 - light cyan
       
    33         {255,84,84},	// 12 - light red
       
    34         {255,84,255},	// 13 - light magenta
       
    35         {255,255,84},   // 14 - yellow
       
    36         {255,255,255},  // 15 - white
       
    37 	};
       
    38 
       
    39 public:
       
    40     void index_to_rgb(int index, SDL_Color &color);
       
    41 };
     6 
    42 
     7 
    43 
     8 class GlyphCache
    44 class GlyphCache
     9 {
    45 {
    10 public:
    46 public:
    25 
    61 
    26 	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);
    27 	void close_font();
    63 	void close_font();
    28 
    64 
    29 	// do not free surface returned!
    65 	// do not free surface returned!
    30 	SDL_Surface *render_glyph(Uint16 ch);
    66 	SDL_Surface *render_cell(Uint16 ch, Uint16 attr);
    31 
    67 
    32 	int get_cell_width() { return _cell_width; };
    68 	int get_cell_width() { return _cell_width; };
    33 	int get_cell_height() { return _cell_height; };
    69 	int get_cell_height() { return _cell_height; };
    34 
    70 
    35 private:
    71 private:
    36 	TTF_Font *_font_regular;
    72 	TTF_Font *_font_regular;
    37 	TTF_Font *_font_bold;
    73 	TTF_Font *_font_bold;
    38 	int _cell_width;
    74 	int _cell_width;
    39 	int _cell_height;
    75 	int _cell_height;
    40 	GlyphCache _cache;
    76 	GlyphCache _cache;
       
    77 	ColorMap _colormap;
    41 };
    78 };
    42 
    79 
    43 
    80 
    44 struct TerminalCell
    81 struct TerminalCell
    45 {
    82 {
    50 
    87 
    51 
    88 
    52 class TerminalScreen
    89 class TerminalScreen
    53 {
    90 {
    54 public:
    91 public:
    55 	TerminalScreen(): _screen_surface(NULL), _render() {};
    92 	TerminalScreen():
       
    93 		_screen_surface(NULL), _cells_front(0), _cells_back(0), _render(),
       
    94 		_pixel_width(0), _pixel_height(0) {};
    56 	~TerminalScreen() {};
    95 	~TerminalScreen() {};
    57 
    96 
    58 	void select_font(const char *fname_regular, const char *fname_bold, int ptsize);
    97 	void select_font(const char *fname_regular, const char *fname_bold, int ptsize);
    59 	void resize(int pxwidth, int pxheight);
    98 	void resize(int pxwidth, int pxheight);
    60 
    99 
   117 
   156 
   118 	void erase() { _screen.erase(); };
   157 	void erase() { _screen.erase(); };
   119 	void putch(int x, int y, Uint16 ch) { _screen.putch(x, y, ch, _attr); };
   158 	void putch(int x, int y, Uint16 ch) { _screen.putch(x, y, ch, _attr); };
   120 	void commit() { _screen.commit(); };
   159 	void commit() { _screen.commit(); };
   121 
   160 
   122 	Uint16 prepare_attr(Uint8 fg, Uint8 bg, Uint8 style) { return fg | bg << 8 | style << 16; };
   161 	Uint16 prepare_attr(Uint8 fg, Uint8 bg, Uint8 style) { return fg | bg << 4 | style << 8; };
   123 	void set_attr(Uint16 value) { _attr = value; };
   162 	void set_attr(Uint16 value) { _attr = value; };
   124 
   163 
   125 	void set_cursor(int x, int y) { _cursor_x = x; _cursor_y = y; };
   164 	void set_cursor(int x, int y) { _cursor_x = x; _cursor_y = y; };
   126 	void show_cursor(bool visible) { _cursor_visible = visible; };
   165 	void show_cursor(bool visible) { _cursor_visible = visible; };
   127 
   166