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

Author Topic: SFML Input Manager  (Read 11043 times)

0 Members and 1 Guest are viewing this topic.

dabbertorres

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • website/blog
SFML Input Manager
« on: March 28, 2014, 05:10:05 am »
Hi, I thought it'd be good to share something I made recently.

https://github.com/dabbertorres/SwiftInputManager

Here's the example of use on github:
#include <SFML/System.hpp>
#include <SFML/Window.hpp>

#include "KeyboardManager.hpp"
#include "MouseManager.hpp"

int main(int argc, char** argv)
{
    sf::Window window(sf::VideoMode(800, 600, 32), "SFML");

    swift::KeyboardManager keyboard;
    swift::MouseManager mouse;

    keyboard.newBinding("exit", sf::Keyboard::Escape, [&window]()
    {
        window.close();
    });

    mouse.newBinding("exit", sf::Mouse::Right, [&window]()
    {
        window.close();
    });

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

        while(window.pollEvent(event))
        {
            keyboard(event);
            mouse(event);

            switch(event.type)
            {
                case sf::Event::Closed:
                    window.close();
                    break;
                default:
                    break;
            }
        }

        window.clear();

        window.display();
    }

    return 0;
}
 

Multiple uses of the same button are possible too.

An improvement that I'd like to make in the future though are for multiple keypresses, (Ctrl+A, etc).

Nothing too exciting, but in my own bigger projects I figured this would be a nice way of keeping all of the input handling of keypresses and such wrapped up. It works great for me, so I figured I'd share it, and if anyone is willing, share what they think of it. Thoughts on the quality of the code (I'd love feedback, I haven't shared much of my own, and as I'm pretty much self-taught...), etc, would be great!

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: SFML Input Manager
« Reply #1 on: April 06, 2014, 12:20:40 am »
Looks rather useful.  Just wondering even though it might be overkill why not a SFML.net version too? :P C# using events but not sure about the other .net languages though so it should be interesting.
I have many ideas but need the help of others to find way to make use of them.

dabbertorres

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • website/blog
Re: SFML Input Manager
« Reply #2 on: April 06, 2014, 12:34:58 am »
I don't know C# all that well is mainly why. May be a good starting to point to get to know the language better though.

I believe it is possible to call C++ code from C# though, you may do some research into that. If you got that down, that'd definitely give you a look into more of the stuff people have made on here.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: SFML Input Manager
« Reply #3 on: April 06, 2014, 12:42:25 am »
StormWingDelta, you don't need to go around asking for everything to be written in C# for you  :) Anyways its not difficult to port it yourself if you want it + you get experience. To my point, something very similar to this already exists in my NetEXT Input module if you dare to take a look.  ;)

Quote
I believe it is possible to call C++ code from C# though, you may do some research into that. If you got that down, that'd definitely give you a look into more of the stuff people have made on here.

On a side note this is not directly possible, you need a C style function interface to call unmanaged code, hence CSFML.
« Last Edit: April 06, 2014, 12:44:22 am by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

MorleyDev

  • Full Member
  • ***
  • Posts: 219
  • "It is not enough for code to work."
    • View Profile
    • http://www.morleydev.co.uk/
Re: SFML Input Manager
« Reply #4 on: April 06, 2014, 01:21:37 am »
On a side note this is not directly possible, you need a C style function interface to call unmanaged code, hence CSFML.

Not strictly true. You can use C++/CLI to interface between C++ and .NET, wrapping C++ classes in a CLI wrapper. But this isn't compatible with Mono (to the best of my knowledge).
UnitTest11 - A unit testing library in C++ written to take advantage of C++11.

All code is guilty until proven innocent, unworthy until tested, and pointless without singular and well-defined purpose.

Doodlemeat

  • Guest
Re: SFML Input Manager
« Reply #5 on: April 06, 2014, 01:41:49 am »
This could be useful if extended with more functions.
Take a look at Thors action handling http://www.bromeon.ch/libraries/thor/

Also a really near feature would be to support multiple mice devices and keyboards. In WIN, this is done using raw input. Take a look at the ManyMouse library. Its a pretty old lib but it works (using it in a current project). You have to read from connected HID. ManyMouse works has also support for Mac OSX and Linux, but I have only tested it on Windows so far.
But you have to create the multiple keyboard by yourself but I believe you can take a lot of the code from ManyMouse.
http://icculus.org/manymouse/

As for now, I have no reason to use this since I am using Thor.
Good luck and I really hope more people considers to be wanting multiple device support  ::)

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: SFML Input Manager
« Reply #6 on: April 06, 2014, 01:44:18 am »
Not strictly true. You can use C++/CLI to interface between C++ and .NET, wrapping C++ classes in a CLI wrapper. But this isn't compatible with Mono (to the best of my knowledge).

Yes that is true and it is possible to do that. But as you said, it isn't cross platform and MS doesn't even support it anymore.  :P
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Re: SFML Input Manager
« Reply #7 on: June 15, 2014, 11:16:03 pm »
I'm late but I find your code very interesting and useful. I just don't get why you name the bindings, this std::string is useless isn't it ?

It could be improved following Thor's combinaisons of actions... but you made this to be simple I think. If one does need more, he can use Thor directly :)

dabbertorres

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • website/blog
Re: SFML Input Manager
« Reply #8 on: June 15, 2014, 11:52:29 pm »
Thanks! Glad to hear someone else found it useful in some way.

And yeah, I guess the strings are "useless", but I found that to be the best way of having different keys in the map. For example, if you want to call something upon the press of a key, AND something called upon release of the same key. I considered an enum, but then the user would have to make his own/add to it, and I didn't like that.

It definitely could be improved. I like what Nexus did with Thor's Actions, I might try to improve this more in the future, but we'll see.

dabbertorres

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • website/blog
Re: SFML Input Manager
« Reply #9 on: August 11, 2014, 04:31:20 am »
Been a while, but I made a small update to this recently, by adding a call function.

It does two things: Make the std::string keys somewhat useful, and essentially allows you to simulate a keypress in code, by directly calling the function you bound to a key.

I found it useful in a few cases I ran into.