Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Joystick support for up to 4 Devices. #39

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 67 additions & 0 deletions examples/joysticks.lua
@@ -0,0 +1,67 @@
--
-- simple demo of joystick input
-- by torpor (seclorum@me.com)
--
-- the joystick[] table is global, and contains .x/.y/.name fields
-- describing the joystick. the JOYSTICK global contains the
-- number of physical joysticks detected by LOAD81 at startup.
--
-- the maximum number of joysticks available is 4.
--

-- each joystick will have its own color, and since there
-- a maximum of 4 joysticks available then we have 4
-- unique colors to use
colors = {}
colors = {
{r=255,g=0,b=255,a=1},
{r=0,g=0,b=255,a=1},
{r=255,g=0,b=0,a=1},
{r=0,g=255,b=0,a=1}
}

function setup()
background(0,0,0,0)
-- divide the screen in 4, one quad for each joystick device
joy_quad = HEIGHT / 4;
end

-- simple map function
function map(x, in_min, in_max, out_min, out_max)
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
end

function draw_joystick_info(joynum)
-- each joystick gets its own color
fill(colors[joynum].r,colors[joynum].g,colors[joynum].b,colors[joynum].a);

-- text explaining joystick name and x/y values goes in a 'quarter' of the screen
text(10, joynum * joy_quad, string.format("# %d %s x:%d/y:%d",
joynum, joystick[joynum].name,
joystick[joynum].x, joystick[joynum].y));

-- each joystick returns x/y axes values from -32767 to 32767, so we map to
-- the screen size
dot_x, dot_y = 0;
dot_x = map(joystick[joynum].x, 32767, -32767, WIDTH, 0);
dot_y = map(joystick[joynum].y, 32767, -32767, 0, HEIGHT);

-- draw the joystick dot
ellipse(dot_x, dot_y, 10, 10);
end


function draw()
-- clear the screen
background(0,0,0,0);

if JOYSTICKS == 0 then
fill (255,0,0,1);
text(10, joy_quad, string.format("No joysticks detected .. plug one in and try again!"));
end

-- draw the info for each joystick
for jn = 1, JOYSTICKS, 1 do
draw_joystick_info(jn);
end
end
4 changes: 3 additions & 1 deletion framebuffer.c
Expand Up @@ -8,7 +8,7 @@ SDL_Surface *sdlInit(int width, int height, int bpp, int fullscreen) {
SDL_Surface *screen;

if (fullscreen) flags |= SDL_FULLSCREEN;
if (SDL_Init(SDL_INIT_VIDEO) == -1) {
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) == -1) {
fprintf(stderr, "SDL Init error: %s\n", SDL_GetError());
return NULL;
}
Expand All @@ -22,6 +22,7 @@ SDL_Surface *sdlInit(int width, int height, int bpp, int fullscreen) {
* keys are translated into characters with automatic support for modifiers
* (for instance shift modifier to print capital letters and symbols). */
SDL_EnableUNICODE(SDL_ENABLE);

return screen;
}

Expand All @@ -34,6 +35,7 @@ frameBuffer *createFrameBuffer(int width, int height, int bpp, int fullscreen) {
SDL_initFramerate(&fb->fps_mgr);
/* Load the bitmap font */
bfLoadFont((char**)BitmapFont);

return fb;
}

Expand Down
5 changes: 4 additions & 1 deletion framebuffer.h
Expand Up @@ -9,11 +9,14 @@
#define FONT_HEIGHT 16
#define FONT_KERNING 10

#define MAX_JOYSTICKS 4

typedef struct frameBuffer {
int width;
int height;
SDL_Surface *screen;
FPSmanager fps_mgr;
SDL_Surface *screen;
SDL_Joystick *joysticks[MAX_JOYSTICKS];
} frameBuffer;

frameBuffer *createFrameBuffer(int width, int height, int bpp, int fullscreen);
Expand Down
135 changes: 133 additions & 2 deletions load81.c
Expand Up @@ -276,6 +276,68 @@ void draw(void) {
}
}

/* update the joystick[] table, by given field
e.g. joystick[1].x = 100
stack:
joystick global
[1] joynum
.x field
= 100 value
*/
void updateJoystickState(int joynum, char *field, int value)
{
lua_getglobal(l81.L, "joystick");

if (lua_isnil(l81.L,-1)) {
lua_pop(l81.L,1);
lua_newtable(l81.L);
lua_setglobal(l81.L,"joystick");
lua_getglobal(l81.L,"joystick");
}

if (lua_istable(l81.L, -1)) {
lua_pushnumber(l81.L, joynum);
lua_gettable(l81.L, -2);
lua_pushstring(l81.L, field);
lua_pushnumber(l81.L, value);
lua_settable(l81.L, -3);
}

lua_pop(l81.L, 2);
}

/*
update the joystick[] table, .name field
e.g. joystick[1].name = "nub0"
stack:
joystick global
[1] joynum
.name field
= "nub0" value
*/
void updateJoystickName(int joynum, const char *name)
{
lua_getglobal(l81.L, "joystick");

if (lua_isnil(l81.L,-1)) {
lua_pop(l81.L,1);
lua_newtable(l81.L);
lua_setglobal(l81.L,"joystick");
lua_getglobal(l81.L,"joystick");
}

if (lua_istable(l81.L, -1)) {
lua_pushnumber(l81.L, joynum);
lua_gettable(l81.L, -2);
lua_pushstring(l81.L, "name");
lua_pushstring(l81.L, name);
lua_settable(l81.L, -3);
}

lua_pop(l81.L, 2);
}


/* Update the keyboard.pressed and mouse.pressed Lua table. */
void updatePressedState(char *object, char *keyname, int pressed) {
lua_getglobal(l81.L,object); /* $keyboard */
Expand Down Expand Up @@ -306,6 +368,18 @@ void mouseMovedEvent(int x, int y, int xrel, int yrel) {
setTableFieldNumber("mouse","yrel",-yrel);
}

void joystickXMovedEvent(int joy_num, Sint16 x) {
if (joy_num < MAX_JOYSTICKS) {
updateJoystickState(joy_num, "x", x);
}
}

void joystickYMovedEvent(int joy_num, Sint16 y) {
if (joy_num < MAX_JOYSTICKS) {
updateJoystickState(joy_num, "y", y);
}
}

void mouseButtonEvent(int button, int pressed) {
char buttonname[32];

Expand Down Expand Up @@ -357,6 +431,16 @@ int processSdlEvents(void) {
case SDL_MOUSEBUTTONUP:
mouseButtonEvent(event.button.button,0);
break;
case SDL_JOYAXISMOTION: /* Handle Joystick Motion */
//printf("joystick %d moved on %d axis\n", event.jaxis.which, event.jaxis.axis);
if( event.jaxis.axis == 0) { /* x-axis */
joystickXMovedEvent(event.jaxis.which + 1, event.jaxis.value); /* C vs. Lua offsets */
}
if( event.jaxis.axis == 1) { /* y-axis */
joystickYMovedEvent(event.jaxis.which + 1, event.jaxis.value); /* C vs. Lua offsets */
}
break;

case SDL_QUIT:
exit(0);
break;
Expand Down Expand Up @@ -422,16 +506,59 @@ int loadProgram(void) {
editorClearError();
return 0;
}

void initJoysticks(frameBuffer *fb) {
int cur_joy;
/* Initialize Joysticks */
SDL_JoystickEventState(SDL_ENABLE);

for(cur_joy=0; cur_joy < SDL_NumJoysticks(); cur_joy++ ) {
fb->joysticks[cur_joy] = SDL_JoystickOpen(cur_joy);

updateJoystickName(cur_joy + 1, SDL_JoystickName(cur_joy));
#if 0
{
printf("joy %d init: %p\n", cur_joy, fb->joysticks[cur_joy]);
printf("Joystick name: %s\n ", SDL_JoystickName(cur_joy));
printf("Axis: %d\nt", SDL_JoystickNumAxes(fb->joysticks[cur_joy]));
printf("Trackballs:%d\n", SDL_JoystickNumBalls(fb->joysticks[cur_joy]));
printf("Hats: %d\n", SDL_JoystickNumHats(fb->joysticks[cur_joy]));
printf("Buttons: %d\n", SDL_JoystickNumButtons(fb->joysticks[cur_joy]));
}
#endif

}
}

/*
Close joysticks.
*/
void closeJoysticks(frameBuffer *fb) {
int cur_joy;
for(cur_joy=0; cur_joy < SDL_NumJoysticks(); cur_joy++) {
if (fb->joysticks[cur_joy]) {
SDL_JoystickClose( fb->joysticks[cur_joy]);
updateJoystickName(cur_joy + 1, "none");
}
}
setNumber("JOYSTICKS", 0);
}

void initScreen(void) {
l81.fb = createFrameBuffer(l81.width,l81.height,
l81.bpp,l81.opt_full_screen);
}

void resetProgram(void) {
char *initscript =
"keyboard={}; keyboard['pressed']={};"
"mouse={}; mouse['pressed']={};";
"keyboard={}; keyboard['pressed']={};" \
"mouse={}; mouse['pressed']={};" \
"joystick={};" \
"joystick[1]={x=0;y=0;name='none'};" \
"joystick[2]={x=0;y=0;name='none'};" \
"joystick[3]={x=0;y=0;name='none'};" \
"joystick[4]={x=0;y=0;name='none'};";
/* !J! should set joystick[] from MAX_JOYSTICKS */

l81.epoch = 0;
if (l81.L) lua_close(l81.L);
Expand All @@ -443,6 +570,7 @@ void resetProgram(void) {
luaopen_debug(l81.L);
setNumber("WIDTH",l81.width);
setNumber("HEIGHT",l81.height);
setNumber("JOYSTICKS", SDL_NumJoysticks());
luaL_loadbuffer(l81.L,initscript,strlen(initscript),"initscript");
lua_pcall(l81.L,0,0,0);

Expand All @@ -453,6 +581,8 @@ void resetProgram(void) {
setTableFieldNumber("mouse","xrel",0);
setTableFieldNumber("mouse","yrel",0);

initJoysticks(l81.fb);

/* Register API */
lua_pushcfunction(l81.L,fillBinding);
lua_setglobal(l81.L,"fill");
Expand Down Expand Up @@ -552,5 +682,6 @@ int main(int argc, char **argv) {
}
editorRun();
}
closeJoysticks(l81.fb);
return 0;
}