TermOx
canvas.hpp
1 #ifndef TERMOX_TERMINAL_DETAIL_CANVAS_HPP
2 #define TERMOX_TERMINAL_DETAIL_CANVAS_HPP
3 #include <iosfwd>
4 #include <memory>
5 #include <utility>
6 #include <vector>
7 
8 #include <termox/painter/color.hpp>
9 #include <termox/painter/glyph.hpp>
10 #include <termox/widget/area.hpp>
11 #include <termox/widget/point.hpp>
12 
13 namespace ox::detail {
14 
16 
18 class Canvas {
19  private:
20  using Buffer_t = std::vector<Glyph>;
21 
22  public:
24  using Diff = std::vector<std::pair<ox::Point, ox::Glyph>>;
25 
26  public:
28  Canvas(ox::Area a);
29 
30  public:
32  [[nodiscard]] auto area() const -> ox::Area;
33 
35  [[nodiscard]] auto at(ox::Point p) const -> ox::Glyph;
36 
38  [[nodiscard]] auto at(ox::Point p) -> ox::Glyph&;
39 
40  public:
42 
43  void resize(ox::Area a);
44 
45  public:
47  [[nodiscard]] auto begin() -> Buffer_t::iterator;
48 
50  [[nodiscard]] auto begin() const -> Buffer_t::const_iterator;
51 
53  [[nodiscard]] auto end() -> Buffer_t::iterator;
54 
56  [[nodiscard]] auto end() const -> Buffer_t::const_iterator;
57 
59  void reset();
60 
61  private:
62  Buffer_t buffer_;
63  ox::Area area_;
64 
65  std::unique_ptr<Canvas> resize_buffer_ = nullptr;
66 
67  private:
68  // Does not swap resize_buffer_
69  void swap(Canvas& x);
70 };
71 
73 
74 void merge(Canvas const& next, Canvas& current);
75 
77 
80 void merge_and_diff(Canvas const& next,
81  Canvas& current,
82  Canvas::Diff& diff_out);
83 
85 
88 void generate_color_diff(Color color,
89  Canvas const& canvas,
90  Canvas::Diff& diff_out);
91 
93 
94 void generate_full_diff(Canvas const& canvas, Canvas::Diff& diff_out);
95 
97 auto print(Canvas::Diff const& diff, std::ostream& os) -> std::ostream&;
98 
99 } // namespace ox::detail
100 #endif // TERMOX_TERMINAL_DETAIL_CANVAS_HPP
Color numbers [0 - 180] are valid.
Definition: color.hpp:16
A 2D field of Glyphs, useful as a screen buffer.
Definition: canvas.hpp:18
auto begin() -> Buffer_t::iterator
Return begin iterator to internal buffer.
Definition: canvas.cpp:71
void resize(ox::Area a)
Resize the Canvas to the given Area a.
Definition: canvas.cpp:55
std::vector< std::pair< ox::Point, ox::Glyph > > Diff
Type used to model differences between two Canvas objects.
Definition: canvas.hpp:24
auto end() -> Buffer_t::iterator
Return end iterator to internal buffer.
Definition: canvas.cpp:78
auto at(ox::Point p) const -> ox::Glyph
Return the Glyph at Point p.
Definition: canvas.cpp:41
Canvas(ox::Area a)
Construct a new Canvas with Area of a.
Definition: canvas.cpp:36
auto area() const -> ox::Area
Return the current size of the Canvas.
Definition: canvas.cpp:39
void reset()
Sets all Glyphs to default construction.
Definition: canvas.cpp:85
Holds a description of a paintable tile on the screen.
Definition: glyph.hpp:11