1 #include "SDL/SDL.h"
#include "lua.hpp"
5 //returns the number of joysticks
6 static int l_num_joysticks(lua_State
*L
){
7 lua_pushinteger(L
, SDL_NumJoysticks());
12 static int l_open_joystick(lua_State
*L
){
13 lua_pushlightuserdata(L
,SDL_JoystickOpen((int)lua_tonumber(L
,1)));
18 static int l_close_joystick(lua_State
*L
){
19 SDL_JoystickClose((SDL_Joystick
*)lua_touserdata(L
,1));
24 static int l_mouse_state(lua_State
*L
){
25 int x
=0; int y
=0; bool left
=false; bool right
=false;
28 Uint8 mouse_state
= SDL_GetMouseState(&x
, &y
);
29 if(mouse_state
&SDL_BUTTON(1))
31 if(mouse_state
&SDL_BUTTON(3))
34 lua_pushinteger(L
, (lua_Integer
)x
);
35 lua_pushinteger(L
, (lua_Integer
)y
);
36 lua_pushboolean(L
, left
);
37 lua_pushboolean(L
, right
);
42 //return an event to lua
43 static int l_get_event(lua_State
*L
){
46 //if there is an event, then return it, else nil
47 if(SDL_PollEvent(&e
)){
50 lua_pushstring(L
, "keyboard");
51 lua_pushstring(L
, "pressed");
52 lua_pushinteger(L
, (int)e
.key
.keysym
.sym
);
56 lua_pushstring(L
, "keyboard");
57 lua_pushstring(L
, "released");
58 lua_pushinteger(L
, (int)e
.key
.keysym
.sym
);
62 lua_pushstring(L
, "mouse");
63 lua_pushstring(L
, "motion");
64 lua_pushinteger(L
,(int)e
.motion
.x
);
65 lua_pushinteger(L
,(int)e
.motion
.y
);
66 lua_pushinteger(L
,(int)e
.motion
.xrel
);
67 lua_pushinteger(L
,(int)e
.motion
.yrel
);
70 case SDL_MOUSEBUTTONDOWN
:
71 lua_pushstring(L
, "mouse");
72 lua_pushstring(L
, "pressed");
73 lua_pushinteger(L
,(int)e
.button
.button
);
74 lua_pushinteger(L
,(int)e
.button
.x
);
75 lua_pushinteger(L
,(int)e
.button
.y
);
78 case SDL_MOUSEBUTTONUP
:
79 lua_pushstring(L
, "mouse");
80 lua_pushstring(L
, "released");
81 lua_pushinteger(L
,(int)e
.button
.button
);
82 lua_pushinteger(L
,(int)e
.button
.x
);
83 lua_pushinteger(L
,(int)e
.button
.y
);
86 case SDL_JOYAXISMOTION
:
87 lua_pushstring(L
, "joystick");
88 lua_pushstring(L
, "axis_motion");
89 lua_pushinteger(L
,(int)(e
.jaxis
.which
+1));
90 lua_pushinteger(L
,(int)(e
.jaxis
.axis
+1));
91 lua_pushinteger(L
,(int)e
.jaxis
.value
);
94 case SDL_JOYBALLMOTION
:
95 lua_pushstring(L
, "joystick");
96 lua_pushstring(L
, "ball_motion");
97 lua_pushinteger(L
,(int)(e
.jball
.which
+1));
98 lua_pushinteger(L
,(int)(e
.jball
.ball
+1));
99 lua_pushinteger(L
,(int)e
.jball
.xrel
);
100 lua_pushinteger(L
,(int)e
.jball
.yrel
);
103 case SDL_JOYHATMOTION
:
104 lua_pushstring(L
, "joystick");
105 lua_pushstring(L
, "hat_motion");
106 lua_pushinteger(L
,(int)(e
.jhat
.which
+1));
107 lua_pushinteger(L
,(int)(e
.jhat
.hat
+1));
108 lua_pushinteger(L
,(int)e
.jhat
.value
);
111 case SDL_JOYBUTTONDOWN
:
112 lua_pushstring(L
, "joystick");
113 lua_pushstring(L
, "pressed");
114 lua_pushinteger(L
,(int)(e
.jbutton
.which
+1));
115 lua_pushinteger(L
,(int)(e
.jbutton
.button
+1));
118 case SDL_JOYBUTTONUP
:
119 lua_pushstring(L
, "joystick");
120 lua_pushstring(L
, "released");
121 lua_pushinteger(L
,(int)(e
.jbutton
.which
+1));
122 lua_pushinteger(L
,(int)(e
.jbutton
.button
+1));
127 case SDL_VIDEORESIZE
:
128 case SDL_VIDEOEXPOSE
: