Skip to content

Commit

Permalink
gfx/sfml: Implement pixel-drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
robinlinden committed Oct 13, 2023
1 parent 50ebf8b commit 0df01d6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
4 changes: 4 additions & 0 deletions gfx/gfx_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/Event.hpp>

#include <array>
#include <memory>
#include <string_view>

Expand Down Expand Up @@ -78,6 +79,9 @@ int main(int argc, char **argv) {
gfx::FontStyle::Italic | gfx::FontStyle::Bold | gfx::FontStyle::Underlined
| gfx::FontStyle::Strikethrough,
kHotPink);
auto px = std::to_array<std::uint8_t>(
{100, 100, 100, 0xff, 200, 200, 200, 0xff, 50, 50, 50, 0xff, 200, 0, 0, 0xff});
canvas->draw_pixels({1, 1, 2, 2}, px);

window.display();
}
Expand Down
19 changes: 19 additions & 0 deletions gfx/sfml_canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@

#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/RenderTarget.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Text.hpp>
#include <SFML/Graphics/View.hpp>
#include <spdlog/spdlog.h>

#include <algorithm>
#include <cassert>
#include <filesystem>
#include <optional>
#include <string>
Expand Down Expand Up @@ -114,6 +116,7 @@ void SfmlCanvas::set_viewport_size(int width, int height) {

void SfmlCanvas::clear(Color c) {
target_.clear(sf::Color(c.as_rgba_u32()));
textures_.clear();
}

void SfmlCanvas::fill_rect(geom::Rect const &rect, Color color) {
Expand Down Expand Up @@ -238,4 +241,20 @@ void SfmlCanvas::draw_text(
target_.draw(drawable);
}

void SfmlCanvas::draw_pixels(geom::Rect const &rect, std::span<std::uint8_t const> rgba_data) {
assert(rgba_data.size() == static_cast<std::size_t>(rect.width * rect.height * 4));
sf::Image img;
// Textures need to be kept around while they're displayed. This will be
// cleared when the canvas is cleared.
sf::Texture &texture = textures_.emplace_back();
texture.create(static_cast<unsigned>(rect.width), static_cast<unsigned>(rect.height));
texture.update(rgba_data.data());
sf::Sprite sprite{texture};
sprite.setPosition(static_cast<float>(rect.x), static_cast<float>(rect.y));
target_.draw(sprite);
sf::RectangleShape shape{{static_cast<float>(rect.width), static_cast<float>(rect.height)}};
shape.setTexture(&texture);
target_.draw(shape);
}

} // namespace gfx
5 changes: 4 additions & 1 deletion gfx/sfml_canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
#include "gfx/icanvas.h"

#include <SFML/Graphics/Shader.hpp>
#include <SFML/Graphics/Texture.hpp>

#include <map>
#include <memory>
#include <vector>

namespace sf {
class Font;
Expand All @@ -36,12 +38,13 @@ class SfmlCanvas : public ICanvas {
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;
void draw_text(geom::Position, std::string_view, Font, FontSize, FontStyle, Color) override;
void draw_pixels(geom::Rect const &, std::span<std::uint8_t const>) override {}
void draw_pixels(geom::Rect const &, std::span<std::uint8_t const> rgba_data) override;

private:
sf::RenderTarget &target_;
sf::Shader border_shader_{};
std::map<std::string, std::shared_ptr<sf::Font>, std::less<>> font_cache_;
std::vector<sf::Texture> textures_;

int scale_{1};
int tx_{0};
Expand Down

0 comments on commit 0df01d6

Please sign in to comment.