Added option to disable termbox if not needed
This commit is contained in:
parent
3ccf158547
commit
bb53693ace
94
main.c
94
main.c
|
@ -1,6 +1,7 @@
|
|||
#include <stdio.h>
|
||||
#include <lua5.3/lualib.h>
|
||||
#include <lua5.3/lauxlib.h>
|
||||
#include <signal.h>
|
||||
#include "termbox_render.h"
|
||||
#include "oct_termbox_sprite.h"
|
||||
#include "oct_networking.h"
|
||||
|
@ -17,6 +18,7 @@
|
|||
#define OCT_HELP 4
|
||||
#define OCT_VERS 5
|
||||
#define OCT_INVALID_ARGUMENT 6
|
||||
#define OCT_NETWORK_ERROR 7
|
||||
|
||||
#define OCT_MAX_FILENAME_SIZE 1024
|
||||
|
||||
|
@ -33,7 +35,9 @@ struct {
|
|||
// Might come in handy later to keep filename
|
||||
// e.g. if I ever want to allow downloading a script from peer
|
||||
char lua_file[OCT_MAX_FILENAME_SIZE];
|
||||
} args;
|
||||
int needs_termbox;
|
||||
int type;
|
||||
} config;
|
||||
|
||||
void usage();
|
||||
void version();
|
||||
|
@ -41,6 +45,9 @@ int process_args(int argc, char* argv[]);
|
|||
int initialize_everything(char* lua_file);
|
||||
int deinitialize_everything();
|
||||
|
||||
int finish = 0;
|
||||
void handle_sigint() {finish = 1;};
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
int process_args_result = process_args(argc, argv);
|
||||
switch (process_args_result) {
|
||||
|
@ -69,24 +76,42 @@ int main(int argc, char* argv[]) {
|
|||
version();
|
||||
return EXIT_SUCCESS;
|
||||
break;
|
||||
case OCT_NETWORK_ERROR:
|
||||
fprintf(stderr, "Error: network\n");
|
||||
deinitialize_everything();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// initialize SIGINT handler
|
||||
struct sigaction sa = {0};
|
||||
sa.sa_handler = handle_sigint;
|
||||
sigaction(SIGINT, &sa, NULL);
|
||||
|
||||
struct tb_event ev;
|
||||
int finish = 0;
|
||||
while (!finish) {
|
||||
tb_clear();
|
||||
tb_peek_event(&ev, 10);
|
||||
if (ev.key == TB_KEY_ESC) {
|
||||
finish = 1;
|
||||
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();
|
||||
}
|
||||
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]);
|
||||
}
|
||||
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();
|
||||
return EXIT_SUCCESS;
|
||||
|
@ -97,8 +122,8 @@ int process_args(int argc, char* argv[]) {
|
|||
return OCT_NO_LUA_FILE;
|
||||
}
|
||||
|
||||
// Set args.port to default
|
||||
strncpy(args.port, OCT_DEFAULT_PORT, 6);
|
||||
// Set config.port to default
|
||||
strncpy(config.port, OCT_DEFAULT_PORT, 6);
|
||||
int log_level = OCT_LOG_LEVEL_ERROR;
|
||||
char log_file[OCT_MAX_FILENAME_SIZE];
|
||||
int log_file_spec = 0;
|
||||
|
@ -109,7 +134,7 @@ int process_args(int argc, char* argv[]) {
|
|||
// strcmp instead of strncmp
|
||||
if (strcmp(argv[i], "-p") == 0) {
|
||||
if (i+1 < argc) {
|
||||
strncpy(args.port, argv[i+1], 6);
|
||||
strncpy(config.port, argv[i+1], 6);
|
||||
i++;
|
||||
}
|
||||
else {
|
||||
|
@ -167,10 +192,10 @@ int process_args(int argc, char* argv[]) {
|
|||
|
||||
oct_log_init(log_file_spec ? log_file : NULL, log_level);
|
||||
|
||||
strncpy(args.lua_file, argv[argc-1], OCT_MAX_FILENAME_SIZE);
|
||||
OCT_LOG_INFO("Running lua file: %s", args.lua_file);
|
||||
strncpy(config.lua_file, argv[argc-1], OCT_MAX_FILENAME_SIZE);
|
||||
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) {
|
||||
|
@ -194,24 +219,33 @@ int initialize_everything(char* lua_file) {
|
|||
if (luaL_dofile(L, lua_file)) return OCT_LUA_FILE_NOT_FOUND;
|
||||
OCT_LOG_INFO("Begin running oct_init()");
|
||||
lua_getglobal(L, "oct_init");
|
||||
lua_call(L, 0, 1);
|
||||
int type = lua_tointeger(L, -1);
|
||||
lua_call(L, 0, 2);
|
||||
config.type = lua_tointeger(L, -2);
|
||||
config.needs_termbox = lua_tointeger(L, -1);
|
||||
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");
|
||||
if (!oct_network_server_init(args.port)) {
|
||||
OCT_LOG_ERROR("Could not establish a socket on port %s\n", args.port);
|
||||
return 0;
|
||||
if (!oct_network_server_init(config.port)) {
|
||||
OCT_LOG_ERROR("Could not establish a socket on port %s\n", config.port);
|
||||
return OCT_NETWORK_ERROR;
|
||||
}
|
||||
}
|
||||
else if (type == OCT_TYPE_CLIENT) {
|
||||
else if (config.type == OCT_TYPE_CLIENT) {
|
||||
//oct_network_client_init();
|
||||
}
|
||||
else {
|
||||
// Do nothing, offline
|
||||
}
|
||||
|
||||
tb_init();
|
||||
if (config.needs_termbox) {
|
||||
tb_init();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include <lua5.3/lauxlib.h>
|
||||
|
||||
#include "oct_termbox_sprite.h"
|
||||
#include "oct_log.h"
|
||||
|
||||
struct otsl oct_tb_sprite_list;
|
||||
|
||||
|
@ -122,6 +123,11 @@ int oct_tb_sprite_new(lua_State *L) {
|
|||
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");
|
||||
|
@ -129,3 +135,8 @@ void oct_tb_initialize_lua(lua_State *L) {
|
|||
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");
|
||||
}
|
||||
|
|
|
@ -45,6 +45,10 @@ int oct_tb_sprite__index(lua_State *L);
|
|||
int oct_tb_sprite__newindex(lua_State *L);
|
||||
int oct_tb_sprite__tostring(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);
|
||||
// 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
|
||||
|
|
|
@ -9,3 +9,6 @@ end
|
|||
|
||||
OCT_TYPE_SERVER = 1;
|
||||
OCT_TYPE_CLIENT = 2;
|
||||
OCT_NEEDS_TERMBOX = 1;
|
||||
OCT_NOT_NEEDS_TERMBOX = 0;
|
||||
|
||||
|
|
1
pong.lua
1
pong.lua
|
@ -85,6 +85,7 @@ function oct_init()
|
|||
score_p2_sprite["shape"] = "Player 2: " .. tostring(score_p2);
|
||||
init_text["shape"] = "Press SPACE to begin";
|
||||
pause();
|
||||
return OCT_TYPE_CLIENT, OCT_NEEDS_TERMBOX;
|
||||
end
|
||||
|
||||
function oct_loop(key, ch)
|
||||
|
|
|
@ -4,12 +4,13 @@ require("termbox_defs")
|
|||
text = oct_tb_sprite_new();
|
||||
|
||||
function oct_init()
|
||||
text["shape"] = "THIS IS A TEST SERVER";
|
||||
text["x"] = 50;
|
||||
text["y"] = 10;
|
||||
return OCT_TYPE_SERVER;
|
||||
-- text["shape"] = "THIS IS A TEST SERVER";
|
||||
-- text["x"] = 50;
|
||||
-- text["y"] = 10;
|
||||
OCT_LOG_ERROR("THIS IS A TEST SERVER");
|
||||
return OCT_TYPE_SERVER, OCT_NOT_NEEDS_TERMBOX;
|
||||
end
|
||||
|
||||
function oct_loop(key)
|
||||
-- Do nothing, for now
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue