better controller handling
This commit is contained in:
65
lib/ui.c
65
lib/ui.c
@ -36,13 +36,14 @@ static int screenHeight;
|
||||
void ui_init(){
|
||||
screenWidth = XRES*scale;
|
||||
screenHeight = YRES*scale;
|
||||
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
|
||||
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER);
|
||||
printf("SDL INIT\n");
|
||||
TTF_Init();
|
||||
printf("TTF INIT\n");
|
||||
|
||||
SDL_JoystickEventState(SDL_ENABLE);
|
||||
SDL_JoystickOpen(0);
|
||||
//SDL_JoystickEventState(SDL_ENABLE);
|
||||
//SDL_JoystickOpen(0);
|
||||
SDL_GameControllerOpen(0);
|
||||
|
||||
char buffer[1024];
|
||||
strcpy(buffer, emu_get_context()->app_path);
|
||||
@ -412,6 +413,19 @@ void ui_on_joy_button(u8 button, bool down) {
|
||||
}
|
||||
}
|
||||
|
||||
void ui_on_controller_button(u8 button, bool down) {
|
||||
switch(button){
|
||||
case SDL_CONTROLLER_BUTTON_A: gamepad_get_state()->b = down; break;
|
||||
case SDL_CONTROLLER_BUTTON_B: gamepad_get_state()->a = down; break;
|
||||
case SDL_CONTROLLER_BUTTON_START: gamepad_get_state()->start = down; break;
|
||||
case SDL_CONTROLLER_BUTTON_BACK: gamepad_get_state()->select = down; break;
|
||||
case SDL_CONTROLLER_BUTTON_DPAD_DOWN: gamepad_get_state()->down = down; break;
|
||||
case SDL_CONTROLLER_BUTTON_DPAD_UP: gamepad_get_state()->up = down; break;
|
||||
case SDL_CONTROLLER_BUTTON_DPAD_LEFT: gamepad_get_state()->left = down; break;
|
||||
case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: gamepad_get_state()->right = down; break;
|
||||
}
|
||||
}
|
||||
|
||||
void ui_on_joy_hat(u8 hat, u8 value) {
|
||||
if(hat == 0){
|
||||
gamepad_get_state()->up = value & SDL_HAT_UP;
|
||||
@ -446,6 +460,29 @@ void ui_on_Joy_axis(u8 axis, int16_t value) {
|
||||
}
|
||||
}
|
||||
|
||||
void ui_on_controller_axis(u8 axis, int16_t value) {
|
||||
if(axis == SDL_CONTROLLER_AXIS_LEFTY) {
|
||||
gamepad_get_state()->up = false;
|
||||
gamepad_get_state()->down = false;
|
||||
if(value < -DEADZONE) {
|
||||
gamepad_get_state()->up = true;
|
||||
}
|
||||
if(value > DEADZONE) {
|
||||
gamepad_get_state()->down = true;
|
||||
}
|
||||
}
|
||||
if(axis == SDL_CONTROLLER_AXIS_LEFTX) {
|
||||
gamepad_get_state()->left = false;
|
||||
gamepad_get_state()->right = false;
|
||||
if(value < -DEADZONE) {
|
||||
gamepad_get_state()->left = true;
|
||||
}
|
||||
if(value > DEADZONE) {
|
||||
gamepad_get_state()->right = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ui_handle_events(){
|
||||
SDL_Event e;
|
||||
while(SDL_PollEvent(&e) > 0) {
|
||||
@ -468,21 +505,35 @@ void ui_handle_events(){
|
||||
}
|
||||
|
||||
if(e.type == SDL_JOYBUTTONDOWN) {
|
||||
ui_on_joy_button(e.jbutton.button, true);
|
||||
//ui_on_joy_button(e.jbutton.button, true);
|
||||
}
|
||||
|
||||
if(e.type == SDL_JOYBUTTONUP) {
|
||||
ui_on_joy_button(e.jbutton.button, false);
|
||||
//ui_on_joy_button(e.jbutton.button, false);
|
||||
}
|
||||
|
||||
if(e.type == SDL_JOYHATMOTION) {
|
||||
ui_on_joy_hat(e.jhat.hat, e.jhat.value);
|
||||
// ui_on_joy_hat(e.jhat.hat, e.jhat.value);
|
||||
}
|
||||
|
||||
if(e.type == SDL_JOYAXISMOTION) {
|
||||
ui_on_Joy_axis(e.jaxis.axis, e.jaxis.value);
|
||||
//ui_on_Joy_axis(e.jaxis.axis, e.jaxis.value);
|
||||
}
|
||||
|
||||
if(e.type == SDL_CONTROLLERBUTTONDOWN) {
|
||||
ui_on_controller_button(e.cbutton.button, true);
|
||||
}
|
||||
|
||||
if(e.type == SDL_CONTROLLERBUTTONUP) {
|
||||
ui_on_controller_button(e.cbutton.button, false);
|
||||
}
|
||||
|
||||
if(e.type == SDL_CONTROLLERAXISMOTION) {
|
||||
ui_on_controller_axis(e.caxis.axis, e.caxis.value);
|
||||
}
|
||||
|
||||
//if(e.type == sdl_controllerhat)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user