Skip to content

Commit baf26c8

Browse files
committed
ScreenHandler.h: Changing the screen buffer data structures from being 2D arrays to 1D arrays.
* Not saving as much as I did when I implemented the dirty-region tracking algorithm but could make it a bit more optimized. * With this change, the screenbuffers would not be spread out in different regions in memory but be rather contiguous, which is great for the cache. I believe these buffers fit comfortably in L2 or L3. * Fixing some variable name typos. Nothing serious.
1 parent 8b25721 commit baf26c8

File tree

1 file changed

+51
-46
lines changed

1 file changed

+51
-46
lines changed

screen/ScreenHandler.h

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@ namespace t8
5757
std::unique_ptr<Text> m_text;
5858

5959
// Draw from top to bottom.
60-
std::array<std::array<char, NC>, NR> screen_buffer, prev_screen_buffer;
61-
std::array<std::array<Color, NC>, NR> fg_color_buffer, prev_fg_color_buffer;
62-
std::array<std::array<Color, NC>, NR> bg_color_buffer, prev_bg_color_buffer;
63-
std::array<std::array<bool, NC>, NR> dirty_flag_buffer;
60+
std::array<char, NC*NR> screen_buffer, prev_screen_buffer;
61+
std::array<Color, NC*NR> fg_color_buffer, prev_fg_color_buffer;
62+
std::array<Color, NC*NR> bg_color_buffer, prev_bg_color_buffer;
63+
std::array<bool, NC*NR> dirty_flag_buffer;
6464
Color prev_clear_bg_color = Color::Default;
6565

66+
inline int index(int r, int c) const noexcept { return NC*r + c; }
67+
6668
std::string color2str(Color col) const
6769
{
6870
switch (col)
@@ -100,23 +102,19 @@ namespace t8
100102

101103
void clear()
102104
{
103-
for (auto& row : screen_buffer)
104-
for (auto& col : row)
105-
col = ' ';
106-
for (auto& row : fg_color_buffer)
107-
for (auto& col : row)
108-
col = Color::Default;
109-
for (auto& row : bg_color_buffer)
110-
for (auto& col : row)
111-
col = Color::Transparent;
112-
for (auto& row : dirty_flag_buffer)
113-
for (auto& col : row)
114-
col = false;
105+
for (auto& ch : screen_buffer)
106+
ch = ' ';
107+
for (auto& col : fg_color_buffer)
108+
col = Color::Default;
109+
for (auto& col : bg_color_buffer)
110+
col = Color::Transparent;
111+
for (auto& df : dirty_flag_buffer)
112+
df = false;
115113
}
116114

117115
bool test_empty(int r, int c) const
118116
{
119-
return screen_buffer[r][c] == ' ';
117+
return screen_buffer[index(r, c)] == ' ';
120118
}
121119

122120
// write_buffer using StringBox.
@@ -170,9 +168,10 @@ namespace t8
170168
int c_tot = c + ci;
171169
if (c_tot >= 0 && c_tot < NC)
172170
{
173-
auto& scr_ch = screen_buffer[r][c_tot];
174-
auto& scr_fg = fg_color_buffer[r][c_tot];
175-
auto& scr_bg = bg_color_buffer[r][c_tot];
171+
int idx = index(r, c_tot);
172+
auto& scr_ch = screen_buffer[idx];
173+
auto& scr_fg = fg_color_buffer[idx];
174+
auto& scr_bg = bg_color_buffer[idx];
176175
if (scr_ch == ' '
177176
&& scr_bg == Color::Transparent)
178177
{
@@ -203,8 +202,9 @@ namespace t8
203202
{
204203
if (box.is_inside(r, c))
205204
{
206-
if (bg_color_buffer[r][c] == from_bg_color)
207-
bg_color_buffer[r][c] = to_bg_color;
205+
auto& col = bg_color_buffer[index(r, c)];
206+
if (col == from_bg_color)
207+
col = to_bg_color;
208208
}
209209
}
210210
}
@@ -218,7 +218,7 @@ namespace t8
218218
{
219219
if (box.is_inside(r, c))
220220
{
221-
bg_color_buffer[r][c] = to_bg_color;
221+
bg_color_buffer[index(r, c)] = to_bg_color;
222222
}
223223
}
224224
}
@@ -230,7 +230,7 @@ namespace t8
230230
{
231231
for (int c = 0; c < NC; ++c)
232232
{
233-
bg_color_buffer[r][c] = to_bg_color;
233+
bg_color_buffer[index(r, c)] = to_bg_color;
234234
}
235235
}
236236
}
@@ -243,7 +243,7 @@ namespace t8
243243
{
244244
if (box.is_inside(r, c))
245245
{
246-
fg_color_buffer[r][c] = to_fg_color;
246+
fg_color_buffer[index(r, c)] = to_fg_color;
247247
}
248248
}
249249
}
@@ -255,7 +255,7 @@ namespace t8
255255
{
256256
for (int c = 0; c < NC; ++c)
257257
{
258-
fg_color_buffer[r][c] = to_fg_color;
258+
fg_color_buffer[index(r, c)] = to_fg_color;
259259
}
260260
}
261261
}
@@ -271,11 +271,12 @@ namespace t8
271271
{
272272
for (int c = 0; c < NC; ++c)
273273
{
274-
auto bg_curr = resolve_bg_color(bg_color_buffer[r][c], clear_bg_color);
275-
auto bg_prev = resolve_bg_color(prev_bg_color_buffer[r][c], prev_clear_bg_color);
276-
dirty_flag_buffer[r][c] =
277-
screen_buffer[r][c] != prev_screen_buffer[r][c]
278-
|| fg_color_buffer[r][c] != prev_fg_color_buffer[r][c]
274+
int idx = index(r, c);
275+
auto bg_curr = resolve_bg_color(bg_color_buffer[idx], clear_bg_color);
276+
auto bg_prev = resolve_bg_color(prev_bg_color_buffer[idx], prev_clear_bg_color);
277+
dirty_flag_buffer[idx] =
278+
screen_buffer[idx] != prev_screen_buffer[idx]
279+
|| fg_color_buffer[idx] != prev_fg_color_buffer[idx]
279280
|| bg_curr != bg_prev;
280281
}
281282
}
@@ -314,10 +315,11 @@ namespace t8
314315
{
315316
for (int c = 0; c < NC; ++c)
316317
{
317-
Color bg_col_buf = bg_color_buffer[r][c];
318+
int idx = index(r, c);
319+
Color bg_col_buf = bg_color_buffer[idx];
318320
if (bg_col_buf == Color::Transparent || bg_col_buf == Color::Transparent2)
319321
bg_col_buf = clear_bg_color;
320-
colored_str[i++] = { screen_buffer[r][c], fg_color_buffer[r][c], bg_col_buf };
322+
colored_str[i++] = { screen_buffer[idx], fg_color_buffer[idx], bg_col_buf };
321323
}
322324
colored_str[i++] = { '\n', Color::Default, Color::Default };
323325
}
@@ -334,14 +336,15 @@ namespace t8
334336
chunk.text.reserve(16); // #FIXME: Magic number.
335337
for (int c = 0; c < NC; ++c)
336338
{
337-
if (dirty_flag_buffer[r][c])
339+
int idx = index(r, c);
340+
if (dirty_flag_buffer[idx])
338341
{
339342
if (chunk.text.empty())
340343
chunk.pos = { r+1, c+1 };
341-
Color bg_col_buf = bg_color_buffer[r][c];
344+
Color bg_col_buf = bg_color_buffer[idx];
342345
if (bg_col_buf == Color::Transparent || bg_col_buf == Color::Transparent2)
343346
bg_col_buf = clear_bg_color;
344-
chunk.text.emplace_back(screen_buffer[r][c], fg_color_buffer[r][c], bg_col_buf);
347+
chunk.text.emplace_back(screen_buffer[idx], fg_color_buffer[idx], bg_col_buf);
345348
}
346349
else if (!chunk.text.empty())
347350
{
@@ -376,9 +379,10 @@ namespace t8
376379
if (!stlutils::contains(offscreen_buffer.dst_fill_bg_colors, textel.bg_color))
377380
continue;
378381

379-
textel.ch = screen_buffer[r][c];
380-
textel.fg_color = fg_color_buffer[r][c];
381-
textel.bg_color = bg_color_buffer[r][c];
382+
int idx = index(r, c);
383+
textel.ch = screen_buffer[idx];
384+
textel.fg_color = fg_color_buffer[idx];
385+
textel.bg_color = bg_color_buffer[idx];
382386

383387
if (stlutils::contains(offscreen_buffer.exclude_src_chars, textel.ch))
384388
continue;
@@ -404,7 +408,7 @@ namespace t8
404408
auto& line = ret[r];
405409
line.resize(NC);
406410
for (int c = 0; c < NC; ++c)
407-
line[c] = screen_buffer[r][c];
411+
line[c] = screen_buffer[index(r, c)];
408412
}
409413
return ret;
410414
}
@@ -416,9 +420,10 @@ namespace t8
416420
{
417421
for (int c = 0; c < NC; ++c)
418422
{
419-
texture.set_textel_char(r, c, screen_buffer[r][c]);
420-
texture.set_textel_fg_color(r, c, fg_color_buffer[r][c]);
421-
texture.set_textel_bg_color(r, c, bg_color_buffer[r][c]);
423+
int idx = index(r, c);
424+
texture.set_textel_char(r, c, screen_buffer[idx]);
425+
texture.set_textel_fg_color(r, c, fg_color_buffer[idx]);
426+
texture.set_textel_bg_color(r, c, bg_color_buffer[idx]);
422427
}
423428
}
424429
return texture;
@@ -429,7 +434,7 @@ namespace t8
429434
for (int r = 0; r < NR; ++r)
430435
{
431436
for (int c = 0; c < NC; ++c)
432-
printf("%c", screen_buffer[r][c]);
437+
printf("%c", screen_buffer[index(r, c)]);
433438
printf("\n");
434439
}
435440
}
@@ -439,7 +444,7 @@ namespace t8
439444
for (int r = 0; r < NR; ++r)
440445
{
441446
for (int c = 0; c < NC; ++c)
442-
printf("%s", color2str(fg_color_buffer[r][c]).c_str());
447+
printf("%s", color2str(fg_color_buffer[index(r, c)]).c_str());
443448
printf("\n");
444449
}
445450
}
@@ -449,7 +454,7 @@ namespace t8
449454
for (int r = 0; r < NR; ++r)
450455
{
451456
for (int c = 0; c < NC; ++c)
452-
printf("%s", color2str(bg_color_buffer[r][c]).c_str());
457+
printf("%s", color2str(bg_color_buffer[index(r, c)]).c_str());
453458
printf("\n");
454459
}
455460
}

0 commit comments

Comments
 (0)