Now you can move the square around with the keyboard.

master
Zed A. Shaw 6 months ago
parent 4cb1465dbc
commit 37199bdd19
  1. 6
      README.md
  2. 30
      sfmldemo/main.cpp

@ -32,6 +32,12 @@ To get this working the rough (ROUGH) steps on Windows are:
I'll have more extensive instructions in a later blog post, but if you have time try this out and let me know how it went at help@learncodethehardway.com. Please let me know if you tried a different compiler, Windows version, etc. If you're on OSX or Linux it should work the same but Linux people might want to use their package manager instead. I'll have more extensive instructions in a later blog post, but if you have time try this out and let me know how it went at help@learncodethehardway.com. Please let me know if you tried a different compiler, Windows version, etc. If you're on OSX or Linux it should work the same but Linux people might want to use their package manager instead.
### "Keyboard without any keys"
If you get a weird error message of, "We got a keyboard without any keys" it's because of a security feature in OSX. Go to `Security settings->Input Monitoring` and select your Terminal. Check it, enter your admin password, then restart your Terminal. Now you can...read the keyboard in your own software.
No, this does not enhance security at all. These people have gone full on insane at this point.
## Linux ## Linux
I actually don't have a Linux computer ready to test, but if you have a brand of Linux you like then try the OSX instructions and email me at [help@learncodethehardway.com](mailto:help@learncodethehardway.com). I actually don't have a Linux computer ready to test, but if you have a brand of Linux you like then try the OSX instructions and email me at [help@learncodethehardway.com](mailto:help@learncodethehardway.com).

@ -30,8 +30,6 @@ void ImGui_update(sf::RenderWindow &window, sf::Clock &deltaClock, sf::Time &tic
void Window_update(sf::RenderWindow &window, sf::Shape &shape) { void Window_update(sf::RenderWindow &window, sf::Shape &shape) {
window.clear(); window.clear();
sf::Vector2u size = window.getSize();
shape.setPosition(size.x / 2, size.y / 2);
window.draw(shape); window.draw(shape);
ImGui::SFML::Render(window); ImGui::SFML::Render(window);
window.display(); window.display();
@ -50,6 +48,8 @@ int main() {
ImGui_setup(window); ImGui_setup(window);
sf::CircleShape shape(100.f, 4); sf::CircleShape shape(100.f, 4);
sf::Vector2u size = window.getSize();
shape.setPosition(size.x / 2, size.y / 2);
shape.setOrigin(100.f, 100.f); shape.setOrigin(100.f, 100.f);
shape.setFillColor(sf::Color(150, 50, 250)); shape.setFillColor(sf::Color(150, 50, 250));
shape.setOutlineThickness(10.f); shape.setOutlineThickness(10.f);
@ -72,21 +72,39 @@ int main() {
while (window.isOpen()) { while (window.isOpen()) {
sf::Event event; sf::Event event;
// is this a main event loop
while (window.pollEvent(event)) { while (window.pollEvent(event)) {
ImGui::SFML::ProcessEvent(window, event); ImGui::SFML::ProcessEvent(window, event);
if (event.type == sf::Event::Closed) { switch(event.type) {
fmt::print("Exiting...\n");
window.close(); case sf::Event::Closed:
thread.terminate(); fmt::print("Exiting...\n");
window.close();
thread.terminate();
break;
case sf::Event::KeyPressed:
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
shape.move(-20, 0);
} else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
shape.move(20, 0);
} else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
shape.move(0, -20);
} else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
shape.move(0, 20);
}
break;
} }
} }
// should this move up to the pollEvent loop?
sf::Time since = clock.getElapsedTime(); sf::Time since = clock.getElapsedTime();
if(since - tick > sf::seconds(1)) { if(since - tick > sf::seconds(1)) {
tick = since; tick = since;
} }
shape.rotate(1); shape.rotate(1);
ImGui_update(window, deltaClock, tick); ImGui_update(window, deltaClock, tick);

Loading…
Cancel
Save