Sign in to follow this  
tashaklikedi

I need help with joystick handling

Recommended Posts

Hi, I'm trying to create a double dragon - like game, and I'm trying to implement a joystick.

The problem is that until now I programmed relative to the SDL 's keyboard event handling system, which involves the key states "keydown" when a key is pressed, and "keyup" when a key is released. But I can't find a way to do the keyup part with the SDL joystick functions.

The reason I want to use this is that when you double tap a horizontal direction key, the character runs. For that I developed a counter system, in which when I release the direction key, there is a counter that is incremented and that says that I pressed forward once.

now I will paste the code here.

this is the key release function for the left key:
[source lang="cpp"]
case SDLK_LEFT: {
moveLeft = false;
runCounterLeft++;
runCounterRight = 0;

}
break;[/source]

and this is the function that activates the run function:

[source lang="cpp"]void CNinja::runToggle()
{


if(runToggleSwitch) // the counter is at most at 1
{
if(runCounterRight == 1)
{

{
runStart = SDL_GetTicks(); //time offset for the last released direction key
runToggleSwitch = false;
}
}
if(runCounterLeft == 1)
{

{
runStart = SDL_GetTicks();
runToggleSwitch = false;
}
}


}

if(runStart + 200 < SDL_GetTicks()) // resetting the counter states if two taps are delayed too much
{
runToggleSwitch = true;
runCounterLeft = 0;
runCounterRight = 0;
}


if((runCounterRight > 1||runCounterLeft > 1)&&runToggleSwitch == false) // if the counter is not reset and if it reached 2, we initiate the run function (which will be triggered by the bool running = true)
{

if(SDL_GetTicks() - runStart > 500) //i'm not sure anymore but now that I see it it looks like the same as above :/ not sure if this block is necessary
{
running = false;
runToggleSwitch = true;
}
else running = true;
runToggleSwitch = true;
runCounterRight = 0;
runCounterLeft = 0;
}
}[/source]

first and simpler : do anyone have a more versatile alternative for this to my system?

second: how can I adapt this to the SDL joystick?

Share this post


Link to post
Share on other sites
Instead of explaining it all, Lazy Foo's tutorial should sum it up for you; well, at least somewhat:

[url="http://lazyfoo.net/SDL_tutorials/lesson25/index.php"]http://lazyfoo.net/SDL_tutorials/lesson25/index.php[/url]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this