- [NEW] res() without arguments returns width and height of window.

- [NEW] camera() without arguments returns 'x' and 'y' of camera.
- [NEW] palcolor() to set or get a color from the palette (replaces setcolor() & getcolor() ).
- [NEW] paltrans() to set or get which color index is transparent (replaces settrans() & gettrans() ).
- [RENAME] sspr() to blit(), spr_r() to blit_r()
- [NEW] mouse() returns both x and y mouse coordinates.
- [DEPRECATED] setcolor(), getcolor(), settrans(), gettrans(), spr(), sspr(), spr_r(), mousex(), mousey(), abs(), ceil(), flr(), sgn(), sin(), cos(), atan2(), sqrt(), max(), mid(), min(), tostr(), ascii(), strlen(), fopen(), fopenres(), fclose(), feof(), fwritei(), fwrited(), fwrites(), fwritew(), fwriteb(), fwriteln(), freadi(), freadd(), freads(), freadw(), freadb()
- [FIX] Now the background on resizable windows  is filled with black instead of garbage.
- [FIX] Compiling on Linux uses POSIX functions.
This commit is contained in:
2024-02-15 13:54:17 +01:00
parent 4b6a9d8188
commit cddd79f05e
7 changed files with 130 additions and 327 deletions

247
lua.cpp
View File

@@ -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");