From f1ff0606e7dccd45f1d85688e20d7bcdd140330b Mon Sep 17 00:00:00 2001 From: 0xmac Date: Thu, 23 Jan 2025 11:29:49 +0100 Subject: [PATCH] Added Drawstring function --- src/sdlinit.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/sdlinit.c b/src/sdlinit.c index 8919d25..c56a872 100644 --- a/src/sdlinit.c +++ b/src/sdlinit.c @@ -1,5 +1,6 @@ #include #include "config.h" +#include "font.h" static SDL_Window *window; static SDL_Renderer *renderer; @@ -44,3 +45,30 @@ void initWindow(){ void clearScreen(uint32_t pixels[], uint32_t color){ for (uint32_t i = 0; i < SDL_X_SIZE * SDL_Y_SIZE; i++) pixels[i] = color; } + +#define CHARMARGIN 0 +void drawString(const char* string, uint32_t pixels[], uint16_t screenXsize, uint32_t foreground, uint32_t background, uint16_t x, uint16_t y, uint8_t scale){ + uint16_t xCount = x; + uint16_t yCount = y; + + for (int i = 0; string[i]; i++){ + if (string[i] > 31 && string[i] < 127){ + for (int _x = 0; _x < FONT_WIDTH * scale; _x++){ + if (xCount + _x >= screenXsize){ + xCount = x; + yCount += (FONT_HEIGHT + CHARMARGIN) * scale; + } + + for (int _y = 0; _y < FONT_HEIGHT * scale; _y++){ + uint8_t value = typeface[((string[i] - 32) * FONT_HEIGHT) + (_y / scale)] & (128 >> (_x / scale)); + if (value && foreground != 0) pixels[(( yCount + _y) * screenXsize) + (xCount + _x)] = foreground; + else if (!value && background != 0) pixels[(( yCount + _y) * screenXsize) + (xCount + _x)] = background; + } + } + xCount += (FONT_WIDTH + CHARMARGIN) * scale; + } else if (string[i] == '\n'){ + xCount = x; + yCount += (FONT_HEIGHT + CHARMARGIN) * scale; + } + } +}