sdlterm/src/sdlterm.h
changeset 47 537d7c6b48a2
child 48 1f00e90fd72a
equal deleted inserted replaced
46:2b43a7f38c34 47:537d7c6b48a2
       
     1 #include "SDL.h"
       
     2 #include "SDL_ttf.h"
       
     3 
       
     4 #include <map>
       
     5 #include <vector>
       
     6 
       
     7 
       
     8 class GlyphRenderer
       
     9 {
       
    10 public:
       
    11 	GlyphRenderer();
       
    12 	~GlyphRenderer();
       
    13 
       
    14 	void open_font(const char *fname_regular, const char *fname_bold, int ptsize);
       
    15 	void close_font();
       
    16 
       
    17 	SDL_Surface *render_glyph(Uint16 ch);
       
    18 
       
    19 	int get_cell_width() { return _cell_width; };
       
    20 	int get_cell_height() { return _cell_height; };
       
    21 
       
    22 private:
       
    23 	TTF_Font *_font_regular;
       
    24 	TTF_Font *_font_bold;
       
    25 	int _cell_width;
       
    26 	int _cell_height;
       
    27 	std::map<Uint16, SDL_Surface*> _cache;
       
    28 };
       
    29 
       
    30 
       
    31 struct TerminalCell
       
    32 {
       
    33 	Uint16 ch;
       
    34 	Uint16 attr;
       
    35 };
       
    36 
       
    37 
       
    38 class TerminalScreen
       
    39 {
       
    40 public:
       
    41 	TerminalScreen(): _screen_surface(NULL), _cells(NULL), _render() {};
       
    42 	~TerminalScreen();
       
    43 
       
    44 	void select_font(const char *fname_regular, const char *fname_bold, int ptsize);
       
    45 	void resize(int pxwidth, int pxheight);
       
    46 
       
    47 	void erase();
       
    48 	void putch(int x, int y, Uint16 ch, Uint16 attr);
       
    49 	void commit();
       
    50 
       
    51 private:
       
    52 	SDL_Surface *_screen_surface;
       
    53 	TerminalCell *_cells;
       
    54 	GlyphRenderer _render;
       
    55 
       
    56 	int _pixel_width;  // terminal window width in pixels
       
    57 	int _pixel_height;
       
    58 	int _width;  // width in characters
       
    59 	int _height; // height in characters
       
    60 	int _cell_width;  // character cell width in pixels
       
    61 	int _cell_height;
       
    62 
       
    63 	void _reset_cells();
       
    64 };
       
    65 
       
    66 
       
    67 enum class EventType : Uint8
       
    68 {
       
    69 	quit,
       
    70 	resize,
       
    71 	keypress,
       
    72 	mousedown,
       
    73 	mouseup,
       
    74 	mousemove,
       
    75 	mousewheel
       
    76 };
       
    77 
       
    78 
       
    79 struct WindowEvent
       
    80 {
       
    81 	EventType type;
       
    82 };
       
    83 
       
    84 
       
    85 struct KeyboardEvent
       
    86 {
       
    87 	EventType type;
       
    88 };
       
    89 
       
    90 
       
    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 };
       
   104 
       
   105 
       
   106 class Terminal
       
   107 {
       
   108 public:
       
   109 	Terminal();
       
   110 	~Terminal();
       
   111 
       
   112 	void select_font(const char *fname_regular, const char *fname_bold, int ptsize)
       
   113 		{ _screen.select_font(fname_regular, fname_bold, ptsize); };
       
   114 	void resize(int pxwidth, int pxheight) { _screen.resize(pxwidth, pxheight); };
       
   115 
       
   116 	void erase() { _screen.erase(); };
       
   117 	void putch(int x, int y, Uint16 ch) { _screen.putch(x, y, ch, _attr); };
       
   118 	void commit() { _screen.commit(); };
       
   119 
       
   120 	Uint16 prepare_attr(Uint8 fg, Uint8 bg, Uint8 style) { return fg | bg << 8 | style << 16; };
       
   121 	void set_attr(Uint16 value) { _attr = value; };
       
   122 
       
   123 	void set_cursor(int x, int y) { _cursor_x = x; _cursor_y = y; };
       
   124 	void show_cursor(bool visible) { _cursor_visible = visible; };
       
   125 
       
   126 	void get_next_event(Event &event);
       
   127 
       
   128 private:
       
   129 	TerminalScreen _screen;
       
   130 	Uint16 _attr;
       
   131 	int _cursor_x;
       
   132 	int _cursor_y;
       
   133 	bool _cursor_visible;
       
   134 };
       
   135