Fixed some little errors with the drawing functions.
[luagame.git] / funcs_input.cpp
blob20b13ec67bd056ceffd3e8d36631e74bc1c62bbb
1 /*
2 Copyright (c)2006-2007 - Brett Lajzer
4 See LICENSE for license information.
5 */
7 #include "SDL/SDL.h" #include "lua.hpp"
8 #include "globals.h"
9 #include "funcs_input.h"
11 //returns the number of joysticks
12 int l_num_joysticks(lua_State *L){
13 lua_pushinteger(L, SDL_NumJoysticks());
14 return 1;
17 //get mouse position
18 int l_mouse_state(lua_State *L){
19 int x=0; int y=0; bool left=false; bool right=false;
21 SDL_PumpEvents();
22 Uint8 mouse_state = SDL_GetMouseState(&x, &y);
23 if(mouse_state&SDL_BUTTON(1))
24 left = true;
25 if(mouse_state&SDL_BUTTON(3))
26 right = true;
28 lua_pushinteger(L, (lua_Integer)x);
29 lua_pushinteger(L, (lua_Integer)y);
30 lua_pushboolean(L, left);
31 lua_pushboolean(L, right);
33 return 4;
36 //return an event to lua
37 int l_get_event(lua_State *L){
38 SDL_Event e;
40 //if there is an event, then return it, else nil
41 if(SDL_PollEvent(&e)){
42 switch(e.type){
43 case SDL_KEYDOWN:
44 lua_pushstring(L, "keyboard");
45 lua_pushstring(L, "pressed");
46 lua_pushinteger(L, (int)e.key.keysym.sym);
47 return 3;
48 break;
49 case SDL_KEYUP:
50 lua_pushstring(L, "keyboard");
51 lua_pushstring(L, "released");
52 lua_pushinteger(L, (int)e.key.keysym.sym);
53 return 3;
54 break;
55 case SDL_MOUSEMOTION:
56 lua_pushstring(L, "mouse");
57 lua_pushstring(L, "motion");
58 lua_pushinteger(L,(int)e.motion.x);
59 lua_pushinteger(L,(int)e.motion.y);
60 lua_pushinteger(L,(int)e.motion.xrel);
61 lua_pushinteger(L,(int)e.motion.yrel);
62 return 6;
63 break;
64 case SDL_MOUSEBUTTONDOWN:
65 lua_pushstring(L, "mouse");
66 lua_pushstring(L, "pressed");
67 lua_pushinteger(L,(int)e.button.button);
68 lua_pushinteger(L,(int)e.button.x);
69 lua_pushinteger(L,(int)e.button.y);
70 return 5;
71 break;
72 case SDL_MOUSEBUTTONUP:
73 lua_pushstring(L, "mouse");
74 lua_pushstring(L, "released");
75 lua_pushinteger(L,(int)e.button.button);
76 lua_pushinteger(L,(int)e.button.x);
77 lua_pushinteger(L,(int)e.button.y);
78 return 5;
79 break;
80 case SDL_JOYAXISMOTION:
81 lua_pushstring(L, "joystick");
82 lua_pushstring(L, "axis_motion");
83 lua_pushinteger(L,(int)(e.jaxis.which+1));
84 lua_pushinteger(L,(int)(e.jaxis.axis+1));
85 lua_pushinteger(L,(int)e.jaxis.value);
86 return 5;
87 break;
88 case SDL_JOYBALLMOTION:
89 lua_pushstring(L, "joystick");
90 lua_pushstring(L, "ball_motion");
91 lua_pushinteger(L,(int)(e.jball.which+1));
92 lua_pushinteger(L,(int)(e.jball.ball+1));
93 lua_pushinteger(L,(int)e.jball.xrel);
94 lua_pushinteger(L,(int)e.jball.yrel);
95 return 6;
96 break;
97 case SDL_JOYHATMOTION:
98 lua_pushstring(L, "joystick");
99 lua_pushstring(L, "hat_motion");
100 lua_pushinteger(L,(int)(e.jhat.which+1));
101 lua_pushinteger(L,(int)(e.jhat.hat+1));
102 lua_pushinteger(L,(int)e.jhat.value);
103 return 5;
104 break;
105 case SDL_JOYBUTTONDOWN:
106 lua_pushstring(L, "joystick");
107 lua_pushstring(L, "pressed");
108 lua_pushinteger(L,(int)(e.jbutton.which+1));
109 lua_pushinteger(L,(int)(e.jbutton.button+1));
110 return 4;
111 break;
112 case SDL_JOYBUTTONUP:
113 lua_pushstring(L, "joystick");
114 lua_pushstring(L, "released");
115 lua_pushinteger(L,(int)(e.jbutton.which+1));
116 lua_pushinteger(L,(int)(e.jbutton.button+1));
117 return 4;
118 break;
119 case SDL_QUIT:
120 lua_pushstring(L, "quit");
121 return 1;
122 break;
123 case SDL_SYSWMEVENT:
124 case SDL_VIDEORESIZE:
125 break; //not supported yet
126 case SDL_VIDEOEXPOSE:
127 break; //not supported yet
128 case SDL_USEREVENT:
129 lua_pushnil(L);
130 return 1;
131 break;
134 lua_pushnil(L);
135 return 1;