Browse Source

Add draw functions to display text

Godzil 3 years ago
parent
commit
b8a98b1b16
3 changed files with 55 additions and 1 deletions
  1. 2 1
      source/CMakeLists.txt
  2. 51 0
      source/display.c
  3. 2 0
      source/include/display.h

+ 2 - 1
source/CMakeLists.txt

@@ -2,8 +2,9 @@ cmake_minimum_required(VERSION 3.1)
 
 find_package(SDL2 REQUIRED)
 find_package(SDL2_image REQUIRED)
+find_package(SDL2_ttf REQUIRED)
 
-set(SDL2_LIBS ${SDL2_LIBRARIES} ${SDL2IMAGE_LIBRARY})
+set(SDL2_LIBS ${SDL2_LIBRARIES} ${SDL2IMAGE_LIBRARY} ${SDL2_TTF_LIBRARY})
 set(SDL2_INCDIR ${SDL2_INCLUDE_DIR} ${SDL2_IMAGE_INCLUDE_DIR})
 
 file(GLOB_RECURSE TDE_SOURCES *.c)

+ 51 - 0
source/display.c

@@ -11,6 +11,7 @@
 #include <stdbool.h>
 
 #include <SDL.h>
+#include <SDL_ttf.h>
 
 #include <log.h>
 #include <display.h>
@@ -26,6 +27,8 @@ SDL_Texture *frameBufferTexture = NULL;
 int32_t windowWidth = 1024;
 int32_t windowHeight = 768;
 
+static TTF_Font *font = NULL;
+
 /***********************************************************************************************************************
  * Functions
  **********************************************************************************************************************/
@@ -45,6 +48,13 @@ bool initialiseWindow(bool fullScreen)
 
     Log(TLOG_DEBUG, NULL, "SDL properly initialised!");
 
+    ret = TTF_Init();
+    if (ret)
+    {
+        Log(TLOG_PANIC, NULL, "SDL_ttf Initialisation error!! error: %s", SDL_GetError());
+        goto exit;
+    }
+
     if (fullScreen)
     {
         Log(TLOG_DEBUG, NULL, "Will go fullscreen! Fasten your seatbelts!");
@@ -74,6 +84,13 @@ bool initialiseWindow(bool fullScreen)
     }
     Log(TLOG_DEBUG, NULL, "SDL Renderer created!");
 
+    font = TTF_OpenFont("assets/pico8.ttf", 8);
+    if (font == NULL)
+    {
+        Log(TLOG_WARNING, NULL, "Cannot open the font for printing...: %s", SDL_GetError());
+        goto exit;
+    }
+
     if (fullScreen)
     {
         SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
@@ -87,6 +104,10 @@ exit:
 
 void destroyWindow()
 {
+    if (font != NULL)
+    {
+        TTF_CloseFont(font);
+    }
     if (frameBuffer != NULL)
     {
         free(frameBuffer);
@@ -102,6 +123,36 @@ void renderFrameBuffer()
     SDL_RenderCopy(renderer, frameBufferTexture, NULL, NULL);
 }
 
+void drawText(int32_t x, int32_t y, colour_t colour, const char *format, ...)
+{
+    va_list va;
+    uint8_t r = ((colour >> 16) & 0xFF);
+    uint8_t g = ((colour >>  8) & 0xFF);
+    uint8_t b = ((colour      ) & 0xFF);
+    SDL_Color c = { r, g, b , 255};
+    char buffer[512];
+
+    va_start(va, format);
+    vsnprintf(buffer, 512, format, va);
+    va_end(va);
+
+    SDL_Surface *surface = TTF_RenderText_Blended(font, buffer, c);
+    SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
+
+    int labelWidth, labelHeight;
+
+    SDL_QueryTexture(texture, NULL, NULL, &labelWidth, &labelHeight);
+
+    SDL_Rect destRect =
+    {
+        x, y, labelWidth, labelHeight
+    };
+    SDL_RenderCopyEx(renderer, texture, NULL, &destRect,0, NULL, SDL_FLIP_NONE);
+
+    SDL_FreeSurface(surface);
+    SDL_DestroyTexture(texture);
+}
+
 void drawPixel(int32_t x, int32_t y, colour_t colour)
 {
     if ((x >= 0) && (x < windowWidth) && (y >= 0) && (y < windowHeight))

+ 2 - 0
source/include/display.h

@@ -54,6 +54,8 @@ void drawVLine(int32_t x, int32_t y0, int32_t y1, colour_t colour);
 void drawHLine(int32_t x0, int32_t y, int32_t x1, colour_t colour);
 void drawLine(int32_t x0, int32_t y0, int32_t x1, int32_t y1, colour_t colour);
 
+void drawText(int32_t x, int32_t y, colour_t colour, const char *format, ...);
+
 static inline void intSwap(int *a, int *b)
 {
     int tmp = *a;