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

Author Topic: sf::Input.getKeys()  (Read 7981 times)

0 Members and 1 Guest are viewing this topic.

Zweistein

  • Newbie
  • *
  • Posts: 29
    • View Profile
sf::Input.getKeys()
« on: June 12, 2010, 02:10:04 am »
Hello,

I would like to have a function sf::Input.getKeys() like that:

Code: [Select]
bool[Key::Count] sf::Input Input::getKeys() {
 return myKeys;
}


The Problem is, that iw ould like to know if a Key was pressed in this frame. like an "OnPress" Event. With that function i could test the array in the current frame and in the last frame.

Maybe you can even add this to the library, so i dont have to program it on my own? Same would be with Mouseclicks.

bool Input::isKeyHold()
bool Input::isKeyPressed()
bool Input::isKeyReleased()

lg

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
sf::Input.getKeys()
« Reply #1 on: June 12, 2010, 02:59:11 am »
What you need to do is compare the keys that are down this frame and in the previous frame. Have an array of bools that stores the key states from the previous frame. Then, to check if a key was pressed, the previous frame's array would say it's not pressed, and the current frame's array would say it is pressed, and vice-versa for released.
I use the latest build of SFML2

Zweistein

  • Newbie
  • *
  • Posts: 29
    • View Profile
sf::Input.getKeys()
« Reply #2 on: June 12, 2010, 02:14:25 pm »
Quote
Have an array of bools that stores the key states from the previous frame


Yeah, thats why i would like to get the "myKeys" variable from the "Input" class. It is such an array.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Input.getKeys()
« Reply #3 on: June 12, 2010, 03:17:18 pm »
Quote from: "Zweistein"
I would like to have a function sf::Input.getKeys() like that:
Code: [Select]
bool[Key::Count] sf::Input Input::getKeys() {
 return myKeys;
}
This is not possible because C++ doesn't allow to return arrays from functions.

Why can't you use bool sf::Input::IsKeyDown(sf::Key::Code key)? That function fits exactly your purpose. You don't even have to store an own array.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

kitchen

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: sf::Input.getKeys()
« Reply #4 on: June 12, 2010, 04:54:27 pm »
Quote from: "Nexus"
This is not possible because C++ doesn't allow to return arrays from functions.


This is untrue. His syntax may be wrong, but it is possible.

Code: [Select]
bool * foo(int bar)
{
    bool *baz = new bool[bar];
    return baz;
}

Zweistein

  • Newbie
  • *
  • Posts: 29
    • View Profile
sf::Input.getKeys()
« Reply #5 on: June 12, 2010, 05:53:59 pm »
Quote
Why can't you use bool sf::Input::IsKeyDown(sf::Key::Code key)? That function fits exactly your purpose. You don't even have to store an own array.


No, it doesnt exactly. Per Example i want to change from Game to GUI on pressing the Escape key. I wouldn t check that with "isKeyDown(sf::Key::Escape)", because then i would switch 60 times a Second from Game to GUI. So the User would have to Press the Key 1/60 Seconds long :)

But it would work with isKeyReleased(sf::Key::Escape), because this is true only once per Keyhit.

I can implement it for myself, but maybe its faster and easier to have it in SFML. My Implementation however looks that way:


Code: [Select]

void Input::refresh() {
m_KeyPresses.clear();
m_KeyReleases.clear();

while (m_Window.GetEvent(Event))
{
switch (Event.Type)
{
case sf::Event::KeyPressed:
m_KeyPresses.push_back(Event.Key.Code);
break;

case sf::Event::KeyReleased:
m_KeyReleases.push_back(Event.Key.Code);
break;
}
}
}

bool Input::isKeyPressed(const sf::Key::Code& _KeyCode)
{
for(std::list<sf::Key::Code>::iterator IT = m_KeyPresses.begin(); IT != m_KeyPresses.end(); IT++) if (*IT == _KeyCode) return true;
return false;
}

bool Input::isKeyReleased(const sf::Key::Code& _KeyCode)
{
for(std::list<sf::Key::Code>::iterator IT = m_KeyReleases.begin(); IT != m_KeyReleases.end(); IT++) if (*IT == _KeyCode) return true;
return false;
}

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
sf::Input.getKeys()
« Reply #6 on: June 12, 2010, 07:32:26 pm »
Quote from: "kitchen"
Quote from: "Nexus"
This is not possible because C++ doesn't allow to return arrays from functions.
This is untrue.
It is true. What you return in your example, is a pointer, not an array.

Quote from: "Zweistein"
No, it doesnt exactly.
Ah, I see. Actually, I had exactly the same idea some time ago. I considered it quite useful to handle realtime and onetime events in a uniform way.

But I believe this is out of SFML's scope, because it's not really basic functionality. It would probably restrict the current event polling system. I don't know either how many users require this feature. Anyway, this is just my point of view, maybe Laurent communicates his opinion.

I would implement the input handler similarly to you. However, you could make the code faster (logarithmic instead of linear complexity) when you use std::set instead of std::list. But regarding the fact that there will be only a few events every frame and that dynamic allocation costs much more than a search, std::vector combined with std::find() might be the fastest. Just try and find out what's the most efficient on your compiler. But std::list is not, that's for sure. ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::Input.getKeys()
« Reply #7 on: June 12, 2010, 09:44:11 pm »
Quote
But I believe this is out of SFML's scope, because it's not really basic functionality. It would probably restrict the current event polling system. I don't know either how many users require this feature. Anyway, this is just my point of view, maybe Laurent communicates his opinion.

I have nothing to add to this :)
Laurent Gomila - SFML developer

kitchen

  • Newbie
  • *
  • Posts: 14
    • View Profile
sf::Input.getKeys()
« Reply #8 on: June 13, 2010, 12:41:52 am »
Quote from: "Nexus"
Quote from: "kitchen"
Quote from: "Nexus"
This is not possible because C++ doesn't allow to return arrays from functions.
This is untrue.
It is true. What you return in your example, is a pointer, not an array.


You're wrong, so I'll leave you with some questions you can try to answer. What's the difference between a pointer and an array? How would you return an array from a function in C++?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
sf::Input.getKeys()
« Reply #9 on: June 13, 2010, 01:08:06 am »
Quote from: "kitchen"
You're wrong, so I'll leave you with some questions you can try to answer. What's the difference between a pointer and an array?

Sorry, but I am certainly right. C++ doesn't allow arrays to be return types of functions.
Quote from: "C++ Standard, §8.3.5/6"
Functions shall not have a return type of type array or function [...]

Static arrays and pointers are completely different language features. While arrays can be used as collections which store multiple values of the same type, pointers represent indirections pointing to another object or to NULL. However, both concepts are often confused because there exists an implicit conversion of every array to a pointer to its first element. Another reason might be that the result of a new[] expression is often called array, although it is actually a pointer (let's call it "dynamic array").

Quote from: "kitchen"
How would you return an array from a function in C++?
As I said, you can't return arrays directly. You can use a wrapper like std::tr1::array, or return a pointer to dynamically allocated memory containing multiple elements.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

kitchen

  • Newbie
  • *
  • Posts: 14
    • View Profile
sf::Input.getKeys()
« Reply #10 on: June 13, 2010, 02:17:14 pm »
Quote from: "Nexus"
Quote from: "kitchen"
You're wrong, so I'll leave you with some questions you can try to answer. What's the difference between a pointer and an array?

Sorry, but I am certainly right. C++ doesn't allow arrays to be return types of functions.
Quote from: "C++ Standard, §8.3.5/6"
Functions shall not have a return type of type array or function [...]

Static arrays and pointers are completely different language features. While arrays can be used as collections which store multiple values of the same type, pointers represent indirections pointing to another object or to NULL. However, both concepts are often confused because there exists an implicit conversion of every array to a pointer to its first element. Another reason might be that the result of a new[] expression is often called array, although it is actually a pointer (let's call it "dynamic array").

Quote from: "kitchen"
How would you return an array from a function in C++?
As I said, you can't return arrays directly. You can use a wrapper like std::tr1::array, or return a pointer to dynamically allocated memory containing multiple elements.


While you are technically correct, you're arguing semantics. Returning a pointer might as well be the same exact thing as returning an array. Just because technically you would call it one thing, does not mean the end result isn't another. That is, while I am returning a pointer, for all intents and purposes I am returning an array.

So when the OP posted his snippet of incorrect code where the desired result of it was clear, instead of arguing and saying definitively that something he was trying to accomplish was impossible, you could've pointed him in the direction of how to do it.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
sf::Input.getKeys()
« Reply #11 on: June 13, 2010, 04:21:54 pm »
Quote from: "kitchen"
So when the OP posted his snippet of incorrect code where the desired result of it was clear, instead of arguing and saying definitively that something he was trying to accomplish was impossible, you could've pointed him in the direction of how to do it.
Okay, I understand that. Sorry for focussing too much on the technical details, but I find the removal of spread C++ misbelieves (like the equality of pointers and arrays) important.

The reason why I didn't show Zweistein how to return arrays (semantically) is that I thought it would be the wrong approach. In the same post, I gave him the hint to use sf::Input::IsKeyDown() instead.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

kitchen

  • Newbie
  • *
  • Posts: 14
    • View Profile
sf::Input.getKeys()
« Reply #12 on: June 14, 2010, 01:02:25 am »
Quote from: "Nexus"
Sorry for focussing too much on the technical details, but I find the removal of spread C++ misbelieves (like the equality of pointers and arrays) important.


That is understandable.

Zweistein

  • Newbie
  • *
  • Posts: 29
    • View Profile
sf::Input.getKeys()
« Reply #13 on: June 14, 2010, 02:28:34 pm »
Ok, thanks for the answers.

Quote
But std::list is not, that's for sure.


Thanks for that Tipp, too. I dont understand when to use what container yet.

kitchen

  • Newbie
  • *
  • Posts: 14
    • View Profile
sf::Input.getKeys()
« Reply #14 on: June 14, 2010, 03:43:24 pm »
Quote from: "Zweistein"
Ok, thanks for the answers.

Quote
But std::list is not, that's for sure.


Thanks for that Tipp, too. I dont understand when to use what container yet.


This should help you out :)

 

anything