refactored code a bit to avoid duplication

pull/2/merge
HW 13 years ago
parent 1271d908dc
commit c9ab02084f

@ -20,9 +20,7 @@
#include <string.h>
#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);

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

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

Loading…
Cancel
Save