diff options
-rw-r--r-- | src/Makefile.am | 27 | ||||
-rw-r--r-- | src/calculer.c | 34 | ||||
-rw-r--r-- | src/cli.c | 9 | ||||
-rw-r--r-- | src/disp.c | 21 | ||||
-rw-r--r-- | src/ray.c | 24 |
5 files changed, 73 insertions, 42 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index a2817d3..7094734 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,5 +1,6 @@ bin_PROGRAMS = calculer AM_CFLAGS = -Wall -Wextra +calculer_CFLAGS = $(AM_CFLAGS) AM_LDFLAGS = calculer_SOURCES = calculer.c disp.c @@ -7,16 +8,32 @@ if DEBUG_COND noinst_LTLIBRARIES = ray.la ray_la_SOURCES = ray.c ray_la_LDFLAGS = -module -rpath @abs_srcdir@ -lraylib -else + +noinst_LTLIBRARIES += cli.la +cli_la_SOURCES = cli.c +cli_la_LDFLAGS = -module -rpath @abs_srcdir@ + +if USE_READLINE_COND +cli_la_LDFLAGS += -lreadline +else # USE_READLINE_COND +EXTRA_cli_la_SOURCES = linenoise.c +cli_la_AM_CFLAGS = -I../linenoise/ +BUILT_SOURCES: linenoise_lib.c +linenoise_lib.c: + cp ../linenoise/linenoise.c ./$@ +endif # USE_READLINE_COND + +else # DEBUG_COND AM_CFLAGS += -DNDEBUG -Wno-unused-variable -endif if USE_READLINE_COND AM_LDFLAGS += -lreadline -else +else # USE_READLINE_COND calculer_SOURCES += linenoise.c AM_CFLAGS += -I../linenoise/ BUILT_SOURCES: linenoise.c linenoise.c: - cp ../linenoise/linenoise.c . -endif + cp ../linenoise/linenoise.c ./$@ +endif # USE_READLINE_COND + +endif # DEBUG_COND diff --git a/src/calculer.c b/src/calculer.c index de2dcf8..eab2bde 100644 --- a/src/calculer.c +++ b/src/calculer.c @@ -18,6 +18,7 @@ #include "config.h" #include <assert.h> +#include <limits.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> @@ -25,7 +26,6 @@ #include <time.h> #include <unistd.h> -#include "_readline.h" #include "disp.h" static int lower = 0; @@ -187,43 +187,27 @@ main(int argc, char *argv[]) logfile = fopen(logpath, "a"); assert(logfile); /* TODO: handle non-fatal error properly */ disp = get_disp(disp_name); + disp->init(); while (1) { - char prompt[32] = {0}; int x, y, res; - char *input; - int input_val; + int input; x = calculer_rand(); y = calculer_rand(); res = x+y; - - sprintf(prompt, "%d + %d = ", x, y); + disp->display_calc(x, y); void *measure_data = measure_before(); - input = readline(prompt); - - if ((!input) || (*input == '\0')) + input = disp->read_input(); + if (input == INT_MAX) { - free(input); + disp->destroy(); fclose(logfile); exit(0); } - - input_val = atoi(input); - if (measure_after(measure_data, input_val == res)) - { - free(input); - fclose(logfile); - exit(1); - } - - if (input_val == res) - printf("RIGHT\n"); - else - printf("WRONG\n"); - - free(input); + measure_after(measure_data, input == res); + disp->display_res(input == res, 0); } fclose(logfile); @@ -17,21 +17,28 @@ #include "config.h" #include <stdlib.h> +#include <stdio.h> +#include <limits.h> #include "_readline.h" +#include "disp.h" void init(void) {} void display_calc(int x, int y) { - printf(prompt, "%d + %d = ", x, y); + printf("%d + %d = ", x, y); } int read_input(void) { char *txt = readline(""); + + if ((!txt) || (*txt == '\0')) + return INT_MAX; + return atoi(txt); } @@ -24,12 +24,14 @@ #include "disp.h" -#define dlsym_and_check(dest, name) \ - dest->name = dlsym(dest->dl_handle, #name); \ - if (!dest->name) { \ - dlclose(dest->dl_handle); \ - free(dest); \ - return NULL; \ +#define dlsym_and_check(dest, name) \ + dest->name = dlsym(dest->dl_handle, #name); \ + if ((!dest->name) || (dest->name == dest->dl_handle)) { \ + char *msg = dlerror(); \ + fprintf(stderr, "calculer: %s\n", msg); \ + dlclose(dest->dl_handle); \ + free(dest); \ + return NULL; \ } struct disp * @@ -37,10 +39,10 @@ get_disp(char *disp_name) { #ifndef NDEBUG /* displays are shared libraries */ - char so_path[16] = {0}; - strcpy(so_path, "./"); + char so_path[64] = {0}; + strcpy(so_path, "./src/.libs/"); strcat(so_path, disp_name); - strcat(so_path, ".la"); + strcat(so_path, ".so.0.0.0"); void *handle = dlopen(so_path, RTLD_LAZY); if (!handle) @@ -52,6 +54,7 @@ get_disp(char *disp_name) struct disp *disp = malloc(sizeof(*disp)); disp->dl_handle = handle; + dlsym_and_check(disp, init); dlsym_and_check(disp, display_calc); dlsym_and_check(disp, read_input); dlsym_and_check(disp, display_res); @@ -16,17 +16,29 @@ */ #include "config.h" +#include <limits.h> #include <raylib.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> #include "disp.h" #define DEFAULT_HEIGHT 200 -#define DEFAULT_WIDTH 16*9/DEFAULT_HEIGHT +#define DEFAULT_WIDTH 16*DEFAULT_HEIGHT/9 void init(void) { + printf("info: raylib interface initialized\n"); InitWindow(DEFAULT_WIDTH, DEFAULT_HEIGHT, "calculer"); + + if (!IsWindowReady()) + { + fprintf(stderr, "calculer: error initialiazing Raylib \ +window\n"); + exit(1); + } SetTargetFPS(24); } @@ -39,7 +51,15 @@ display_calc(int x, int y) int read_input(void) { - + if (!WindowShouldClose()) + { + sleep(1); + return 0; + } + else + { + return INT_MAX; + } } void |