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?
Show differencesHistory of post edits
#1tashaklikedi
Posted 26 September 2012 - 03:32 AM
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?
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?