Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <iostream>
#include <thread>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
void handleEvents(sf::RenderWindow &window)
{
while(window.isOpen())
{
sf::Event event;
while (window.waitEvent(event))
{
switch (event.type) {
case sf::Event::Closed:
std::cout << "Window closed" << std::endl;
window.close();
break;
case sf::Event::Resized:
std::cout << "Window resized" << std::endl;
break;
case sf::Event::LostFocus:
std::cout << "Window lost focus" << std::endl;
break;
case sf::Event::GainedFocus:
std::cout << "Window gained focus" << std::endl;
break;
case sf::Event::TextEntered:
std::cout << "Window text entered: " << event.text.unicode << std::endl;
break;
case sf::Event::KeyPressed:
std::cout << "Window key pressed (code): " << event.key.code << std::endl;
break;
case sf::Event::KeyReleased:
std::cout << "Window key released (code): " << event.key.code << std::endl;
break;
case sf::Event::MouseWheelMoved:
std::cout << "Window mouse wheel moved (delta): " << event.mouseWheel.delta << std::endl;
break;
case sf::Event::MouseButtonPressed:
std::cout << "Window mouse button pressed (code): " << event.mouseButton.button << std::endl;
break;
case sf::Event::MouseButtonReleased:
std::cout << "Window mouse button released (code): " << event.mouseButton.button << std::endl;
break;
case sf::Event::MouseMoved:
std::cout << "Window mouse moved (position): (" << event.mouseMove.x << "," << event.mouseMove.y << ")" << std::endl;
break;
case sf::Event::MouseEntered:
std::cout << "Window mouse entered" << std::endl;
break;
case sf::Event::MouseLeft:
std::cout << "Window mouse left" << std::endl;
break;
default:
std::cout << "Window other event" << std::endl;
}
}
}
}
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Hello World - SFML");
window.setFramerateLimit(60);
std::thread thr(handleEvents, std::ref(window));
sf::RectangleShape shape(sf::Vector2f(20.0, 20.0));
shape.setOutlineColor(sf::Color::Red);
shape.setOutlineThickness(5);
shape.setPosition(400, 300);
sf::Clock clock;
while(window.isOpen())
{
sf::Time delta = clock.restart();
shape.move(delta.asSeconds() * 10.0f, 0.0f);
window.clear(sf::Color(0,0,255));
window.draw(shape);
window.display();
}
thr.join();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment