Now you can move the square around with the keyboard.

master
Zed A. Shaw 7 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.
### "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
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) {
window.clear();
sf::Vector2u size = window.getSize();
shape.setPosition(size.x / 2, size.y / 2);
window.draw(shape);
ImGui::SFML::Render(window);
window.display();
@ -50,6 +48,8 @@ int main() {
ImGui_setup(window);
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.setFillColor(sf::Color(150, 50, 250));
shape.setOutlineThickness(10.f);
@ -72,21 +72,39 @@ int main() {
while (window.isOpen()) {
sf::Event event;
// is this a main event loop
while (window.pollEvent(event)) {
ImGui::SFML::ProcessEvent(window, event);
if (event.type == sf::Event::Closed) {
fmt::print("Exiting...\n");
window.close();
thread.terminate();
switch(event.type) {
case sf::Event::Closed:
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();
if(since - tick > sf::seconds(1)) {
tick = since;
}
shape.rotate(1);
ImGui_update(window, deltaClock, tick);

Loading…
Cancel
Save