Skip to content

Commit

Permalink
img: Use our own gfx-abstractions to render images in the example
Browse files Browse the repository at this point in the history
  • Loading branch information
robinlinden committed Oct 24, 2023
1 parent 2695676 commit 5826739
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
1 change: 1 addition & 0 deletions img/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ cc_binary(
":gif",
":png",
":qoi",
"//gfx:sfml",
"@sfml//:graphics",
"@sfml//:window",
],
Expand Down
19 changes: 6 additions & 13 deletions img/img_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
#include "img/png.h"
#include "img/qoi.h"

#include <SFML/Graphics/Image.hpp>
#include "gfx/sfml_canvas.h"

#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Window/Event.hpp>

#include <algorithm>
Expand Down Expand Up @@ -110,12 +109,7 @@ int main(int argc, char **argv) {
window.setVerticalSyncEnabled(true);
window.setActive(true);

sf::Image sf_image{};
sf_image.create(width, height, bytes.data());
sf::Texture texture{};
texture.loadFromImage(sf_image);
sf::Sprite sprite{};
sprite.setTexture(texture);
gfx::SfmlCanvas canvas{window};

bool running = true;
while (running) {
Expand All @@ -126,16 +120,15 @@ int main(int argc, char **argv) {
running = false;
break;
case sf::Event::Resized:
window.setView(sf::View{sf::FloatRect{
0, 0, static_cast<float>(event.size.width), static_cast<float>(event.size.height)}});
canvas.set_viewport_size(event.size.width, event.size.height);
break;
default:
break;
}
}

window.clear();
window.draw(sprite);
canvas.clear(gfx::Color{});
canvas.draw_pixels({0, 0, static_cast<int>(width), static_cast<int>(height)}, bytes);
window.display();
}
}

0 comments on commit 5826739

Please sign in to comment.