SDL Input: Now with a clean C module interface. Added a bit of doc to the header.

This commit is contained in:
bgk 2008-08-31 16:38:41 +00:00
parent f49b0a6ad6
commit 2c5a319c0c
3 changed files with 222 additions and 92 deletions

View File

@ -1353,23 +1353,11 @@ void sdlPollEvents()
} }
break; break;
case SDL_JOYHATMOTION: case SDL_JOYHATMOTION:
sdlUpdateJoyHat(event.jhat.which,
event.jhat.hat,
event.jhat.value);
break;
case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP: case SDL_JOYBUTTONUP:
sdlUpdateJoyButton(event.jbutton.which,
event.jbutton.button,
event.jbutton.state == SDL_PRESSED);
break;
case SDL_JOYAXISMOTION: case SDL_JOYAXISMOTION:
sdlUpdateJoyAxis(event.jaxis.which,
event.jaxis.axis,
event.jaxis.value);
break;
case SDL_KEYDOWN: case SDL_KEYDOWN:
sdlUpdateKey(event.key.keysym.sym, true); inputProcessSDLEvent(event);
break; break;
case SDL_KEYUP: case SDL_KEYUP:
switch(event.key.keysym.sym) { switch(event.key.keysym.sym) {
@ -1575,7 +1563,7 @@ void sdlPollEvents()
default: default:
break; break;
} }
sdlUpdateKey(event.key.keysym.sym, false); inputProcessSDLEvent(event);
break; break;
} }
} }
@ -2172,7 +2160,7 @@ int main(int argc, char **argv)
#if WITH_LIRC #if WITH_LIRC
StartLirc(); StartLirc();
#endif #endif
sdlCheckKeys(); inputInitJoysticks();
if(cartridgeType == 0) { if(cartridgeType == 0) {
srcWidth = 240; srcWidth = 240;
@ -2611,3 +2599,28 @@ void systemGbBorderOn()
filterFunction = initFilter(filter, systemColorDepth, srcWidth); filterFunction = initFilter(filter, systemColorDepth, srcWidth);
} }
bool systemReadJoypads()
{
return true;
}
u32 systemReadJoypad(int which)
{
return inputReadJoypad(which);
}
void systemUpdateMotionSensor()
{
inputUpdateMotionSensor();
}
int systemGetSensorX()
{
return inputGetSensorX();
}
int systemGetSensorY()
{
return inputGetSensorY();
}

View File

@ -1,9 +1,30 @@
// VBA-M, A Nintendo Handheld Console Emulator
// Copyright (C) 2008 VBA-M development team
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or(at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "inputSDL.h" #include "inputSDL.h"
#include <SDL.h>
#define SDLBUTTONS_NUM 14 #define SDLBUTTONS_NUM 14
static void sdlUpdateKey(int key, bool down);
static void sdlUpdateJoyButton(int which, int button, bool pressed);
static void sdlUpdateJoyHat(int which, int hat, int value);
static void sdlUpdateJoyAxis(int which, int axis, int value);
static bool sdlCheckJoyKey(int key);
bool sdlButtons[4][SDLBUTTONS_NUM] = { bool sdlButtons[4][SDLBUTTONS_NUM] = {
{ false, false, false, false, false, false, { false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false,
@ -33,7 +54,7 @@ int sdlDefaultJoypad = 0;
int autoFire = 0; int autoFire = 0;
bool autoFireToggle = false; bool autoFireToggle = false;
u16 joypad[4][SDLBUTTONS_NUM] = { uint16_t joypad[4][SDLBUTTONS_NUM] = {
{ SDLK_LEFT, SDLK_RIGHT, { SDLK_LEFT, SDLK_RIGHT,
SDLK_UP, SDLK_DOWN, SDLK_UP, SDLK_DOWN,
SDLK_z, SDLK_x, SDLK_z, SDLK_x,
@ -47,7 +68,7 @@ u16 joypad[4][SDLBUTTONS_NUM] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
}; };
u16 defaultJoypad[SDLBUTTONS_NUM] = { uint16_t defaultJoypad[SDLBUTTONS_NUM] = {
SDLK_LEFT, SDLK_RIGHT, SDLK_LEFT, SDLK_RIGHT,
SDLK_UP, SDLK_DOWN, SDLK_UP, SDLK_DOWN,
SDLK_z, SDLK_x, SDLK_z, SDLK_x,
@ -57,23 +78,23 @@ u16 defaultJoypad[SDLBUTTONS_NUM] = {
SDLK_q, SDLK_w SDLK_q, SDLK_w
}; };
u16 motion[4] = { uint16_t motion[4] = {
SDLK_KP4, SDLK_KP6, SDLK_KP8, SDLK_KP2 SDLK_KP4, SDLK_KP6, SDLK_KP8, SDLK_KP2
}; };
u16 defaultMotion[4] = { uint16_t defaultMotion[4] = {
SDLK_KP4, SDLK_KP6, SDLK_KP8, SDLK_KP2 SDLK_KP4, SDLK_KP6, SDLK_KP8, SDLK_KP2
}; };
int sensorX = 2047; int sensorX = 2047;
int sensorY = 2047; int sensorY = 2047;
void inputSetKeymap(int joy, EKey key, u16 code) void inputSetKeymap(int joy, EKey key, uint16_t code)
{ {
joypad[joy][key] = code; joypad[joy][key] = code;
} }
void inputSetMotionKeymap(EKey key, u16 code) void inputSetMotionKeymap(EKey key, uint16_t code)
{ {
motion[key] = code; motion[key] = code;
} }
@ -112,7 +133,7 @@ bool inputToggleAutoFire(EKey key)
} }
} }
void sdlUpdateKey(int key, bool down) static void sdlUpdateKey(int key, bool down)
{ {
int i; int i;
for(int j = 0; j < 4; j++) { for(int j = 0; j < 4; j++) {
@ -131,7 +152,7 @@ void sdlUpdateKey(int key, bool down)
} }
} }
void sdlUpdateJoyButton(int which, static void sdlUpdateJoyButton(int which,
int button, int button,
bool pressed) bool pressed)
{ {
@ -162,7 +183,7 @@ void sdlUpdateJoyButton(int which,
} }
} }
void sdlUpdateJoyHat(int which, static void sdlUpdateJoyHat(int which,
int hat, int hat,
int value) int value)
{ {
@ -225,7 +246,7 @@ void sdlUpdateJoyHat(int which,
} }
} }
void sdlUpdateJoyAxis(int which, static void sdlUpdateJoyAxis(int which,
int axis, int axis,
int value) int value)
{ {
@ -256,7 +277,7 @@ void sdlUpdateJoyAxis(int which,
} }
} }
bool sdlCheckJoyKey(int key) static bool sdlCheckJoyKey(int key)
{ {
int dev = (key >> 12) - 1; int dev = (key >> 12) - 1;
int what = key & 0xfff; int what = key & 0xfff;
@ -284,7 +305,7 @@ bool sdlCheckJoyKey(int key)
return true; return true;
} }
void sdlCheckKeys() void inputInitJoysticks()
{ {
sdlNumDevices = SDL_NumJoysticks(); sdlNumDevices = SDL_NumJoysticks();
@ -349,19 +370,43 @@ void sdlCheckKeys()
SDL_JoystickEventState(SDL_ENABLE); SDL_JoystickEventState(SDL_ENABLE);
} }
bool systemReadJoypads() void inputProcessSDLEvent(const SDL_Event &event)
{ {
return true; switch(event.type)
{
case SDL_KEYDOWN:
sdlUpdateKey(event.key.keysym.sym, true);
break;
case SDL_KEYUP:
sdlUpdateKey(event.key.keysym.sym, false);
break;
case SDL_JOYHATMOTION:
sdlUpdateJoyHat(event.jhat.which,
event.jhat.hat,
event.jhat.value);
break;
case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP:
sdlUpdateJoyButton(event.jbutton.which,
event.jbutton.button,
event.jbutton.state == SDL_PRESSED);
break;
case SDL_JOYAXISMOTION:
sdlUpdateJoyAxis(event.jaxis.which,
event.jaxis.axis,
event.jaxis.value);
break;
}
} }
u32 systemReadJoypad(int which) uint32_t inputReadJoypad(int which)
{ {
int realAutoFire = autoFire; int realAutoFire = autoFire;
if(which < 0 || which > 3) if(which < 0 || which > 3)
which = sdlDefaultJoypad; which = sdlDefaultJoypad;
u32 res = 0; uint32_t res = 0;
if(sdlButtons[which][KEY_BUTTON_A]) if(sdlButtons[which][KEY_BUTTON_A])
res |= 1; res |= 1;
@ -409,7 +454,7 @@ u32 systemReadJoypad(int which)
return res; return res;
} }
void systemUpdateMotionSensor() void inputUpdateMotionSensor()
{ {
if(sdlMotionButtons[KEY_LEFT]) { if(sdlMotionButtons[KEY_LEFT]) {
sensorX += 3; sensorX += 3;
@ -456,12 +501,12 @@ void systemUpdateMotionSensor()
} }
} }
int systemGetSensorX() int inputGetSensorX()
{ {
return sensorX; return sensorX;
} }
int systemGetSensorY() int inputGetSensorY()
{ {
return sensorY; return sensorY;
} }

View File

@ -1,26 +1,98 @@
// VBA-M, A Nintendo Handheld Console Emulator
// Copyright (C) 2008 VBA-M development team
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or(at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifndef VBAM_SDL_INPUT_H #ifndef VBAM_SDL_INPUT_H
#define VBAM_SDL_INPUT_H #define VBAM_SDL_INPUT_H
#include "../System.h" #include <SDL.h>
enum EKey { enum EKey {
KEY_LEFT, KEY_RIGHT, KEY_LEFT,
KEY_UP, KEY_DOWN, KEY_RIGHT,
KEY_BUTTON_A, KEY_BUTTON_B, KEY_UP,
KEY_BUTTON_START, KEY_BUTTON_SELECT, KEY_DOWN,
KEY_BUTTON_L, KEY_BUTTON_R, KEY_BUTTON_A,
KEY_BUTTON_SPEED, KEY_BUTTON_CAPTURE, KEY_BUTTON_B,
KEY_BUTTON_AUTO_A, KEY_BUTTON_AUTO_B KEY_BUTTON_START,
KEY_BUTTON_SELECT,
KEY_BUTTON_L,
KEY_BUTTON_R,
KEY_BUTTON_SPEED,
KEY_BUTTON_CAPTURE,
KEY_BUTTON_AUTO_A,
KEY_BUTTON_AUTO_B
}; };
void inputSetKeymap(int joy, EKey key, u16 code); /**
void inputSetMotionKeymap(EKey key, u16 code); * Init the joysticks needed by the keymap. Verify that the keymap is compatible
* with the joysticks. If it's not the case, revert to the default keymap.
*/
void inputInitJoysticks();
/**
* Define which key controls an emulated joypad button
* @param joy Emulated joypad index (there may be up to 4 joypads for the SGB)
* @param key Emulated joypad button
* @param code Code defining an actual joypad / keyboard button
*/
void inputSetKeymap(int joy, EKey key, uint16_t code);
/**
* Define which keys control motion detection emulation
* @param key Emulated joypad button
* @param code Code defining an actual joypad / keyboard button
*/
void inputSetMotionKeymap(EKey key, uint16_t code);
/**
* Toggle Auto fire for the specified button. Only A, B, R, L are supported.
* @param key Emulated joypad button
* @return Auto fire enabled
*/
bool inputToggleAutoFire(EKey key); bool inputToggleAutoFire(EKey key);
void sdlUpdateKey(int key, bool down); /**
void sdlUpdateJoyButton(int which, int button, bool pressed); * Update the emulated pads state with an SDL event
void sdlUpdateJoyHat(int which, int hat, int value); * @param SDL_Event An event that has just occured
void sdlUpdateJoyAxis(int which, int axis, int value); */
void sdlCheckKeys(); void inputProcessSDLEvent(const SDL_Event &event);
/**
* Read the state of an emulated joypad
* @param which Emulated joypad index
* @return Joypad state
*/
uint32_t inputReadJoypad(int which);
/**
* Compute the motion sensor X and Y values
*/
void inputUpdateMotionSensor();
/**
* Get the motion sensor X value
* @return motion sensor X value
*/
int inputGetSensorX();
/**
* Get the motion sensor Y value
* @return motion sensor Y value
*/
int inputGetSensorY();
#endif // VBAM_SDL_INPUT_H #endif // VBAM_SDL_INPUT_H