Expose PenArray dimensions and tiles to Lua

develop
lethosor 2015-03-28 18:35:07 -04:00
parent e2b6ae9beb
commit bbb41078e7
3 changed files with 38 additions and 1 deletions

@ -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 }

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

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