Skip to content

Commit

Permalink
Merge pull request #447 from paulscottrobson/joystick
Browse files Browse the repository at this point in the history
SDL2 Joystick support enabled
  • Loading branch information
paulscottrobson authored Apr 11, 2024
2 parents 1594e18 + 325fb11 commit 9b07c4d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 20 deletions.
20 changes: 4 additions & 16 deletions basic/test.bsc
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
'
' BASIC Mouse cursor manipulation
'
mouse show
mouse to 260,180
cls
repeat
b = mouse(x,y,s)
print chr$(20);x;" ";y;" ";b;" ";s;" "
until false
end
'
proc send.message(g,f)
while peek($FF00):wend
poke $FF01,f:poke $FF00,g
while peek($FF00):wend
endproc

b = joypad(1,x,y)
print b,x,y,joycount()
until 0
2 changes: 2 additions & 0 deletions emulator/include/gfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ int GFXXRender(SDL_Surface *surface);
void GFXSetFrequency(int freq,int channel);

int GFXReadJoystick0(void);
int GFXControllerCount(void);
unsigned int GFXReadController(int id);

class Beeper
{
Expand Down
10 changes: 8 additions & 2 deletions emulator/src/core/hardware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,11 +654,17 @@ void ResetSystem(void) {
// ***************************************************************************************

uint8_t GMPGetControllerCount(void) {
return 0;
return GFXControllerCount();
}

// ***************************************************************************************
//
// Read controller status
//
// ***************************************************************************************

uint32_t GMPReadDigitalController(uint8_t index) {
return 0;
return GFXReadController(index);
}

// ***************************************************************************************
Expand Down
68 changes: 66 additions & 2 deletions emulator/src/framework/gfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@
#include "emscripten.h"
#endif

#define MAX_CONTROLLERS (4)

static SDL_Window *mainWindow = NULL;
static SDL_Surface *mainSurface = NULL;
static int controllerCount = 0;
static SDL_Joystick *controllers[MAX_CONTROLLERS];

static int background;

#define RED(x) ((((x) >> 8) & 0xF) * 17)
Expand All @@ -33,6 +38,7 @@ static int background;

static void _GFXInitialiseKeyRecord(void);
static void _GFXUpdateKeyRecord(int scancode,int isDown);
static void GFXFindControllers(void);

// *******************************************************************************************************************************
//
Expand All @@ -42,7 +48,7 @@ static void _GFXUpdateKeyRecord(int scancode,int isDown);

void GFXOpenWindow(const char *title,int width,int height,int colour) {

if (SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) { // Try to initialise SDL Video and Audio
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_GAMECONTROLLER) < 0) { // Try to initialise SDL Video and Audio
exit(printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError()));
}

Expand All @@ -54,6 +60,9 @@ void GFXOpenWindow(const char *title,int width,int height,int colour) {

mainSurface = SDL_GetWindowSurface(mainWindow); // Get a surface to draw on.

GFXFindControllers();


background = colour; // Remember required backgrounds.
_GFXInitialiseKeyRecord(); // Set up key system.
Beeper::open();
Expand Down Expand Up @@ -401,4 +410,59 @@ void GFXSetFrequency(int freq,int channel) {

void GFXSilence(void) {
Beeper::setVolume(0.0);
}
}

// *******************************************************************************************************************************
//
// Get controller count
//
// *******************************************************************************************************************************

int GFXControllerCount(void) {
return controllerCount;
}

// *******************************************************************************************************************************
//
// Read controller, format compatible with Firmware
//
// *******************************************************************************************************************************

unsigned int GFXReadController(int id) {
int bitPattern = 0;
Sint16 dx = SDL_JoystickGetAxis(controllers[id],0);
if (abs(dx) >= 1024) {
bitPattern |= (dx < 0) ? 0x01:0x02;
}
Sint16 dy = SDL_JoystickGetAxis(controllers[id],1);
if (abs(dy) >= 1024) {
bitPattern |= (dy < 0) ? 0x04:0x08;
}
int buttons = SDL_JoystickNumButtons(controllers[id]);
buttons = (buttons >= 4) ? 4 : buttons;
for (int b = 0;b < buttons;b++) {
if (SDL_JoystickGetButton(controllers[id],b)) {
bitPattern |= (0x10 << b);
}
}
return bitPattern;
}

// *******************************************************************************************************************************
//
// Search for controllers
//
// *******************************************************************************************************************************

static void GFXFindControllers(void) {
controllerCount = 0; // Discover controllers.
for (int i = 0; i < SDL_NumJoysticks(); i++) {
if (controllerCount < MAX_CONTROLLERS) {
controllers[controllerCount] = SDL_JoystickOpen(i);
if (controllers[controllerCount] == NULL) {
exit(printf("Failed to open controller %d\n",i));
}
controllerCount++;
}
}
}

0 comments on commit 9b07c4d

Please sign in to comment.