srctree

Robin Linden parent 20f108ed a34f0769
gfx: Add a draw_text method to IPainter

inlinesplit
gfx/gfx_example.cpp added: 53, removed: 9, total 44
@@ -53,6 +53,8 @@ int main(int argc, char **argv) {
painter->fill_rect({200, 200, 100, 100}, gfx::Color{0, 0, 0xAA});
painter->fill_rect({x / 4 + 50, y / 3 + 50, x / 2, y / 3}, gfx::Color{0xAA, 0, 0, 0x33});
 
painter->draw_text({100, 50}, "hello!"sv, {"arial"}, {16}, gfx::Color{});
 
window.display();
}
}
 
gfx/ipainter.h added: 53, removed: 9, total 44
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2021 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2021-2022 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
@@ -8,8 +8,18 @@
#include "geom/geom.h"
#include "gfx/color.h"
 
#include <string_view>
 
namespace gfx {
 
struct Font {
std::string_view font;
};
 
struct FontSize {
int px{10};
};
 
class IPainter {
public:
virtual ~IPainter() = default;
@@ -18,6 +28,7 @@ public:
virtual void set_scale(int scale) = 0;
virtual void add_translation(int dx, int dy) = 0;
virtual void fill_rect(geom::Rect const &, Color) = 0;
virtual void draw_text(geom::Position, std::string_view, Font, FontSize, Color) = 0;
};
 
} // namespace gfx
 
gfx/opengl_painter.h added: 53, removed: 9, total 44
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2021 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2021-2022 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
@@ -22,6 +22,7 @@ public:
}
 
void fill_rect(geom::Rect const &, Color) override;
void draw_text(geom::Position, std::string_view, Font, FontSize, Color) override {}
 
private:
int translation_x{};
 
gfx/paint_command_saver.h added: 53, removed: 9, total 44
@@ -7,6 +7,7 @@
 
#include "gfx/ipainter.h"
 
#include <string>
#include <utility>
#include <variant>
#include <vector>
@@ -40,7 +41,17 @@ struct FillRectCmd {
[[nodiscard]] constexpr bool operator==(FillRectCmd const &) const = default;
};
 
using PaintCommand = std::variant<SetViewportSizeCmd, SetScaleCmd, AddTranslationCmd, FillRectCmd>;
struct DrawTextCmd {
geom::Position position{};
std::string text{};
std::string font{};
int size{};
Color color{};
 
[[nodiscard]] bool operator==(DrawTextCmd const &) const = default;
};
 
using PaintCommand = std::variant<SetViewportSizeCmd, SetScaleCmd, AddTranslationCmd, FillRectCmd, DrawTextCmd>;
 
class PaintCommandSaver : public IPainter {
public:
@@ -49,6 +60,9 @@ public:
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 fill_rect(geom::Rect const &rect, Color color) override { cmds_.emplace_back(FillRectCmd{rect, color}); }
void draw_text(geom::Position position, std::string_view text, Font font, FontSize size, Color color) override {
cmds_.emplace_back(DrawTextCmd{position, std::string{text}, std::string{font.font}, size.px, color});
}
 
//
[[nodiscard]] std::vector<PaintCommand> take_commands() { return std::exchange(cmds_, {}); }
@@ -66,6 +80,10 @@ public:
constexpr void operator()(AddTranslationCmd const &cmd) { painter_.add_translation(cmd.dx, cmd.dy); }
constexpr void operator()(FillRectCmd const &cmd) { painter_.fill_rect(cmd.rect, cmd.color); }
 
void operator()(DrawTextCmd const &cmd) {
painter_.draw_text(cmd.position, cmd.text, {cmd.font}, {cmd.size}, cmd.color);
}
 
private:
IPainter &painter_;
};
 
gfx/paint_command_saver_test.cpp added: 53, removed: 9, total 44
@@ -6,7 +6,11 @@
 
#include "etest/etest.h"
 
#include <string>
#include <string_view>
 
using namespace gfx;
using namespace std::literals;
 
using etest::expect_eq;
 
@@ -51,6 +55,12 @@ int main() {
expect_eq(saver.take_commands(), PaintCommands{FillRectCmd{{1, 2, 3, 4}, {0xab, 0xcd, 0xef}}});
});
 
etest::test("PaintCommandSaver::draw_text", [] {
PaintCommandSaver saver;
saver.draw_text({1, 2}, "hello!"sv, {"comic sans"}, {11}, {1, 2, 3});
expect_eq(saver.take_commands(), PaintCommands{DrawTextCmd{{1, 2}, "hello!"s, {"comic sans"}, 11, {1, 2, 3}}});
});
 
etest::test("replay_commands", [] {
PaintCommandSaver saver;
saver.set_scale(10);
@@ -59,6 +69,7 @@ int main() {
saver.set_scale(1);
saver.add_translation(1234, 5678);
saver.fill_rect({9, 9, 9, 9}, {0x12, 0x34, 0x56});
saver.draw_text({10, 10}, "beep beep boop!"sv, {"helvetica"}, {42}, {3, 2, 1});
auto cmds = saver.take_commands();
 
PaintCommandSaver replayed;
 
gfx/sfml_painter.h added: 53, removed: 9, total 44
@@ -26,6 +26,7 @@ public:
}
 
void fill_rect(geom::Rect const &, Color) override;
void draw_text(geom::Position, std::string_view, Font, FontSize, Color) override {}
 
private:
sf::RenderTarget &target_;