소스 검색

added custom fonts

quantumde1 9 달 전
부모
커밋
8e7372dd1f
3개의 변경된 파일과 69개의 추가작업 그리고 1개의 파일을 삭제
  1. 3 0
      bindings/dlang/source/aperture.d
  2. 4 1
      include/fonts.h
  3. 62 0
      source/gl/fonts.c

+ 3 - 0
bindings/dlang/source/aperture.d

@@ -176,5 +176,8 @@ extern (C) @nogc {
         int IsKeyPressed(int key);
         int IsKeyDown(int key);
         void UpdateInput();
+        void DrawTextWithFont(char* text, int x, int y, float fontSize, Color color = Color(255, 255, 255, 255));
+        void LoadFont(char* pathToFolder);
+        void UnloadFont();
     }
 }

+ 4 - 1
include/fonts.h

@@ -4,4 +4,7 @@
 
 extern const char font6x8_ascii[128][6];
 
-void DrawText(const char* text, float x, float y, float scale, Color color);
+void DrawText(const char* text, float x, float y, float scale, Color color);
+void DrawTextWithFont(unsigned char* text, int x, int y, float fontSize, Color color);
+void LoadFont(char* pathToFolder);
+void UnloadFont();

+ 62 - 0
source/gl/fonts.c

@@ -7,6 +7,10 @@
 #include "GL/gl.h"
 #endif
 
+#include "stdio.h"
+#include "dirent.h"
+#include "string.h"
+
 #include "../../include/base.h"
 
 const unsigned char font6x8_ascii[128][6] = {
@@ -191,4 +195,62 @@ void DrawText(const char* text, float x, float y, float scale, Color color) {
     }
     
     glPopMatrix();
+}
+
+#include "../../include/textures.h"
+
+Texture fontTextures[256];
+
+void LoadFont(char* pathToFolder) {
+    DIR *fontFolder;
+    struct dirent *entry;
+    printf("%s\n", "loading font");
+    
+    fontFolder = opendir(pathToFolder);
+    if (fontFolder == NULL) {
+        perror("directory cant be opened");
+        return;
+    }
+    
+    int currentSymbol = 0;
+    
+    while ((entry = readdir(fontFolder)) != NULL) {
+        if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
+            continue;
+        }
+        
+        char fullPath[512];
+        
+        strcpy(fullPath, pathToFolder);
+        
+        if (fullPath[strlen(fullPath)-1] != '/') {
+            strcat(fullPath, "/");
+        }
+        strcat(fullPath, entry->d_name);
+        
+        printf("%s\n", fullPath);
+        fontTextures[currentSymbol] = LoadTexture(fullPath);
+        currentSymbol++;
+    }
+    
+    closedir(fontFolder);
+    return;
+}
+
+void DrawTextWithFont(unsigned char* text, int x, int y, float fontSize, Color color) {
+    unsigned int currentSymbol = 0;
+    unsigned int currentX = x;
+    while (text[currentSymbol] != '\0') {
+        DrawTexture(fontTextures[text[currentSymbol]], currentX, y, fontSize, color);
+        currentX = currentX + fontTextures[text[currentSymbol]].width*fontSize;
+        currentSymbol++;
+    }
+}
+
+void UnloadFont() {
+    int currentTexture = 0;
+    while (currentTexture < 256) {
+        UnloadTexture(fontTextures[currentTexture]);
+        currentTexture++;
+    }
 }