How are you getting user input with SDL in your menu?

Started by
3 comments, last by Foot Soldier 17 years ago
I am trying to figure out the best way to get user input in the control panel. I am using SDL, Guichan and have the means to get the input, but not the whole idea or what is the best/easiest method of doing this for all controls. I have a gamepad, and the usual keyboard/mouse. But with all the possible inputs what/how should I go about this. This is new to me since I haven't had the need to do this before since my game hasn't gotten to this point. Thanks
Advertisement
Wait, so is your problem getting the input from SDL and passing that information to GUI chan to interpret? Or are you just talking about the best way to process SDL input in general?


I think the answer (regardless) is: it depends. The way I do it is to have my input manager class monitor the change in keyboard and gamepad/joystick state every frame (we don't use mouse input in our game) and then it sets various boolean flags to state whether a key has been pressed, released, or is being held down. Here's a link to the header/source file that shows how this is done:

Input manager header file

Input manager source file


Now technically I think you could avoid the event polling every frame and just grab the keyboard state instead, but because we allow the player to re-map the keys its actually a bit easier for us this way, since we don't have to translate the key mappings to the commands all the time. Hope this helps a little.

Hero of Allacrost - A free, open-source 2D RPG in development.
Latest release June, 2015 - GameDev annoucement

Oh, and for the GUI controls (we use our own built from scratch GUI engine) its a bit inefficient how we do it there. Basically, when navigating a series of menus (on an equip screen in an RPG for example) you have to determine which window has the active context (ie the cursor). Depending on the window that is active, every frame you examine the relevant user input keys (directional, confirm, cancel, etc) and then if we are dealing with an option box (a series of options that the user can select, like an inventory list) we have to pass that input to the OptionBox class via a call to that class (e.g. HandleUpKey()).

Its kinda nasty and technically we could hook it up so that the option box could do that input handling/checking itself, but then the video engine would be dependent on our video engine, and also it gives the programmer less control over interpreting commands (although I'm sure we could work around that).

Hero of Allacrost - A free, open-source 2D RPG in development.
Latest release June, 2015 - GameDev annoucement

I want to get the user input like doom or any FPS games does where you click on the box and hit the key or mouse button you want to assign to that action. I am not sure on how to go about that. I know how to get input and such, more the method to the madness I am looking for. Hope that clears it up.
I would basically have a conditional statement in the SDL_KEYDOWN message, that goes off if they just hit a button to set a key. Here is the example:

- User presses GUI button to set the "foreward" key. Pressing this button sets a varible "foreward_key_check" to true;
- In the message loop, in the keydown section, you check if "foreward_key_check" is true. If so, you set the key that is being dealt with as the foreward key.
- Once you set the foreward key, set "foreward_key_check" to false.
- In the code for handling the key press to move foreward, instead of checking a constant, you check for if the key == a variable that holds the key the user chose.

Of course, wrap this up in tidy classes or functions or whatever, but this is the general idea. Do the same check in mouse code as well.
Sure is a big 'ol world.

This topic is closed to new replies.

Advertisement