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

Author Topic: [SFML 2.0]Problem with repeating buttons and events.  (Read 10911 times)

0 Members and 1 Guest are viewing this topic.

noct

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • nctdev.pl
[SFML 2.0]Problem with repeating buttons and events.
« on: August 29, 2014, 02:48:26 am »
Hello. Well, it's more like a few questions, not only a problem. I think this is all connected with each-other, so I've posted only one topic.

This is in the main loop of application (60 fps limit)
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::LAlt && paused == 0)
{
  cout << "game paused" << endl;
  paused = 1;
}

I'm using bool paused like that:
if (!paused)
{
  okno.display();
}
1. Why Event::KeyPressed and Event::KeyReleased are not working with keys other than similar to Alt, Home, End etc?
I've checked it by using:
if (event.type == sf::Event::KeyPressed)
{
  cout << event.key.code << endl;
}
It returns key codes, but for letters/digits it does not return anything.

2. I'm trying to pause my game, how, correctly, should I do this? By stopping Window.display()?

3. How to resume after pausing?
I was trying to use:
if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::LAlt) && paused == 1)
{
cout << "game started" << endl;
paused = 0;
}
And here comes another problem - game was starting and stopping few times, till I released LAlt. So, how can I disable "key repetition"?
I guess Window.setKeyRepeatEnabled(false) works only with events (which imho aren't working correctly here). Additionally I'd have to lock this only when needed (I can't have this locked ex. while im using W to move up).

I hope you, guys, will understand what do I think about, even with my B2 ;)
« Last Edit: August 29, 2014, 02:49:59 am by noct »

Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
Re: [SFML 2.0]Problem with repeating buttons and events.
« Reply #1 on: August 29, 2014, 03:41:10 am »
I can't answer the first question because I have not understood it, I'll link this in case you haven't seen it yet. Regarding question 2 and 3 I'm quite sure this still holds true. Have you considered switching to sfml 2.1 or the latest version? I see more good than harm in doing that. By the way, I'm pretty sure most of the people posting in this forum are not native speakers :) (C1 here 8)).

noct

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • nctdev.pl
Re: [SFML 2.0]Problem with repeating buttons and events.
« Reply #2 on: August 29, 2014, 12:55:04 pm »
I've already read both.
I've seen this code @Poraft post:
if((event.Type == sf::Event::KeyReleased) && (event.Key.Code == sf::Key::P))

So, he is able to use sf::Event::KeyReleased (and Pressed ) With letter P. I can't do this. I've even copied his code. Seems like sf::Event::KeyReleased works in my code only for Alt, Shift etc, like I've said.

Additionally, when I use anything with event.Type == sf::Event::KeyPressed), my pointer stops:

sf::Vertex lineX[ 2 ] =
{
    sf::Vertex( sf::Vector2f( mysz.x, 0 ) ),
    sf::Vertex( sf::Vector2f( mysz.x, mapY*blocksize ) )
};

sf::Vertex lineY[ 2 ] =
{
    sf::Vertex( sf::Vector2f( 0, mysz.y ) ),
    sf::Vertex( sf::Vector2f( mapX*blocksize, mysz.y ) )
};

Where mysz is get from sf::Vector2i mysz = sf::Mouse::getPosition(window);

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: [SFML 2.0]Problem with repeating buttons and events.
« Reply #3 on: August 29, 2014, 08:17:23 pm »
I'm using bool paused like that:
if (!paused)
{
  okno.display();
}
Is paused a boolean or not? If so, why are you assigning it 1 and 0 instead of true and false, and why are you testing for comparison to 1 and 0 instead of paused and !paused? Mixing is confusing :p

I've checked it by using:
if (event.type == sf::Event::KeyPressed)
{
  cout << event.key.code << endl;
}
It returns key codes, but for letters/digits it does not return anything.
Can you create a complete and minimal example which uses this code and still has the problem you're describing so that we can compile and test it for ourselves? Does this bit of code work in other projects?

What is your B2?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

noct

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • nctdev.pl
Re: [SFML 2.0]Problem with repeating buttons and events.
« Reply #4 on: August 29, 2014, 11:08:17 pm »
I thought it's the same in boolean variables. (isn't it?)

Ofc, here you have:
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>

using namespace std;

int main()
 {
sf::RenderWindow okno(sf::VideoMode(100, 100, 32), "Aplikacja", sf::Style::Close);
okno.setKeyRepeatEnabled(false);
okno.setFramerateLimit(60);

sf::Event event;

while(okno.isOpen())
{
okno.clear(sf::Color(255, 255, 255));

  while (okno.pollEvent(event))
    {
        if (event.type == sf::Event::Closed) {okno.close(); }
    }

    if (event.type == sf::Event::KeyPressed)      
     {
       cout << event.key.code << endl;
     }

okno.display();

}
return 0;
}
I think, I've pasted enough.
Just click some keys on your keyboard.

>What is your B2?
http://www.examenglish.com/CEFR/cefr.php




#edit
Yes it's working in every project I've made. But - only for alt, ctrl, home keys. It should work fine for every key (letters, numbers), as the example says.
« Last Edit: August 29, 2014, 11:11:49 pm by noct »

Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
Re: [SFML 2.0]Problem with repeating buttons and events.
« Reply #5 on: August 29, 2014, 11:30:57 pm »
 
while (okno.pollEvent(event))
{
     if (event.type == sf::Event::Closed) {okno.close(); }
}
if (event.type == sf::Event::KeyPressed)      
{
     cout << event.key.code << endl;
}
Are you sure this is your code? Shouldn't it be like this instead?
while (okno.pollEvent(event))
{
     if (event.type == sf::Event::Closed)
         okno.close();
     else if (event.type == sf::Event::KeyPressed)      
              cout << event.key.code << endl;
}
 

noct

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • nctdev.pl
Re: [SFML 2.0]Problem with repeating buttons and events.
« Reply #6 on: August 29, 2014, 11:56:25 pm »
Oh.
So, okno.pollEvent(event) is checking all event events in okno, right?
How am I able to manage multiple events in one window? Or should I use this one for everything?
As there's while in main loop, won't it cause lags/something like that?
It's working (even for letters), but I've got a question - why wasn't it working earlier? (or working partially?)

So, I've got this:
while (okno.pollEvent(event))
{
    if (event.type == sf::Event::Closed) {okno.close(); }

    else if (event.type == sf::Event::KeyPressed)
    {
            if(event.key.code == sf::Keyboard::P && paused == 0)
            {
            cout << "Gra Wstrzymana" << endl;
            paused = 1;
            }
        else if(event.key.code == sf::Keyboard::P && paused == 1)
            {
            cout << "Gra Wznowiona" << endl;
            paused = 0;
            }
    }

}

And now, what with okno.setKeyRepeatEnabled(false); ? This code starts and stops my game in loop as long, as I'm holding P. How to disable this? (without any variables - I think that making "lock" variable for every key is a bad idea)

I want to write "skills"- functions, which would be called with keys (like Q, W, E, R). And when (wasd) moving it's simple (checking key state), in this case it's a little more complicated, as I need to check if a key was pressed-released (once)


#edit:
Problem solved - I should use else if instead of if. Shame on me, and thank you for pointing this out. Now it's working as it should be.
And, the last question: How am I able to manage multiple events in one window? Or should I use this one for everything?
« Last Edit: August 30, 2014, 12:34:32 am by noct »

Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
Re: [SFML 2.0]Problem with repeating buttons and events.
« Reply #7 on: August 30, 2014, 12:43:39 am »
Quote
So, okno.pollEvent(event) is checking all event events in okno, right?
How am I able to manage multiple events in one window? Or should I use this one for everything?
As there's while in main loop, won't it cause lags/something like that?
As you can see here SFML builds a LIFO ( I think) queue with every event happened in the last frame, pollEvent gives you the last even that has been registered. A loop is the only way to handle the events. What you do in the loop depends on what you need, but i recommend using the Thor library as soon as you've understood the flaws in your code (cleaner and sexier code :P).
Quote
It's working (even for letters), but I've got a question - why wasn't it working earlier? (or working partially?)
You were polling for the last event of the queue. The order is decided either by the OS or by how SFML was implemented (no time to check the source  ::)). The last IF statement was working on the same first event so it could only work if by chance the last event in the queue was keypressed.
Quote
And now, what with okno.setKeyRepeatEnabled(false); ? This code starts and stops my game in loop as long, as I'm holding P. How to disable this? (without any variables - I think that making "lock" variable for every key is a bad idea)
This is happening because you create a sf::event out of the scope and you don't poll the queue correctly.
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Window/Mouse.hpp>
#include <SFML/Window/Keyboard.hpp>
using namespace std;

int main()
{
        sf::RenderWindow okno(sf::VideoMode(800, 600), "title");
        while (okno.isOpen())
        {
                sf::Event event;
                while (okno.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                        {
                                okno.close();
                        }
                        else
                                if (event.type == sf::Event::KeyPressed)
                                {
                                        if (event.key.code == sf::Keyboard::P)
                                        {
                                                cout << "Gra Wstrzymana" << endl;
                                        }
                                }
                        else
                                if (event.type == sf::Event::KeyReleased) // pause until p is pressed
                                {
                                        if (event.key.code == sf::Keyboard::P)
                                        {
                                                cout << "Gra Wznowiona" << endl;
                                        }
                                }
                }
                okno.clear(sf::Color::Black);
                //okno.draw();
                okno.display();
        }
        return 0;
}
 
Quote
I want to write "skills"- functions, which would be called with keys (like Q, W, E, R). And when (wasd) moving it's simple (checking key state), in this case it's a little more complicated, as I need to check if a key was pressed-released (once)
I you can either do something like
while (okno.pollEvent(event))
{
        switch (event.type)
        {
        case sf::Event::KeyPressed:
                switch (event.key.code)
                {
                case sf::Keyboard::Q:
                        attackwithFireballs();
                        break;
                }
                break;
        default:
                break;
        }
}
 
if/else statements work too, however I'll always recommend Thor or a custom implementation for handling actions.
Quote
If I have:
bool abc;, does abc = 0; is equal to abc = false;?
The fact that the bool CLASS may be able to implicitly convert an integer number to bool shouldn't concern you. You MUST use either true or false because those are the keywords that were meant to work with bools.

TL;DR: you must read more tutorials and the SFML documentation.

noct

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • nctdev.pl
Re: [SFML 2.0]Problem with repeating buttons and events.
« Reply #8 on: August 30, 2014, 01:32:49 am »
Can I use while (okno.pollEvent(event_name)) more than once in the main loop? For different events ofc. Or it has no sense?
What I mean is - one event for special keys (like esc, alt, shift), and one for normal keys (letters and digits). I'd like to do it that way, to add some clarity to my code.

And after all, it works fine, event.type == sf::Event::KeyPressed (correctly used) is returning value only one time, when i press a key (well, thats legit :P)

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: [SFML 2.0]Problem with repeating buttons and events.
« Reply #9 on: August 30, 2014, 01:38:22 am »
The while loop processes (and then discards) all of the events that your window has received since the previous cycle. You should process all of the events while (pun not intended) you are in that loop. Basically, every event should be dealt with immediately (as soon as possible :P).
You can test all of the different types within that loop. You can use a series of ifs - probably nested to group the types - but they should probably be else ifs. You can also use switch here but ifs can be safer as breaks are often forgotten from switches  ;D
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: [SFML 2.0]Problem with repeating buttons and events.
« Reply #10 on: August 30, 2014, 01:43:45 am »
There's no way to make pollEvent only poll for certain kinds of events, so no, you can't do that directly.  But you can easily track special and normal characters separately when you process the keyboard events.  You can organize your input code however you want as long as you understand what pollEvent actually does.

It might be misleading to say that events should be "dealt with" as soon as possible.  It is true that they should be taken off the event queue as soon as possible (which is what looping on pollEvent() does), but you can simply set a bunch of local variables in this initial processing and save all the real interesting logic for a later part of your game loop.  In fact, that's usually much better.
« Last Edit: August 30, 2014, 01:45:32 am by Ixrec »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: [SFML 2.0]Problem with repeating buttons and events.
« Reply #11 on: August 30, 2014, 01:50:00 am »
@Ixrec, my wording did sound misleading; you're right! "Dealt with" really meant that the events should be processed as they will be no longer available afterwards. Processed, to be more clear, does not mean advance your logic etc. based upon them, more taken note of and prepared for later usage. hehe
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

noct

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • nctdev.pl
Re: [SFML 2.0]Problem with repeating buttons and events.
« Reply #12 on: August 30, 2014, 01:55:37 am »
I've got:
sf::Event event;
// (...)
while (okno.pollEvent(event))
{
         if (event.type == sf::Event::Closed)
         {
            okno.close();
         }

    else if (event.type == sf::Event::KeyPressed)
        {
            if(event.key.code == sf::Keyboard::P && paused == 0)
            {
             cout << "Gra Wstrzymana" << endl;
             paused = 1;
            }
            else if(event.key.code == sf::Keyboard::P && paused == 1)
            {
             cout << "Gra Wznowiona" << endl;
             paused = 0;
            }
            else if(event.key.code == sf::Keyboard::R)
            {
             //do something;
            }
        }

}

Now, I'd like to work on other keys. Also I would not like to keep this part in the same place (it's uncomfortable for me) Should I do it like above (just adding else ifs, or with second event:

// (code from above, then)
sf::Event otherEvent;
while (okno.pollEvent(otherEvent))
// if's, etc.
 

Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
Re: [SFML 2.0]Problem with repeating buttons and events.
« Reply #13 on: August 30, 2014, 01:58:46 am »
I've spent more than 20 mins writing the last reply, have you read it yet? Why do you keep using bool = signed integer?

noct

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • nctdev.pl
Re: [SFML 2.0]Problem with repeating buttons and events.
« Reply #14 on: August 30, 2014, 02:06:07 am »
@Strelok: Oh, actually I've read it twice (nothing is tl to read, when it can help). This is temporary, because now I'm on my phone, and I'm to lazy to change it. Aand I think it's not about this, in this case :)