From 151aab3d2c86e4be182b09c850e5a607659a0316 Mon Sep 17 00:00:00 2001 From: Jay Vaughan Date: Tue, 13 Mar 2012 00:23:35 +0100 Subject: [PATCH 1/9] Feature: Joystick support for up to 4 devices. --- examples/joysticks.lua | 18 ++++++ framebuffer.c | 4 +- framebuffer.h | 5 +- load81.c | 134 ++++++++++++++++++++++++++++++++++++++++- 4 files changed, 157 insertions(+), 4 deletions(-) create mode 100755 examples/joysticks.lua diff --git a/examples/joysticks.lua b/examples/joysticks.lua new file mode 100755 index 0000000..5d7f4c5 --- /dev/null +++ b/examples/joysticks.lua @@ -0,0 +1,18 @@ +function setup() + background(0,0,0,0) +end + +function draw() + + background(0,0,0); + + fill(0,255,0,1) + text(10,HEIGHT/2,string.format("#JOYSTICKS=%d %s x:%d/y:%d %s x:%d/y:%d %s x:%d/y:%d %s x:%d/y:%d", + JOYSTICKS, + joystick[1].name, joystick[1].x, joystick[1].y, + joystick[2].name, joystick[2].x, joystick[2].y, + joystick[3].name, joystick[3].x, joystick[3].y, + joystick[4].name, joystick[4].x, joystick[4].y + )) + +end diff --git a/framebuffer.c b/framebuffer.c index c235395..8217599 100644 --- a/framebuffer.c +++ b/framebuffer.c @@ -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; } @@ -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; } @@ -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; } diff --git a/framebuffer.h b/framebuffer.h index 6675286..c03940d 100644 --- a/framebuffer.h +++ b/framebuffer.h @@ -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); diff --git a/load81.c b/load81.c index 68631ff..d0881ee 100644 --- a/load81.c +++ b/load81.c @@ -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 */ @@ -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]; @@ -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; @@ -422,7 +506,41 @@ 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]); + } +} + void initScreen(void) { l81.fb = createFrameBuffer(l81.width,l81.height, l81.bpp,l81.opt_full_screen); @@ -430,8 +548,14 @@ void initScreen(void) { 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); @@ -443,6 +567,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); @@ -453,6 +578,10 @@ void resetProgram(void) { setTableFieldNumber("mouse","xrel",0); setTableFieldNumber("mouse","yrel",0); + closeJoysticks(l81.fb); + + initJoysticks(l81.fb); + /* Register API */ lua_pushcfunction(l81.L,fillBinding); lua_setglobal(l81.L,"fill"); @@ -552,5 +681,6 @@ int main(int argc, char **argv) { } editorRun(); } + closeJoysticks(l81.fb); return 0; } From f8d8c33dddf1ad10abf571f5ef23ad97e7b5027b Mon Sep 17 00:00:00 2001 From: Jay Vaughan Date: Tue, 13 Mar 2012 18:45:09 +0100 Subject: [PATCH 2/9] Slightly cleaner demo --- examples/joysticks.lua | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/examples/joysticks.lua b/examples/joysticks.lua index 5d7f4c5..52df27c 100755 --- a/examples/joysticks.lua +++ b/examples/joysticks.lua @@ -1,18 +1,22 @@ +-- +-- simple demo of joystick input +-- + function setup() background(0,0,0,0) + cell_size = HEIGHT / 4; end -function draw() - - background(0,0,0); - - fill(0,255,0,1) - text(10,HEIGHT/2,string.format("#JOYSTICKS=%d %s x:%d/y:%d %s x:%d/y:%d %s x:%d/y:%d %s x:%d/y:%d", - JOYSTICKS, - joystick[1].name, joystick[1].x, joystick[1].y, - joystick[2].name, joystick[2].x, joystick[2].y, - joystick[3].name, joystick[3].x, joystick[3].y, - joystick[4].name, joystick[4].x, joystick[4].y - )) +function draw_joystick_info(joynum) + text(10, joynum * cell_size, string.format("# %d %s x:%d/y:%d", + joynum, joystick[joynum].name, + joystick[joynum].x, joystick[joynum].y)); +end +function draw() + fill (0,255,0,1); + background(0,0,0,0); + for jn = 1, JOYSTICKS, 1 do + draw_joystick_info(jn); + end end From 9e2fef189d935ee8c074d8560e79698808b86560 Mon Sep 17 00:00:00 2001 From: Jay Vaughan Date: Tue, 13 Mar 2012 18:50:15 +0100 Subject: [PATCH 3/9] Fix bug with joysticks closing too soon. --- examples/joysticks.lua | 5 +++++ load81.c | 2 -- 2 files changed, 5 insertions(+), 2 deletions(-) mode change 100755 => 100644 examples/joysticks.lua diff --git a/examples/joysticks.lua b/examples/joysticks.lua old mode 100755 new mode 100644 index 52df27c..edeb512 --- a/examples/joysticks.lua +++ b/examples/joysticks.lua @@ -16,6 +16,11 @@ end function draw() fill (0,255,0,1); background(0,0,0,0); + + if JOYSTICKS == 0 then + text(10, cell_size, string.format("No joysticks detected .. plug one in!")); + end + for jn = 1, JOYSTICKS, 1 do draw_joystick_info(jn); end diff --git a/load81.c b/load81.c index d0881ee..1687856 100644 --- a/load81.c +++ b/load81.c @@ -578,8 +578,6 @@ void resetProgram(void) { setTableFieldNumber("mouse","xrel",0); setTableFieldNumber("mouse","yrel",0); - closeJoysticks(l81.fb); - initJoysticks(l81.fb); /* Register API */ From 9d9ead10a85455835ed91ba71894542a497275e2 Mon Sep 17 00:00:00 2001 From: Jay Vaughan Date: Tue, 13 Mar 2012 19:21:35 +0100 Subject: [PATCH 4/9] Sexier joystick demo. --- examples/joysticks.lua | 23 +++++++++++++++++++++-- load81.c | 5 ++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/examples/joysticks.lua b/examples/joysticks.lua index edeb512..199cfb3 100644 --- a/examples/joysticks.lua +++ b/examples/joysticks.lua @@ -2,15 +2,34 @@ -- simple demo of joystick input -- +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) cell_size = HEIGHT / 4; end +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) + fill(colors[joynum].r,colors[joynum].g,colors[joynum].b,colors[joynum].a); text(10, joynum * cell_size, string.format("# %d %s x:%d/y:%d", joynum, joystick[joynum].name, joystick[joynum].x, joystick[joynum].y)); + + 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); + ellipse(dot_x, dot_y, 10, 10); + end function draw() @@ -18,8 +37,8 @@ function draw() background(0,0,0,0); if JOYSTICKS == 0 then - text(10, cell_size, string.format("No joysticks detected .. plug one in!")); - end + text(10, cell_size, string.format("No joysticks detected .. plug one in!")); + end for jn = 1, JOYSTICKS, 1 do draw_joystick_info(jn); diff --git a/load81.c b/load81.c index 1687856..e6ac2e7 100644 --- a/load81.c +++ b/load81.c @@ -536,9 +536,12 @@ void initJoysticks(frameBuffer *fb) { void closeJoysticks(frameBuffer *fb) { int cur_joy; for(cur_joy=0; cur_joy < SDL_NumJoysticks(); cur_joy++) { - if (fb->joysticks[cur_joy]) + if (fb->joysticks[cur_joy]) { SDL_JoystickClose( fb->joysticks[cur_joy]); + updateJoystickName(cur_joy + 1, "none"); + } } + setNumber("JOYSTICKS", 0); } void initScreen(void) { From 5fb59b6c8bbd1785b2e9c6ea4e61296a31ab9e69 Mon Sep 17 00:00:00 2001 From: Jay Vaughan Date: Tue, 13 Mar 2012 19:32:01 +0100 Subject: [PATCH 5/9] Attribution. --- examples/joysticks.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/joysticks.lua b/examples/joysticks.lua index 199cfb3..1510a42 100644 --- a/examples/joysticks.lua +++ b/examples/joysticks.lua @@ -1,5 +1,6 @@ -- -- simple demo of joystick input +-- by torpor (seclorum@me.com) -- colors = {} From 5fa7507d0d581495cda022c3cb6b80e72957c14d Mon Sep 17 00:00:00 2001 From: Jay Vaughan Date: Tue, 13 Mar 2012 19:41:29 +0100 Subject: [PATCH 6/9] Comments for joystick demo. --- examples/joysticks.lua | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/examples/joysticks.lua b/examples/joysticks.lua index 1510a42..05ecc7c 100644 --- a/examples/joysticks.lua +++ b/examples/joysticks.lua @@ -3,6 +3,8 @@ -- by torpor (seclorum@me.com) -- +-- each joystick has its own color, since there are four joysticks +-- maximum available then we have four unique colors colors = {} colors = { {r=255,g=0,b=255,a=1}, @@ -13,34 +15,45 @@ colors = { function setup() background(0,0,0,0) - cell_size = HEIGHT / 4; + -- 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(10, joynum * cell_size, string.format("# %d %s x:%d/y:%d", + + -- 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); - ellipse(dot_x, dot_y, 10, 10); + -- draw the joystick dot + ellipse(dot_x, dot_y, 10, 10); end + function draw() - fill (0,255,0,1); + -- clear the screen background(0,0,0,0); if JOYSTICKS == 0 then - text(10, cell_size, string.format("No joysticks detected .. plug one in!")); + 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 From 7dc24c1f74541e1e77b6dfa71e2bae9f0ffedf41 Mon Sep 17 00:00:00 2001 From: Jay Vaughan Date: Tue, 13 Mar 2012 21:32:51 +0100 Subject: [PATCH 7/9] Retab for consistency with antirez, and more comments in demo. --- examples/joysticks.lua | 11 +++- load81.c | 146 ++++++++++++++++++++--------------------- 2 files changed, 82 insertions(+), 75 deletions(-) diff --git a/examples/joysticks.lua b/examples/joysticks.lua index 05ecc7c..ca00558 100644 --- a/examples/joysticks.lua +++ b/examples/joysticks.lua @@ -2,9 +2,16 @@ -- 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 has its own color, since there are four joysticks --- maximum available then we have four unique colors +-- 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}, diff --git a/load81.c b/load81.c index e6ac2e7..ffde7f0 100644 --- a/load81.c +++ b/load81.c @@ -277,16 +277,16 @@ 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 + 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"); + lua_getglobal(l81.L, "joystick"); if (lua_isnil(l81.L,-1)) { lua_pop(l81.L,1); @@ -295,29 +295,29 @@ void updateJoystickState(int joynum, char *field, int value) 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); - } + 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); + 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 + 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"); + lua_getglobal(l81.L, "joystick"); if (lua_isnil(l81.L,-1)) { lua_pop(l81.L,1); @@ -326,15 +326,15 @@ void updateJoystickName(int joynum, const char *name) 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); - } + 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); + lua_pop(l81.L, 2); } @@ -369,15 +369,15 @@ void mouseMovedEvent(int x, int y, int xrel, int yrel) { } void joystickXMovedEvent(int joy_num, Sint16 x) { - if (joy_num < MAX_JOYSTICKS) { - updateJoystickState(joy_num, "x", 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); - } + if (joy_num < MAX_JOYSTICKS) { + updateJoystickState(joy_num, "y", y); + } } void mouseButtonEvent(int button, int pressed) { @@ -431,15 +431,15 @@ 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_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); @@ -508,22 +508,22 @@ int loadProgram(void) { } void initJoysticks(frameBuffer *fb) { - int cur_joy; - /* Initialize Joysticks */ + 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); + 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)); + 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])); + { + 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 @@ -534,14 +534,14 @@ void initJoysticks(frameBuffer *fb) { Close joysticks. */ void closeJoysticks(frameBuffer *fb) { - int cur_joy; + 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"); - } + if (fb->joysticks[cur_joy]) { + SDL_JoystickClose( fb->joysticks[cur_joy]); + updateJoystickName(cur_joy + 1, "none"); + } } - setNumber("JOYSTICKS", 0); + setNumber("JOYSTICKS", 0); } void initScreen(void) { @@ -553,12 +553,12 @@ void resetProgram(void) { char *initscript = "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 */ + "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); @@ -570,7 +570,7 @@ void resetProgram(void) { luaopen_debug(l81.L); setNumber("WIDTH",l81.width); setNumber("HEIGHT",l81.height); - setNumber("JOYSTICKS", SDL_NumJoysticks()); + setNumber("JOYSTICKS", SDL_NumJoysticks()); luaL_loadbuffer(l81.L,initscript,strlen(initscript),"initscript"); lua_pcall(l81.L,0,0,0); @@ -581,7 +581,7 @@ void resetProgram(void) { setTableFieldNumber("mouse","xrel",0); setTableFieldNumber("mouse","yrel",0); - initJoysticks(l81.fb); + initJoysticks(l81.fb); /* Register API */ lua_pushcfunction(l81.L,fillBinding); @@ -682,6 +682,6 @@ int main(int argc, char **argv) { } editorRun(); } - closeJoysticks(l81.fb); + closeJoysticks(l81.fb); return 0; } From 352592cd557c7e6e5a4481fb5efe3a90f77ebb25 Mon Sep 17 00:00:00 2001 From: Jay Vaughan Date: Wed, 14 Mar 2012 10:53:59 +0100 Subject: [PATCH 8/9] Properly pop the Lua stack in Joysticks --- load81.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/load81.c b/load81.c index ffde7f0..f78c4a9 100644 --- a/load81.c +++ b/load81.c @@ -303,7 +303,7 @@ void updateJoystickState(int joynum, char *field, int value) lua_settable(l81.L, -3); } - lua_pop(l81.L, 2); + lua_pop(l81.L, 3); } /* @@ -334,7 +334,7 @@ void updateJoystickName(int joynum, const char *name) lua_settable(l81.L, -3); } - lua_pop(l81.L, 2); + lua_pop(l81.L, 3); } From 80df0d7a8d9ee05f7bf17c94f137a40b47d8d9be Mon Sep 17 00:00:00 2001 From: Jay Vaughan Date: Wed, 14 Mar 2012 11:02:59 +0100 Subject: [PATCH 9/9] Nope, that wasn't right. --- load81.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/load81.c b/load81.c index f78c4a9..ffde7f0 100644 --- a/load81.c +++ b/load81.c @@ -303,7 +303,7 @@ void updateJoystickState(int joynum, char *field, int value) lua_settable(l81.L, -3); } - lua_pop(l81.L, 3); + lua_pop(l81.L, 2); } /* @@ -334,7 +334,7 @@ void updateJoystickName(int joynum, const char *name) lua_settable(l81.L, -3); } - lua_pop(l81.L, 3); + lua_pop(l81.L, 2); }