PDA

View Full Version : Ideas for configurable SDL controls?



paul_nicholls
20-07-2009, 12:06 AM
Hi all,
I am trying to come up with ideas on how I can make configurable controls using SDL for my game...

I want to be able to assign different actions to key events, mouse events, and joystick events (when applicable).

At the moment I have methods in my game class that receives events from the keyboard, mouse, and joysticks (if plugged in) using the SDLinput unit like so:

Procedure TGame.OnKeyUp(var Key: TSDLKey; Shift: TSDLMod; unicode : UInt16);
Var
State : TDODBaseState;
Begin
If UpCase(Chr(Key)) = 'S' Then TakeScreenShot('Screenshot.tga');

State := TDODBaseState(FStateMachine.CurrentState);
If Assigned(State) Then
State.OnKeyUp(Key,Shift,unicode)
Else
If Key = SDLK_ESCAPE Then ExitApplication;
End;
{................................................. .............................}

{................................................. .............................}
Procedure TGame.OnKeyDown(var Key: TSDLKey; Shift: TSDLMod; unicode : UInt16);
Begin
End;
{................................................. .............................}

{................................................. .............................}
Procedure TGame.OnMouseUp(Button : Integer; Shift: TSDLMod; MousePos : TPoint);
Begin
End;
{................................................. .............................}

{................................................. .............................}
Procedure TGame.OnMouseDown(Button : Integer; Shift: TSDLMod; MousePos : TPoint);
Begin
End;
{................................................. .............................}

{................................................. .............................}
Procedure TGame.OnMouseMove(Shift: TSDLMod; CurrentPos : TPoint; RelativePos : TPoint);
Begin
End;
{................................................. .............................}

{................................................. .............................}
Procedure TGame.OnJoystickUp(Which: UInt8; Button: UInt8; State: SInt16);
Begin
End;
{................................................. .............................}

{................................................. .............................}
Procedure TGame.OnJoystickDown(Which: UInt8; Button: UInt8; State: SInt16);
Begin
End;
{................................................. .............................}

{................................................. .............................}
Procedure TGame.OnAxisMove(Which: UInt8; Axis: UInt8; Value: SInt16);
Begin
If Abs(Value) > 20 Then
Begin
If Axis = 0 Then
Begin
FMoveLeft := Value < 0;
FMoveRight := Value > 0;
End
Else
If Axis = 1 Then
Begin
FMoveUp := Value < 0;
FMoveDown := Value > 0;
End;
End;
End;
{................................................. .............................}

{................................................. .............................}
Procedure TGame.OnBallMove(Which: UInt8; Ball: UInt8; RelativePos: TPoint);
Begin
End;
{................................................. .............................}

{................................................. .............................}
Procedure TGame.OnHatMove(Which: UInt8; Hat: UInt8; Value: SInt16);
Begin
End;


Instead of hard-coding the controls, I want to somehow define actions using user defined keys, buttons, etc.

I THOUGHT there was some post ages ago on this subject, but I can't seem to find one...

cheers,
Paul

vgo
20-07-2009, 10:43 AM
Basically what I did for my cross-platform input code was that I first made a unit with the generic input codes and classes. Then I derived new classes (for SDL, DirectInput and Windows) from this, for example in the OnKeyUp the SDL input is "translated" to the generic input codes with a simple table lookup and passed on to the actual OnKeyUp for doing the input handling.

The lookup table is read from a file or created on the fly with the default configuration and the key assignment is easy to do. I made generic input codes for mouse and joystick buttons too and now I can map any (key press) action to keyboard, mouse button or joystick button.

For example, I have TInputHandler class and the TSDLInputHandler is derived from this, when the game engine initializes it chooses which input handler to use. All the input handler classes have the Poll() method that does the actual key polling etc. In the game engine I have another input class TGameInput which has public OnKeyUp etc. events that are triggered for handling the game input.

The simplest way is to have just the generic input class that is used to handle the game input with public events and the platform dependent classes are derived from this. The base class does all the event triggering, the derived classes only translate the input to the generic input codes to pass on to the game (MyInput.Poll(); MyInput.TriggerEvents(); ). Don't know if that makes any sense, but that's how it works. :D

paul_nicholls
21-07-2009, 04:13 AM
Basically what I did for my cross-platform input code was that I first made a unit with the generic input codes and classes. Then I derived new classes (for SDL, DirectInput and Windows) from this, for example in the OnKeyUp the SDL input is "translated" to the generic input codes with a simple table lookup and passed on to the actual OnKeyUp for doing the input handling.

The lookup table is read from a file or created on the fly with the default configuration and the key assignment is easy to do. I made generic input codes for mouse and joystick buttons too and now I can map any (key press) action to keyboard, mouse button or joystick button.

For example, I have TInputHandler class and the TSDLInputHandler is derived from this, when the game engine initializes it chooses which input handler to use. All the input handler classes have the Poll() method that does the actual key polling etc. In the game engine I have another input class TGameInput which has public OnKeyUp etc. events that are triggered for handling the game input.

The simplest way is to have just the generic input class that is used to handle the game input with public events and the platform dependent classes are derived from this. The base class does all the event triggering, the derived classes only translate the input to the generic input codes to pass on to the game (MyInput.Poll(); MyInput.TriggerEvents(); ). Don't know if that makes any sense, but that's how it works. :D


Hi vgo, thanks for the explanation :)

I THINK it makes sense, but just in case do you have a code snippet or example you can show me?

cheers,
Paul