aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTristan Riehs <tristan.riehs@bordeaux-inp.fr>2024-06-22 19:32:23 +0900
committerTristan Riehs <tristan.riehs@bordeaux-inp.fr>2024-06-22 19:32:23 +0900
commita2717d73257a8a03fc0d4840c2d0b0eda8640092 (patch)
treedb42677fbd2b1fa3526feb4982a9d540306f0072 /src
parent7dda54e577fdfb4b68b4d162d06952b03876083e (diff)
Start displaying results
New approach for saving state.
Diffstat (limited to 'src')
-rw-r--r--src/ray.c98
1 files changed, 78 insertions, 20 deletions
diff --git a/src/ray.c b/src/ray.c
index e0c0833..0e89f24 100644
--- a/src/ray.c
+++ b/src/ray.c
@@ -17,24 +17,30 @@
#include "config.h"
#include <raylib.h>
+#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <unistd.h>
#include "disp.h"
#define DEFAULT_HEIGHT 200
-#define DEFAULT_WIDTH 16*DEFAULT_HEIGHT/9
+#define DEFAULT_WIDTH (DEFAULT_HEIGHT*16/9)
#define MAX_FONTSIZE 100
struct state {
- int input;
int x;
int y;
+ int input;
+ bool right;
+ int ms;
};
-static struct state *current_state = NULL;
+#define STATE_COUNT 16
+static struct state states[STATE_COUNT] = {0};
+static int current_state = 0;
void
init(void)
@@ -61,7 +67,7 @@ window\n");
ClearBackground(RAYWHITE);
EndDrawing();
- current_state = calloc(1, sizeof(*current_state));
+ current_state = 0;
TraceLog(LOG_INFO, "window successfully initialized");
}
@@ -83,7 +89,7 @@ draw_calc(void)
int txt_width;
int font_size;
- sprintf(txt, "%d + %d", current_state->x, current_state->y);
+ sprintf(txt, "%d + %d", states[current_state].x, states[current_state].y);
win_width = GetScreenWidth();
win_height = GetScreenHeight();
@@ -109,14 +115,14 @@ draw_calc(void)
win_height/6, font_size, GRAY);
TraceLog(LOG_INFO, "calulation %d + %d successfully drawn",
- current_state->x, current_state->y);
+ states[current_state].x, states[current_state].y);
}
void
display_calc(int x, int y)
{
- current_state->x = x;
- current_state->y = y;
+ states[current_state].x = x;
+ states[current_state].y = y;
BeginDrawing();
ClearBackground(RAYWHITE);
draw_calc();
@@ -131,7 +137,7 @@ draw_input(void)
int txt_width;
int font_size;
- sprintf(txt, "%d", current_state->input);
+ sprintf(txt, "%d", states[current_state].input);
win_width = GetScreenWidth();
win_height = GetScreenHeight();
@@ -153,20 +159,64 @@ draw_input(void)
}
static void
+draw_res(void)
+{
+ char txt[64] = {0};
+ char *suffix;
+ Color col;
+ int win_width, win_height;
+ int txt_width;
+ int font_size;
+
+ win_width = GetScreenWidth();
+ win_height = GetScreenHeight();
+
+ for (int i = 0; i < STATE_COUNT; i++)
+ {
+ j = (current_state + i)%STATE_COUNT;
+
+ if (states[j].ms == 0)
+ break;
+
+ if (states[j].right)
+ {
+ suffix = "RIGHT";
+ col = GREEN;
+ }
+ else
+ {
+ suffix = "WRONG";
+ col = RED;
+ }
+
+ sprintf(txt, "%d + %d = %d (%s) %dms",
+ states[j].x, states[j].y,
+ states[j].input,
+ suffix, states[j].ms);
+
+ font_size = min(win_height/8, MAX_FONTSIZE/2);
+ txt_width = MeasureText(txt, font_size);
+ /* TODO: draw every results */
+ DrawText(txt, win_width-txt_width, 0, font_size, col);
+ }
+}
+
+static void
redraw(void)
{
BeginDrawing();
ClearBackground(RAYWHITE);
draw_calc();
draw_input();
+ draw_res();
EndDrawing();
}
static void
add_input_digit(int digit)
{
- current_state->input *= 10;
- current_state->input += digit;
+ states[current_state].input *= 10;
+ states[current_state].input += digit;
redraw();
}
@@ -177,7 +227,7 @@ check_digit(int digit)
{
add_input_digit(digit);
TraceLog(LOG_INFO, "digit %d added to input", digit);
- TraceLog(LOG_INFO, "input is now %d", current_state->input);
+ TraceLog(LOG_INFO, "input is now %d", states[current_state].input);
}
}
@@ -192,7 +242,7 @@ int
read_input(void)
{
TraceLog(LOG_INFO, "waiting for user input");
- current_state->input = 0;
+ states[current_state].input = 0;
while (!WindowShouldClose())
{
@@ -201,14 +251,15 @@ read_input(void)
if (IsWindowResized())
redraw();
if (IsKeyPressed(KEY_ENTER))
- return current_state->input;
+ return states[current_state].input;
if (IsKeyPressed(KEY_Q) || IsKeyPressed(KEY_A))
return DISP_QUIT;
if (IsKeyPressed(KEY_R) || IsKeyPressed(KEY_G))
return DISP_RELOAD;
if (IsKeyPressed(KEY_BACKSPACE))
{
- current_state->input = current_state->input / 10;
+ states[current_state].input =
+ states[current_state].input / 10;
redraw();
}
check_digits();
@@ -218,15 +269,16 @@ read_input(void)
}
void
-display_res(bool, int)
+display_res(bool right, int ms)
{
- /* TODO: display results */
+ states[current_state].right = right;
+ states[current_state].ms = ms;
+ redraw();
}
void
destroy(void)
{
- free(current_state);
CloseWindow();
}
@@ -234,14 +286,20 @@ destroy(void)
void *
pre_reload(void)
{
+ char *data = malloc(sizeof(states) + sizeof(current_state));
+ memcpy(data, &states, sizeof(states));
+ memcpy(data + sizeof(states), &current_state, sizeof(current_state));
TraceLog(LOG_INFO, "pre-reload actions done");
- return current_state;
+ return data;
}
void
post_reload(void *state)
{
- current_state = state;
+ char *data = state;
+ current_state = *(data + sizeof(states));
+ memcpy(&states, data, sizeof(states));
+ free(data);
TraceLog(LOG_INFO, "post-reload actions done");
}
#endif