Added in errors, ability to specify lua file

This commit is contained in:
j4nk 2023-01-08 19:46:10 -05:00
parent c65a87fab6
commit 1607652f62
2 changed files with 29 additions and 6 deletions

35
main.c
View File

@ -7,13 +7,28 @@
#define TB_IMPL
#include "termbox.h"
// Errors
#define OCT_NO_LUA_FILE 1
#define OCT_LUA_FILE_NOT_FOUND 2
#define OCT_LUA_FILE_MISSING_OCT_INIT 3
lua_State *L;
int initialize_everything();
int process_args(int argc, char* argv[]);
int initialize_everything(char* lua_file);
int deinitialize_everything();
int main(int argc, char* argv[]) {
if (!initialize_everything()) {
fprintf(stderr, "Couldn't initialize Open Card Table");
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;
}
struct tb_event ev;
int finish = 0;
@ -37,7 +52,15 @@ int main(int argc, char* argv[]) {
return EXIT_SUCCESS;
}
int initialize_everything() {
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) {
if (!oct_tb_sprite_list_initialize()) {
return 0;
}
@ -48,11 +71,11 @@ int initialize_everything() {
}
luaL_openlibs(L);
oct_tb_initialize_lua(L);
luaL_dofile(L, "pong.lua");
if (luaL_dofile(L, lua_file)) return OCT_LUA_FILE_NOT_FOUND;
lua_getglobal(L, "oct_init");
lua_call(L, 0, 0);
tb_init();
return 1;
return 0;
}
int deinitialize_everything() {

BIN
test

Binary file not shown.