From 7dda54e577fdfb4b68b4d162d06952b03876083e Mon Sep 17 00:00:00 2001
From: Tristan Riehs <tristan.riehs@bordeaux-inp.fr>
Date: Sat, 22 Jun 2024 18:18:26 +0900
Subject: Implement saving state

---
 src/ray.c | 53 ++++++++++++++++++++++++++++++++---------------------
 1 file changed, 32 insertions(+), 21 deletions(-)

diff --git a/src/ray.c b/src/ray.c
index 8fd5fbe..e0c0833 100644
--- a/src/ray.c
+++ b/src/ray.c
@@ -28,9 +28,13 @@
 
 #define MAX_FONTSIZE 100
 
-static int current_input = 0;
-static int current_x;
-static int current_y;
+struct state {
+	int input;
+	int x;
+	int y;
+};
+
+static struct state *current_state = NULL;
 
 void
 init(void)
@@ -57,6 +61,8 @@ window\n");
 	ClearBackground(RAYWHITE);
 	EndDrawing();
 
+	current_state = calloc(1, sizeof(*current_state));
+
 	TraceLog(LOG_INFO, "window successfully initialized");
 }
 
@@ -77,7 +83,7 @@ draw_calc(void)
 	int txt_width;
 	int font_size;
 
-	sprintf(txt, "%d + %d", current_x, current_y);
+	sprintf(txt, "%d + %d", current_state->x, current_state->y);
 
 	win_width = GetScreenWidth();
 	win_height = GetScreenHeight();
@@ -90,24 +96,27 @@ draw_calc(void)
 	TraceLog(LOG_DEBUG, "txt_width = %d", txt_width);
 	TraceLog(LOG_DEBUG, "font_size = %d", font_size);
 
-	if (txt_width > win_width)
+	int max_txt_width = win_width*3/4/2;
+
+	if (txt_width > max_txt_width)
 	{
-		float scale = ((float) txt_width)/win_width;
+		float scale = ((float) txt_width)/max_txt_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);
+	DrawText(txt, (win_width/4)-(txt_width/2),
+		 win_height/6, font_size, GRAY);
 
 	TraceLog(LOG_INFO, "calulation %d + %d successfully drawn",
-		 current_x, current_y);
+		 current_state->x, current_state->y);
 }
 
 void
 display_calc(int x, int y)
 {
-	current_x = x;
-	current_y = y;
+	current_state->x = x;
+	current_state->y = y;
 	BeginDrawing();
 	ClearBackground(RAYWHITE);
 	draw_calc();
@@ -122,7 +131,7 @@ draw_input(void)
 	int txt_width;
 	int font_size;
 
-	sprintf(txt, "%d", current_input);
+	sprintf(txt, "%d", current_state->input);
 
 	win_width = GetScreenWidth();
 	win_height = GetScreenHeight();
@@ -137,7 +146,8 @@ draw_input(void)
 		txt_width = MeasureText(txt, font_size);
 	}
 
-	DrawText(txt, (win_width-txt_width)/2, win_height*4/6, font_size, DARKGRAY);
+	DrawText(txt, (win_width/4)-(txt_width/2),
+		 win_height*4/6, font_size, DARKGRAY);
 
 	TraceLog(LOG_INFO, "input successfully drawn");
 }
@@ -155,8 +165,8 @@ redraw(void)
 static void
 add_input_digit(int digit)
 {
-	current_input *= 10;
-	current_input += digit;
+	current_state->input *= 10;
+	current_state->input += digit;
 	redraw();
 }
 
@@ -167,7 +177,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_input);
+		TraceLog(LOG_INFO, "input is now %d", current_state->input);
 	}
 }
 
@@ -182,7 +192,7 @@ int
 read_input(void)
 {
 	TraceLog(LOG_INFO, "waiting for user input");
-	current_input = 0;
+	current_state->input = 0;
 
 	while (!WindowShouldClose())
 	{
@@ -191,14 +201,14 @@ read_input(void)
 		if (IsWindowResized())
 			redraw();
 		if (IsKeyPressed(KEY_ENTER))
-			return current_input;
+			return 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_input = current_input / 10;
+			current_state->input = current_state->input / 10;
 			redraw();
 		}
 		check_digits();
@@ -210,12 +220,13 @@ read_input(void)
 void
 display_res(bool, int)
 {
-
+	/* TODO: display results */
 }
 
 void
 destroy(void)
 {
+	free(current_state);
 	CloseWindow();
 }
 
@@ -224,13 +235,13 @@ void *
 pre_reload(void)
 {
 	TraceLog(LOG_INFO, "pre-reload actions done");
-	return NULL;
+	return current_state;
 }
 
 void
 post_reload(void *state)
 {
-	(void) state;
+	current_state = state;
 	TraceLog(LOG_INFO, "post-reload actions done");
 }
 #endif
-- 
cgit v1.2.3