From 2284646db66fc117246225bd30298ddbef8ed13a Mon Sep 17 00:00:00 2001 From: physick Date: Sun, 6 Sep 2026 16:10:24 +0500 Subject: win condition --- src/scenes/puzzleScene.c | 35 ++++++++++++++++++++++++++++++----- src/tests/runner.c | 14 +++++++++++++- src/tests/test.h | 3 +++ src/tests/ui_elements.c | 35 +++++++++++++++++++++++++++++++++++ src/ui_elements/UI.c | 8 ++++++++ src/ui_elements/UI.h | 5 +++++ 6 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 src/tests/ui_elements.c (limited to 'src') diff --git a/src/scenes/puzzleScene.c b/src/scenes/puzzleScene.c index 52652fe..1b11327 100644 --- a/src/scenes/puzzleScene.c +++ b/src/scenes/puzzleScene.c @@ -16,6 +16,8 @@ typedef struct { // Miliseconds elapsed float time_acc; float blackout_state; + // Set when the solution is submitted. Defines whether the puzzle was solved when the scene is reinvoked + enum PUZZLE_TEST_RESULT result; PopUpState state; } puzzleSceneData; @@ -39,19 +41,26 @@ void DrawControls(Scene *self, Rectangle rect) { .height = rect.height - BORDER * 4, }; - int acc = panel.width - panel.height + BORDER * 4; + int acc = panel.width + BORDER * 4; if (data->submitButton != NULL) { + acc -= (panel.height + PADDING); Rectangle submit_trg = { .x = acc, .y = panel.y, .width = panel.height, .height = panel.height, }; - // Hacky. Should find a way to update in update() DrawButton(data->submitButton, &submit_trg); - acc -= (panel.height + PADDING); } + + // Drawing a remaining time + char timer_buf[32]; + PrintTime(timer_buf, data->distraction_timer); + Vector2 timer_size = MeasureTextEx(*GetMainFont(), timer_buf, panel.height, 1); + acc -= (timer_size.x) + PADDING; + // DrawRectangle(acc, panel.y, timer_size.x, timer_size.y, COLORSCHEME_GREEN); + DrawTextEx(*GetMainFont(), timer_buf, (Vector2) { .x = acc, .y = panel.y + 2 }, panel.height, 1, COLORSCHEME_BROWN); } Rectangle GetBodyRect() { @@ -65,6 +74,16 @@ Rectangle GetBodyRect() { return outp; } +void reopenPuzzleScene(void *self, PopUpState PreviousSceneState) { + if (self == NULL) return; + puzzleSceneData *data = ((Scene *)self)->data; + if (data == NULL) return; + if (data->result == RESULT_OK) { + TraceLog(LOG_INFO, "YIPPEE!!! (puzzle accepted)"); + DeleteSceneFromStack(); + } +} + bool drawPuzzleScene(void *self) { puzzleSceneData *data = ((Scene *)self)->data; DrawBackground(); @@ -110,11 +129,17 @@ bool drawPuzzleScene(void *self) { void SubmitPuzzle(Scene *self) { puzzleSceneData *data = ((Scene *)self)->data; + if (data == NULL) { + TraceLog(LOG_ERROR, "Failed to submit a puzzle: the data is NULL"); + return; + } char *script = GetText(data->EditorInfo); SubmitionResult *res = SubmitSolution(data->PuzzlePtr, script); - if (res != NULL) + if (res != NULL) { + data->result = res->result; AddScene(CreateResultView(res)); + } } void AddTime(Scene *self, size_t additionalTime) { @@ -178,7 +203,7 @@ Scene *CreatePuzzleScene(Puzzle *puzzle) { outp->Deint = deinitPuzzleScene; outp->IsTransparent = false; - outp->Reopen = NULL; + outp->Reopen = reopenPuzzleScene; outp->GetCurrentState = NULL; puzzleSceneData *data = malloc(sizeof(puzzleSceneData)); diff --git a/src/tests/runner.c b/src/tests/runner.c index bdc4031..58861da 100644 --- a/src/tests/runner.c +++ b/src/tests/runner.c @@ -1,10 +1,13 @@ #include "test.h" #include +#include +#include const UnitTest TESTS[] = { (UnitTest) { .Name = "Arena out of mem", .Function = ArenaOutOfMemory }, (UnitTest) { .Name = "Arena allocation", .Function = ArenaAllocation }, - (UnitTest) { .Name = "Test difficulty calculation", .Function = difficultyCalculator}, + (UnitTest) { .Name = "Test difficulty calculation", .Function = difficultyCalculator }, + (UnitTest) { .Name = "Timer display", .Function = timerPrintDisplay }, }; void SetGreenColor() { @@ -36,3 +39,12 @@ int main(void) { ResetColor(); return 0; } + +char *reportError(const char *format, ...) { + char outp[16656]; + va_list argptr; + va_start(argptr, format); + vsprintf(outp, format, argptr); + va_end(argptr); + return strdup(outp); +} diff --git a/src/tests/test.h b/src/tests/test.h index 5994ecb..456c9e2 100644 --- a/src/tests/test.h +++ b/src/tests/test.h @@ -8,10 +8,13 @@ typedef struct { char *(*Function)(); } UnitTest; +char *reportError(const char *format, ...); + // ========== TESTS ========== char *ArenaAllocation(); char *ArenaOutOfMemory(); char *difficultyCalculator(); +char *timerPrintDisplay(); #endif // TESTS_H diff --git a/src/tests/ui_elements.c b/src/tests/ui_elements.c new file mode 100644 index 0000000..a13492d --- /dev/null +++ b/src/tests/ui_elements.c @@ -0,0 +1,35 @@ +#include "../ui_elements/UI.h" +#include "test.h" +#include +#include +#include + +char *timerPrintDisplay() { + char buf[16]; + + size_t time = 0; + PrintTime(buf, time); + if (strcmp(buf, "00:00")) return reportError("The 00:00 test failed: got: '%s'", buf); + + time = 3; + PrintTime(buf, time); + if (strcmp(buf, "00:03")) return reportError("The 00:03 test failed: got: '%s'", buf); + + time = 30; + PrintTime(buf, time); + if (strcmp(buf, "00:30")) return reportError("The 00:30 test failed: got: '%s'", buf); + + time = 60; + PrintTime(buf, time); + if (strcmp(buf, "01:00")) return reportError("The 01:00 test failed: got: '%s'", buf); + + time = 63; + PrintTime(buf, time); + if (strcmp(buf, "01:03")) return reportError("The 01:03 test failed: got: '%s'", buf); + + time = 60 * 100; + PrintTime(buf, time); + if (strcmp(buf, "100:00")) return reportError("The 100:00 test failed: got: '%s'", buf); + + return NULL; +} diff --git a/src/ui_elements/UI.c b/src/ui_elements/UI.c index ea3463a..f9b7527 100644 --- a/src/ui_elements/UI.c +++ b/src/ui_elements/UI.c @@ -139,6 +139,7 @@ void GetPanesSplit(Rectangle *pane1, Rectangle *pane2, Rectangle body, float pro pane2->width = (body.width - PADDING) * (1 - proportion_h); } +// !! Not used anywhere at this moment void DrawPageView(int y, unsigned int pages, unsigned int current_page) { if (pages == 0) return; float width = 2 * PAGE_DOTS_PADDING @@ -168,3 +169,10 @@ void DrawPageView(int y, unsigned int pages, unsigned int current_page) { x_pos += PAGE_DOTS_SPACING + PAGE_DOTS_RADIUS * 2; } } + +void PrintTime(char *trg, size_t seconds) { + if (trg == NULL) return; + size_t disp_minutes = seconds / 60; + size_t disp_seconds = seconds % 60; + sprintf(trg, "%02zu:%02zu", disp_minutes, disp_seconds); +} diff --git a/src/ui_elements/UI.h b/src/ui_elements/UI.h index e48804e..356c2f4 100644 --- a/src/ui_elements/UI.h +++ b/src/ui_elements/UI.h @@ -50,6 +50,7 @@ static const int PAGE_DOTS_RADIUS = 3; static const char PAGE_DOTS_TRANSPARENCY = (char)192; /// Draws the little dots that represent the page you are on +// !! Not used anywhere at this moment void DrawPageView(int y, unsigned int pages, unsigned int current_page); // ========== TEXT EDITOR ========== @@ -104,4 +105,8 @@ void FreeTextArea(TextArea *ta); /// and vertical orientation void GetPanesSplit(Rectangle *pane1, Rectangle *pane2, Rectangle body, float proportion_h, float proportion_v); +/// Prints the time in the format MM:SS to the trg. +/// It is expected that size of outp buffer is at least 16 bytes +void PrintTime(char *trg, size_t seconds); + #endif // UI_ELEMENTS_H -- cgit v1.3