11#ifndef SFMLWINDOW_HPP_
12#define SFMLWINDOW_HPP_
15#include <SFML/Graphics.hpp>
18#include "IWindow2.hpp"
49 SfmlWindow(int32_t screenWidth, int32_t screenHeight, std::string title)
50 : _window(sf::VideoMode({
static_cast<uint32_t
>(screenWidth),
static_cast<uint32_t
>(screenHeight)}), title) {
53 _window.setKeyRepeatEnabled(
false);
61 return _window.isOpen();
69 const sf::Vector2i position = _window.getPosition();
70 return {
static_cast<double>(position.x),
static_cast<double>(position.y)};
74 _window.setPosition({
static_cast<int>(position.x),
static_cast<int>(position.y)});
78 const sf::Vector2u size = _window.getSize();
79 return {
static_cast<double>(size.x),
static_cast<double>(size.y)};
83 _window.setSize({
static_cast<unsigned>(size.x),
static_cast<unsigned>(size.y)});
91 _window.setFramerateLimit(limit);
97 _window.setMouseCursorVisible(visible);
101 return static_cast<int32_t
>(_deltaTime.asMilliseconds());
110 while (
const std::optional<sf::Event> event = _window.pollEvent()) {
111 _events.push_back(*event);
114 return !_events.empty();
118 for (
const auto &event : _events) {
119 const auto *pressed =
event.getIf<sf::Event::KeyPressed>();
121 if (event.is<sf::Event::Closed>() ||
122 (pressed && pressed->scancode == sf::Keyboard::Scancode::Escape)) {
137 _deltaTime = _deltaClock.restart();
141 void drawPoly(graphic::IPolygon *polygon)
override;
142 void drawSprite(graphic::ISprite *sprite)
override;
143 void drawText(graphic::IText *text)
override;
172 const sf::Vector2f size(_window.getSize());
174 _window.setView(sf::View(sf::FloatRect({0.f, 0.f}, size)));
177 static size_t index(sf::Keyboard::Scancode code) {
178 const auto raw =
static_cast<int>(code);
179 return (raw < 0) ? 0 :
static_cast<size_t>(raw);
191 void feedEvent(
const sf::Event &event) {
192 if (
const auto *pressed = event.getIf<sf::Event::KeyPressed>())
193 _keysDown[index(pressed->scancode)] =
true;
194 else if (
const auto *released = event.getIf<sf::Event::KeyReleased>())
195 _keysDown[index(released->scancode)] =
false;
196 else if (
const auto *down = event.getIf<sf::Event::MouseButtonPressed>())
197 _mouseDown[size_t(down->button)] =
true;
198 else if (
const auto *up = event.getIf<sf::Event::MouseButtonReleased>())
199 _mouseDown[size_t(up->button)] =
false;
200 else if (
const auto *moved = event.getIf<sf::Event::MouseMoved>())
201 _mousePosition = moved->position;
202 else if (event.is<sf::Event::Resized>())
204 else if (event.is<sf::Event::FocusLost>()) {
206 _keysDown.fill(
false);
207 _mouseDown.fill(
false);
211 sf::RenderWindow _window;
215 std::vector<sf::Event> _events;
217 sf::Clock _deltaClock;
220 std::array<bool, sf::Keyboard::ScancodeCount> _keysDown{};
222 std::array<bool, sf::Mouse::ButtonCount> _mouseDown{};
224 sf::Vector2i _mousePosition{};
233 sf::Transform transform;
234 transform.translate(sfmlPolygon->_position);
236 _window.draw(sfmlPolygon->_vertices, transform);
241 _window.draw(sfmlSprite->_sprite);
246 _window.draw(sfmlText->_text);
262template <
typename E,
typename Match>
263static bool anyEvent(
const std::vector<sf::Event> &events, Match match) {
264 for (
const auto &event : events)
265 if (const auto *typed = event.getIf<E>())
272 std::vector<Keys> keys;
274 for (
const auto &[key, code] : _keys)
275 if (_window._keysDown[SfmlWindow::index(code)])
281 const auto code = _keys.at(key);
283 return anyEvent<sf::Event::KeyPressed>(_window._events,
284 [code](
const auto &pressed) { return pressed.scancode == code; });
288 const auto code = _keys.at(key);
290 return anyEvent<sf::Event::KeyReleased>(_window._events,
291 [code](
const auto &released) { return released.scancode == code; });
298 const auto button = _buttons.at(key);
300 return anyEvent<sf::Event::MouseButtonPressed>(_window._events,
301 [button](
const auto &pressed) { return pressed.button == button; });
305 const auto button = _buttons.at(key);
307 return anyEvent<sf::Event::MouseButtonReleased>(_window._events,
308 [button](
const auto &released) { return released.button == button; });
315 const sf::Vector2i position = _window._mousePosition;
316 return Vector2f{
static_cast<double>(position.x),
static_cast<double>(position.y)};
320 const sf::Vector2i target{
static_cast<int>(position.x),
static_cast<int>(position.y)};
322 _window._mousePosition = target;
323 sf::Mouse::setPosition(target, _window._window);
330 for (
const auto &event : _window._events)
331 if (
const auto *scroll = event.getIf<sf::Event::MouseWheelScrolled>())
332 if (scroll->wheel == sf::Mouse::Wheel::Vertical)
333 delta += scroll->delta;
340 return isAvailable() && sf::Joystick::isButtonPressed(_index, _buttons.at(button));
345 const auto index = _index;
346 const auto raw = _buttons.at(button);
348 return anyEvent<sf::Event::JoystickButtonPressed>(_window._events,
349 [index, raw](
const auto &pressed) { return pressed.joystickId == index && pressed.button == raw; });
353 const auto index = _index;
354 const auto raw = _buttons.at(button);
356 return anyEvent<sf::Event::JoystickButtonReleased>(_window._events,
357 [index, raw](
const auto &released) { return released.joystickId == index && released.button == raw; });
Unlike raylib's GamepadButton (normalized Xbox-style layout), sf::Joystick exposes raw numbered butto...
Definition SfmlGamepad.hpp:35
bool isAvailable() const override
Definition SfmlGamepad.hpp:42
Maps the contract's keys to sfml scancodes, and reads the state the window accumulated while popping ...
Definition SfmlKeyboard.hpp:41
Maps the contract's buttons to sfml buttons, and reads the state the window accumulated while popping...
Definition SfmlMouse.hpp:29
Definition SfmlPolygon.hpp:25
Sfml Sprite class - references a SfmlTexture, does not own it. Deleting the sprite never touches the ...
Definition SfmlSprite.hpp:27
Sfml Text class - references a SfmlFont, does not own it. Deleting the text never touches the font.
Definition SfmlText.hpp:28
Sfml Window class - implements IWindow2 only, sfml has no 3D.
Definition SfmlWindow.hpp:46
Vector2f getPosition() override
Definition SfmlWindow.hpp:68
void setMouseVisibility(bool visible) override
Definition SfmlWindow.hpp:96
void beginDraw() override
Definition SfmlWindow.hpp:131
bool isOpen() override
Definition SfmlWindow.hpp:60
void setFrameLimit(int32_t limit) override
Definition SfmlWindow.hpp:90
void close() override
Definition SfmlWindow.hpp:64
Vector2f getSize() override
Definition SfmlWindow.hpp:77
void endDraw() override
Definition SfmlWindow.hpp:135
int32_t getDelta() override
Definition SfmlWindow.hpp:100
void setPosition(Vector2f position) override
Definition SfmlWindow.hpp:73
void eventClose() override
Definition SfmlWindow.hpp:117
void setSize(Vector2f size) override
Definition SfmlWindow.hpp:82
SfmlWindow(int32_t screenWidth, int32_t screenHeight, std::string title)
Definition SfmlWindow.hpp:49
bool pollEvent() override
Drains the whole queue into _events and folds it into state. Called again in the same frame it finds ...
Definition SfmlWindow.hpp:109
bool isKeyReleased(Keys key) const override
Definition SfmlWindow.hpp:287
void drawText(graphic::IText *text) override
Definition SfmlWindow.hpp:244
bool isButtonDown(Button button) const override
Definition SfmlWindow.hpp:339
bool isButtonPressed(Buttons key) const override
Definition SfmlWindow.hpp:297
float GetMouseWheelMove() const override
Definition SfmlWindow.hpp:326
void drawSprite(graphic::ISprite *sprite) override
Definition SfmlWindow.hpp:239
bool isButtonDown(Buttons key) const override
Definition SfmlWindow.hpp:311
bool isKeyDown(Keys key) const override
Definition SfmlWindow.hpp:294
bool isKeyPressed(Keys key) const override
Definition SfmlWindow.hpp:280
bool isButtonPressed(Button button) const override
Definition SfmlWindow.hpp:344
bool isButtonUp(Buttons key) const override
Definition SfmlWindow.hpp:312
bool isButtonUp(Button button) const override
Definition SfmlWindow.hpp:342
bool isButtonReleased(Button button) const override
Definition SfmlWindow.hpp:352
bool isButtonReleased(Buttons key) const override
Definition SfmlWindow.hpp:304
std::vector< Keys > whichKeyDown() const override
Definition SfmlWindow.hpp:271
void setPosition(Vector2f position) override
Definition SfmlWindow.hpp:319
void drawPoly(graphic::IPolygon *polygon) override
Definition SfmlWindow.hpp:227
Vector2f getPosition() const override
Definition SfmlWindow.hpp:314
bool isKeyUp(Keys key) const override
Definition SfmlWindow.hpp:295