From bd35674b215485dbb79ce9b481f596cfe299878b Mon Sep 17 00:00:00 2001 From: Scott Duensing Date: Wed, 11 Mar 2020 20:54:48 -0500 Subject: [PATCH] Added getMousePosition() and getControllerAxis() --- singe/Framework.singe | 7 ++ singe/preBuild.sh | 48 +++++------- singe/singe.c | 165 +++++++++++++++++++++++++++++++++--------- 3 files changed, 156 insertions(+), 64 deletions(-) diff --git a/singe/Framework.singe b/singe/Framework.singe index 4114f0281..14036c888 100644 --- a/singe/Framework.singe +++ b/singe/Framework.singe @@ -346,6 +346,13 @@ GAMEPAD_3_MAX = GAMEPAD_1_MAX + 200 GAMEPAD_4_MIN = GAMEPAD_1_MAX + 300 GAMEPAD_4_MAX = GAMEPAD_1_MAX + 300 +GAMEPAD_AXIS_LEFT_X = 0 +GAMEPAD_AXIS_LEFT_Y = 1 +GAMEPAD_AXIS_RIGHT_X = 2 +GAMEPAD_AXIS_RIGHT_Y = 3 +GAMEPAD_AXIS_LEFT_TRIGGER = 4 +GAMEPAD_AXIS_RIGHT_TRIGGER = 5 + MOUSE_1 = { BUTTON_LEFT = { name = "BUTTON_LEFT", value = 1000 }, BUTTON_RIGHT = { name = "BUTTON_RIGHT", value = 1001 }, diff --git a/singe/preBuild.sh b/singe/preBuild.sh index 359fa226d..fc4704e20 100755 --- a/singe/preBuild.sh +++ b/singe/preBuild.sh @@ -21,19 +21,19 @@ if [[ -z $1 ]]; then - G_PLATFORM=pi + G_PLATFORM=mac # G_PLATFORM=mingw # G_PLATFORM=linux - G_BITS=32 + G_BITS=64 G_THIRDPARTY=$(pwd)/thirdparty G_DEST="$(pwd)/../thirdparty-build/${G_PLATFORM}/${G_BITS}" - G_TYPE=static + G_TYPE=static else G_THIRDPARTY=$1 G_BITS=$3 G_PLATFORM=$4 G_DEST=$2/$4/$3 - G_TYPE=static + G_TYPE=static fi G_INSTALLED="${G_DEST}/installed" @@ -45,50 +45,42 @@ case "${G_PLATFORM}" in pi) G_CROSS_HOME="/opt/cross/pi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin" G_OS="linux" - G_CCOMPILER="${G_CROSS_HOME}/arm-linux-gnueabihf-gcc" - G_CPPCOMPILER="${G_CROSS_HOME}/arm-linux-gnueabihf-g++" G_CROSS="arm-linux-gnueabihf" + G_CCOMPILER="${G_CROSS_HOME}/${G_CROSS}-gcc" + G_CPPCOMPILER="${G_CROSS_HOME}/${G_CROSS}-g++" G_LUAPLAT="posix" G_ARCH="armv6" G_SYSROOT="/opt/cross/pi/buster" ;; - pi32) - G_OS="linux" - G_CCOMPILER="arm-linux-gnueabihf-gcc" - G_CPPCOMPILER="arm-linux-gnueabihf-g++" - G_CROSS="arm-linux-gnueabihf" - G_LUAPLAT="posix" - G_ARCH="armv6" - G_SYSROOT="/opt/cross/pi/buster" - ;; - - pi64) - G_OS="linux" - G_CCOMPILER="aarch64-linux-gnu-gcc" - G_CPPCOMPILER="aarch64-linux-gnu-g++" - G_CROSS="aarch64-linux-gnu" - G_LUAPLAT="posix" - G_ARCH="aarch64" - ;; - linux) G_OS="linux" + G_CROSS="x86_64-linux-gnu" G_CCOMPILER="gcc" G_CPPCOMPILER="g++" - G_CROSS="x86_64-linux-gnu" G_LUAPLAT="linux" G_ARCH="x86_64" ;; mingw) G_OS="mingw32" - G_CCOMPILER="x86_64-w64-mingw32-gcc" - G_CPPCOMPILER="x86_64-w64-mingw32-g++" G_CROSS="x86_64-w64-mingw32" + G_CCOMPILER="${G_CROSS}-gcc" + G_CPPCOMPILER="${G_CROSS}-g++" G_LUAPLAT="generic" G_ARCH="x86_64" ;; + + mac) + G_OS="mac" + G_CROSS="x86_64-apple-darwin15" + G_CCOMPILER="o64-clang" + G_CPPCOMPILER="o64-clang++" + G_LUAPLAT="generic" + G_ARCH="x86_64" + # ***HACK*** for macOS + export RANLIB=${G_CROSS}-ranlib + ;; esac diff --git a/singe/singe.c b/singe/singe.c index 9d7b557e3..8debea95e 100644 --- a/singe/singe.c +++ b/singe/singe.c @@ -44,11 +44,11 @@ #define AUDIO_MAX_VOLUME 63 #define MAX_TITLE_LENGTH 1024 -#define MAX_MICE 128 -#define SCROLLWHEEL_DISPLAY_TICKS 100 -#define NOMOUSE -1 -#define KEYBD_ARRAY_SIZE 15 -#define MOUSE_ARRAY_SIZE 6 +#define MAX_MICE 4 +#define MOUSE_AXIS_COUNT 2 +#define MAX_CONTROLLERS 4 +#define CONTROLLER_AXIS_COUNT 6 +#define AXIS_COUNT (MAX_CONTROLLERS * CONTROLLER_AXIS_COUNT + MAX_MICE * MOUSE_AXIS_COUNT) typedef struct MouseS { @@ -118,7 +118,37 @@ enum { LDP_SEARCHING, LDP_STOPPED, LDP_PLAYING, - LDP_PAUSE, + LDP_PAUSE +}; + +enum { + // Index into _controlMappings array + INPUT_UP = 0, + INPUT_LEFT, + INPUT_DOWN, + INPUT_RIGHT, + INPUT_1P_START, + INPUT_2P_START, + INPUT_ACTION_1, + INPUT_ACTION_2, + INPUT_ACTION_3, + INPUT_1P_COIN, + INPUT_2P_COIN, + INPUT_SKILL_EASY, + INPUT_SKILL_MEDIUM, + INPUT_SKILL_HARD, + INPUT_SERVICE, + INPUT_TEST_MODE, + INPUT_RESET_CPU, + INPUT_SCREENSHOT, + INPUT_QUIT, + INPUT_PAUSE, + INPUT_CONSOLE, + // Added in Singe 2.x + INPUT_ACTION_4, + INPUT_TILT, + INPUT_GRAB, + INPUT_COUNT }; @@ -169,6 +199,7 @@ static int32_t _videoHandle = -1; static int32_t _fontQuality = 1; static int32_t _mouseMode = MOUSE_SINGLE; static int32_t _mouseCount = 0; +static int32_t _axisCache[AXIS_COUNT]; static double _overlayScaleX = 1; // Difference between overlay and video static double _overlayScaleY = 1; // Difference between overlay and video static bool _pauseState = false; // by RDG2010 @@ -184,36 +215,6 @@ static SoundT *_soundList = NULL; static FontT *_fontList = NULL; static FontT *_fontCurrent = NULL; - -// Index into _controlMappings array -#define INPUT_UP 0 -#define INPUT_LEFT 1 -#define INPUT_DOWN 2 -#define INPUT_RIGHT 3 -#define INPUT_1P_START 4 -#define INPUT_2P_START 5 -#define INPUT_ACTION_1 6 -#define INPUT_ACTION_2 7 -#define INPUT_ACTION_3 8 -#define INPUT_1P_COIN 9 -#define INPUT_2P_COIN 10 -#define INPUT_SKILL_EASY 11 -#define INPUT_SKILL_MEDIUM 12 -#define INPUT_SKILL_HARD 13 -#define INPUT_SERVICE 14 -#define INPUT_TEST_MODE 15 -#define INPUT_RESET_CPU 16 -#define INPUT_SCREENSHOT 17 -#define INPUT_QUIT 18 -#define INPUT_PAUSE 19 -#define INPUT_CONSOLE 20 -// Added in Singe 2.x -#define INPUT_ACTION_4 21 -#define INPUT_TILT 22 -#define INPUT_GRAB 23 -#define INPUT_COUNT 24 - - static MappingT _controlMappings[] = { { "INPUT_UP", 0, 0, NULL }, { "INPUT_LEFT", 1, 0, NULL }, @@ -244,6 +245,7 @@ static MappingT _controlMappings[] = { int32_t apiColorBackground(lua_State *L); int32_t apiColorForeground(lua_State *L); +int32_t apiControllerGetAxis(lua_State *L); int32_t apiDaphneGetHeight(lua_State *L); int32_t apiDaphneGetWidth(lua_State *L); int32_t apiDaphneScreenshot(lua_State *L); @@ -269,6 +271,7 @@ int32_t apiFontPrint32_t(lua_State *L); int32_t apiFontQuality(lua_State *L); int32_t apiFontSelect(lua_State *L); int32_t apiFontToSprite(lua_State *L); +int32_t apiMouseGetPosition(lua_State *L); int32_t apiOverlayClear(lua_State *L); int32_t apiOverlayGetHeight(lua_State *L); int32_t apiOverlayGetWidth(lua_State *L); @@ -395,6 +398,39 @@ int32_t apiColorForeground(lua_State *L) { } +int32_t apiControllerGetAxis(lua_State *L) { + int32_t n = lua_gettop(L); + int32_t c = 0; + int32_t a = 0; + int32_t v = 0; + double d = 0; + bool result = false; + + if (n == 2) { + if (lua_isnumber(L, 1)) { + if (lua_isnumber(L, 2)) { + d = lua_tonumber(L, 1); c = (int32_t)d; + d = lua_tonumber(L, 2); a = (int32_t)d; + if ((c < 0) || (c >= MAX_CONTROLLERS)) luaDie(L, "controllerGetAxis", "Invalid controller index: %d", c); + if ((a < 0) || (a >= CONTROLLER_AXIS_COUNT)) luaDie(L, "controllerGetAxis", "Invalid controller axis: %d", a); + v = _axisCache[c * CONTROLLER_AXIS_COUNT + a]; + result = true; + } + } + } + + if (result) { + luaTrace(L, "controllerGetAxis", "%d %d %d", c, a, v); + lua_pushinteger(L, v); + } else { + luaTrace(L, "controllerGetAxis", "Failed!"); + lua_pushinteger(L, -1); + } + + return 1; +} + + int32_t apiDaphneGetHeight(lua_State *L) { int32_t y; SDL_GetWindowSize(_window, NULL, &y); @@ -1578,6 +1614,38 @@ int32_t apiMouseSetMode(lua_State *L) { } +int32_t apiMouseGetPosition(lua_State *L) { + int32_t n = lua_gettop(L); + int32_t m = 0; + int32_t x = 0; + int32_t y = 0; + double d = 0; + bool result = false; + + if (n == 1) { + if (lua_isnumber(L, 1)) { + d = lua_tonumber(L, 1); m = (int32_t)d; + if ((m < 0) || (m >= MAX_MICE)) luaDie(L, "apiMouseGetPosition", "Invalid mouse index: %d", m); + x = _axisCache[MAX_CONTROLLERS * CONTROLLER_AXIS_COUNT + m * MOUSE_AXIS_COUNT]; + y = _axisCache[MAX_CONTROLLERS * CONTROLLER_AXIS_COUNT + m * MOUSE_AXIS_COUNT + 1]; + result = true; + } + } + + if (result) { + luaTrace(L, "apiMouseGetPosition", "%d %d %d", m, x, y); + lua_pushinteger(L, x); + lua_pushinteger(L, y); + } else { + luaTrace(L, "apiMouseGetPosition", "Failed!"); + lua_pushinteger(L, -1); + lua_pushinteger(L, -1); + } + + return 2; +} + + int32_t apiMouseHowMany(lua_State *L) { luaTrace(L, "mouseHowMany", "%d", _mouseCount); lua_pushinteger(L, _mouseCount); @@ -2274,6 +2342,8 @@ void singe(SDL_Window *window, SDL_Renderer *renderer) { lua_register(_luaContext, "colorBackground", apiColorBackground); lua_register(_luaContext, "colorForeground", apiColorForeground); + lua_register(_luaContext, "controllerGetAxis", apiControllerGetAxis); + lua_register(_luaContext, "daphneGetHeight", apiDaphneGetHeight); lua_register(_luaContext, "daphneGetWidth", apiDaphneGetWidth); lua_register(_luaContext, "daphneScreenshot", apiDaphneScreenshot); @@ -2303,6 +2373,8 @@ void singe(SDL_Window *window, SDL_Renderer *renderer) { lua_register(_luaContext, "fontSelect", apiFontSelect); lua_register(_luaContext, "fontToSprite", apiFontToSprite); + lua_register(_luaContext, "mouseGetPosition", apiMouseGetPosition); + lua_register(_luaContext, "overlayClear", apiOverlayClear); lua_register(_luaContext, "overlayGetHeight", apiOverlayGetHeight); lua_register(_luaContext, "overlayGetWidth", apiOverlayGetWidth); @@ -2477,6 +2549,11 @@ void singe(SDL_Window *window, SDL_Renderer *renderer) { SDL_SetWindowGrab(_window, SDL_TRUE); SDL_ShowCursor(SDL_DISABLE); + // Clear axis cache + for (x=0; xy * _overlayScaleY); xr = (int32_t)(mouse->relx * _overlayScaleX); yr = (int32_t)(mouse->relx * _overlayScaleY); + // Remember this change + _axisCache[MAX_CONTROLLERS * 2 + mouseEvent.device * 2] = x; + _axisCache[MAX_CONTROLLERS * 2 + mouseEvent.device * 2 + 1] = y; + // Fire event callLua("onMouseMoved", "iiiii", x, y, xr, yr, mouseEvent.device); break; @@ -2649,6 +2738,10 @@ void singe(SDL_Window *window, SDL_Renderer *renderer) { xr = (int32_t)(mouse->relx * _overlayScaleX); yr = (int32_t)(mouse->relx * _overlayScaleY); //utilSay("Mouse %d: Absolute %d, %d", mouseEvent.device, x, y); + // Remember this change + _axisCache[MAX_CONTROLLERS * 2 + mouseEvent.device * 2] = x; + _axisCache[MAX_CONTROLLERS * 2 + mouseEvent.device * 2 + 1] = y; + // Fire event callLua("onMouseMoved", "iiiii", x, y, xr, yr, mouseEvent.device); break;