diff options
| -rw-r--r-- | src/resources/resources.c | 24 | ||||
| -rw-r--r-- | src/resources/resources.h | 2 | ||||
| -rw-r--r-- | src/scenes/puzzleScene.c | 6 | ||||
| -rw-r--r-- | src/ui_elements/UI.c | 49 | ||||
| -rw-r--r-- | src/ui_elements/UI.h | 3 |
5 files changed, 80 insertions, 4 deletions
diff --git a/src/resources/resources.c b/src/resources/resources.c index 031ee25..b8a292e 100644 --- a/src/resources/resources.c +++ b/src/resources/resources.c @@ -9,3 +9,27 @@ void LoadResources() { } Font *GetMainFont() { return &MainFont; } + +void DrawStripe(int start_step, int end_step, Color color) { + int height = GetScreenHeight(); + Vector2 p1 = { .x = start_step, .y = 0 }, + p2 = { .x = end_step, .y = height }, + p3 = { .x = start_step, .y = height }; + DrawTriangle(p1, p2, p3, color); + p2 = p3; + p3 = p1; + p3.x += (start_step - end_step); + DrawTriangle(p1, p2, p3, color); +} + +void DrawBackground() { + ClearBackground(COLORSCHEME_WHITE); + double step = GetScreenWidth() / 6.0f; + DrawStripe(step * 2, step, COLORSCHEME_YELLOW); + DrawStripe(step * 3 - 1, step * 2 - 1, COLORSCHEME_ORANGE); + DrawStripe(step * 4 - 2, step * 3 - 2, COLORSCHEME_RED); + DrawStripe(step * 5 - 3, step * 4 - 3, COLORSCHEME_BROWN); + DrawRectangle(step * 5 - 3, 0, + step * 2, GetScreenHeight(), + COLORSCHEME_BROWN); +} diff --git a/src/resources/resources.h b/src/resources/resources.h index 5cc24ff..c53da99 100644 --- a/src/resources/resources.h +++ b/src/resources/resources.h @@ -3,6 +3,8 @@ #ifndef RESOURCES_H #define RESOURCES_H +void DrawBackground(); + /// #670916 as RGBA static const Color COLORSCHEME_BROWN = { 103, 9, 22, 255 }; /// #CF042C as RGBA diff --git a/src/scenes/puzzleScene.c b/src/scenes/puzzleScene.c index 0377c03..1706ff2 100644 --- a/src/scenes/puzzleScene.c +++ b/src/scenes/puzzleScene.c @@ -65,7 +65,7 @@ void GetPanesSplit(Rectangle *pane1, Rectangle *pane2, Rectangle body, float pro bool drawPuzzleScene(void *self) { puzzleSceneData *data = ((Scene *)self)->data; - ClearBackground(COLORSCHEME_WHITE); + DrawBackground(); DrawPuzzleName(data->PuzzlePtr->Name); Rectangle body = GetBodyRect(); @@ -73,9 +73,9 @@ bool drawPuzzleScene(void *self) { GetPanesSplit(&pane1, &pane2, body, 0.5, 0.4); DrawRecBordered(pane1, WHITE, COLORSCHEME_ORANGE, BORDER); DrawRecBordered(pane2, WHITE, COLORSCHEME_ORANGE, BORDER); - - // DrawRecBordered(body, COLORSCHEME_WHITE, COLORSCHEME_ORANGE, BORDER); + DrawTextInRec(pane1, data->PuzzlePtr->Description, + GetMainFont(), 20, COLORSCHEME_BROWN); return true; } diff --git a/src/ui_elements/UI.c b/src/ui_elements/UI.c index fb469e3..7e00aa3 100644 --- a/src/ui_elements/UI.c +++ b/src/ui_elements/UI.c @@ -1,7 +1,56 @@ #include "UI.h" #include <raylib.h> +const int MAX_LINES = 256; +const int SPACING_H = 1; + void DrawRecBordered(Rectangle rec, Color bg, Color border_color, int border_width) { DrawRectangleRounded(rec, 0.015f, 8, bg); DrawRectangleRoundedLinesEx(rec, 0.015f, 8, border_width, border_color); } + +float MeasureChar(Font *font, int height, char symbol) { + char buf[2] = { symbol, 0 }; + return MeasureTextEx(*font, buf, height, SPACING_H).x; +} + +int CalculateSpans(Rectangle rec, char *text, Font *font, int height, int *spans) { + spans[0] = 0; + int ptr = 0, span_ptr = 1, last_space_ptr; + float acc = 0; + while (text[ptr] != 0x0) { + float char_width = MeasureChar(font, height, text[ptr]); + acc += char_width + SPACING_H; + if (text[ptr] == ' ') last_space_ptr = ptr; + if (acc > rec.width) { + spans[span_ptr] = last_space_ptr + 1; + ptr = last_space_ptr; + span_ptr++; + acc = 0; + } + ptr++; + } + return span_ptr + 1; +} + +void DrawTextInRec(Rectangle rec, char *text, Font *font, int height, Color color) { + int spans[MAX_LINES]; + int lines = CalculateSpans(rec, text, font, height, spans); + + if (lines == 2) { + Vector2 pos = {.x = rec.x, .y = rec.y}; + DrawTextEx(*font, text, pos, height, SPACING_H, color); + return; + } + + int vertical_acc = rec.y; + for (int i = 1; i < lines; i++) { + Vector2 pos = { .x = rec.x, .y = vertical_acc }; + char *line_start = text + spans[i-1]; + int line_length = spans[i] - spans[i-1]; + DrawTextEx(*font, + TextFormat("%.*s", line_length, line_start), + pos, height, SPACING_H, color); + vertical_acc += height; + } +} diff --git a/src/ui_elements/UI.h b/src/ui_elements/UI.h index 4e70482..b98ece0 100644 --- a/src/ui_elements/UI.h +++ b/src/ui_elements/UI.h @@ -3,6 +3,7 @@ #ifndef UI_ELEMENTS_H #define UI_ELEMENTS_H -void DrawRecBordered(Rectangle rec, Color bg, Color border, int border_width); +void DrawRecBordered(Rectangle rec, Color bg, Color border_color, int border_width); +void DrawTextInRec(Rectangle rec, char *text, Font *font, int height, Color color); #endif // UI_ELEMENTS_H |
