From bbb41078e7a8b78d7047591e86263e4f3de5e75e Mon Sep 17 00:00:00 2001 From: lethosor Date: Sat, 28 Mar 2015 18:35:07 -0400 Subject: [PATCH] Expose PenArray dimensions and tiles to Lua --- library/LuaApi.cpp | 27 +++++++++++++++++++++++++++ library/include/modules/Screen.h | 3 +++ library/modules/Screen.cpp | 9 ++++++++- 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index cccd8d88c..f7de14876 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -1080,6 +1080,31 @@ static int dfhack_penarray_clear(lua_State *L) return 0; } +static int dfhack_penarray_get_dims(lua_State *L) +{ + PenArray *parr = check_penarray_native(L, 1); + lua_pushinteger(L, parr->get_dimx()); + lua_pushinteger(L, parr->get_dimy()); + return 2; +} + +static int dfhack_penarray_get_tile(lua_State *L) +{ + PenArray *parr = check_penarray_native(L, 1); + unsigned int x = luaL_checkint(L, 2); + unsigned int y = luaL_checkint(L, 3); + if (x < parr->get_dimx() && y < parr->get_dimy()) + { + Pen pen = parr->get_tile(x, y); + Lua::Push(L, pen); + } + else + { + lua_pushnil(L); + } + return 1; +} + static int dfhack_penarray_set_tile(lua_State *L) { PenArray *parr = check_penarray_native(L, 1); @@ -1107,6 +1132,8 @@ static int dfhack_penarray_draw(lua_State *L) static const luaL_Reg dfhack_penarray_funcs[] = { { "new", dfhack_penarray_new }, { "clear", dfhack_penarray_clear }, + { "get_dims", dfhack_penarray_get_dims }, + { "get_tile", dfhack_penarray_get_tile }, { "set_tile", dfhack_penarray_set_tile }, { "draw", dfhack_penarray_draw }, { NULL, NULL } diff --git a/library/include/modules/Screen.h b/library/include/modules/Screen.h index 47bcdd0a6..3e9097c54 100644 --- a/library/include/modules/Screen.h +++ b/library/include/modules/Screen.h @@ -124,6 +124,9 @@ namespace DFHack PenArray(unsigned int bufwidth, unsigned int bufheight, void *buf); ~PenArray(); void clear(); + unsigned int get_dimx() { return dimx; } + unsigned int get_dimy() { return dimy; } + Pen get_tile(unsigned int x, unsigned int y); void set_tile(unsigned int x, unsigned int y, Screen::Pen pen); void draw(unsigned int x, unsigned int y, unsigned int width, unsigned int height, unsigned int bufx = 0, unsigned int bufy = 0); diff --git a/library/modules/Screen.cpp b/library/modules/Screen.cpp index e7fd2df04..4866c80c1 100644 --- a/library/modules/Screen.cpp +++ b/library/modules/Screen.cpp @@ -451,7 +451,7 @@ PenArray::PenArray(unsigned int bufwidth, unsigned int bufheight) PenArray::PenArray(unsigned int bufwidth, unsigned int bufheight, void *buf) :dimx(bufwidth), dimy(bufheight), static_alloc(true) { - buffer = (Pen*)((uint8_t*)buf + sizeof(PenArray)); + buffer = (Pen*)((PenArray*)buf + 1); clear(); } @@ -472,6 +472,13 @@ void PenArray::clear() } } +Pen PenArray::get_tile(unsigned int x, unsigned int y) +{ + if (x < dimx && y < dimy) + return buffer[(y * dimx) + x]; + return Pen(0, 0, 0, 0, false); +} + void PenArray::set_tile(unsigned int x, unsigned int y, Screen::Pen pen) { if (x < dimx && y < dimy)