Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: sf::Keyboard::isKeyPressed() gives weird results in Release build  (Read 4382 times)

0 Members and 1 Guest are viewing this topic.

Sir Wolf

  • Newbie
  • *
  • Posts: 6
    • View Profile
I'm using Visual Studio 2010 with default compiler and all that.
Edit: Also, I'm using dynamic SFML2 binaries I compiled myself. Both a version pulled a bit over a month ago and a recent one I built yesterday do not work.

I noticed that a game I'm working on functioned differently when built in the default Release configuration than in the Debug configuration. It's an early prototype, it only has a sprite moving left and right on the screen from keyboard input, but in Release mode it didn't move at all. After some examination I noticed I could still press Esc to close the SFML window, so not all input was disfunctional.

Edit: So Esc worked, but WASD for movement didn't. I figured I would compare if a same action executed by different keys would work, comparing Esc and WASD. I tried the following minimal code (posted and reported as working by eXpl0it3r).

#include <SFML/Graphics.hpp>

int main ()
{
        sf::RenderWindow window(sf::VideoMode(1024, 768), "Test");
        window.setFramerateLimit(20);

        while(window.isOpen())
        {
                sf::Event event;

                while(window.pollEvent(event))
                {
                        if(event.type == sf::Event::Closed)
                                window.close();
                }
               
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                {
                        window.close();
                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
                {
                        window.close();
                }

                window.clear();
                window.display();
        }

    return 0;
}

For me, this piece of code does not work in Release mode. Esc closes the window if I press it first. D doesn't close the window. If I first press D, and after that press Esc, the Esc doesn't close the window anymore. And I am linking to correct binaries, using release files in release mode as I should.

I switched from using sf::Keyboard::isKeyPressed() to polling events from the SFML window and catching KeyPressed and KeyReleased events. This works as expected and my project now functions as it should.

Does anyone have some idea so far, with the information provided, what could be causing this behaviour? What could be causing my code to behave so differently in Release and Debug configurations, that's what I'm primarily worried about as I might have just hidden the problem instead of really solving it.
« Last Edit: July 27, 2012, 01:39:00 pm by Sir Wolf »
"We don't stop playing because we grow old; we grow old because we stop playing."
-George Bernard Shawn

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: sf::Keyboard::isKeyPressed() gives weird results in Release build
« Reply #1 on: July 23, 2012, 08:54:23 pm »
I've used the following minimal code and had no problems closing the window with D or Esc in release mode:

#include <SFML/Graphics.hpp>

int main ()
{
        sf::RenderWindow window(sf::VideoMode(1024, 768), "Test");
        window.setFramerateLimit(20);

        while(window.isOpen())
        {
                sf::Event event;

                while(window.pollEvent(event))
                {
                        if(event.type == sf::Event::Closed)
                                window.close();
                }
               
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                {
                        window.close();
                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
                {
                        window.close();
                }

                window.clear();
                window.display();
        }

    return 0;
}

What exacte SFML version are you using?
Did you compile dynamically or staticlly?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Sir Wolf

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: sf::Keyboard::isKeyPressed() gives weird results in Release build
« Reply #2 on: July 23, 2012, 09:00:44 pm »
What exacte SFML version are you using?
Did you compile dynamically or staticlly?

Dynamically, SFML2, built myself from sources I pulled via Git about a month ago.

I have a feeling I just linked the release build of my project to the debug binaries of SFML or somesuch. I guess that would be the most obvious explanation. Edit: Nope, triple checked this, I'm linking correctly.

Out of curiosity, does someone know of something else that could be causing this?
« Last Edit: July 27, 2012, 01:35:53 pm by Sir Wolf »
"We don't stop playing because we grow old; we grow old because we stop playing."
-George Bernard Shawn

Sir Wolf

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: sf::Keyboard::isKeyPressed() gives weird results in Release build
« Reply #3 on: July 25, 2012, 01:05:57 pm »
I've used the following minimal code and had no problems closing the window with D or Esc in release mode:

// Code snipped

To me, the code you provided, copy pasted, does not work in Release mode. Esc closes the window if I press it first. D doesn't close the window. If I first press D, and after that press Esc, the Esc doesn't close the window anymore. And I am linking to correct binaries, using release files in release mode as I should.

I'll try pulling a fresh SFML source and building it again to see if it works with the most recent snapshot.
"We don't stop playing because we grow old; we grow old because we stop playing."
-George Bernard Shawn

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: sf::Keyboard::isKeyPressed() gives weird results in Release build
« Reply #4 on: July 25, 2012, 01:22:57 pm »
It should work with the latest and probably also a bit older versions, as I'm also using VS10.
Otherwise something is off on your end...
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Sir Wolf

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: sf::Keyboard::isKeyPressed() gives weird results in Release build
« Reply #5 on: July 25, 2012, 01:27:02 pm »
It should work with the latest and probably also a bit older versions, as I'm also using VS10.
Otherwise something is off on your end...

Yes, that seems to be the case. But I have no idea what could be the culprit, where to start even looking. I'm thinking something like a build setting when I build SFML or I'm linking to some wrong binary somewhere despite having triple checked it. Something really silly I must be doing somewhere I have missed.
"We don't stop playing because we grow old; we grow old because we stop playing."
-George Bernard Shawn