Commit 9c428361 authored by Steven Fuller's avatar Steven Fuller

very basic joystick support.

parent 597376eb
...@@ -170,28 +170,6 @@ static void INL_ShutMouse(void) ...@@ -170,28 +170,6 @@ static void INL_ShutMouse(void)
{ {
} }
///////////////////////////////////////////////////////////////////////////
//
// INL_StartJoy() - Detects & auto-configures the specified joystick
// The auto-config assumes the joystick is centered
//
///////////////////////////////////////////////////////////////////////////
static boolean INL_StartJoy(word joy)
{
return false;
}
///////////////////////////////////////////////////////////////////////////
//
// INL_ShutJoy() - Cleans up the joystick stuff
//
///////////////////////////////////////////////////////////////////////////
static void INL_ShutJoy(word joy)
{
JoysPresent[joy] = false;
}
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
// //
// IN_Startup() - Starts up the Input Mgr // IN_Startup() - Starts up the Input Mgr
...@@ -235,8 +213,11 @@ void IN_Shutdown(void) ...@@ -235,8 +213,11 @@ void IN_Shutdown(void)
return; return;
INL_ShutMouse(); INL_ShutMouse();
for (i = 0;i < MaxJoys;i++) for (i = 0;i < MaxJoys;i++) {
INL_ShutJoy(i); INL_ShutJoy(i);
JoysPresent[i] = false;
}
INL_ShutKbd(); INL_ShutKbd();
IN_Started = false; IN_Started = false;
......
...@@ -160,6 +160,9 @@ extern void IN_Startup(), IN_Shutdown(), IN_ClearKeysDown(), ...@@ -160,6 +160,9 @@ extern void IN_Startup(), IN_Shutdown(), IN_ClearKeysDown(),
IN_SetupJoy(word joy,word minx,word maxx,word miny,word maxy), IN_SetupJoy(word joy,word minx,word maxx,word miny,word maxy),
IN_Ack(); IN_Ack();
boolean INL_StartJoy(word joy);
void INL_ShutJoy(word joy);
extern boolean IN_UserInput(longword delay); extern boolean IN_UserInput(longword delay);
extern const char *IN_GetScanName(ScanCode); extern const char *IN_GetScanName(ScanCode);
......
...@@ -2,6 +2,28 @@ ...@@ -2,6 +2,28 @@
#include "SDL.h" #include "SDL.h"
#define JoyScaleMax 32768
#define JoyScaleShift 8
typedef struct {
SDL_Joystick* joystick;
word joyMinX;
word joyMinY;
word threshMinX;
word threshMinY;
word threshMaxX;
word threshMaxY;
word joyMaxX;
word joyMaxY;
word joyMultXL;
word joyMultYL;
word joyMultXH;
word joyMultYH;
} JoystickDef;
static JoystickDef JoyDefs[MaxJoys];
static SDL_Surface *surface; static SDL_Surface *surface;
static unsigned int sdl_palettemode; static unsigned int sdl_palettemode;
...@@ -111,6 +133,8 @@ void VL_Startup() ...@@ -111,6 +133,8 @@ void VL_Startup()
SDL_ShowCursor(0); SDL_ShowCursor(0);
SDL_WM_SetCaption(GAMENAME, GAMENAME); SDL_WM_SetCaption(GAMENAME, GAMENAME);
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
} }
/* /*
...@@ -145,9 +169,9 @@ void VL_SetPalette(const byte *palette) ...@@ -145,9 +169,9 @@ void VL_SetPalette(const byte *palette)
for (i = 0; i < 256; i++) for (i = 0; i < 256; i++)
{ {
colors[i].r = palette[i*3+0] << 2; colors[i].r = ((int)palette[i*3+0] * 255) / 63;
colors[i].g = palette[i*3+1] << 2; colors[i].g = ((int)palette[i*3+1] * 255) / 63;
colors[i].b = palette[i*3+2] << 2; colors[i].b = ((int)palette[i*3+2] * 255) / 63;
} }
SDL_SetPalette(surface, sdl_palettemode, colors, 0, 256); SDL_SetPalette(surface, sdl_palettemode, colors, 0, 256);
} }
...@@ -336,6 +360,9 @@ void INL_Update() ...@@ -336,6 +360,9 @@ void INL_Update()
memcpy(DebouncedKeyboard, InternalKeyboard, sizeof(DebouncedKeyboard)); memcpy(DebouncedKeyboard, InternalKeyboard, sizeof(DebouncedKeyboard));
/* poll joysticks */
SDL_JoystickUpdate();
if (SDL_PollEvent(&event)) { if (SDL_PollEvent(&event)) {
do { do {
switch(event.type) { switch(event.type) {
...@@ -420,6 +447,43 @@ byte IN_MouseButtons() ...@@ -420,6 +447,43 @@ byte IN_MouseButtons()
return retr; return retr;
} }
///////////////////////////////////////////////////////////////////////////
//
// INL_StartJoy() - Detects & auto-configures the specified joystick
// The auto-config assumes the joystick is centered
//
///////////////////////////////////////////////////////////////////////////
boolean INL_StartJoy(word joy)
{
JoystickDef* def;
def = &JoyDefs[joy];
memset(def, 0, sizeof(*def));
def->joystick = SDL_JoystickOpen(joy);
return (def->joystick != NULL);
}
///////////////////////////////////////////////////////////////////////////
//
// INL_ShutJoy() - Cleans up the joystick stuff
//
///////////////////////////////////////////////////////////////////////////
void INL_ShutJoy(word joy)
{
JoystickDef* def;
def = &JoyDefs[joy];
if (def->joystick != NULL) {
SDL_JoystickClose(def->joystick);
}
memset(def, 0, sizeof(*def));
}
/* /*
=================== ===================
= =
...@@ -430,7 +494,7 @@ byte IN_MouseButtons() ...@@ -430,7 +494,7 @@ byte IN_MouseButtons()
byte IN_JoyButtons() byte IN_JoyButtons()
{ {
return 0; return ((INL_GetJoyButtons(0) << 0) | (INL_GetJoyButtons(1) << 2));
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
...@@ -440,8 +504,17 @@ byte IN_JoyButtons() ...@@ -440,8 +504,17 @@ byte IN_JoyButtons()
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
void IN_GetJoyAbs(word joy,word *xp,word *yp) void IN_GetJoyAbs(word joy,word *xp,word *yp)
{ {
JoystickDef* def;
def = &JoyDefs[joy];
if (def->joystick != NULL) {
*xp = ((int)SDL_JoystickGetAxis(def->joystick, 1) + 32768) / 16;
*yp = ((int)SDL_JoystickGetAxis(def->joystick, 0) + 32768) / 16;
} else {
*xp = 0; *xp = 0;
*yp = 0; *yp = 0;
}
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
...@@ -452,7 +525,56 @@ void IN_GetJoyAbs(word joy,word *xp,word *yp) ...@@ -452,7 +525,56 @@ void IN_GetJoyAbs(word joy,word *xp,word *yp)
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
void INL_GetJoyDelta(word joy,int *dx,int *dy) void INL_GetJoyDelta(word joy,int *dx,int *dy)
{ {
word x,y;
JoystickDef *def;
IN_GetJoyAbs(joy,&x,&y);
def = JoyDefs + joy;
if (x < def->threshMinX)
{
if (x < def->joyMinX)
x = def->joyMinX;
x = -(x - def->threshMinX);
x *= def->joyMultXL;
x >>= JoyScaleShift;
*dx = (x > 127)? -127 : -x;
}
else if (x > def->threshMaxX)
{
if (x > def->joyMaxX)
x = def->joyMaxX;
x = x - def->threshMaxX;
x *= def->joyMultXH;
x >>= JoyScaleShift;
*dx = (x > 127)? 127 : x;
}
else
*dx = 0; *dx = 0;
if (y < def->threshMinY)
{
if (y < def->joyMinY)
y = def->joyMinY;
y = -(y - def->threshMinY);
y *= def->joyMultYL;
y >>= JoyScaleShift;
*dy = (y > 127)? -127 : -y;
}
else if (y > def->threshMaxY)
{
if (y > def->joyMaxY)
y = def->joyMaxY;
y = y - def->threshMaxY;
y *= def->joyMultYH;
y >>= JoyScaleShift;
*dy = (y > 127)? 127 : y;
}
else
*dy = 0; *dy = 0;
} }
...@@ -464,9 +586,32 @@ void INL_GetJoyDelta(word joy,int *dx,int *dy) ...@@ -464,9 +586,32 @@ void INL_GetJoyDelta(word joy,int *dx,int *dy)
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
word INL_GetJoyButtons(word joy) word INL_GetJoyButtons(word joy)
{ {
JoystickDef* def;
def = &JoyDefs[joy];
if (def->joystick != NULL) {
return (SDL_JoystickGetButton(def->joystick, 0) << 0)
| (SDL_JoystickGetButton(def->joystick, 1) << 1);
}
return 0; return 0;
} }
//
// INL_SetJoyScale() - Sets up scaling values for the specified joystick
//
static void INL_SetJoyScale(word joy)
{
JoystickDef *def;
def = &JoyDefs[joy];
def->joyMultXL = JoyScaleMax / (def->threshMinX - def->joyMinX);
def->joyMultXH = JoyScaleMax / (def->joyMaxX - def->threshMaxX);
def->joyMultYL = JoyScaleMax / (def->threshMinY - def->joyMinY);
def->joyMultYH = JoyScaleMax / (def->joyMaxY - def->threshMaxY);
}
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
// //
// IN_SetupJoy() - Sets up thresholding values and calls INL_SetJoyScale() // IN_SetupJoy() - Sets up thresholding values and calls INL_SetJoyScale()
...@@ -475,4 +620,24 @@ word INL_GetJoyButtons(word joy) ...@@ -475,4 +620,24 @@ word INL_GetJoyButtons(word joy)
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
void IN_SetupJoy(word joy,word minx,word maxx,word miny,word maxy) void IN_SetupJoy(word joy,word minx,word maxx,word miny,word maxy)
{ {
word d,r;
JoystickDef *def;
def = &JoyDefs[joy];
def->joyMinX = minx;
def->joyMaxX = maxx;
r = maxx - minx;
d = r / 3;
def->threshMinX = ((r / 2) - d) + minx;
def->threshMaxX = ((r / 2) + d) + minx;
def->joyMinY = miny;
def->joyMaxY = maxy;
r = maxy - miny;
d = r / 3;
def->threshMinY = ((r / 2) - d) + miny;
def->threshMaxY = ((r / 2) + d) + miny;
INL_SetJoyScale(joy);
} }
...@@ -1411,7 +1411,9 @@ int CalibrateJoystick() ...@@ -1411,7 +1411,9 @@ int CalibrateJoystick()
IN_GetJoyAbs(joystickport,&xmax,&ymax); IN_GetJoyAbs(joystickport,&xmax,&ymax);
SD_PlaySound(SHOOTSND); SD_PlaySound(SHOOTSND);
while (IN_JoyButtons()); do {
IN_CheckAck();
} while (IN_JoyButtons());
// //
// ASSIGN ACTUAL VALUES HERE // ASSIGN ACTUAL VALUES HERE
...@@ -1458,6 +1460,9 @@ void CP_Control() ...@@ -1458,6 +1460,9 @@ void CP_Control()
case JOYENABLE: case JOYENABLE:
joystickenabled^=1; joystickenabled^=1;
if (joystickenabled)
if (!CalibrateJoystick())
joystickenabled = 0;
DrawCtlScreen(); DrawCtlScreen();
CusItems.curpos=-1; CusItems.curpos=-1;
ShootSnd(); ShootSnd();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment