diff options
Diffstat (limited to 'src/ray.c')
-rw-r--r-- | src/ray.c | 97 |
1 files changed, 39 insertions, 58 deletions
@@ -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"); } |