open-card-table/main.c

87 lines
1.9 KiB
C
Raw Normal View History

2023-01-06 19:12:24 -05:00
#include <stdio.h>
#include <lua5.3/lualib.h>
#include <lua5.3/lauxlib.h>
2023-01-06 19:12:24 -05:00
#include "termbox_render.h"
#include "oct_termbox_sprite.h"
#define TB_IMPL
#include "termbox.h"
2023-01-06 19:12:24 -05:00
// Errors
#define OCT_NO_LUA_FILE 1
#define OCT_LUA_FILE_NOT_FOUND 2
#define OCT_LUA_FILE_MISSING_OCT_INIT 3
2023-01-06 19:12:24 -05:00
lua_State *L;
int process_args(int argc, char* argv[]);
int initialize_everything(char* lua_file);
2023-01-06 19:12:24 -05:00
int deinitialize_everything();
2023-01-06 19:12:24 -05:00
int main(int argc, char* argv[]) {
int process_args_result = process_args(argc, argv);
switch (process_args_result) {
case OCT_NO_LUA_FILE:
fprintf(stderr, "No lua file given\n");
return EXIT_FAILURE;
break;
case OCT_LUA_FILE_NOT_FOUND:
fprintf(stderr, "Could not open file: %s\n", argv[argc-1]);
return EXIT_FAILURE;
break;
2023-01-06 19:12:24 -05:00
}
struct tb_event ev;
int finish = 0;
while (!finish) {
tb_clear();
tb_peek_event(&ev, 10);
if (ev.key == TB_KEY_ESC) {
finish = 1;
}
lua_getglobal(L, "oct_loop");
lua_pushinteger(L, ev.key);
lua_pushinteger(L, ev.ch);
lua_call(L, 2, 0);
for (uint32_t i=0; i < oct_tb_sprite_list.new_index; i++) {
oct_render_termbox_sprite(oct_tb_sprite_list.sprite_list[i]);
}
tb_present();
}
deinitialize_everything();
return EXIT_SUCCESS;
}
int process_args(int argc, char* argv[]) {
if (argc == 1) { // Didn't specify a file
return OCT_NO_LUA_FILE;
}
// TODO define other cmd line args here
return initialize_everything(argv[argc-1]); // lua file should always be last argument
}
int initialize_everything(char* lua_file) {
2023-01-06 19:12:24 -05:00
if (!oct_tb_sprite_list_initialize()) {
return 0;
}
L = luaL_newstate();
if (L == NULL) {
fprintf(stderr, "Can't initialize Lua\n");
return 0;
}
luaL_openlibs(L);
oct_tb_initialize_lua(L);
if (luaL_dofile(L, lua_file)) return OCT_LUA_FILE_NOT_FOUND;
2023-01-06 19:12:24 -05:00
lua_getglobal(L, "oct_init");
lua_call(L, 0, 0);
tb_init();
return 0;
2023-01-06 19:12:24 -05:00
}
int deinitialize_everything() {
tb_shutdown();
lua_close(L);
oct_tb_sprite_list_deinitialize();
return 1;
}