Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Extending Gosu / InputHandler class (C++ and Ruby)
- By ChadyG Date 2010-02-22 06:50
I haven't seen anyone sharing code for something like this, so I figured once I got one built for myself I'd share.

This is a manager for high level input based on three types of game input;
simple actions
    - simple key binding
chords
    - multiple keys pressed at once for an action
sequences
    - multiple keys pressed in order for an action

Each of these are presented in a way such that you provide a named action, and this class will poll Gosu's input methods to determine the state of it.  Simple actions are pretty straight forward, if any of the buttons are pressed, the action will fire.  Chords and sequences are a little bit more complicated.  Chords allow the user some amount of time (here it is hard coded to 10 frames, but you can change that however you please) to press down all of the buttons.  Sequences permit the user some threshold between keystrokes to fire (this threshold is determined when creating the action).  All three of these input types will have one of five states;
idle - not active
process - chord or sequence has started
begin - the key has just been pressed
active - the key(s) is/are held down
finish - the/a key has just been released

I did not implement any event triggering code, and instead opted for a query interface.  This was simply just because it was easier to write (plus, for my projects I don't have much need for anything more complicated :D ).

At any rate, I started out mocking the code in Ruby then created the C++ code I needed.  Since I'm a nice guy (so I'm told), and I know most of you enjoy using the Ruby end of Gosu, I finished the Ruby version and wrote a simple test case (instead of proper documentation).  If any of you want to use the C++ code, hopefully you can figure out how to use it from the header file, if not I'll be happy to answer questions here.  As for the Ruby, you should be able to gather everything from the test case.  But I'll still answer any questions you have if I can.

Happy coding!

http://dl.dropbox.com/u/6366/ChadyG_InputHandler.zip
note: This code is fresh as of today, so it may have some bugs.  Also I realize the Ruby version could be cleaner, but it should still be usable.
- By jlnr (dev) Date 2010-02-23 11:52
The sequences remind me that I still haven't seen a Gosu or Ruby fighting game… hey, when is the next Ludum Dare… :>
- By RavensKrag Date 2010-03-09 05:53
This is very useful code, and I really do appreciate it, but the Ruby code looks quite awful.  I'm going to try to rewrite it to make it look cleaner and not use the instance variable @time, as I think that Gosu::milliseconds or some other preexisting variable should do the job.  If that ever happens, I'll try to remember to post the resultant code back up here.

It'd be cool to see someone turn this into C wrapped in Ruby all packaged nice and neat in a gem, but that might be hoping for too much ^_^;
Up Topic Gosu / Extending Gosu / InputHandler class (C++ and Ruby)

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill