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

Author Topic: From Event to Key  (Read 1886 times)

0 Members and 2 Guests are viewing this topic.

Rexona for men

  • Newbie
  • *
  • Posts: 24
    • View Profile
From Event to Key
« on: October 26, 2011, 05:13:51 pm »
Hello guys

I played a bit around with the SFML and encounter a problem, for which I havent a solution.

I work on a little game with a spaceship, which fly around.
I have a main game loop with event,move objects, clear screen, draw objects and display.
However, I stuck on the event to move part

my function is like this
Code: [Select]

switch (Event.Type)
some events
.
.
case Window.KeyPressed:
    InputManager (Window.GetInput () )


And InputManager looks like this
Code: [Select]

void InputManager (sf::Key::Code Input)
switch (Input)
    case sf::Key::Code::Left
        Move (left)
.
.
some other key case



Ive thought about seperate the functions, but I dont like the idea.
For me, an input is an event.
Of course there is the function IsKeyPressed, but it only returns true or false and not the pressed key.
Also sf::Input cant be put into a switch statement, as like IsKeyPressed.
I dont have a solution, maybe anyone have a better Idea?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
From Event to Key
« Reply #1 on: October 26, 2011, 05:25:37 pm »
I don't really understand your problem.

Why don't you want this?
Code: [Select]
const sf::Input& input = app.GetInput();
if (input.IsKeyDown(sf::Key::Left))
    ship.MoveLeft();
...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Rexona for men

  • Newbie
  • *
  • Posts: 24
    • View Profile
From Event to Key
« Reply #2 on: October 26, 2011, 05:51:14 pm »
I thought about such a solution like this but first , I dont want such a huge if-statement, because a switch statement would be much clearer in this case in my opinion.
And second, I dont want to separate keyinput from event.

To make it maybe a bit clearer, I want that a pressed key counts as an event like Window.Close ()

At the moment, I try this (but have to wait a bit for the spaceship sprite)

Code: [Select]

switch (event.Type)
some events
.
.
case KeyPressed
    InputManager (event.Key.Code) // go on with the pressed key


Will this code work?


Hm, I thnik I will post the whole code  :D

GameProcedure
Code: [Select]

void Game::GameProcedure ()
{
    while (Window.IsOpened () )
    {
        sf::Event Event;
        while (Window.GetEvent (Event) )
        {
switch (Event.Type)
{
case sf::Event::Closed:
{
Window.Close ();
}
break;
case sf::Event::KeyPressed:
{
InputManager (Event.Key.Code);
}
break;
}
        }
        Window.Clear ();

Window.Draw (S_Background);

        Window.Display ();
    }
}



And here is InputManager
Code: [Select]

void Game::InputManager (sf::Key::Code Input)
{
float ElapsedTime = Window.GetFrameTime ();

switch (Input)
{
case sf::Key::Left:
{
MyPlayer.Move (-400 * ElapsedTime, 0);
}
case sf::Key::Right:
{
MyPlayer.Move (400 * ElapsedTime, 0);
}
}
}

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
From Event to Key
« Reply #3 on: October 26, 2011, 06:41:14 pm »
Quote from: "Rexona for men"
And second, I dont want to separate keyinput from event.
Ah okay. You can't handle events and realtime states uniformly with native SFML.

But coincidentally (:D) I have written an extension which is capable of doing that. You can not use switch however, since it only works with integral constants. The idea is to have "actions" which are represented by IDs (here std::string, but this can be any type). You can even combine multiple events to a single action:
Code: [Select]
thor::ActionMap<std::string> map(window);

// Press left arrow or A key to trigger "left" action
map["left"] = thor::Action(sf::Key::Left) || thor::Action(sf::Key::A);
map["right"] = thor::Action(sf::Key::Right) || thor::Action(sf::Key::D);
map["exit"] = thor::Action(sf::Event::Closed);

...
// in your loop
map.Update();
if (map.IsActive("left"))
    ship.MoveLeft();
else if (map.IsActive("right"))
    ship.MoveRight();
else if (map.IsActive("close"))
    window.Close();

Another option (especially useful if you want to know more about the triggering event) are callback functions which can be linked to those actions.
Code: [Select]
thor::ActionMap<std::string>::CallbackSystem callbacks;
callbacks.Connect("left", std::bind(&Ship::Move, &ship, -400 * elapsedTime, 0.f) );
// automatically calls ship.Move(-400 * elapsedTime, 0) when left key is pressed

// in loop
map.Update();
map.InvokeCallbacks(callbacks);

This functionality is part of my library Thor, here are links to documentation and tutorial. But you need a current revision of SFML 2.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Rexona for men

  • Newbie
  • *
  • Posts: 24
    • View Profile
From Event to Key
« Reply #4 on: October 26, 2011, 08:46:19 pm »
Thank you for your help, but my new Method work fine  :D

 

anything