Files
cardboy/Firmware/sdk/sfml-port/src/main.cpp
2025-07-31 16:12:35 +02:00

76 lines
2.9 KiB
C++

#include <SFML/Graphics.hpp>
#include <barrier>
#include <latch>
#include <optional>
#include <thread>
#include "GridWindow.hpp"
#include "SfmlWindow.hpp"
#include "TextWindow.hpp"
int main() {
EventLoop loop;
std::latch barrier{1};
SfmlSurface* surface_ptr;
int i = 0;
std::thread loop_thread{[&] {
SfmlSurface surface(&loop);
surface_ptr = &surface;
barrier.count_down();
surface.set_window<GridWindow<SfmlSurface, 2, 2>>();
GridWindow<SfmlSurface, 2, 2>* window =
static_cast<GridWindow<SfmlSurface, 2, 2>*>(surface.get_window());
window->set_window<TextWindow<SubSurface<SfmlSurface>, std::string>>(0, 0, &loop, "hello");
window->set_window<TextWindow<SubSurface<SfmlSurface>, std::string>>(0, 1, &loop, "hello1");
window->set_window<GridWindow<SubSurface<SfmlSurface>, 2, 2>>(1, 0);
GridWindow<SubSurface<SfmlSurface>, 2, 2>* window2 =
static_cast<GridWindow<SubSurface<SfmlSurface>, 2, 2>*>(
window->get_subsurface(1, 0).get_window());
window->set_window<TextWindow<SubSurface<SfmlSurface>, std::string>>(1, 1, &loop, "hello3");
window2->set_window<TextWindow<SubSurface<SfmlSurface>, std::string>>(
0, 0, &loop, "hello2");
window2->set_window<TextWindow<SubSurface<SfmlSurface>, std::string>>(
0, 1, &loop, "hello4");
window2->set_window<TextWindow<SubSurface<SfmlSurface>, std::string>>(
1, 0, &loop, "hello5");
window2->set_window<TextWindow<SubSurface<SfmlSurface>, std::string>>(
1, 1, &loop, "hello6");
loop.run([&] {
surface._sf_window.clear();
surface._texture.update(surface._image);
surface._sf_window.draw(surface._sprite);
surface._sf_window.display();
static_cast<TextWindow<SubSurface<SfmlSurface>, std::string>*>(
window->get_subsurface(0, 0).get_window())
->push(TextUpdateEvent<std::string>{std::string("Hello, SFML!") + std::to_string(i++)});
});
}};
barrier.wait();
while (surface_ptr->_sf_window.isOpen()) {
while (const std::optional event = surface_ptr->_sf_window.pollEvent()) {
if (event->is<sf::Event::Closed>()) {
surface_ptr->_sf_window.close();
loop.push(LoopQuitEvent{});
}
if (event->is<sf::Event::Resized>()) {
auto newSize = event->getIf<sf::Event::Resized>()->size;
surface_ptr->push(SurfaceResizeEvent{newSize.x, newSize.y});
}
if (event->is<sf::Event::KeyPressed>()) {
auto key = event->getIf<sf::Event::KeyPressed>();
surface_ptr->push(KeyboardEvent{static_cast<Key>(key->code)});
}
}
}
loop_thread.join();
}