diff options
-rw-r--r-- | src/Makefile.am | 4 | ||||
-rw-r--r-- | src/calc_data.c | 35 | ||||
-rw-r--r-- | src/calc_data.h | 38 | ||||
-rw-r--r-- | src/input.c | 28 | ||||
-rw-r--r-- | src/input.h | 24 | ||||
-rw-r--r-- | src/ray.c | 97 | ||||
-rw-r--r-- | src/tui.c | 100 |
7 files changed, 266 insertions, 60 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index ba7136b..d8f6457 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -6,7 +6,7 @@ calculer_SOURCES = calculer.c disp.c if DEBUG_COND noinst_LTLIBRARIES = ray.la -ray_la_SOURCES = ray.c +ray_la_SOURCES = ray.c calc_data.c input.c ray_la_LDFLAGS = -module -rpath @abs_srcdir@ -lraylib noinst_LTLIBRARIES += cli.la @@ -14,7 +14,7 @@ cli_la_SOURCES = cli.c cli_la_LDFLAGS = -module -rpath @abs_srcdir@ noinst_LTLIBRARIES += tui.la -tui_la_SOURCES = tui.c +tui_la_SOURCES = tui.c calc_data.c input.c tui_la_LDFLAGS = -module -rpath @abs_srcdir@ -lncurses if USE_READLINE_COND diff --git a/src/calc_data.c b/src/calc_data.c new file mode 100644 index 0000000..ee4e1df --- /dev/null +++ b/src/calc_data.c @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2024 Tristan Riehs + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include "calc_data.h" + +int +next_calc(int i) +{ + if (i == CALC_COUNT - 1) + return 0; + else + return i+1; +} + +int +loop_backwards(int i) +{ + if (i == 0) + return CALC_COUNT-1; + else + return i-1; +} diff --git a/src/calc_data.h b/src/calc_data.h new file mode 100644 index 0000000..871a249 --- /dev/null +++ b/src/calc_data.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2024 Tristan Riehs + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#ifndef CALC_DATA_H +#define CALC_DATA_H + +#include <stdbool.h> + +struct calc_data { + int x; + int y; + int input; + bool right; + int ms; +}; + +#define CALC_COUNT 16 + +/* Go to the next calculation index. */ +int next_calc(int); + +/* Loop backwards and cycle through available calculations. */ +int loop_backwards(int); + +#endif diff --git a/src/input.c b/src/input.c new file mode 100644 index 0000000..7710723 --- /dev/null +++ b/src/input.c @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2024 Tristan Riehs + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include "input.h" + +int +add_input_digit(int input, int digit) +{ + return input*10 + digit; +} + +int rm_input_digit(int input) +{ + return input/10; +} diff --git a/src/input.h b/src/input.h new file mode 100644 index 0000000..2df0db5 --- /dev/null +++ b/src/input.h @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2024 Tristan Riehs + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#ifndef INPUT_H +#define INPUT_H + +int add_input_digit(int input, int digit); + +int rm_input_digit(int input); + +#endif @@ -24,23 +24,16 @@ #include <unistd.h> #include "disp.h" +#include "calc_data.h" +#include "input.h" #define DEFAULT_HEIGHT 200 #define DEFAULT_WIDTH (DEFAULT_HEIGHT*16/9) #define MAX_FONTSIZE 100 -struct state { - int x; - int y; - int input; - bool right; - int ms; -}; - -#define STATE_COUNT 16 -static struct state states[STATE_COUNT] = {0}; -static int current_state = 0; +static struct calc_data calcs[CALC_COUNT] = {0}; +static int current_idx = 0; void init(void) @@ -67,7 +60,7 @@ window\n"); ClearBackground(RAYWHITE); EndDrawing(); - current_state = 0; + current_idx = 0; TraceLog(LOG_INFO, "window successfully initialized"); } @@ -89,7 +82,7 @@ draw_calc(void) int txt_width; int font_size; - sprintf(txt, "%d + %d", states[current_state].x, states[current_state].y); + sprintf(txt, "%d + %d", calcs[current_idx].x, calcs[current_idx].y); win_width = GetScreenWidth(); win_height = GetScreenHeight(); @@ -115,7 +108,7 @@ draw_calc(void) win_height/6, font_size, GRAY); TraceLog(LOG_INFO, "calulation %d + %d successfully drawn", - states[current_state].x, states[current_state].y); + calcs[current_idx].x, calcs[current_idx].y); } static void @@ -126,7 +119,7 @@ draw_input(void) int txt_width; int font_size; - sprintf(txt, "%d", states[current_state].input); + sprintf(txt, "%d", calcs[current_idx].input); win_width = GetScreenWidth(); win_height = GetScreenHeight(); @@ -158,7 +151,7 @@ draw_res(void) win_width = GetScreenWidth(); win_height = GetScreenHeight(); - font_size = min(win_height/STATE_COUNT, MAX_FONTSIZE/2); + font_size = min(win_height/CALC_COUNT, MAX_FONTSIZE/2); txt_width = MeasureText("60 + 60 = 120 10000ms", font_size); @@ -168,31 +161,25 @@ draw_res(void) font_size = font_size/scale; } - for (int i = 1; i < STATE_COUNT; i++) + int j = loop_backwards(current_idx); + int line = 0; + while ((calcs[j].ms != 0) && (line < CALC_COUNT)) { - int j = current_state - i; - - if (j < 0) - j += STATE_COUNT; - - TraceLog(LOG_DEBUG, "i = %d", i); - TraceLog(LOG_DEBUG, "j = %d", j); - - if (states[j].ms == 0) - break; - - if (states[j].right) + if (calcs[j].right) col = GREEN; else col = RED; sprintf(txt, "%d + %d = %d %dms", - states[j].x, states[j].y, - states[j].input, states[j].ms); + calcs[j].x, calcs[j].y, + calcs[j].input, calcs[j].ms); txt_width = MeasureText(txt, font_size); DrawText(txt, win_width-txt_width, - (int) font_size*1.2*(i-1), font_size, col); + (int) font_size*1.2*line, font_size, col); + + j = loop_backwards(j); + line++; } } @@ -210,19 +197,11 @@ redraw(void) void display_calc(int x, int y) { - current_state = (current_state + 1)%STATE_COUNT; - states[current_state].x = x; - states[current_state].y = y; - states[current_state].ms = 0; - states[current_state].input = 0; - redraw(); -} - -static void -add_input_digit(int digit) -{ - states[current_state].input *= 10; - states[current_state].input += digit; + current_idx = (current_idx + 1)%CALC_COUNT; + calcs[current_idx].x = x; + calcs[current_idx].y = y; + calcs[current_idx].ms = 0; + calcs[current_idx].input = 0; redraw(); } @@ -231,9 +210,11 @@ check_digit(int digit) { if (IsKeyPressed(KEY_ZERO + digit)) { - add_input_digit(digit); + calcs[current_idx].input = + add_input_digit(calcs[current_idx].input, digit); TraceLog(LOG_INFO, "digit %d added to input", digit); - TraceLog(LOG_INFO, "input is now %d", states[current_state].input); + TraceLog(LOG_INFO, "input is now %d", calcs[current_idx].input); + redraw(); } } @@ -248,7 +229,7 @@ int read_input(void) { TraceLog(LOG_INFO, "waiting for user input"); - states[current_state].input = 0; + calcs[current_idx].input = 0; while (!WindowShouldClose()) { @@ -257,7 +238,7 @@ read_input(void) if (IsWindowResized()) redraw(); if (IsKeyPressed(KEY_ENTER)) - return states[current_state].input; + return calcs[current_idx].input; if (IsKeyPressed(KEY_Q) || IsKeyPressed(KEY_A)) return DISP_QUIT; #ifndef NDEBUG @@ -266,8 +247,8 @@ read_input(void) #endif if (IsKeyPressed(KEY_BACKSPACE)) { - states[current_state].input = - states[current_state].input / 10; + calcs[current_idx].input = + rm_input_digit(calcs[current_idx].input); redraw(); } check_digits(); @@ -279,8 +260,8 @@ read_input(void) void display_res(bool right, int ms) { - states[current_state].right = right; - states[current_state].ms = ms; + calcs[current_idx].right = right; + calcs[current_idx].ms = ms; redraw(); } @@ -294,9 +275,9 @@ destroy(void) void * pre_reload(void) { - char *data = malloc(sizeof(states) + sizeof(current_state)); - memcpy(data, &states, sizeof(states)); - memcpy(data + sizeof(states), ¤t_state, sizeof(current_state)); + char *data = malloc(sizeof(calcs) + sizeof(current_idx)); + memcpy(data, &calcs, sizeof(calcs)); + memcpy(data + sizeof(calcs), ¤t_idx, sizeof(current_idx)); TraceLog(LOG_INFO, "pre-reload actions done"); return data; } @@ -305,8 +286,8 @@ void post_reload(void *state) { char *data = state; - current_state = *(data + sizeof(states)); - memcpy(&states, data, sizeof(states)); + memcpy(&calcs, data, sizeof(calcs)); + memcpy(¤t_idx, data + sizeof(calcs), sizeof(current_idx)); free(data); TraceLog(LOG_INFO, "post-reload actions done"); } diff --git a/src/tui.c b/src/tui.c new file mode 100644 index 0000000..0706b2d --- /dev/null +++ b/src/tui.c @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2024 Tristan Riehs + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include <curses.h> +#include <stdlib.h> +#include <string.h> + +#include "disp.h" +#include "calc_data.h" + +static struct calc_data calcs[CALC_COUNT] = {0}; +static int current_idx = 0; +static WINDOW *win; + +void +init(void) +{ + win = initscr(); + clear(); + refresh(); +} + +void +display_calc(int x, int y) +{ + calcs[current_idx].x = x; + calcs[current_idx].y = y; + clear(); + printw("%d + %d = ", x, y); + refresh(); +} + +int +read_input(void) +{ + char c = getchar(); + + switch(c) + { +#ifndef NDEBUG + case 'g': + case 'r': + return DISP_RELOAD; +#endif + case 'q': + return DISP_QUIT; + default: + return DISP_ERR; + } + + return DISP_ERR; +} + +void +display_res(bool right, int ms) +{ + (void) right; + (void) ms; +} + +void +destroy(void) +{ + endwin(); +} + +#ifndef NDEBUG +void * +pre_reload(void) +{ + char *data = malloc(sizeof(calcs) + sizeof(current_idx)); + memcpy(data, &calcs, sizeof(calcs)); + memcpy(data + sizeof(calcs), ¤t_idx, sizeof(current_idx)); + endwin(); + return data; +} + +void +post_reload(void *state) +{ + char *data = state; + memcpy(&calcs, data, sizeof(calcs)); + memcpy(¤t_idx, data + sizeof(calcs), sizeof(current_idx)); + free(data); + init(); +} +#endif |