sdlterm/src/sdlterm.h
changeset 48 1f00e90fd72a
parent 47 537d7c6b48a2
child 49 1611c462c3e3
equal deleted inserted replaced
47:537d7c6b48a2 48:1f00e90fd72a
     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 class GlyphCache
       
     9 {
       
    10 public:
       
    11 	SDL_Surface *lookup_glyph(Uint16 ch);
       
    12 	void put_glyph(Uint16 ch, SDL_Surface *srf);
       
    13 	void flush();
       
    14 
       
    15 private:
       
    16 	std::map<Uint16, SDL_Surface*> _glyph_map;
       
    17 };
     6 
    18 
     7 
    19 
     8 class GlyphRenderer
    20 class GlyphRenderer
     9 {
    21 {
    10 public:
    22 public:
    12 	~GlyphRenderer();
    24 	~GlyphRenderer();
    13 
    25 
    14 	void open_font(const char *fname_regular, const char *fname_bold, int ptsize);
    26 	void open_font(const char *fname_regular, const char *fname_bold, int ptsize);
    15 	void close_font();
    27 	void close_font();
    16 
    28 
       
    29 	// do not free surface returned!
    17 	SDL_Surface *render_glyph(Uint16 ch);
    30 	SDL_Surface *render_glyph(Uint16 ch);
    18 
    31 
    19 	int get_cell_width() { return _cell_width; };
    32 	int get_cell_width() { return _cell_width; };
    20 	int get_cell_height() { return _cell_height; };
    33 	int get_cell_height() { return _cell_height; };
    21 
    34 
    22 private:
    35 private:
    23 	TTF_Font *_font_regular;
    36 	TTF_Font *_font_regular;
    24 	TTF_Font *_font_bold;
    37 	TTF_Font *_font_bold;
    25 	int _cell_width;
    38 	int _cell_width;
    26 	int _cell_height;
    39 	int _cell_height;
    27 	std::map<Uint16, SDL_Surface*> _cache;
    40 	GlyphCache _cache;
    28 };
    41 };
    29 
    42 
    30 
    43 
    31 struct TerminalCell
    44 struct TerminalCell
    32 {
    45 {
    46 
    59 
    47 	void erase();
    60 	void erase();
    48 	void putch(int x, int y, Uint16 ch, Uint16 attr);
    61 	void putch(int x, int y, Uint16 ch, Uint16 attr);
    49 	void commit();
    62 	void commit();
    50 
    63 
       
    64 	int get_width() { return _width; };
       
    65 	int get_height() { return _height; };
       
    66 	int get_cell_width() { return _cell_width; };
       
    67 	int get_cell_height() { return _cell_height; };
       
    68 
    51 private:
    69 private:
    52 	SDL_Surface *_screen_surface;
    70 	SDL_Surface *_screen_surface;
    53 	TerminalCell *_cells;
    71 	TerminalCell *_cells;
    54 	GlyphRenderer _render;
    72 	GlyphRenderer _render;
    55 
    73 
    62 
    80 
    63 	void _reset_cells();
    81 	void _reset_cells();
    64 };
    82 };
    65 
    83 
    66 
    84 
    67 enum class EventType : Uint8
    85 struct Event
    68 {
    86 {
    69 	quit,
    87 	enum { QUIT, RESIZE, KEYPRESS, MOUSEDOWN, MOUSEUP, MOUSEMOVE, MOUSEWHEEL };
    70 	resize,
    88 	int type;
    71 	keypress,
       
    72 	mousedown,
       
    73 	mouseup,
       
    74 	mousemove,
       
    75 	mousewheel
       
    76 };
       
    77 
    89 
    78 
    90 	union
    79 struct WindowEvent
    91 	{
    80 {
    92 		struct
    81 	EventType type;
    93 		{
    82 };
    94 			char keyname[10];
    83 
    95 			Uint16 unicode;
    84 
    96 		} key;
    85 struct KeyboardEvent
    97 		struct
    86 {
    98 		{
    87 	EventType type;
    99 			int x, y;
    88 };
   100 			int button;
    89 
   101 		} mouse;
    90 
   102 	};
    91 struct MouseEvent
       
    92 {
       
    93 	EventType type;
       
    94 };
       
    95 
       
    96 
       
    97 union Event
       
    98 {
       
    99 	EventType type;
       
   100 	WindowEvent window;
       
   101 	KeyboardEvent key;
       
   102 	MouseEvent mouse;
       
   103 };
   103 };
   104 
   104 
   105 
   105 
   106 class Terminal
   106 class Terminal
   107 {
   107 {
   123 	void set_cursor(int x, int y) { _cursor_x = x; _cursor_y = y; };
   123 	void set_cursor(int x, int y) { _cursor_x = x; _cursor_y = y; };
   124 	void show_cursor(bool visible) { _cursor_visible = visible; };
   124 	void show_cursor(bool visible) { _cursor_visible = visible; };
   125 
   125 
   126 	void get_next_event(Event &event);
   126 	void get_next_event(Event &event);
   127 
   127 
       
   128 	int get_width() { return _screen.get_width(); };
       
   129 	int get_height() { return _screen.get_height(); };
       
   130 
   128 private:
   131 private:
   129 	TerminalScreen _screen;
   132 	TerminalScreen _screen;
   130 	Uint16 _attr;
   133 	Uint16 _attr;
   131 	int _cursor_x;
   134 	int _cursor_x;
   132 	int _cursor_y;
   135 	int _cursor_y;
   133 	bool _cursor_visible;
   136 	bool _cursor_visible;
       
   137 
       
   138 	const char *_translate_keyname(SDLKey sym);
   134 };
   139 };
   135 
   140