open-card-table/oct_termbox_sprite.c

143 lines
4.3 KiB
C

#include <string.h>
#include <lua5.3/lualib.h>
#include <lua5.3/lauxlib.h>
#include "oct_termbox_sprite.h"
#include "oct_log.h"
struct otsl oct_tb_sprite_list;
struct luaL_Reg oct_tb_sprite_metamethods[] =
{
{"__index", oct_tb_sprite__index},
{"__newindex", oct_tb_sprite__newindex},
{"__tostring", oct_tb_sprite__tostring},
{NULL, NULL}
};
int oct_tb_sprite_list_initialize() {
oct_tb_sprite_list.sprite_list = calloc(OCT_INITIAL_NUM_SPRITES, sizeof(struct oct_tb_sprite*));
if (!oct_tb_sprite_list.sprite_list) {
perror("Could not allocate memory for sprite list");
return 0;
}
oct_tb_sprite_list.new_index = 0;
oct_tb_sprite_list.size = OCT_INITIAL_NUM_SPRITES;
oct_tb_sprite_list.initialized = 1;
return 1;
}
int oct_tb_sprite_list_deinitialize() {
free(oct_tb_sprite_list.sprite_list);
oct_tb_sprite_list.sprite_list = NULL;
oct_tb_sprite_list.new_index = 0;
oct_tb_sprite_list.size = 0;
oct_tb_sprite_list.initialized = 0;
return 1;
}
int oct_tb_sprite__index(lua_State *L) {
char index[OCT_SPRITE_MEMBER_NAME_MAXLENGTH];
struct oct_tb_sprite* self = (struct oct_tb_sprite*)lua_touserdata(L, -2);
strncpy(index, luaL_checkstring(L, -1), OCT_SPRITE_MEMBER_NAME_MAXLENGTH);
if (!strncmp(index, "x", OCT_SPRITE_MEMBER_NAME_MAXLENGTH)) {
lua_pushinteger(L, self->x);
}
else if (!strncmp(index, "y", OCT_SPRITE_MEMBER_NAME_MAXLENGTH)) {
lua_pushinteger(L, self->y);
}
else if (!strncmp(index, "fg", OCT_SPRITE_MEMBER_NAME_MAXLENGTH)) {
lua_pushinteger(L, self->fg);
}
else if (!strncmp(index, "bg", OCT_SPRITE_MEMBER_NAME_MAXLENGTH)) {
lua_pushinteger(L, self->bg);
}
else if (!strncmp(index, "shape", OCT_SPRITE_MEMBER_NAME_MAXLENGTH)) {
lua_pushstring(L, self->shape);
}
else {
lua_pushnil(L);
}
return 1;
}
int oct_tb_sprite__newindex(lua_State *L) {
char index[OCT_SPRITE_MEMBER_NAME_MAXLENGTH];
struct oct_tb_sprite* self = (struct oct_tb_sprite*)lua_touserdata(L, -3);
strncpy(index, luaL_checkstring(L, -2), OCT_SPRITE_MEMBER_NAME_MAXLENGTH);
if (!strncmp(index, "x", OCT_SPRITE_MEMBER_NAME_MAXLENGTH)) {
self->x = luaL_checkinteger(L, -1);
}
else if (!strncmp(index, "y", OCT_SPRITE_MEMBER_NAME_MAXLENGTH)) {
self->y = luaL_checkinteger(L, -1);
}
else if (!strncmp(index, "fg", OCT_SPRITE_MEMBER_NAME_MAXLENGTH)) {
self->fg = (uintattr_t)luaL_checkinteger(L, -1);
}
else if (!strncmp(index, "bg", OCT_SPRITE_MEMBER_NAME_MAXLENGTH)) {
self->bg = (uintattr_t)luaL_checkinteger(L, -1);
}
else if (!strncmp(index, "shape", OCT_SPRITE_MEMBER_NAME_MAXLENGTH)) {
strncpy(self->shape, luaL_checkstring(L, -1), OCT_MAX_SPRITE_SIZE);
}
return 1;
}
int oct_tb_sprite__tostring(lua_State *L) {
struct oct_tb_sprite* self = (struct oct_tb_sprite*)lua_touserdata(L, -1);
char buffer[OCT_SPRITE_PRINT_BUFFER_SIZE];
snprintf(buffer,
OCT_SPRITE_PRINT_BUFFER_SIZE,
"{\n"
"\tx: %d\n"
"\ty: %d\n"
"\tfg: %d\n"
"\tbg: %d\n"
"\tshape: %s\n"
"}",
self->x, self->y, self->bg, self->fg, self->shape);
lua_pushstring(L, buffer);
return 1;
}
int oct_tb_sprite_new(lua_State *L) {
if (oct_tb_sprite_list.size == oct_tb_sprite_list.new_index) {
if (!reallocarray(oct_tb_sprite_list.sprite_list,
sizeof(struct oct_tb_sprite*),
oct_tb_sprite_list.size+OCT_SPRITE_LIST_REALLOC_LENGTH)) {
perror("Could not reallocate memory for sprite list");
}
}
struct oct_tb_sprite* new_sprite = (struct oct_tb_sprite*)lua_newuserdata(L, sizeof(struct oct_tb_sprite));
new_sprite->x = OCT_OUT_OF_SCREEN;
new_sprite->y = OCT_OUT_OF_SCREEN;
new_sprite->fg = 0;
new_sprite->bg = 1;
strncpy(new_sprite->shape, "", OCT_MAX_SPRITE_SIZE);
oct_tb_sprite_list.sprite_list[oct_tb_sprite_list.new_index] = new_sprite;
lua_pushstring(L, "oct_tb_sprite_metatable");
lua_gettable(L, LUA_REGISTRYINDEX);
lua_setmetatable(L, -2);
oct_tb_sprite_list.new_index++;
return 1;
}
int oct_tb_sprite_new_uninit(lua_State *L) {
OCT_LOG_WARNING("Attempted to create new sprite when termbox was not requested!");
return -1;
}
void oct_tb_initialize_lua(lua_State *L) {
lua_pushcfunction(L, oct_tb_sprite_new);
lua_setglobal(L, "oct_tb_sprite_new");
if (luaL_newmetatable(L, "oct_tb_sprite_metatable")) {
luaL_setfuncs(L, oct_tb_sprite_metamethods, 0);
}
}
void oct_tb_change_oct_tb_sprite_new(lua_State *L) {
lua_pushcfunction(L, oct_tb_sprite_new_uninit);
lua_setglobal(L, "oct_tb_sprite_new");
}