diff options
author | Paweł Redman <pawel.redman@gmail.com> | 2016-04-05 14:01:50 +0200 |
---|---|---|
committer | Paweł Redman <pawel.redman@gmail.com> | 2016-04-05 14:01:50 +0200 |
commit | c7acaee6651a84ef9102b29350c37803c8f435a9 (patch) | |
tree | 68af0d065a3580c32558feda7dc918e112771238 | |
parent | 77a74fcea5c628bb426b5cce1012e4e5504b10f1 (diff) |
Console WIP.
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | src/console.c | 137 | ||||
-rw-r--r-- | src/console.h | 6 | ||||
-rw-r--r-- | src/main.c | 22 |
4 files changed, 163 insertions, 6 deletions
@@ -1,7 +1,7 @@ CC = gcc CFLAGS += -g -Wall CPPFLAGS += -MMD -LDFLAGS += -lm -lSDL2 -lSDL2_image +LDFLAGS += -lm -lSDL2 -lSDL2_image -lreadline PP_BOLD := $(shell tput bold) PP_RESET := $(shell tput sgr0) @@ -10,7 +10,7 @@ PP_LD := $(PP_BOLD)$(shell tput setf 2)LD$(PP_RESET) PP_RM := $(PP_BOLD)$(shell tput setf 4)RM$(PP_RESET) SRC := src/main.c src/physics.c src/renderer.c src/ui.c src/itc.c \ - src/common.c + src/common.c src/console.c OBJ := $(SRC:src/%.c=obj/%.o) OUT := cem diff --git a/src/console.c b/src/console.c new file mode 100644 index 0000000..13e33f7 --- /dev/null +++ b/src/console.c @@ -0,0 +1,137 @@ +#include "console.h" +#include <readline/readline.h> + +typedef struct con_var_s con_var_t; +struct con_var_s { + char *name; + char *value; + con_var_t *next; +}; + +con_var_t *con_vars[256]; + +size_t con_var_hash(char *text) +{ + char *p; + size_t rv; + + for (rv = 0, p = text; *p; p++) + rv += 119 * ((*p) + 83); + + return rv & 0xFF; +} + +con_var_t *con_var_find(char *name, size_t *ret_hash) +{ + size_t hash; + con_var_t *var; + + hash = con_var_hash(name); + + if (ret_hash) + *ret_hash = hash; + + for (var = con_vars[hash]; var; var = var->next) + if (!strcmp(var->name, name)) + return var; + + return NULL; +} + +con_var_t *con_var_add(char *name) +{ + con_var_t *var; + size_t hash; + + var = con_var_find(name, &hash); + if (var) + return var; + + var = malloc(sizeof(con_var_t)); + assert(var); + + var->next = con_vars[hash]; + con_vars[hash] = var; + + var->name = strdup(name); + assert(var->name); + + var->value = NULL; + + return var; +} + +void con_var_set(char *name, char *value) +{ + con_var_t *var; + + var = con_var_add(name); + + free(var->value); + + if (value) { + var->value = strdup(value); + assert(var->value); + } else + var->value = NULL; +} + +char *con_var_get(char *name) +{ + con_var_t *var; + + var = con_var_find(name, NULL); + if (!var) + return NULL; + + return var->value; +} + +float con_var_get_float(char *name) +{ + char *str; + + str = con_var_get(name); + if (!str) + return 0.0f; + + return atof(str); +} + +size_t con_var_get_size(char *name) +{ + char *str; + + str = con_var_get(name); + if (!str) + return 0; + + return strtoul(name, NULL, 10); +} + +int con_init(void) +{ + con_var_set("kurwa", "chuj"); + return 0; +} + + +void con_quit(void) +{ +} + + + +int con_thread_f(phy_sim *sim) +{ + printf("%f\n", con_var_get_float("kurwa")); + + while (1) { + char *line; + + line = readline("(cem) "); + printf("line=%s\n", line); + } + + return 0; +} diff --git a/src/console.h b/src/console.h new file mode 100644 index 0000000..2204ce9 --- /dev/null +++ b/src/console.h @@ -0,0 +1,6 @@ +#include "common.h" +#include "physics.h" + +int con_init(void); +void con_quit(void); +int con_thread_f(phy_sim *sim); @@ -5,15 +5,14 @@ #include "physics.h" #include "renderer.h" #include "ui.h" - -void itc_test(void); +#include "console.h" int main(void) { int rv = 0; r_window *first_window; phy_sim sim; - SDL_Thread *sim_thread; + SDL_Thread *sim_thread, *con_thread; if (r_init()) { con_printf("fatal error: renderer initialization failed\n"); @@ -54,6 +53,21 @@ int main(void) goto quit; } + if (con_init()) { + con_printf("fatal error: console initialization failed\n"); + rv = 1; + goto quit; + } + + con_thread = SDL_CreateThread((SDL_ThreadFunction)con_thread_f, + "con_thread", &sim); + if (!con_thread) { + con_printf("fatal error: SDL_CreateThread failed: %s\n", + SDL_GetError()); + rv = 1; + goto quit; + } + while (1) { SDL_Event event; @@ -72,7 +86,7 @@ quit: itc_chan_push(&sim.ctl, PHY_CMD_QUIT, NULL); SDL_WaitThread(sim_thread, NULL); - phy_sim_destroy(&sim); + phy_sim_destroy(&sim); ui_quit(); r_quit(); |