Skip to content

Commit

Permalink
ScreenHandler now owns its own Text object, which simplifies its API …
Browse files Browse the repository at this point in the history
…and other code using it.
  • Loading branch information
razterizer committed Oct 24, 2024
1 parent bd063cc commit ca988bd
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
5 changes: 2 additions & 3 deletions Examples/SpriteHandler_examples.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ namespace sprite_handler
void example1()
{
ScreenHandler<20, 40> sh;
Text tt;
SpriteHandler sprh;
keyboard::KeyPressData kpd;
auto keyboard = std::make_unique<keyboard::StreamKeyboard>();
Expand Down Expand Up @@ -184,7 +183,7 @@ namespace sprite_handler
return_cursor();
sh.clear();
sprh.draw(sh, anim_frame);
sh.print_screen_buffer(tt, Color::Black);
sh.print_screen_buffer(Color::Black);
Delay::sleep(0'200'000);

kpd = keyboard->readKey();
Expand All @@ -196,7 +195,7 @@ namespace sprite_handler
}

quit:
end_screen(sh, tt);
end_screen(sh);
}

}
5 changes: 2 additions & 3 deletions GameEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ class GameEngine
std::chrono::time_point<std::chrono::steady_clock> real_start_time_s;
OneShot time_inited;

Text t;
ScreenHandler<NR, NC> sh;

Color bg_color = Color::Default;
Expand Down Expand Up @@ -261,7 +260,7 @@ class GameEngine
private:
void pre_quit()
{
end_screen(sh, t);
end_screen(sh);
if (m_params.enable_terminal_window_resize)
if (term_win_rows > 0 && term_win_cols > 0)
resize_terminal_window(term_win_rows, term_win_cols);
Expand Down Expand Up @@ -445,7 +444,7 @@ class GameEngine
update();
}

sh.print_screen_buffer(t, bg_color);
sh.print_screen_buffer(bg_color);
//sh.print_screen_buffer_chars();
//sh.print_screen_buffer_fg_colors();
//sh.print_screen_buffer_bg_colors();
Expand Down
10 changes: 8 additions & 2 deletions ScreenHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ RGBA clamp(const RGBA& t) { return RGBA { math::clamp(t.r, 0., 1.), math::clamp(
template<int NR = 30, int NC = 80>
class ScreenHandler
{
std::unique_ptr<Text> m_text;

// Draw from top to bottom.
std::array<std::array<char, NC>, NR> screen_buffer;
std::array<std::array<Color, NC>, NR> fg_color_buffer;
Expand Down Expand Up @@ -75,6 +77,10 @@ class ScreenHandler
std::vector<OrderedText> ordered_texts;

public:
ScreenHandler()
: m_text(std::make_unique<Text>())
{}

void clear()
{
for (auto& row : screen_buffer)
Expand Down Expand Up @@ -269,7 +275,7 @@ class ScreenHandler
}
}

void print_screen_buffer(Text& t, Color bg_color) const
void print_screen_buffer(Color bg_color) const
{
std::vector<std::tuple<char, Color, Color>> colored_str;
colored_str.resize(NR*(NC + 1));
Expand All @@ -285,7 +291,7 @@ class ScreenHandler
}
colored_str[i++] = { '\n', Color::Default, Color::Default };
}
t.print_complex(colored_str);
m_text->print_complex(colored_str);
}

void print_screen_buffer_chars() const
Expand Down
4 changes: 2 additions & 2 deletions ScreenUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,14 @@ void begin_screen()
}

template<int NR, int NC>
void end_screen(ScreenHandler<NR, NC>& sh, Text& t)
void end_screen(ScreenHandler<NR, NC>& sh)
{
auto orig_colors [[maybe_unused]] = restore_terminal_colors();
#ifndef __APPLE__
sh.clear();
sh.replace_fg_color(orig_colors.fg_color);
sh.replace_bg_color(orig_colors.bg_color);
sh.print_screen_buffer(t, orig_colors.bg_color);
sh.print_screen_buffer(orig_colors.bg_color);
#endif
restore_cursor();
show_cursor();
Expand Down

0 comments on commit ca988bd

Please sign in to comment.