11#ifndef SFMLWINDOW_HPP_
12#define SFMLWINDOW_HPP_
15#include <SFML/Graphics.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();
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; });
bool isAvailable() const override
notice if a gamepad is actually plugged in
Definition SfmlGamepad.hpp:42
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
Vector2f getPosition() override
Where the window sits on the desktop, in pixels, origin top-left of the primary screen.
Definition SfmlWindow.hpp:68
friend class SfmlGamepad
Definition SfmlWindow.hpp:147
friend class SfmlKeyboard
Definition SfmlWindow.hpp:145
void setMouseVisibility(bool visible) override
Show or hide the system cursor over THIS window.
Definition SfmlWindow.hpp:96
void beginDraw() override
allowing to draw 2D on the window
Definition SfmlWindow.hpp:131
bool isOpen() override
notice if the window is open
Definition SfmlWindow.hpp:60
void setFrameLimit(int32_t limit) override
Set the Frame Limit object.
Definition SfmlWindow.hpp:90
void close() override
close the window
Definition SfmlWindow.hpp:64
Vector2f getSize() override
The drawable area, in pixels - NOT the desktop size. A hit-test built on IMouse::getPosition() compar...
Definition SfmlWindow.hpp:77
void endDraw() override
end the 2D drawing
Definition SfmlWindow.hpp:135
int32_t getDelta() override
Get the Delta object.
Definition SfmlWindow.hpp:100
void setPosition(Vector2f position) override
Move the window on the desktop.
Definition SfmlWindow.hpp:73
void eventClose() override
notice the window that a close was requested
Definition SfmlWindow.hpp:117
void setSize(Vector2f size) override
Resize the drawable area.
Definition SfmlWindow.hpp:82
SfmlWindow(int32_t screenWidth, int32_t screenHeight, std::string title)
Definition SfmlWindow.hpp:49
friend class SfmlMouse
Definition SfmlWindow.hpp:146
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
Button
Definition IGamepad.hpp:30
Keys
Scan codes.
Definition IKeyboard.hpp:86
Buttons
Definition IMouse.hpp:39
Polygon interface.
Definition IPolygon.hpp:34
Sprite interface.
Definition ISprite.hpp:22
Text interface.
Definition IText.hpp:26
A window that can draw 2D. Adds only the 2D drawing surface on top of IWindow's lifecycle/input.
Definition IWindow2.hpp:25
bool isKeyReleased(Keys key) const override
Check if the key is released.
Definition SfmlWindow.hpp:287
void drawText(graphic::IText *text) override
draw a text
Definition SfmlWindow.hpp:244
bool isButtonDown(Button button) const override
notice if the button is down
Definition SfmlWindow.hpp:339
bool isButtonPressed(Buttons key) const override
notice if a button is pressed
Definition SfmlWindow.hpp:297
float GetMouseWheelMove() const override
get the mouse wheel move
Definition SfmlWindow.hpp:326
void drawSprite(graphic::ISprite *sprite) override
draw a sprite
Definition SfmlWindow.hpp:239
bool isButtonDown(Buttons key) const override
notice if a button is down
Definition SfmlWindow.hpp:311
bool isKeyDown(Keys key) const override
Check if the key is down.
Definition SfmlWindow.hpp:294
bool isKeyPressed(Keys key) const override
Check if the key is pressed.
Definition SfmlWindow.hpp:280
bool isButtonPressed(Button button) const override
notice if the button is pressed
Definition SfmlWindow.hpp:344
bool isButtonUp(Buttons key) const override
notice if a button is up
Definition SfmlWindow.hpp:312
bool isButtonUp(Button button) const override
notice if the button is up
Definition SfmlWindow.hpp:342
bool isButtonReleased(Button button) const override
notice if the button is released
Definition SfmlWindow.hpp:352
bool isButtonReleased(Buttons key) const override
notice if a button is released
Definition SfmlWindow.hpp:304
std::vector< Keys > whichKeyDown() const override
Every key HELD right now - like isKeyDown, without having to name the key.
Definition SfmlWindow.hpp:271
void setPosition(Vector2f position) override
set the position of the mouse
Definition SfmlWindow.hpp:319
void drawPoly(graphic::IPolygon *polygon) override
draw a polygon
Definition SfmlWindow.hpp:227
Vector2f getPosition() const override
get the position of the mouse
Definition SfmlWindow.hpp:314
bool isKeyUp(Keys key) const override
Check if the key is up.
Definition SfmlWindow.hpp:295
Vector2< double > Vector2f
Definition Vector2.hpp:112
T y
Definition Vector2.hpp:21
T x
Definition Vector2.hpp:21