diff --git a/blitbuffer.c b/blitbuffer.c index a3a7eab6e..a3cfd2b39 100644 --- a/blitbuffer.c +++ b/blitbuffer.c @@ -20,9 +20,7 @@ #include #include "blitbuffer.h" -static int newBlitBuffer(lua_State *L) { - int w = luaL_checkint(L, 1); - int h = luaL_checkint(L, 2); +int newBlitBufferNative(lua_State *L, int w, int h, BlitBuffer **newBuffer) { BlitBuffer *bb = (BlitBuffer*) lua_newuserdata(L, sizeof(BlitBuffer)); luaL_getmetatable(L, "blitbuffer"); lua_setmetatable(L, -2); @@ -36,9 +34,18 @@ static int newBlitBuffer(lua_State *L) { } memset(bb->data, 0, bb->pitch * h); bb->allocated = 1; + if(newBuffer != NULL) { + *newBuffer = bb; + } return 1; } +static int newBlitBuffer(lua_State *L) { + int w = luaL_checkint(L, 1); + int h = luaL_checkint(L, 2); + return newBlitBufferNative(L, w, h, NULL); +} + static int getWidth(lua_State *L) { BlitBuffer *bb = (BlitBuffer*) luaL_checkudata(L, 1, "blitbuffer"); lua_pushinteger(L, bb->w); diff --git a/blitbuffer.h b/blitbuffer.h index 6b1774938..d5f8a8b34 100644 --- a/blitbuffer.h +++ b/blitbuffer.h @@ -31,6 +31,7 @@ typedef struct BlitBuffer { uint8_t allocated; } BlitBuffer; +int newBlitBufferNative(lua_State *L, int w, int h, BlitBuffer **newBuffer); int luaopen_blitbuffer(lua_State *L); #endif diff --git a/ft.c b/ft.c index 312de3fe9..0131d684d 100644 --- a/ft.c +++ b/ft.c @@ -112,18 +112,11 @@ static int renderGlyph(lua_State *L) { lua_newtable(L); - BlitBuffer *bb = (BlitBuffer*) lua_newuserdata(L, sizeof(BlitBuffer)); - luaL_getmetatable(L, "blitbuffer"); - lua_setmetatable(L, -2); - - bb->w = w; - bb->pitch = (w + 1) / 2; - bb->h = h; - bb->data = malloc(bb->pitch * h); - if(bb->data == NULL) { - return luaL_error(L, "cannot allocate memory for blitbuffer"); + BlitBuffer *bb; + int result = newBlitBufferNative(L, w, h, &bb); + if(result != 1) { + return result; } - bb->allocated = 1; lua_setfield(L, -2, "bb");