Initial Commit.
[luagame.git] / funcs_input.h
blob727e7470e573d2b15c55da9e30695f5c8c6d05c7
1 #include "SDL/SDL.h" #include "lua.hpp"
2 #include "globals.h"
3 #include <iostream>
5 //returns the number of joysticks
6 static int l_num_joysticks(lua_State *L){
7 lua_pushinteger(L, SDL_NumJoysticks());
8 return 1;
11 //opens a joystick
12 static int l_open_joystick(lua_State *L){
13 lua_pushlightuserdata(L,SDL_JoystickOpen((int)lua_tonumber(L,1)));
14 return 1;
17 //closes a joystick
18 static int l_close_joystick(lua_State *L){
19 SDL_JoystickClose((SDL_Joystick*)lua_touserdata(L,1));
20 return 0;
23 //get mouse position
24 static int l_mouse_state(lua_State *L){
25 int x=0; int y=0; bool left=false; bool right=false;
27 SDL_PumpEvents();
28 Uint8 mouse_state = SDL_GetMouseState(&x, &y);
29 if(mouse_state&SDL_BUTTON(1))
30 left = true;
31 if(mouse_state&SDL_BUTTON(3))
32 right = true;
34 lua_pushinteger(L, (lua_Integer)x);
35 lua_pushinteger(L, (lua_Integer)y);
36 lua_pushboolean(L, left);
37 lua_pushboolean(L, right);
39 return 4;
42 //return an event to lua
43 static int l_get_event(lua_State *L){
44 SDL_Event e;
46 //if there is an event, then return it, else nil
47 if(SDL_PollEvent(&e)){
48 switch(e.type){
49 case SDL_KEYDOWN:
50 lua_pushstring(L, "keyboard");
51 lua_pushstring(L, "pressed");
52 lua_pushinteger(L, (int)e.key.keysym.sym);
53 return 3;
54 break;
55 case SDL_KEYUP:
56 lua_pushstring(L, "keyboard");
57 lua_pushstring(L, "released");
58 lua_pushinteger(L, (int)e.key.keysym.sym);
59 return 3;
60 break;
61 case SDL_MOUSEMOTION:
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);
68 return 6;
69 break;
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);
76 return 5;
77 break;
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);
84 return 5;
85 break;
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);
92 return 5;
93 break;
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);
101 return 6;
102 break;
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);
109 return 5;
110 break;
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));
116 return 4;
117 break;
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));
123 return 4;
124 break;
125 case SDL_QUIT:
126 case SDL_SYSWMEVENT:
127 case SDL_VIDEORESIZE:
128 case SDL_VIDEOEXPOSE:
129 case SDL_USEREVENT:
130 lua_pushnil(L);
131 return 1;
132 break;
135 lua_pushnil(L);
136 return 1;