srctree

Robin Linden parent 8be0f11e 9e1fb39d
gfx: Add a clear method to ICanvas

inlinesplit
gfx/canvas_command_saver.h added: 39, removed: 10, total 29
@@ -36,6 +36,12 @@ struct AddTranslationCmd {
[[nodiscard]] constexpr bool operator==(AddTranslationCmd const &) const = default;
};
 
struct ClearCmd {
Color color{};
 
[[nodiscard]] constexpr bool operator==(ClearCmd const &) const = default;
};
 
struct FillRectCmd {
geom::Rect rect{};
Color color{};
@@ -77,6 +83,7 @@ struct DrawTextCmd {
using CanvasCommand = std::variant<SetViewportSizeCmd,
SetScaleCmd,
AddTranslationCmd,
ClearCmd,
FillRectCmd,
DrawRectCmd,
DrawTextWithFontOptionsCmd,
@@ -88,6 +95,7 @@ public:
void set_viewport_size(int width, int height) override { cmds_.emplace_back(SetViewportSizeCmd{width, height}); }
void set_scale(int scale) override { cmds_.emplace_back(SetScaleCmd{scale}); }
void add_translation(int dx, int dy) override { cmds_.emplace_back(AddTranslationCmd{dx, dy}); }
void clear(Color c) override { cmds_.emplace_back(ClearCmd{c}); }
void fill_rect(geom::Rect const &rect, Color color) override { cmds_.emplace_back(FillRectCmd{rect, color}); }
void draw_rect(
geom::Rect const &rect, Color const &color, Borders const &borders, Corners const &corners) override {
@@ -130,6 +138,7 @@ public:
constexpr void operator()(SetViewportSizeCmd const &cmd) { canvas_.set_viewport_size(cmd.width, cmd.height); }
constexpr void operator()(SetScaleCmd const &cmd) { canvas_.set_scale(cmd.scale); }
constexpr void operator()(AddTranslationCmd const &cmd) { canvas_.add_translation(cmd.dx, cmd.dy); }
constexpr void operator()(ClearCmd const &cmd) { canvas_.clear(cmd.color); }
constexpr void operator()(FillRectCmd const &cmd) { canvas_.fill_rect(cmd.rect, cmd.color); }
 
constexpr void operator()(DrawRectCmd const &cmd) {
 
gfx/canvas_command_saver_test.cpp added: 39, removed: 10, total 29
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2022 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2022-2023 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
@@ -49,6 +49,12 @@ int main() {
expect_eq(saver.take_commands(), CanvasCommands{AddTranslationCmd{-10, 10}});
});
 
etest::test("CanvasCommandSaver::clear", [] {
CanvasCommandSaver saver;
saver.clear({0xab, 0xcd, 0xef});
expect_eq(saver.take_commands(), CanvasCommands{ClearCmd{{0xab, 0xcd, 0xef}}});
});
 
etest::test("CanvasCommandSaver::fill_rect", [] {
CanvasCommandSaver saver;
saver.fill_rect({1, 2, 3, 4}, {0xab, 0xcd, 0xef});
@@ -93,6 +99,7 @@ int main() {
 
etest::test("replay_commands", [] {
CanvasCommandSaver saver;
saver.clear(gfx::Color{});
saver.set_scale(10);
saver.set_scale(5);
saver.set_viewport_size(1, 2);
@@ -102,6 +109,7 @@ int main() {
saver.draw_rect({9, 9, 9, 9}, {0x10, 0x11, 0x12}, {}, {});
saver.draw_text({10, 10}, "beep beep boop!"sv, {"helvetica"}, {42}, FontStyle::Italic, {3, 2, 1});
saver.draw_text({1, 5}, "hello?"sv, {{{"font1"}, {"font2"}}}, {42}, FontStyle::Normal, {1, 2, 3});
saver.clear(gfx::Color{1, 2, 3});
auto cmds = saver.take_commands();
 
CanvasCommandSaver replayed;
 
gfx/icanvas.h added: 39, removed: 10, total 29
@@ -46,6 +46,7 @@ public:
virtual void set_viewport_size(int width, int height) = 0;
virtual void set_scale(int scale) = 0;
virtual void add_translation(int dx, int dy) = 0;
virtual void clear(Color) = 0;
virtual void fill_rect(geom::Rect const &, Color) = 0;
virtual void draw_rect(geom::Rect const &, Color const &, Borders const &, Corners const &) = 0;
virtual void draw_text(geom::Position, std::string_view, std::span<Font const>, FontSize, FontStyle, Color) = 0;
 
gfx/opengl_canvas.cpp added: 39, removed: 10, total 29
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2021 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2021-2023 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
@@ -22,6 +22,11 @@ void OpenGLCanvas::set_viewport_size(int width, int height) {
glLoadIdentity();
}
 
void OpenGLCanvas::clear(Color c) {
glClearColor(c.r / 255.f, c.g / 255.f, c.b / 255.f, c.a / 255.f);
glClear(GL_COLOR_BUFFER_BIT);
}
 
void OpenGLCanvas::fill_rect(geom::Rect const &rect, Color color) {
auto translated{rect.translated(translation_x_, translation_y_)};
auto scaled{translated.scaled(scale_)};
 
gfx/opengl_canvas.h added: 39, removed: 10, total 29
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2021-2022 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2021-2023 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
@@ -21,6 +21,7 @@ public:
translation_y_ += dy;
}
 
void clear(Color) override;
void fill_rect(geom::Rect const &, Color) override;
void draw_rect(geom::Rect const &, Color const &, Borders const &, Corners const &) override {}
void draw_text(geom::Position, std::string_view, std::span<Font const>, FontSize, FontStyle, Color) override {}
 
gfx/sfml_canvas.cpp added: 39, removed: 10, total 29
@@ -108,6 +108,10 @@ void SfmlCanvas::set_viewport_size(int width, int height) {
target_.setView(viewport);
}
 
void SfmlCanvas::clear(Color c) {
target_.clear(sf::Color(c.as_rgba_u32()));
}
 
void SfmlCanvas::fill_rect(geom::Rect const &rect, Color color) {
auto translated{rect.translated(tx_, ty_)};
auto scaled{translated.scaled(scale_)};
 
gfx/sfml_canvas.h added: 39, removed: 10, total 29
@@ -31,6 +31,7 @@ public:
ty_ += dy;
}
 
void clear(Color) override;
void fill_rect(geom::Rect const &, Color) override;
void draw_rect(geom::Rect const &, Color const &, Borders const &, Corners const &) override;
void draw_text(geom::Position, std::string_view, std::span<Font const>, FontSize, FontStyle, Color) override;