That Terminal
A terminal emulator designed for video making purposes.
window.hh
Go to the documentation of this file.
1 #ifndef bqtRenderingScreenHH
2 #define bqtRenderingScreenHH
7 #include <cstdint>
8 #include <vector>
9 #include <tuple>
10 #include <cstring>
11 
12 #include "cset.hh"
13 #include "cell.hh"
14 
16 struct Window
17 {
18  std::vector<Cell> cells;
19  std::size_t xsize;
20  std::size_t ysize;
21  std::size_t cursx=0;
22  std::size_t cursy=0;
23  bool inverse = false;
24  bool cursorvis = true;
25  unsigned cursorcolor = 0xFFFFFF;
26  unsigned mousecolor1 = 0xFFFFFF;
27  unsigned mousecolor2 = 0xFFFFFF;
28  unsigned mouseselectcolor = 0xFFFFFF;
29  Cell blank {};
30 private:
31  std::size_t lastcursx, lastcursy;
32  unsigned lasttimer=0;
33 public:
35  Window(std::size_t xs, std::size_t ys) : cells(xs*ys), xsize(xs), ysize(ys)
36  {
37  Dirtify();
38  }
39 
41  void FillBox(std::size_t x, std::size_t y, std::size_t width, std::size_t height)
42  {
43  for(std::size_t h=0; h<height; ++h)
44  for(std::size_t w=0; w<width; ++w)
45  {
46  auto tx = x+w, ty = y+h;
47  PutCh(tx, ty, blank);
48  }
49  }
53  void FillBox(std::size_t x, std::size_t y, std::size_t width, std::size_t height, Cell with)
54  {
55  for(std::size_t h=0; h<height; ++h)
56  for(std::size_t w=0; w<width; ++w)
57  {
58  auto tx = x+w, ty = y+h;
59  if(with.ch != blank.ch || !cells[ty*xsize+tx].protect)
60  PutCh(tx, ty, with);
61  }
62  }
71  void CopyText(std::size_t tgtx,std::size_t tgty, std::size_t srcx,std::size_t srcy,
72  std::size_t width,std::size_t height)
73  {
74  auto hcopy_oneline = [&](std::size_t ty, std::size_t sy)
75  {
76  if(tgtx < srcx)
77  for(std::size_t w=0; w<width; ++w)
78  PutCh(tgtx+w, ty, cells[sy*xsize+(srcx+w)]);
79  else
80  for(std::size_t w=width; w-- > 0; )
81  PutCh(tgtx+w, ty, cells[sy*xsize+(srcx+w)]);
82  };
83  if(tgty < srcy)
84  for(std::size_t h=0; h<height; ++h)
85  hcopy_oneline(tgty+h, srcy+h);
86  else
87  for(std::size_t h=height; h-- > 0; )
88  hcopy_oneline(tgty+h, srcy+h);
89  }
90 
92  void Dirtify(std::size_t x, std::size_t y)
93  {
94  auto& tgt = cells[y*xsize+x];
95  // Write an invalid character, to make sure it gets properly
96  // cleared when a valid character gets written instead.
97  // This glyph must _not_ register as doublewidth.
98  tgt.ch = 0xFFFE;
99  tgt.dirty = true;
100  }
104  void Dirtify();
105 
109  void PutCh(std::size_t x, std::size_t y, const Cell& c)
110  {
111  auto& tgt = cells[y*xsize+x];
112  if(tgt != c)
113  {
114  tgt = c;
115  tgt.dirty = true;
116  }
117  }
118 
123  void PutCh(std::size_t x, std::size_t y, char32_t c, int cset = 0)
124  {
125  /*
126  cset options:
127  0 = ascii
128  1 = dec graphics
129  */
130  Cell ch = blank;
131  if(cset)
132  {
133  c = TranslateCSet(c, cset);
134  }
135  ch.ch = c;
136  ch.render_size = cells[y*xsize+x].render_size;
137  /*if(c != U' ')
138  {
139  fprintf(stderr, "Ch at (%zu,%zu): <%c>\n", x,y, int(c));
140  }*/
141  PutCh(x, y, ch);
142  }
146  void PutCh_KeepAttr(std::size_t x, std::size_t y, char32_t c, int cset = 0)
147  {
148  auto& cell = cells[y*xsize+x];
149  Cell temp = cell;
150  if(cset)
151  c = TranslateCSet(c, cset);
152  if(temp != cell)
153  {
154  cell = temp;
155  cell.dirty = true;
156  }
157  }
159  void PutCh_KeepChar(std::size_t x, std::size_t y, const Cell& c)
160  {
161  auto& cell = cells[y*xsize+x];
162  Cell temp = c;
163  temp.ch = cell.ch;
164  if(temp != cell)
165  {
166  cell = temp;
167  cell.dirty = true;
168  }
169  }
170 
179  void Render(std::size_t fx, std::size_t fy, std::uint32_t* pixels);
180 
186  void Resize(std::size_t newsx, std::size_t newsy);
187 
193  void LineSetRenderSize(unsigned val);
194 };
195 
196 #endif
Defines Cell, the structure that represents a character on screen.
char32_t TranslateCSet(char32_t c, int cset)
Definition: cset.cc:10
Defines TranslateCSet()
Definition: cell.hh:39
unsigned char render_size
bit 12-13 0=normal,1=doublewidth,2=doublewidth+topline,3=doublewidth+bottomline
Definition: cell.hh:57
bool dirty
bit 31 (placed last for efficient access)
Definition: cell.hh:70
char32_t ch
the character symbol (unicode codepoint)
Definition: cell.hh:44
Definition: window.hh:17
unsigned mousecolor2
Ignored.
Definition: window.hh:27
void PutCh(std::size_t x, std::size_t y, const Cell &c)
Definition: window.hh:109
std::size_t cursx
Cursor location (horizontal)
Definition: window.hh:21
void Dirtify()
Definition: window.cc:285
Window(std::size_t xs, std::size_t ys)
Definition: window.hh:35
std::size_t xsize
Width of window in cells.
Definition: window.hh:19
std::size_t cursy
Cursor location (vertical)
Definition: window.hh:22
void Render(std::size_t fx, std::size_t fy, std::uint32_t *pixels)
Definition: window.cc:83
unsigned cursorcolor
Color of cursor.
Definition: window.hh:25
void PutCh(std::size_t x, std::size_t y, char32_t c, int cset=0)
Definition: window.hh:123
void CopyText(std::size_t tgtx, std::size_t tgty, std::size_t srcx, std::size_t srcy, std::size_t width, std::size_t height)
Definition: window.hh:71
unsigned mousecolor1
Ignored.
Definition: window.hh:26
void LineSetRenderSize(unsigned val)
Definition: window.cc:292
void PutCh_KeepChar(std::size_t x, std::size_t y, const Cell &c)
Definition: window.hh:159
bool cursorvis
Whether cursor is visible.
Definition: window.hh:24
bool inverse
Whether screen-wide inverse effect is in effect.
Definition: window.hh:23
void Resize(std::size_t newsx, std::size_t newsy)
Definition: window.cc:270
Cell blank
The current "blank" cell. It is used as the model cell for inserting empty rows.
Definition: window.hh:29
std::vector< Cell > cells
Storage for cells. Indexed row-first. Size: xsize*ysize *‍/.
Definition: window.hh:18
std::size_t ysize
Height of window in cells.
Definition: window.hh:20
void PutCh_KeepAttr(std::size_t x, std::size_t y, char32_t c, int cset=0)
Definition: window.hh:146
void Dirtify(std::size_t x, std::size_t y)
Definition: window.hh:92
unsigned mouseselectcolor
Ignored.
Definition: window.hh:28
void FillBox(std::size_t x, std::size_t y, std::size_t width, std::size_t height, Cell with)
Definition: window.hh:53
void FillBox(std::size_t x, std::size_t y, std::size_t width, std::size_t height)
Definition: window.hh:41