Added option to disable termbox if not needed

This commit is contained in:
j4nk 2023-05-26 19:43:18 -04:00
parent 3ccf158547
commit bb53693ace
6 changed files with 89 additions and 35 deletions

96
main.c
View File

@ -1,6 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <lua5.3/lualib.h> #include <lua5.3/lualib.h>
#include <lua5.3/lauxlib.h> #include <lua5.3/lauxlib.h>
#include <signal.h>
#include "termbox_render.h" #include "termbox_render.h"
#include "oct_termbox_sprite.h" #include "oct_termbox_sprite.h"
#include "oct_networking.h" #include "oct_networking.h"
@ -17,6 +18,7 @@
#define OCT_HELP 4 #define OCT_HELP 4
#define OCT_VERS 5 #define OCT_VERS 5
#define OCT_INVALID_ARGUMENT 6 #define OCT_INVALID_ARGUMENT 6
#define OCT_NETWORK_ERROR 7
#define OCT_MAX_FILENAME_SIZE 1024 #define OCT_MAX_FILENAME_SIZE 1024
@ -33,7 +35,9 @@ struct {
// Might come in handy later to keep filename // Might come in handy later to keep filename
// e.g. if I ever want to allow downloading a script from peer // e.g. if I ever want to allow downloading a script from peer
char lua_file[OCT_MAX_FILENAME_SIZE]; char lua_file[OCT_MAX_FILENAME_SIZE];
} args; int needs_termbox;
int type;
} config;
void usage(); void usage();
void version(); void version();
@ -41,6 +45,9 @@ int process_args(int argc, char* argv[]);
int initialize_everything(char* lua_file); int initialize_everything(char* lua_file);
int deinitialize_everything(); int deinitialize_everything();
int finish = 0;
void handle_sigint() {finish = 1;};
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
int process_args_result = process_args(argc, argv); int process_args_result = process_args(argc, argv);
switch (process_args_result) { switch (process_args_result) {
@ -69,24 +76,42 @@ int main(int argc, char* argv[]) {
version(); version();
return EXIT_SUCCESS; return EXIT_SUCCESS;
break; break;
case OCT_NETWORK_ERROR:
fprintf(stderr, "Error: network\n");
deinitialize_everything();
return EXIT_FAILURE;
} }
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++) { // initialize SIGINT handler
oct_render_termbox_sprite(oct_tb_sprite_list.sprite_list[i]); struct sigaction sa = {0};
sa.sa_handler = handle_sigint;
sigaction(SIGINT, &sa, NULL);
struct tb_event ev;
if (config.needs_termbox) {
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();
}
}
else {
while (!finish) {
lua_getglobal(L, "oct_loop");
lua_pushinteger(L, 0);
lua_pushinteger(L, 0);
lua_call(L, 2, 0);
} }
tb_present();
} }
deinitialize_everything(); deinitialize_everything();
return EXIT_SUCCESS; return EXIT_SUCCESS;
@ -97,8 +122,8 @@ int process_args(int argc, char* argv[]) {
return OCT_NO_LUA_FILE; return OCT_NO_LUA_FILE;
} }
// Set args.port to default // Set config.port to default
strncpy(args.port, OCT_DEFAULT_PORT, 6); strncpy(config.port, OCT_DEFAULT_PORT, 6);
int log_level = OCT_LOG_LEVEL_ERROR; int log_level = OCT_LOG_LEVEL_ERROR;
char log_file[OCT_MAX_FILENAME_SIZE]; char log_file[OCT_MAX_FILENAME_SIZE];
int log_file_spec = 0; int log_file_spec = 0;
@ -109,7 +134,7 @@ int process_args(int argc, char* argv[]) {
// strcmp instead of strncmp // strcmp instead of strncmp
if (strcmp(argv[i], "-p") == 0) { if (strcmp(argv[i], "-p") == 0) {
if (i+1 < argc) { if (i+1 < argc) {
strncpy(args.port, argv[i+1], 6); strncpy(config.port, argv[i+1], 6);
i++; i++;
} }
else { else {
@ -167,10 +192,10 @@ int process_args(int argc, char* argv[]) {
oct_log_init(log_file_spec ? log_file : NULL, log_level); oct_log_init(log_file_spec ? log_file : NULL, log_level);
strncpy(args.lua_file, argv[argc-1], OCT_MAX_FILENAME_SIZE); strncpy(config.lua_file, argv[argc-1], OCT_MAX_FILENAME_SIZE);
OCT_LOG_INFO("Running lua file: %s", args.lua_file); OCT_LOG_INFO("Running lua file: %s", config.lua_file);
return initialize_everything(args.lua_file); return initialize_everything(config.lua_file);
} }
int initialize_everything(char* lua_file) { int initialize_everything(char* lua_file) {
@ -194,24 +219,33 @@ int initialize_everything(char* lua_file) {
if (luaL_dofile(L, lua_file)) return OCT_LUA_FILE_NOT_FOUND; if (luaL_dofile(L, lua_file)) return OCT_LUA_FILE_NOT_FOUND;
OCT_LOG_INFO("Begin running oct_init()"); OCT_LOG_INFO("Begin running oct_init()");
lua_getglobal(L, "oct_init"); lua_getglobal(L, "oct_init");
lua_call(L, 0, 1); lua_call(L, 0, 2);
int type = lua_tointeger(L, -1); config.type = lua_tointeger(L, -2);
config.needs_termbox = lua_tointeger(L, -1);
OCT_LOG_INFO("Finish running oct_init()"); OCT_LOG_INFO("Finish running oct_init()");
if (type == OCT_TYPE_SERVER) {
OCT_LOG_INFO("Lua script %s termbox", config.needs_termbox ? "requires" : "does not require");
if (!config.needs_termbox) {
oct_tb_change_oct_tb_sprite_new(L);
}
if (config.type == OCT_TYPE_SERVER) {
OCT_LOG_INFO("Lua script is server type"); OCT_LOG_INFO("Lua script is server type");
if (!oct_network_server_init(args.port)) { if (!oct_network_server_init(config.port)) {
OCT_LOG_ERROR("Could not establish a socket on port %s\n", args.port); OCT_LOG_ERROR("Could not establish a socket on port %s\n", config.port);
return 0; return OCT_NETWORK_ERROR;
} }
} }
else if (type == OCT_TYPE_CLIENT) { else if (config.type == OCT_TYPE_CLIENT) {
//oct_network_client_init(); //oct_network_client_init();
} }
else { else {
// Do nothing, offline // Do nothing, offline
} }
if (config.needs_termbox) {
tb_init(); tb_init();
}
return 0; return 0;
} }

View File

@ -3,6 +3,7 @@
#include <lua5.3/lauxlib.h> #include <lua5.3/lauxlib.h>
#include "oct_termbox_sprite.h" #include "oct_termbox_sprite.h"
#include "oct_log.h"
struct otsl oct_tb_sprite_list; struct otsl oct_tb_sprite_list;
@ -122,6 +123,11 @@ int oct_tb_sprite_new(lua_State *L) {
return 1; 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) { void oct_tb_initialize_lua(lua_State *L) {
lua_pushcfunction(L, oct_tb_sprite_new); lua_pushcfunction(L, oct_tb_sprite_new);
lua_setglobal(L, "oct_tb_sprite_new"); lua_setglobal(L, "oct_tb_sprite_new");
@ -129,3 +135,8 @@ void oct_tb_initialize_lua(lua_State *L) {
luaL_setfuncs(L, oct_tb_sprite_metamethods, 0); 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");
}

View File

@ -45,6 +45,10 @@ int oct_tb_sprite__index(lua_State *L);
int oct_tb_sprite__newindex(lua_State *L); int oct_tb_sprite__newindex(lua_State *L);
int oct_tb_sprite__tostring(lua_State *L); int oct_tb_sprite__tostring(lua_State *L);
int oct_tb_sprite_new(lua_State *L); int oct_tb_sprite_new(lua_State *L);
int oct_tb_sprite_new_uninit(lua_State *L);
void oct_tb_initialize_lua(lua_State *L); void oct_tb_initialize_lua(lua_State *L);
// Points lua's oct_tb_sprite_new from C's oct_tb_sprite new to C's
// oct_tb_sprite_new_uninit, which just prints a warning
void oct_tb_change_oct_tb_sprite_new(lua_State *L);
#endif #endif

View File

@ -9,3 +9,6 @@ end
OCT_TYPE_SERVER = 1; OCT_TYPE_SERVER = 1;
OCT_TYPE_CLIENT = 2; OCT_TYPE_CLIENT = 2;
OCT_NEEDS_TERMBOX = 1;
OCT_NOT_NEEDS_TERMBOX = 0;

View File

@ -85,6 +85,7 @@ function oct_init()
score_p2_sprite["shape"] = "Player 2: " .. tostring(score_p2); score_p2_sprite["shape"] = "Player 2: " .. tostring(score_p2);
init_text["shape"] = "Press SPACE to begin"; init_text["shape"] = "Press SPACE to begin";
pause(); pause();
return OCT_TYPE_CLIENT, OCT_NEEDS_TERMBOX;
end end
function oct_loop(key, ch) function oct_loop(key, ch)

View File

@ -4,12 +4,13 @@ require("termbox_defs")
text = oct_tb_sprite_new(); text = oct_tb_sprite_new();
function oct_init() function oct_init()
text["shape"] = "THIS IS A TEST SERVER"; -- text["shape"] = "THIS IS A TEST SERVER";
text["x"] = 50; -- text["x"] = 50;
text["y"] = 10; -- text["y"] = 10;
return OCT_TYPE_SERVER; OCT_LOG_ERROR("THIS IS A TEST SERVER");
return OCT_TYPE_SERVER, OCT_NOT_NEEDS_TERMBOX;
end end
function oct_loop(key) function oct_loop(key)
-- Do nothing, for now
end end