diff options
author | Tristan Riehs <tristan.riehs@bordeaux-inp.fr> | 2024-06-22 17:31:06 +0900 |
---|---|---|
committer | Tristan Riehs <tristan.riehs@bordeaux-inp.fr> | 2024-06-22 17:31:06 +0900 |
commit | 600f6d75f17b2fe7985b437122f695d3169cb916 (patch) | |
tree | 97467fcc0c5791e3e244f332813aab8abb166872 /src/ray.c | |
parent | a7722e51e7443fc464b21b20576f179a43f0a21a (diff) |
Make the display responsive
Diffstat (limited to 'src/ray.c')
-rw-r--r-- | src/ray.c | 112 |
1 files changed, 96 insertions, 16 deletions
@@ -26,6 +26,10 @@ #define DEFAULT_HEIGHT 200 #define DEFAULT_WIDTH 16*DEFAULT_HEIGHT/9 +static int current_input = 0; +static int current_x; +static int current_y; + void init(void) { @@ -63,20 +67,60 @@ min(int x, int y) return y; } +static void +draw_calc(void) +{ + char txt[64] = {0}; + int win_width, win_height; + int txt_width; + int font_size; + + sprintf(txt, "%d + %d", current_x, current_y); + + win_width = GetScreenWidth(); + win_height = GetScreenHeight(); + + font_size = min(win_height/3, 40); + txt_width = MeasureText(txt, font_size); + + if (txt_width > win_width) + { + float scale = txt_width/win_width; + font_size = font_size/scale; + txt_width = MeasureText(txt, font_size); + } + + DrawText(txt, (win_width-txt_width)/2, win_height/6, font_size, GRAY); + + TraceLog(LOG_INFO, "calulation %d + %d successfully drawn", + current_x, current_y); +} + void display_calc(int x, int y) { - char txt[32] = {0}; + current_x = x; + current_y = y; + BeginDrawing(); + ClearBackground(RAYWHITE); + draw_calc(); + EndDrawing(); +} + +static void +draw_input(void) +{ + char txt[64] = {0}; int win_width, win_height; int txt_width; int font_size; - sprintf(txt, "%d + %d", x, y); + sprintf(txt, "%d", current_input); win_width = GetScreenWidth(); win_height = GetScreenHeight(); - font_size = min(win_height/3, 40); + font_size = min(win_height/4, 30); txt_width = MeasureText(txt, font_size); if (txt_width > win_width) @@ -86,38 +130,74 @@ display_calc(int x, int y) txt_width = MeasureText(txt, font_size); } + DrawText(txt, (win_width-txt_width)/2, win_height*4/6, font_size, DARKGRAY); + + TraceLog(LOG_INFO, "input successfully drawn"); +} + +static void +redraw(void) +{ BeginDrawing(); ClearBackground(RAYWHITE); - DrawText(txt, (win_width-txt_width)/2, win_height/6, font_size, GRAY); + draw_calc(); + draw_input(); EndDrawing(); +} + +static void +add_input_digit(int digit) +{ + current_input *= 10; + current_input += digit; + redraw(); +} + +static void +check_digit(int digit) +{ + if (IsKeyPressed(KEY_ZERO + digit)) + { + add_input_digit(digit); + TraceLog(LOG_INFO, "digit %d added to input", digit); + TraceLog(LOG_INFO, "input is now %d", current_input); + } +} - TraceLog(LOG_INFO, "calulation %d + %d successfully drawn", x, y); +static void +check_digits(void) +{ + for (int i = 0; i < 10; i++) + check_digit(i); } int read_input(void) { TraceLog(LOG_INFO, "waiting for user input"); + current_input = 0; + while (!WindowShouldClose()) { - /* BeginDrawing(); */ PollInputEvents(); + + if (IsWindowResized()) + redraw(); if (IsKeyPressed(KEY_ENTER)) - { - TraceLog(LOG_INFO, "enter key pressed"); + return current_input; + if (IsKeyPressed(KEY_Q) || IsKeyPressed(KEY_A)) return DISP_QUIT; - } - if (IsKeyPressed(KEY_Q)) - { - return DISP_QUIT; - } if (IsKeyPressed(KEY_R) || IsKeyPressed(KEY_G)) - { return DISP_RELOAD; + if (IsKeyPressed(KEY_BACKSPACE)) + { + current_input = current_input / 10; + redraw(); } - /* EndDrawing(); */ + check_digits(); } - return INT_MAX; + + return DISP_QUIT; } void |