Finally abstracted constants properly. Changed how the event module works; wasn't really abstract before. Removed libs we're not going to use: native, signal.

This commit is contained in:
rude
2009-10-10 11:11:04 +02:00
parent 0a2ae705dd
commit 87e3c4131c
101 changed files with 3225 additions and 33496 deletions
+235
View File
@@ -0,0 +1,235 @@
/**
* Copyright (c) 2006-2009 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "Event.h"
namespace love
{
namespace event
{
Event::~Event()
{
}
bool Event::getConstant(const char * in, Event::Type & out)
{
return types.find(in, out);
}
bool Event::getConstant(Event::Type in, const char *& out)
{
return types.find(in, out);
}
bool Event::getConstant(const char * in, love::mouse::Mouse::Button & out)
{
return buttons.find(in, out);
}
bool Event::getConstant(love::mouse::Mouse::Button in, const char *& out)
{
return buttons.find(in, out);
}
bool Event::getConstant(const char * in, love::keyboard::Keyboard::Key & out)
{
return keys.find(in, out);
}
bool Event::getConstant(love::keyboard::Keyboard::Key in, const char *& out)
{
return keys.find(in, out);
}
StringMap<Event::Type, Event::TYPE_MAX_ENUM>::Entry Event::typeEntries[] =
{
{"kp", Event::TYPE_KEY_PRESSED},
{"kr", Event::TYPE_KEY_RELEASED},
{"mp", Event::TYPE_MOUSE_PRESSED},
{"mr", Event::TYPE_MOUSE_RELEASED},
{"jp", Event::TYPE_JOYSTICK_PRESSED},
{"jr", Event::TYPE_JOYSTICK_RELEASED},
{"q", Event::TYPE_QUIT},
};
StringMap<Event::Type, Event::TYPE_MAX_ENUM> Event::types(Event::typeEntries, sizeof(Event::typeEntries));
StringMap<love::mouse::Mouse::Button, love::mouse::Mouse::BUTTON_MAX_ENUM>::Entry Event::buttonEntries[] =
{
{"l", love::mouse::Mouse::BUTTON_LEFT},
{"m", love::mouse::Mouse::BUTTON_MIDDLE},
{"r", love::mouse::Mouse::BUTTON_RIGHT},
{"wu", love::mouse::Mouse::BUTTON_WHEELUP},
{"wd", love::mouse::Mouse::BUTTON_WHEELDOWN},
{"x1", love::mouse::Mouse::BUTTON_X1},
{"x2", love::mouse::Mouse::BUTTON_X2},
};
StringMap<love::mouse::Mouse::Button, love::mouse::Mouse::BUTTON_MAX_ENUM> Event::buttons(Event::buttonEntries, sizeof(Event::buttonEntries));
StringMap<love::keyboard::Keyboard::Key, love::keyboard::Keyboard::KEY_MAX_ENUM>::Entry Event::keyEntries[] =
{
{"backspace", love::keyboard::Keyboard::KEY_BACKSPACE},
{"tab", love::keyboard::Keyboard::KEY_TAB},
{"clear", love::keyboard::Keyboard::KEY_CLEAR},
{"return", love::keyboard::Keyboard::KEY_RETURN},
{"pause", love::keyboard::Keyboard::KEY_PAUSE},
{"escape", love::keyboard::Keyboard::KEY_ESCAPE},
{" ", love::keyboard::Keyboard::KEY_SPACE},
{"!", love::keyboard::Keyboard::KEY_EXCLAIM},
{"\"", love::keyboard::Keyboard::KEY_QUOTEDBL},
{"#", love::keyboard::Keyboard::KEY_HASH},
{"$", love::keyboard::Keyboard::KEY_DOLLAR},
{"&", love::keyboard::Keyboard::KEY_AMPERSAND},
{"'", love::keyboard::Keyboard::KEY_QUOTE},
{"(", love::keyboard::Keyboard::KEY_LEFTPAREN},
{")", love::keyboard::Keyboard::KEY_RIGHTPAREN},
{"*", love::keyboard::Keyboard::KEY_ASTERISK},
{"+", love::keyboard::Keyboard::KEY_PLUS},
{",", love::keyboard::Keyboard::KEY_COMMA},
{"-", love::keyboard::Keyboard::KEY_MINUS},
{".", love::keyboard::Keyboard::KEY_PERIOD},
{"/", love::keyboard::Keyboard::KEY_SLASH},
{"0", love::keyboard::Keyboard::KEY_0},
{"1", love::keyboard::Keyboard::KEY_1},
{"2", love::keyboard::Keyboard::KEY_2},
{"3", love::keyboard::Keyboard::KEY_3},
{"4", love::keyboard::Keyboard::KEY_4},
{"5", love::keyboard::Keyboard::KEY_5},
{"6", love::keyboard::Keyboard::KEY_6},
{"7", love::keyboard::Keyboard::KEY_7},
{"8", love::keyboard::Keyboard::KEY_8},
{"9", love::keyboard::Keyboard::KEY_9},
{":", love::keyboard::Keyboard::KEY_COLON},
{";", love::keyboard::Keyboard::KEY_SEMICOLON},
{"<", love::keyboard::Keyboard::KEY_LESS},
{"=", love::keyboard::Keyboard::KEY_EQUALS},
{">", love::keyboard::Keyboard::KEY_GREATER},
{"?", love::keyboard::Keyboard::KEY_QUESTION},
{"@", love::keyboard::Keyboard::KEY_AT},
{"[", love::keyboard::Keyboard::KEY_LEFTBRACKET},
{"\\", love::keyboard::Keyboard::KEY_BACKSLASH},
{"]", love::keyboard::Keyboard::KEY_RIGHTBRACKET},
{"^", love::keyboard::Keyboard::KEY_CARET},
{"_", love::keyboard::Keyboard::KEY_UNDERSCORE},
{"`", love::keyboard::Keyboard::KEY_BACKQUOTE},
{"a", love::keyboard::Keyboard::KEY_A},
{"b", love::keyboard::Keyboard::KEY_B},
{"c", love::keyboard::Keyboard::KEY_C},
{"d", love::keyboard::Keyboard::KEY_D},
{"e", love::keyboard::Keyboard::KEY_E},
{"f", love::keyboard::Keyboard::KEY_F},
{"g", love::keyboard::Keyboard::KEY_G},
{"h", love::keyboard::Keyboard::KEY_H},
{"i", love::keyboard::Keyboard::KEY_I},
{"j", love::keyboard::Keyboard::KEY_J},
{"k", love::keyboard::Keyboard::KEY_K},
{"l", love::keyboard::Keyboard::KEY_L},
{"m", love::keyboard::Keyboard::KEY_M},
{"n", love::keyboard::Keyboard::KEY_N},
{"o", love::keyboard::Keyboard::KEY_O},
{"p", love::keyboard::Keyboard::KEY_P},
{"q", love::keyboard::Keyboard::KEY_Q},
{"r", love::keyboard::Keyboard::KEY_R},
{"s", love::keyboard::Keyboard::KEY_S},
{"t", love::keyboard::Keyboard::KEY_T},
{"u", love::keyboard::Keyboard::KEY_U},
{"v", love::keyboard::Keyboard::KEY_V},
{"w", love::keyboard::Keyboard::KEY_W},
{"x", love::keyboard::Keyboard::KEY_X},
{"y", love::keyboard::Keyboard::KEY_Y},
{"z", love::keyboard::Keyboard::KEY_Z},
{"delete", love::keyboard::Keyboard::KEY_DELETE},
{"kp0", love::keyboard::Keyboard::KEY_KP0},
{"kp1", love::keyboard::Keyboard::KEY_KP1},
{"kp2", love::keyboard::Keyboard::KEY_KP2},
{"kp3", love::keyboard::Keyboard::KEY_KP3},
{"kp4", love::keyboard::Keyboard::KEY_KP4},
{"kp5", love::keyboard::Keyboard::KEY_KP5},
{"kp6", love::keyboard::Keyboard::KEY_KP6},
{"kp7", love::keyboard::Keyboard::KEY_KP7},
{"kp8", love::keyboard::Keyboard::KEY_KP8},
{"kp9", love::keyboard::Keyboard::KEY_KP9},
{"kp.", love::keyboard::Keyboard::KEY_KP_PERIOD},
{"kp/", love::keyboard::Keyboard::KEY_KP_DIVIDE},
{"kp*", love::keyboard::Keyboard::KEY_KP_MULTIPLY},
{"kp-", love::keyboard::Keyboard::KEY_KP_MINUS},
{"kp+", love::keyboard::Keyboard::KEY_KP_PLUS},
{"kpenter", love::keyboard::Keyboard::KEY_KP_ENTER},
{"kp=", love::keyboard::Keyboard::KEY_KP_EQUALS},
{"kpup", love::keyboard::Keyboard::KEY_UP},
{"kpdown", love::keyboard::Keyboard::KEY_DOWN},
{"kpright", love::keyboard::Keyboard::KEY_RIGHT},
{"kpleft", love::keyboard::Keyboard::KEY_LEFT},
{"kpinsert", love::keyboard::Keyboard::KEY_INSERT},
{"kphome", love::keyboard::Keyboard::KEY_HOME},
{"kpend", love::keyboard::Keyboard::KEY_END},
{"kppageup", love::keyboard::Keyboard::KEY_PAGEUP},
{"kppagedown", love::keyboard::Keyboard::KEY_PAGEDOWN},
{"f1", love::keyboard::Keyboard::KEY_F1},
{"f2", love::keyboard::Keyboard::KEY_F2},
{"f3", love::keyboard::Keyboard::KEY_F3},
{"f4", love::keyboard::Keyboard::KEY_F4},
{"f5", love::keyboard::Keyboard::KEY_F5},
{"f6", love::keyboard::Keyboard::KEY_F6},
{"f7", love::keyboard::Keyboard::KEY_F7},
{"f8", love::keyboard::Keyboard::KEY_F8},
{"f9", love::keyboard::Keyboard::KEY_F9},
{"f10", love::keyboard::Keyboard::KEY_F10},
{"f11", love::keyboard::Keyboard::KEY_F11},
{"f12", love::keyboard::Keyboard::KEY_F12},
{"f13", love::keyboard::Keyboard::KEY_F13},
{"f14", love::keyboard::Keyboard::KEY_F14},
{"f15", love::keyboard::Keyboard::KEY_F15},
{"numlock", love::keyboard::Keyboard::KEY_NUMLOCK},
{"capslock", love::keyboard::Keyboard::KEY_CAPSLOCK},
{"scrollock", love::keyboard::Keyboard::KEY_SCROLLOCK},
{"rshift", love::keyboard::Keyboard::KEY_RSHIFT},
{"lshift", love::keyboard::Keyboard::KEY_LSHIFT},
{"rctrl", love::keyboard::Keyboard::KEY_RCTRL},
{"lctrl", love::keyboard::Keyboard::KEY_LCTRL},
{"ralt", love::keyboard::Keyboard::KEY_RALT},
{"lalt", love::keyboard::Keyboard::KEY_LALT},
{"rmeta", love::keyboard::Keyboard::KEY_RMETA},
{"lmeta", love::keyboard::Keyboard::KEY_LMETA},
{"lsuper", love::keyboard::Keyboard::KEY_LSUPER},
{"rsuper", love::keyboard::Keyboard::KEY_RSUPER},
{"mode", love::keyboard::Keyboard::KEY_MODE},
{"compose", love::keyboard::Keyboard::KEY_COMPOSE},
{"help", love::keyboard::Keyboard::KEY_HELP},
{"print", love::keyboard::Keyboard::KEY_PRINT},
{"sysreq", love::keyboard::Keyboard::KEY_SYSREQ},
{"break", love::keyboard::Keyboard::KEY_BREAK},
{"menu", love::keyboard::Keyboard::KEY_MENU},
{"power", love::keyboard::Keyboard::KEY_POWER},
{"euro", love::keyboard::Keyboard::KEY_EURO},
{"undo", love::keyboard::Keyboard::KEY_UNDO},
};
StringMap<love::keyboard::Keyboard::Key, love::keyboard::Keyboard::KEY_MAX_ENUM> Event::keys(Event::keyEntries, sizeof(Event::keyEntries));
} // event
} // love
+58 -29
View File
@@ -23,6 +23,9 @@
// LOVE
#include <common/Module.h>
#include <common/StringMap.h>
#include <keyboard/Keyboard.h>
#include <mouse/Mouse.h>
namespace love
{
@@ -30,40 +33,66 @@ namespace event
{
class Event : public Module
{
protected:
virtual ~Event(){};
public:
enum
enum Type
{
EVENT_NOEVENT = 0,
EVENT_ACTIVEEVENT,
EVENT_KEYDOWN,
EVENT_KEYUP,
EVENT_MOUSEMOTION,
EVENT_MOUSEBUTTONDOWN,
EVENT_MOUSEBUTTONUP,
EVENT_JOYAXISMOTION,
EVENT_JOYBALLMOTION,
EVENT_JOYHATMOTION,
EVENT_JOYBUTTONDOWN,
EVENT_JOYBUTTONUP,
EVENT_QUIT,
EVENT_SYSWMEVENT,
EVENT_RESERVEDA,
EVENT_RESERVEDB,
EVENT_VIDEORESIZE,
EVENT_VIDEOEXPOSE,
EVENT_RESERVED2,
EVENT_RESERVED3,
EVENT_RESERVED4,
EVENT_RESERVED5,
EVENT_RESERVED6,
EVENT_RESERVED7,
EVENT_USEREVENT = 24,
EVENT_NUMEVENTS = 32
TYPE_INVALID,
TYPE_KEY_PRESSED,
TYPE_KEY_RELEASED,
TYPE_MOUSE_PRESSED,
TYPE_MOUSE_RELEASED,
TYPE_JOYSTICK_RELEASED,
TYPE_JOYSTICK_PRESSED,
TYPE_QUIT,
TYPE_MAX_ENUM = 32
};
union Message
{
Type type;
struct
{
Type type;
love::mouse::Mouse::Button b;
unsigned x;
unsigned y;
} mouse;
struct
{
Type type;
unsigned index;
unsigned button;
} joystick;
struct
{
Type type;
love::keyboard::Keyboard::Key k;
unsigned short u;
} keyboard;
};
virtual ~Event();
static bool getConstant(const char * in, Type & out);
static bool getConstant(Type in, const char *& out);
static bool getConstant(const char * in, love::mouse::Mouse::Button & out);
static bool getConstant(love::mouse::Mouse::Button in, const char *& out);
static bool getConstant(const char * in, love::keyboard::Keyboard::Key & out);
static bool getConstant(love::keyboard::Keyboard::Key in, const char *& out);
private:
static StringMap<Type, TYPE_MAX_ENUM>::Entry typeEntries[];
static StringMap<Type, TYPE_MAX_ENUM> types;
static StringMap<love::mouse::Mouse::Button, love::mouse::Mouse::BUTTON_MAX_ENUM>::Entry buttonEntries[];
static StringMap<love::mouse::Mouse::Button, love::mouse::Mouse::BUTTON_MAX_ENUM> buttons;
static StringMap<love::keyboard::Keyboard::Key, love::keyboard::Keyboard::KEY_MAX_ENUM>::Entry keyEntries[];
static StringMap<love::keyboard::Keyboard::Key, love::keyboard::Keyboard::KEY_MAX_ENUM> keys;
}; // Event
} // event
+230 -98
View File
@@ -20,6 +20,9 @@
#include "Event.h"
#include <keyboard/Keyboard.h>
#include <mouse/Mouse.h>
namespace love
{
namespace event
@@ -31,138 +34,267 @@ namespace sdl
return "love.event.sdl";
}
Event::Event()
{
SDL_EnableUNICODE(1);
}
void Event::pump()
{
SDL_PumpEvents();
}
int Event::poll(lua_State * L)
bool Event::poll(Message & message)
{
lua_pushcclosure(L, &poll_i, 0);
return 1;
}
int Event::wait(lua_State * L)
{
SDL_EnableUNICODE(1);
// The union used to get SDL events.
static SDL_Event e;
SDL_WaitEvent(&e);
return Event::pushEvent(L, e);
}
void Event::quit()
{
SDL_Event e;
e.type = Event::EVENT_QUIT;
SDL_PushEvent(&e);
}
int Event::push(lua_State * L)
{
SDL_Event e;
getEvent(L, e);
SDL_PushEvent(&e);
return 0;
}
int Event::poll_i(lua_State * L)
{
SDL_EnableUNICODE(1);
// The union used to get SDL events.
static SDL_Event e;
// Get ONE event.
while(SDL_PollEvent(&e))
{
int args = Event::pushEvent(L, e);
if(args > 0)
return args;
}
// No pending events.
return 0;
if(convert(e, message))
return true;
}
return false;
}
int Event::pushEvent(lua_State * L, SDL_Event & e)
bool Event::wait(Message & message)
{
static SDL_Event e;
bool ok = (SDL_WaitEvent(&e) == 1);
return ok && convert(e, message);
}
bool Event::push(Message & message)
{
static SDL_Event e;
bool ok = convert(message, e);
return ok && (SDL_PushEvent(&e) == 0);
}
bool Event::convert(SDL_Event & e, Message & m)
{
switch(e.type)
{
case SDL_KEYDOWN:
lua_pushinteger(L, e.type);
lua_pushinteger(L, e.key.keysym.sym);
lua_pushinteger(L, e.key.keysym.unicode);
return 3;
m.type = Event::TYPE_KEY_PRESSED;
m.keyboard.u = e.key.keysym.unicode;
return keys.find(e.key.keysym.sym, m.keyboard.k);
case SDL_KEYUP:
lua_pushinteger(L, e.type);
lua_pushinteger(L, e.key.keysym.sym);
return 2;
m.type = Event::TYPE_KEY_RELEASED;
return keys.find(e.key.keysym.sym, m.keyboard.k);
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
lua_pushinteger(L, e.type);
lua_pushinteger(L, e.button.x);
lua_pushinteger(L, e.button.y);
lua_pushinteger(L, e.button.button);
return 4;
m.type = (e.type == SDL_MOUSEBUTTONDOWN) ? Event::TYPE_MOUSE_PRESSED : Event::TYPE_MOUSE_RELEASED;
m.mouse.x = e.button.x;
m.mouse.y = e.button.y;
return buttons.find(e.button.button, m.mouse.b);
case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP:
lua_pushinteger(L, e.type);
lua_pushinteger(L, e.jbutton.which);
lua_pushinteger(L, e.jbutton.button);
return 3;
m.type = (e.type == SDL_JOYBUTTONDOWN) ? Event::TYPE_JOYSTICK_PRESSED : Event::TYPE_JOYSTICK_RELEASED;
m.joystick.button = e.jbutton.button;
m.joystick.index = e.jbutton.which;
return true;
case SDL_QUIT:
lua_pushinteger(L, e.type);
return 1;
default:
break;
m.type = Event::TYPE_QUIT;
return true;
}
return 0;
return false;
}
int Event::getEvent(lua_State * L, SDL_Event & e)
bool Event::convert(Message & m, SDL_Event & e)
{
int type = luaL_checkint(L, 1);
switch(type)
switch(m.type)
{
case EVENT_KEYDOWN:
e.type = type;
e.key.keysym.sym = (SDLKey)luaL_checkint(L, 2);
e.key.keysym.unicode = luaL_checkint(L, 3);
return 3;
case EVENT_KEYUP:
e.type = type;
e.key.keysym.sym = (SDLKey)luaL_checkint(L, 2);
return 2;
case EVENT_MOUSEBUTTONDOWN:
case EVENT_MOUSEBUTTONUP:
e.type = type;
e.button.x = luaL_checkint(L, 2);
e.button.y = luaL_checkint(L, 3);
e.button.button = luaL_checkint(L, 4);
return 4;
case EVENT_JOYBUTTONDOWN:
case EVENT_JOYBUTTONUP:
e.type = type;
e.jbutton.which = luaL_checkint(L, 2);
e.jbutton.button = luaL_checkint(L, 3);
return 3;
case EVENT_QUIT:
e.type = type;
return 1;
default:
e.type = EVENT_NOEVENT;
break;
case Event::TYPE_KEY_PRESSED:
e.type = SDL_KEYDOWN;
e.key.keysym.unicode = (Uint16)m.keyboard.u;
return keys.find(m.keyboard.k, e.key.keysym.sym);;
case Event::TYPE_KEY_RELEASED:
e.type = SDL_KEYUP;
return keys.find(m.keyboard.k, e.key.keysym.sym);
case Event::TYPE_MOUSE_PRESSED:
case Event::TYPE_MOUSE_RELEASED:
e.type = (m.type == Event::TYPE_MOUSE_PRESSED) ? SDL_MOUSEBUTTONDOWN : SDL_MOUSEBUTTONUP;
e.button.x = m.mouse.x;
e.button.y = m.mouse.y;
return buttons.find(m.mouse.b, e.button.button);
case Event::TYPE_JOYSTICK_PRESSED:
case Event::TYPE_JOYSTICK_RELEASED:
e.type = (m.type == Event::TYPE_JOYSTICK_PRESSED) ? SDL_JOYBUTTONDOWN : SDL_JOYBUTTONUP;
e.jbutton.which = m.joystick.index;
e.jbutton.button = m.joystick.button;
return true;
case Event::TYPE_QUIT:
e.type = SDL_QUIT;
return true;
}
return 0;
return true;
}
EnumMap<love::keyboard::Keyboard::Key, SDLKey, love::keyboard::Keyboard::KEY_MAX_ENUM>::Entry Event::keyEntries[] =
{
{ love::keyboard::Keyboard::KEY_BACKSPACE, SDLK_BACKSPACE},
{ love::keyboard::Keyboard::KEY_TAB, SDLK_TAB},
{ love::keyboard::Keyboard::KEY_CLEAR, SDLK_CLEAR},
{ love::keyboard::Keyboard::KEY_RETURN, SDLK_RETURN},
{ love::keyboard::Keyboard::KEY_PAUSE, SDLK_PAUSE},
{ love::keyboard::Keyboard::KEY_ESCAPE, SDLK_ESCAPE },
{ love::keyboard::Keyboard::KEY_SPACE, SDLK_SPACE },
{ love::keyboard::Keyboard::KEY_EXCLAIM, SDLK_EXCLAIM },
{ love::keyboard::Keyboard::KEY_QUOTEDBL, SDLK_QUOTEDBL },
{ love::keyboard::Keyboard::KEY_HASH, SDLK_HASH },
{ love::keyboard::Keyboard::KEY_DOLLAR, SDLK_DOLLAR },
{ love::keyboard::Keyboard::KEY_AMPERSAND, SDLK_AMPERSAND },
{ love::keyboard::Keyboard::KEY_QUOTE, SDLK_QUOTE },
{ love::keyboard::Keyboard::KEY_LEFTPAREN, SDLK_LEFTPAREN },
{ love::keyboard::Keyboard::KEY_RIGHTPAREN, SDLK_RIGHTPAREN },
{ love::keyboard::Keyboard::KEY_ASTERISK, SDLK_ASTERISK },
{ love::keyboard::Keyboard::KEY_PLUS, SDLK_PLUS },
{ love::keyboard::Keyboard::KEY_COMMA, SDLK_COMMA },
{ love::keyboard::Keyboard::KEY_MINUS, SDLK_MINUS },
{ love::keyboard::Keyboard::KEY_PERIOD, SDLK_PERIOD },
{ love::keyboard::Keyboard::KEY_SLASH, SDLK_SLASH },
{ love::keyboard::Keyboard::KEY_0, SDLK_0 },
{ love::keyboard::Keyboard::KEY_1, SDLK_1 },
{ love::keyboard::Keyboard::KEY_2, SDLK_2 },
{ love::keyboard::Keyboard::KEY_3, SDLK_3 },
{ love::keyboard::Keyboard::KEY_4, SDLK_4 },
{ love::keyboard::Keyboard::KEY_5, SDLK_5 },
{ love::keyboard::Keyboard::KEY_6, SDLK_6 },
{ love::keyboard::Keyboard::KEY_7, SDLK_7 },
{ love::keyboard::Keyboard::KEY_8, SDLK_8 },
{ love::keyboard::Keyboard::KEY_9, SDLK_9 },
{ love::keyboard::Keyboard::KEY_COLON, SDLK_COLON },
{ love::keyboard::Keyboard::KEY_SEMICOLON, SDLK_SEMICOLON },
{ love::keyboard::Keyboard::KEY_LESS, SDLK_LESS },
{ love::keyboard::Keyboard::KEY_EQUALS, SDLK_EQUALS },
{ love::keyboard::Keyboard::KEY_GREATER, SDLK_GREATER },
{ love::keyboard::Keyboard::KEY_QUESTION, SDLK_QUESTION },
{ love::keyboard::Keyboard::KEY_AT, SDLK_AT },
{ love::keyboard::Keyboard::KEY_LEFTBRACKET, SDLK_LEFTBRACKET },
{ love::keyboard::Keyboard::KEY_BACKSLASH, SDLK_BACKSLASH },
{ love::keyboard::Keyboard::KEY_RIGHTBRACKET, SDLK_RIGHTBRACKET },
{ love::keyboard::Keyboard::KEY_CARET, SDLK_CARET },
{ love::keyboard::Keyboard::KEY_UNDERSCORE, SDLK_UNDERSCORE },
{ love::keyboard::Keyboard::KEY_BACKQUOTE, SDLK_BACKQUOTE },
{ love::keyboard::Keyboard::KEY_A, SDLK_a },
{ love::keyboard::Keyboard::KEY_B, SDLK_b },
{ love::keyboard::Keyboard::KEY_C, SDLK_c },
{ love::keyboard::Keyboard::KEY_D, SDLK_d },
{ love::keyboard::Keyboard::KEY_E, SDLK_e },
{ love::keyboard::Keyboard::KEY_F, SDLK_f },
{ love::keyboard::Keyboard::KEY_G, SDLK_g },
{ love::keyboard::Keyboard::KEY_H, SDLK_h },
{ love::keyboard::Keyboard::KEY_I, SDLK_i },
{ love::keyboard::Keyboard::KEY_J, SDLK_j },
{ love::keyboard::Keyboard::KEY_K, SDLK_k },
{ love::keyboard::Keyboard::KEY_L, SDLK_l },
{ love::keyboard::Keyboard::KEY_M, SDLK_m },
{ love::keyboard::Keyboard::KEY_N, SDLK_n },
{ love::keyboard::Keyboard::KEY_O, SDLK_o },
{ love::keyboard::Keyboard::KEY_P, SDLK_p },
{ love::keyboard::Keyboard::KEY_Q, SDLK_q },
{ love::keyboard::Keyboard::KEY_R, SDLK_r },
{ love::keyboard::Keyboard::KEY_S, SDLK_s },
{ love::keyboard::Keyboard::KEY_T, SDLK_t },
{ love::keyboard::Keyboard::KEY_U, SDLK_u },
{ love::keyboard::Keyboard::KEY_V, SDLK_v },
{ love::keyboard::Keyboard::KEY_W, SDLK_w },
{ love::keyboard::Keyboard::KEY_X, SDLK_x },
{ love::keyboard::Keyboard::KEY_Y, SDLK_y },
{ love::keyboard::Keyboard::KEY_Z, SDLK_z },
{ love::keyboard::Keyboard::KEY_DELETE, SDLK_DELETE },
{ love::keyboard::Keyboard::KEY_KP0, SDLK_KP0 },
{ love::keyboard::Keyboard::KEY_KP1, SDLK_KP1 },
{ love::keyboard::Keyboard::KEY_KP2, SDLK_KP2 },
{ love::keyboard::Keyboard::KEY_KP3, SDLK_KP3 },
{ love::keyboard::Keyboard::KEY_KP4, SDLK_KP4 },
{ love::keyboard::Keyboard::KEY_KP5, SDLK_KP5 },
{ love::keyboard::Keyboard::KEY_KP6, SDLK_KP6 },
{ love::keyboard::Keyboard::KEY_KP7, SDLK_KP7 },
{ love::keyboard::Keyboard::KEY_KP8, SDLK_KP8 },
{ love::keyboard::Keyboard::KEY_KP9, SDLK_KP9 },
{ love::keyboard::Keyboard::KEY_KP_PERIOD, SDLK_KP_PERIOD },
{ love::keyboard::Keyboard::KEY_KP_DIVIDE, SDLK_KP_DIVIDE },
{ love::keyboard::Keyboard::KEY_KP_MULTIPLY, SDLK_KP_MULTIPLY},
{ love::keyboard::Keyboard::KEY_KP_MINUS, SDLK_KP_MINUS },
{ love::keyboard::Keyboard::KEY_KP_PLUS, SDLK_KP_PLUS },
{ love::keyboard::Keyboard::KEY_KP_ENTER, SDLK_KP_ENTER },
{ love::keyboard::Keyboard::KEY_KP_EQUALS, SDLK_KP_EQUALS },
{ love::keyboard::Keyboard::KEY_UP, SDLK_UP },
{ love::keyboard::Keyboard::KEY_DOWN, SDLK_DOWN },
{ love::keyboard::Keyboard::KEY_RIGHT, SDLK_RIGHT },
{ love::keyboard::Keyboard::KEY_LEFT, SDLK_LEFT },
{ love::keyboard::Keyboard::KEY_INSERT, SDLK_INSERT },
{ love::keyboard::Keyboard::KEY_HOME, SDLK_HOME },
{ love::keyboard::Keyboard::KEY_END, SDLK_END },
{ love::keyboard::Keyboard::KEY_PAGEUP, SDLK_PAGEUP },
{ love::keyboard::Keyboard::KEY_PAGEDOWN, SDLK_PAGEDOWN },
{ love::keyboard::Keyboard::KEY_F1, SDLK_F1 },
{ love::keyboard::Keyboard::KEY_F2, SDLK_F2 },
{ love::keyboard::Keyboard::KEY_F3, SDLK_F3 },
{ love::keyboard::Keyboard::KEY_F4, SDLK_F4 },
{ love::keyboard::Keyboard::KEY_F5, SDLK_F5 },
{ love::keyboard::Keyboard::KEY_F6, SDLK_F6 },
{ love::keyboard::Keyboard::KEY_F7, SDLK_F7 },
{ love::keyboard::Keyboard::KEY_F8, SDLK_F8 },
{ love::keyboard::Keyboard::KEY_F9, SDLK_F9 },
{ love::keyboard::Keyboard::KEY_F10, SDLK_F10 },
{ love::keyboard::Keyboard::KEY_F11, SDLK_F11 },
{ love::keyboard::Keyboard::KEY_F12, SDLK_F12 },
{ love::keyboard::Keyboard::KEY_F13, SDLK_F13 },
{ love::keyboard::Keyboard::KEY_F14, SDLK_F14 },
{ love::keyboard::Keyboard::KEY_F15, SDLK_F15 },
{ love::keyboard::Keyboard::KEY_NUMLOCK, SDLK_NUMLOCK },
{ love::keyboard::Keyboard::KEY_CAPSLOCK, SDLK_CAPSLOCK },
{ love::keyboard::Keyboard::KEY_SCROLLOCK, SDLK_SCROLLOCK },
{ love::keyboard::Keyboard::KEY_RSHIFT, SDLK_RSHIFT },
{ love::keyboard::Keyboard::KEY_LSHIFT, SDLK_LSHIFT },
{ love::keyboard::Keyboard::KEY_RCTRL, SDLK_RCTRL },
{ love::keyboard::Keyboard::KEY_LCTRL, SDLK_LCTRL },
{ love::keyboard::Keyboard::KEY_RALT, SDLK_RALT },
{ love::keyboard::Keyboard::KEY_LALT, SDLK_LALT },
{ love::keyboard::Keyboard::KEY_RMETA, SDLK_RMETA },
{ love::keyboard::Keyboard::KEY_LMETA, SDLK_LMETA },
{ love::keyboard::Keyboard::KEY_LSUPER, SDLK_LSUPER },
{ love::keyboard::Keyboard::KEY_RSUPER, SDLK_RSUPER },
{ love::keyboard::Keyboard::KEY_MODE, SDLK_MODE },
{ love::keyboard::Keyboard::KEY_COMPOSE, SDLK_COMPOSE },
{ love::keyboard::Keyboard::KEY_HELP, SDLK_HELP },
{ love::keyboard::Keyboard::KEY_PRINT, SDLK_PRINT },
{ love::keyboard::Keyboard::KEY_SYSREQ, SDLK_SYSREQ },
{ love::keyboard::Keyboard::KEY_BREAK, SDLK_BREAK },
{ love::keyboard::Keyboard::KEY_MENU, SDLK_MENU },
{ love::keyboard::Keyboard::KEY_POWER, SDLK_POWER },
{ love::keyboard::Keyboard::KEY_EURO, SDLK_EURO },
{ love::keyboard::Keyboard::KEY_UNDO, SDLK_UNDO },
};
EnumMap<love::keyboard::Keyboard::Key, SDLKey, love::keyboard::Keyboard::KEY_MAX_ENUM> Event::keys(Event::keyEntries, sizeof(Event::keyEntries));
EnumMap<love::mouse::Mouse::Button, Uint8, love::mouse::Mouse::BUTTON_MAX_ENUM>::Entry Event::buttonEntries[] =
{
{ love::mouse::Mouse::BUTTON_LEFT, SDL_BUTTON_LEFT},
{ love::mouse::Mouse::BUTTON_MIDDLE, SDL_BUTTON_MIDDLE},
{ love::mouse::Mouse::BUTTON_RIGHT, SDL_BUTTON_RIGHT},
{ love::mouse::Mouse::BUTTON_WHEELUP, SDL_BUTTON_WHEELUP},
{ love::mouse::Mouse::BUTTON_WHEELDOWN, SDL_BUTTON_WHEELDOWN},
{ love::mouse::Mouse::BUTTON_X1, SDL_BUTTON_X1},
{ love::mouse::Mouse::BUTTON_X2, SDL_BUTTON_X2},
};
EnumMap<love::mouse::Mouse::Button, Uint8, love::mouse::Mouse::BUTTON_MAX_ENUM> Event::buttons(Event::buttonEntries, sizeof(Event::buttonEntries));
} // sdl
} // event
} // love
+27 -24
View File
@@ -24,6 +24,7 @@
// LOVE
#include <event/Event.h>
#include <common/runtime.h>
#include <common/EnumMap.h>
// SDL
#include <SDL.h>
@@ -34,13 +35,15 @@ namespace event
{
namespace sdl
{
class Event : public event::Event
class Event : public love::event::Event
{
public:
// Implements Module.
const char * getName() const;
Event();
/**
* Pumps the event queue. This function gathers all the pending input information
* from devices and places it on the event queue. Normally not needed if you poll
@@ -48,38 +51,38 @@ namespace sdl
**/
void pump();
/**
* Returns an iterator function for iterating over pending events.
/**
* Checks if there are messages in the queue.
*
* @param message The next message in the queue, if the return value is true.
* @return True if there a message was found, false otherwise.
**/
int poll(lua_State * L);
bool poll(Message & message);
/**
* Waits for the next event (indefinitely). Useful for creating games where
* the screen and game state only needs updating when the user interacts with
* the window.
**/
int wait(lua_State * L);
bool wait(Message & message);
/**
* Push a message onto the event queue.
*
* @param message The message to push onto the queue.
* @return True on success, false if the queue was full.
**/
bool push(Message & message);
/**
* Push a quit event. Calling this does not mean the application
* will exit immediately, it just means an quit event will be issued.
* How to respond to the quit event is up the application.
**/
void quit();
/**
* Pushes an event into the queue.
**/
int push(lua_State * L);
/**
* The iterator function.
**/
static int poll_i(lua_State * L);
private:
static int pushEvent(lua_State * L, SDL_Event & e);
static int getEvent(lua_State * L, SDL_Event & e);
bool convert(SDL_Event & e, Message & m);
bool convert(Message & m, SDL_Event & e);
static EnumMap<love::keyboard::Keyboard::Key, SDLKey, love::keyboard::Keyboard::KEY_MAX_ENUM>::Entry keyEntries[];
static EnumMap<love::keyboard::Keyboard::Key, SDLKey, love::keyboard::Keyboard::KEY_MAX_ENUM> keys;
static EnumMap<love::mouse::Mouse::Button, Uint8, love::mouse::Mouse::BUTTON_MAX_ENUM>::Entry buttonEntries[];
static EnumMap<love::mouse::Mouse::Button, Uint8, love::mouse::Mouse::BUTTON_MAX_ENUM> buttons;
}; // System
+113 -21
View File
@@ -34,6 +34,100 @@ namespace sdl
{
static Event * instance = 0;
static bool to_message(lua_State * L, Event::Message & msg)
{
const char * str = luaL_checkstring(L, 1);
if(!Event::getConstant(str, msg.type))
return false;
switch(msg.type)
{
case Event::TYPE_KEY_PRESSED:
if(!Event::getConstant(luaL_checkstring(L, 2), msg.keyboard.k))
return false;
msg.keyboard.u = (unsigned short)luaL_optint(L, 3, 0);
return true;
case Event::TYPE_KEY_RELEASED:
if(!Event::getConstant(luaL_checkstring(L, 2), msg.keyboard.k))
return false;
return true;
case Event::TYPE_MOUSE_PRESSED:
case Event::TYPE_MOUSE_RELEASED:
if(!Event::getConstant(luaL_checkstring(L, 2), msg.mouse.b))
return false;
msg.mouse.x = luaL_checkint(L, 3);
msg.mouse.y = luaL_checkint(L, 4);
return true;
case Event::TYPE_JOYSTICK_PRESSED:
case Event::TYPE_JOYSTICK_RELEASED:
msg.joystick.index = luaL_checkint(L, 2);
msg.joystick.button = luaL_checkint(L, 3);
return true;
case Event::TYPE_QUIT:
return true;
}
return false;
}
static int push_message(lua_State * L, const Event::Message & msg)
{
const char * str = 0;
if(!Event::getConstant(msg.type, str))
return 0;
lua_pushstring(L, str);
switch(msg.type)
{
case Event::TYPE_KEY_PRESSED:
if(!Event::getConstant(msg.keyboard.k, str))
return 0;
lua_pushstring(L, str);
lua_pushinteger(L, msg.keyboard.u);
return 3;
case Event::TYPE_KEY_RELEASED:
if(!Event::getConstant(msg.keyboard.k, str))
return 0;
lua_pushstring(L, str);
return 2;
case Event::TYPE_MOUSE_PRESSED:
case Event::TYPE_MOUSE_RELEASED:
if(!Event::getConstant(msg.mouse.b, str))
return 0;
lua_pushinteger(L, msg.mouse.x);
lua_pushinteger(L, msg.mouse.y);
lua_pushstring(L, str);
return 4;
case Event::TYPE_JOYSTICK_PRESSED:
case Event::TYPE_JOYSTICK_RELEASED:
lua_pushinteger(L, msg.joystick.index);
lua_pushinteger(L, msg.joystick.button);
return 3;
case Event::TYPE_QUIT:
return 1;
}
return 0;
}
static int poll_i(lua_State * L)
{
static Event::Message m;
while(instance->poll(m))
{
int args = push_message(L, m);
if(args > 0)
return args;
}
// No pending events.
return 0;
}
int w_pump(lua_State * L)
{
instance->pump();
@@ -42,23 +136,35 @@ namespace sdl
int w_poll(lua_State * L)
{
return instance->poll(L);
lua_pushcclosure(L, &poll_i, 0);
return 1;
}
int w_wait(lua_State * L)
{
return instance->wait(L);
}
static Event::Message m;
if(instance->wait(m))
{
return push_message(L, m);
}
int w_quit(lua_State * L)
{
instance->quit();
return 0;
}
int w_push(lua_State * L)
{
return instance->push(L);
static Event::Message m;
if(!to_message(L, m))
{
luax_pushboolean(L, false);
return 1;
}
luax_pushboolean(L, instance->push(m));
return 1;
}
// List of functions to wrap.
@@ -66,23 +172,10 @@ namespace sdl
{ "pump", w_pump },
{ "poll", w_poll },
{ "wait", w_wait },
{ "quit", w_quit },
{ "push", w_push },
{ 0, 0 }
};
// List of constants.
static const Constant constants[] = {
{ "event_keypressed", Event::EVENT_KEYDOWN },
{ "event_keyreleased", Event::EVENT_KEYUP },
{ "event_mousepressed", Event::EVENT_MOUSEBUTTONDOWN },
{ "event_mousereleased", Event::EVENT_MOUSEBUTTONUP },
{ "event_joystickpressed", Event::EVENT_JOYBUTTONDOWN },
{ "event_joystickreleased", Event::EVENT_JOYBUTTONUP },
{ "event_quit", Event::EVENT_QUIT },
{ 0, 0 }
};
int luaopen_love_event(lua_State * L)
{
if(instance == 0)
@@ -103,7 +196,6 @@ namespace sdl
w.flags = MODULE_T;
w.functions = functions;
w.types = 0;
w.constants = constants;
return luax_register_module(L, w);
}
-1
View File
@@ -34,7 +34,6 @@ namespace sdl
int w_pump(lua_State * L);
int w_poll(lua_State * L);
int w_wait(lua_State * L);
int w_quit(lua_State * L);
int w_push(lua_State * L);
extern "C" LOVE_EXPORT int luaopen_love_event(lua_State * L);