diff --git a/Makefile b/Makefile index 52e7073..eb29ab3 100644 --- a/Makefile +++ b/Makefile @@ -20,8 +20,8 @@ macos_bundle: clang++ $(source) -D MACOS_BUNDLE -Wall -Os -std=c++11 -framework SDL2 -framework SDL2_mixer -F /Library/Frameworks -ffunction-sections -fdata-sections -o mini_bundle -rpath @executable_path/../Frameworks/ -target x86_64-apple-macos10.12 linux: - g++ $(source) -Wall -Os -ffunction-sections -fdata-sections -Wl,--gc-sections -lSDL2 -lSDL2_mixer -o "$(executable)" + g++ $(source) -D LUA_USE_LINUX -Wall -Os -ffunction-sections -fdata-sections -Wl,--gc-sections -lSDL2 -lSDL2_mixer -o "$(executable)" strip -s -R .comment -R .gnu.version --strip-unneeded "$(executable)" linux_debug: - g++ $(source) -D DEBUG -g -Wall -Os -ffunction-sections -fdata-sections -Wl,--gc-sections -lSDL2 -lSDL2_mixer -o "$(executable)_debug" + g++ $(source) -D LUA_USE_LINUX -D DEBUG -g -Wall -Os -ffunction-sections -fdata-sections -Wl,--gc-sections -lSDL2 -lSDL2_mixer -o "$(executable)_debug" diff --git a/data/main.lua b/data/main.lua index d861a5c..b900180 100644 --- a/data/main.lua +++ b/data/main.lua @@ -8,6 +8,8 @@ function _init() keyLeft = tonumber(getconf("keyleft")) or KEY_LEFT _update=normal_update turbo(false) + local perico = "péricòñ" + print(utf8.len(perico)) end function _update() diff --git a/data/other.lua b/data/other.lua index eedb8eb..7f14823 100644 --- a/data/other.lua +++ b/data/other.lua @@ -1,5 +1,6 @@ return { peiv = function() + setcolor(1, 1, 1, 1) return "HOLA OTHER UNIT" end } diff --git a/lua.cpp b/lua.cpp index 857a95a..ca7df56 100644 --- a/lua.cpp +++ b/lua.cpp @@ -109,31 +109,49 @@ extern "C" { return 0; } - static int cpp_setcolor(lua_State *L) { - uint8_t index = luaL_checkinteger(L, 1); - uint8_t r = luaL_checkinteger(L, 2); - uint8_t g = luaL_checkinteger(L, 3); - uint8_t b = luaL_checkinteger(L, 4); - uint32_t color = (r<<16) + (g<<8) + b; - setcolor(index, color); + static int cpp_palcolor(lua_State *L) { + if (lua_gettop(L) >= 1) { + uint8_t index = luaL_checkinteger(L, 1); + if (lua_gettop(L) > 1) { + uint8_t r = luaL_checkinteger(L, 2); + uint8_t g = luaL_optinteger(L, 3, 0); + uint8_t b = luaL_optinteger(L, 4, 0); + uint32_t color = (r<<16) + (g<<8) + b; + setcolor(index, color); + return 0; + } else { + uint32_t color = getcolor(index); + lua_pushinteger(L, (color>>16)&0xff); + lua_pushinteger(L, (color>>8)&0xff); + lua_pushinteger(L, color&0xff); + return 3; + } + } return 0; } + + static int cpp_paltrans(lua_State *L) { + if (lua_gettop(L) == 0) { + lua_pushinteger(L, gettrans()); + return 1; + } else { + uint8_t index = luaL_checkinteger(L, 1); + settrans(index); + return 0; + } + } + + static int cpp_setcolor(lua_State *L) { + return luaL_error(L, "Function 'setcolor' is DEPRECATED. Use 'palcolor' instead."); + } static int cpp_getcolor(lua_State *L) { - uint8_t index = luaL_checkinteger(L, 1); - uint32_t color = getcolor(index); - lua_pushinteger(L, (color>>16)&0xff); - lua_pushinteger(L, (color>>8)&0xff); - lua_pushinteger(L, color&0xff); - return 3; + return luaL_error(L, "Function 'getcolor' is DEPRECATED. Use 'palcolor' instead."); } static int cpp_settrans(lua_State *L) { - uint8_t index = luaL_checkinteger(L, 1); - settrans(index); - return 0; + return luaL_error(L, "Function 'settrans' is DEPRECATED. Use 'paltrans' instead."); } static int cpp_gettrans(lua_State *L) { - lua_pushinteger(L, gettrans()); - return 1; + return luaL_error(L, "Function 'gettrans' is DEPRECATED. Use 'paltrans' instead."); } static int cpp_subpal(lua_State *L) { const int numargs = lua_gettop(L); @@ -282,10 +300,16 @@ extern "C" { } static int cpp_camera(lua_State *L) { - int x = luaL_checknumber(L, 1); - int y = luaL_checknumber(L, 2); - camera(x, y); - return 0; + if (lua_gettop(L) == 0) { + lua_pushinteger(L, camx()); + lua_pushinteger(L, camy()); + return 2; + } else { + int x = luaL_checknumber(L, 1); + int y = luaL_checknumber(L, 2); + camera(x, y); + return 0; + } } static int cpp_view(lua_State *L) { @@ -390,18 +414,18 @@ extern "C" { } static int cpp_spr(lua_State *L) { - uint8_t n = luaL_checkinteger(L, 1); - int x = luaL_checknumber(L, 2); - int y = luaL_checknumber(L, 3); - float w = luaL_optnumber(L, 4, 1.0f); - float h = luaL_optnumber(L, 5, 1.0f); - bool flip_x = lua_toboolean(L, 6); - bool flip_y = lua_toboolean(L, 7); - spr(n, x, y, w, h, flip_x, flip_y); - return 0; + return luaL_error(L, "Function 'spr' is DEPRECATED. No direct replacement. Use 'blit' instead."); } static int cpp_sspr(lua_State *L) { + return luaL_error(L, "Function 'sspr' is DEPRECATED. Use 'blit' instead."); + } + + static int cpp_spr_r(lua_State *L) { + return luaL_error(L, "Function 'spr_r' is DEPRECATED. Use 'blit_r' instead."); + } + + static int cpp_blit(lua_State *L) { int sx = luaL_checknumber(L, 1); int sy = luaL_checknumber(L, 2); int sw = luaL_checknumber(L, 3); @@ -413,11 +437,11 @@ extern "C" { bool flip_x = lua_toboolean(L, 9); bool flip_y = lua_toboolean(L, 10); bool invert = lua_toboolean(L, 11); - sspr(sx, sy, sw, sh, dx, dy, dw, dh, flip_x, flip_y, invert); + blit(sx, sy, sw, sh, dx, dy, dw, dh, flip_x, flip_y, invert); return 0; } - static int cpp_spr_r(lua_State *L) { + static int cpp_blit_r(lua_State *L) { int sx = luaL_checknumber(L, 1); int sy = luaL_checknumber(L, 2); int sw = luaL_checknumber(L, 3); @@ -425,7 +449,7 @@ extern "C" { int dx = luaL_checknumber(L, 5); int dy = luaL_checknumber(L, 6); float a = luaL_checknumber(L, 7); - spr_r(sx, sy, sw, sh, dx, dy, a); + blit_r(sx, sy, sw, sh, dx, dy, a); return 0; } @@ -530,14 +554,18 @@ extern "C" { return 1; } - static int cpp_mousex(lua_State *L) { + static int cpp_mouse(lua_State *L) { lua_pushinteger(L, mousex()); - return 1; + lua_pushinteger(L, mousey()); + return 2; + } + + static int cpp_mousex(lua_State *L) { + return luaL_error(L, "Function 'mousex' is DEPRECATED. Use 'mouse' instead."); } static int cpp_mousey(lua_State *L) { - lua_pushinteger(L, mousey()); - return 1; + return luaL_error(L, "Function 'mousey' is DEPRECATED. Use 'mouse' instead."); } static int cpp_mwheel(lua_State *L) { @@ -569,64 +597,37 @@ extern "C" { } static int cpp_abs(lua_State *L) { - float x = luaL_checknumber(L, 1); - lua_pushnumber(L, abs(x)); - return 1; + return luaL_error(L, "Function 'abs' is DEPRECATED. Use 'math.abs' instead."); } static int cpp_ceil(lua_State *L) { - float x = luaL_checknumber(L, 1); - lua_pushnumber(L, ceil(x)); - return 1; + return luaL_error(L, "Function 'ceil' is DEPRECATED. Use 'math.ceil' instead."); } static int cpp_flr(lua_State *L) { - float x = luaL_checknumber(L, 1); - lua_pushnumber(L, flr(x)); - return 1; + return luaL_error(L, "Function 'flr' is DEPRECATED. Use 'math.floor' instead."); } static int cpp_sgn(lua_State *L) { - float x = luaL_checknumber(L, 1); - lua_pushnumber(L, sgn(x)); - return 1; + return luaL_error(L, "Function 'sgn' is DEPRECATED. No direct replacement though..."); } static int cpp_sin(lua_State *L) { - float x = luaL_checknumber(L, 1); - lua_pushnumber(L, sin(x)); - return 1; + return luaL_error(L, "Function 'sin' is DEPRECATED. Use 'math.sin' instead."); } static int cpp_cos(lua_State *L) { - float x = luaL_checknumber(L, 1); - lua_pushnumber(L, cos(x)); - return 1; + return luaL_error(L, "Function 'cos' is DEPRECATED. Use 'math.cos' instead."); } static int cpp_atan2(lua_State *L) { - float dx = luaL_checknumber(L, 1); - float dy = luaL_checknumber(L, 2); - lua_pushnumber(L, atan2(dx, dy)); - return 1; + return luaL_error(L, "Function 'atan2' is DEPRECATED. Use 'math.atan2' instead."); } static int cpp_sqrt(lua_State *L) { - float x = luaL_checknumber(L, 1); - lua_pushnumber(L, sqrt(x)); - return 1; + return luaL_error(L, "Function 'sqrt' is DEPRECATED. Use 'math.sqrt' instead."); } static int cpp_max(lua_State *L) { - float x = luaL_checknumber(L, 1); - float y = luaL_checknumber(L, 2); - lua_pushnumber(L, max(x, y)); - return 1; + return luaL_error(L, "Function 'max' is DEPRECATED. Use 'math.max' instead."); } static int cpp_mid(lua_State *L) { - float x = luaL_checknumber(L, 1); - float y = luaL_checknumber(L, 2); - float z = luaL_checknumber(L, 3); - lua_pushnumber(L, mid(x, y, z)); - return 1; + return luaL_error(L, "Function 'mid' is DEPRECATED. No direct replacement though..."); } static int cpp_min(lua_State *L) { - float x = luaL_checknumber(L, 1); - float y = luaL_checknumber(L, 2); - lua_pushnumber(L, min(x, y)); - return 1; + return luaL_error(L, "Function 'min' is DEPRECATED. Use 'math.min' instead."); } static int cpp_rnd(lua_State *L) { int x = luaL_checkinteger(L, 1); @@ -639,101 +640,71 @@ extern "C" { return 0; } static int cpp_tostr(lua_State *L) { - int val = luaL_checknumber(L, 1); - lua_pushstring(L, tostr(val)); - return 1; + return luaL_error(L, "Function 'tostr' is DEPRECATED. Use Lua's own methods instead."); } static int cpp_ascii(lua_State *L) { - const char* str = luaL_checkstring(L, 1); - int index = luaL_checkinteger(L, 2); - lua_pushinteger(L, ascii(str, index)); - return 1; + return luaL_error(L, "Function 'ascii' is DEPRECATED. Use 'string.byte' instead (1 based indexing though...)."); } static int cpp_strlen(lua_State *L) { - const char* str = luaL_checkstring(L, 1); - lua_pushinteger(L, utfstrlen(str)); - return 1; + return luaL_error(L, "Function 'strlen' is DEPRECATED. Use 'utf8.len' instead."); } static int cpp_fopen(lua_State *L) { - const char* str = luaL_checkstring(L, 1); - uint8_t mode = luaL_optinteger(L, 2, 0); - fopen(str, mode); - return 0; + return luaL_error(L, "Function 'fopen' is DEPRECATED. Use Lua's own io methods instead."); } static int cpp_fopenres(lua_State *L) { - const char* str = luaL_checkstring(L, 1); - fopenres(str); - return 0; + return luaL_error(L, "Function 'fopenres' is DEPRECATED. Use Lua's own io methods instead."); } static int cpp_fclose(lua_State *L) { - fclose(); - return 0; + return luaL_error(L, "Function 'fclose' is DEPRECATED. Use Lua's own io methods instead."); } static int cpp_feof(lua_State *L) { - lua_pushboolean(L,feof()); - return 1; + return luaL_error(L, "Function 'feof' is DEPRECATED. Use Lua's own io methods instead."); } static int cpp_fwritei(lua_State *L) { - int value = luaL_checkinteger(L, 1); - fwritei(value); - return 0; + return luaL_error(L, "Function 'fwritei' is DEPRECATED. Use Lua's own io methods instead."); } static int cpp_fwrited(lua_State *L) { - float value = luaL_checknumber(L, 1); - fwrited(value); - return 0; + return luaL_error(L, "Function 'fwrited' is DEPRECATED. Use Lua's own io methods instead."); } static int cpp_fwrites(lua_State *L) { - const char* str = luaL_checkstring(L, 1); - fwrites(str); - return 0; + return luaL_error(L, "Function 'fwrites' is DEPRECATED. Use Lua's own io methods instead."); } static int cpp_fwritew(lua_State *L) { - const char* str = luaL_checkstring(L, 1); - fwritew(str); - return 0; + return luaL_error(L, "Function 'fwritew' is DEPRECATED. Use Lua's own io methods instead."); } static int cpp_fwriteb(lua_State *L) { - bool value = lua_toboolean(L, 1); - fwriteb(value); - return 0; + return luaL_error(L, "Function 'fwriteb' is DEPRECATED. Use Lua's own io methods instead."); } static int cpp_fwriteln(lua_State *L) { - fwriteln(); - return 0; + return luaL_error(L, "Function 'fwriteln' is DEPRECATED. Use Lua's own io methods instead."); } static int cpp_freadi(lua_State *L) { - lua_pushinteger(L,freadi()); - return 1; + return luaL_error(L, "Function 'freadi' is DEPRECATED. Use Lua's own io methods instead."); } static int cpp_freadd(lua_State *L) { - lua_pushnumber(L,freadd()); - return 1; + return luaL_error(L, "Function 'freadd' is DEPRECATED. Use Lua's own io methods instead."); } static int cpp_freads(lua_State *L) { - lua_pushstring(L,freads()); - return 1; + return luaL_error(L, "Function 'freads' is DEPRECATED. Use Lua's own io methods instead."); } static int cpp_freadw(lua_State *L) { - lua_pushstring(L,freadw()); - return 1; + return luaL_error(L, "Function 'freadw' is DEPRECATED. Use Lua's own io methods instead."); } static int cpp_freadb(lua_State *L) { - lua_pushboolean(L,freadb()); - return 1; + return luaL_error(L, "Function 'freadb' is DEPRECATED. Use Lua's own io methods instead."); } static int cpp_playmusic(lua_State *L) { @@ -826,10 +797,16 @@ extern "C" { } static int cpp_res(lua_State *L) { - const int w = luaL_optinteger(L, 1, 160); - const int h = luaL_optinteger(L, 2, 120); - setres(w, h); - return 0; + if (lua_gettop(L) >= 2) { + const int w = luaL_optinteger(L, 1, 160); + const int h = luaL_optinteger(L, 2, 120); + setres(w, h); + return 0; + } else { + lua_pushinteger(L, scrw()); + lua_pushinteger(L, scrh()); + return 2; + } } static int cpp_getconf(lua_State *L) { @@ -906,6 +883,8 @@ void push_lua_funcs() { lua_pushcfunction(L,cpp_loadpal); lua_setglobal(L, "loadpal"); lua_pushcfunction(L,cpp_setpal); lua_setglobal(L, "setpal"); + lua_pushcfunction(L,cpp_palcolor); lua_setglobal(L, "palcolor"); + lua_pushcfunction(L,cpp_paltrans); lua_setglobal(L, "paltrans"); lua_pushcfunction(L,cpp_setcolor); lua_setglobal(L, "setcolor"); lua_pushcfunction(L,cpp_getcolor); lua_setglobal(L, "getcolor"); lua_pushcfunction(L,cpp_settrans); lua_setglobal(L, "settrans"); @@ -935,6 +914,8 @@ void push_lua_funcs() { lua_pushcfunction(L,cpp_spr); lua_setglobal(L, "spr"); lua_pushcfunction(L,cpp_sspr); lua_setglobal(L, "sspr"); lua_pushcfunction(L,cpp_spr_r); lua_setglobal(L, "spr_r"); + lua_pushcfunction(L,cpp_blit); lua_setglobal(L, "blit"); + lua_pushcfunction(L,cpp_blit_r); lua_setglobal(L, "blit_r"); lua_pushcfunction(L,cpp_tline); lua_setglobal(L, "tline"); lua_pushcfunction(L,cpp_thline); lua_setglobal(L, "thline"); lua_pushcfunction(L,cpp_tvline); lua_setglobal(L, "tvline"); @@ -946,14 +927,18 @@ void push_lua_funcs() { lua_pushcfunction(L,cpp_anykey); lua_setglobal(L, "anykey"); lua_pushcfunction(L,cpp_pad); lua_setglobal(L, "pad"); lua_pushcfunction(L,cpp_padp); lua_setglobal(L, "padp"); + lua_pushcfunction(L,cpp_mouse); lua_setglobal(L, "mouse"); lua_pushcfunction(L,cpp_mousex); lua_setglobal(L, "mousex"); lua_pushcfunction(L,cpp_mousey); lua_setglobal(L, "mousey"); lua_pushcfunction(L,cpp_mwheel); lua_setglobal(L, "mwheel"); lua_pushcfunction(L,cpp_mbtn); lua_setglobal(L, "mbtn"); - lua_pushcfunction(L,cpp_mbtnp); lua_setglobal(L, "mbtnp"); + lua_pushcfunction(L,cpp_mbtnp); lua_setglobal(L, "mbtnp"); lua_pushcfunction(L,cpp_time); lua_setglobal(L, "time"); lua_pushcfunction(L,cpp_beat); lua_setglobal(L, "beat"); + + + /* [BEGIN DEPRECADES] */ lua_pushcfunction(L,cpp_abs); lua_setglobal(L, "abs"); lua_pushcfunction(L,cpp_ceil); lua_setglobal(L, "ceil"); lua_pushcfunction(L,cpp_flr); lua_setglobal(L, "flr"); @@ -965,12 +950,15 @@ void push_lua_funcs() { lua_pushcfunction(L,cpp_max); lua_setglobal(L, "max"); lua_pushcfunction(L,cpp_mid); lua_setglobal(L, "mid"); lua_pushcfunction(L,cpp_min); lua_setglobal(L, "min"); + /* [END DEPRECADES] */ + lua_pushcfunction(L,cpp_rnd); lua_setglobal(L, "rnd"); lua_pushcfunction(L,cpp_srand); lua_setglobal(L, "srand"); lua_pushcfunction(L,cpp_tostr); lua_setglobal(L, "tostr"); lua_pushcfunction(L,cpp_ascii); lua_setglobal(L, "ascii"); lua_pushcfunction(L,cpp_strlen); lua_setglobal(L, "strlen"); + /* [BEGIN DEPRECADES] */ lua_pushcfunction(L,cpp_fopen); lua_setglobal(L, "fopen"); lua_pushcfunction(L,cpp_fopenres); lua_setglobal(L, "fopenres"); lua_pushcfunction(L,cpp_fclose); lua_setglobal(L, "fclose"); @@ -986,6 +974,7 @@ void push_lua_funcs() { lua_pushcfunction(L,cpp_freads); lua_setglobal(L, "freads"); lua_pushcfunction(L,cpp_freadw); lua_setglobal(L, "freadw"); lua_pushcfunction(L,cpp_freadb); lua_setglobal(L, "freadb"); + /* [END DEPRECADES] */ lua_pushcfunction(L,cpp_playmusic); lua_setglobal(L, "playmusic"); lua_pushcfunction(L,cpp_pausemusic); lua_setglobal(L, "pausemusic"); diff --git a/mini.cpp b/mini.cpp index c3ebd9e..cef1339 100644 --- a/mini.cpp +++ b/mini.cpp @@ -412,6 +412,8 @@ int main(int argc,char*argv[]){ mouse_just_pressed = 0; pad_just_pressed = SDL_CONTROLLER_BUTTON_INVALID; } + SDL_SetRenderDrawColor(mini_ren, 0, 0, 0, 255); + SDL_RenderClear(mini_ren); SDL_LockTexture(mini_bak, NULL, (void**)&pixels, &pitch); for (uint32_t i=0;isize;++i) pixels[i] = palette[screen_surface->p[i]]; SDL_UnlockTexture(mini_bak); @@ -877,7 +879,7 @@ void spr(uint8_t n, int x, int y, float w, float h, bool flip_x, bool flip_y) { } } -void sspr(int sx, int sy, int sw, int sh, int dx, int dy, int dw, int dh, bool flip_x, bool flip_y, bool invert) { +void blit(int sx, int sy, int sw, int sh, int dx, int dy, int dw, int dh, bool flip_x, bool flip_y, bool invert) { if (dw==0) dw=sw; if (dh==0) dh=sh; float sdx = float(sw)/float(dw); @@ -898,7 +900,7 @@ void sspr(int sx, int sy, int sw, int sh, int dx, int dy, int dw, int dh, bool f } } -void spr_r(int sx, int sy, int sw, int sh, int x, int y, float a) +void blit_r(int sx, int sy, int sw, int sh, int x, int y, float a) { const int x0 = sw>>1; const int y0 = sh>>1; @@ -1103,158 +1105,10 @@ bool beat(int16_t i) { } } -/*float abs(float x) { - return SDL_fabsf(x); -}*/ - -float flr(float x) { - return SDL_floorf(x); -} - -float sgn(float x) { - return x >= 0 ? 1 : -1; -} - -#ifdef __WINDOWS__ - -float ceil(float x) { - return SDL_ceilf(x); -} - -float sin(float x) { - return SDL_sinf(x); -} - -float cos(float x) { - return SDL_cosf(x); -} - -float atan2(float dx, float dy) { - return SDL_atan2f(dx, dy); -} - -float sqrt(float x) { - return SDL_sqrtf(x); -} - -#endif - -float max(float x, float y) { - return SDL_max(x, y); -} - -float mid(float x, float y, float z) { - return max(x, min(y, z)); -} - -float min(float x, float y) { - return SDL_min(x, y); -} - -int utfstrlen(const char *str) { - const int size_in_bytes = SDL_strlen(str); - int size_in_utfchars = 0; - for (int i=0;i