Created
August 11, 2018 14:54
-
-
Save thebirk/0b321c3ed4d58de93b7cfdcd4b1cd9d7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SDL2/SDL_image.h> | |
SDL_Texture *fontTexture = 0; | |
const char *layout = | |
"ABCDEFGHIJKLMNOP" | |
"QRSTUVWXYZ!?. " | |
"1234567890" | |
; | |
void initText() { | |
IMG_Init(IMG_INIT_PNG); | |
fontTexture = IMG_LoadTexture(renderer, "./font.png"); | |
} | |
int indexOfChar(int c) { | |
for(int i = 0; i < strlen(layout); i++) { | |
if(layout[i] == c) return i; | |
} | |
return -1; | |
} | |
void drawText(int x, int y, Color color, const char *fmt, ...) { | |
char buffer[4096]; | |
va_list args; | |
va_start(args, fmt); | |
vsnprintf(buffer, 4096, fmt, args); | |
va_end(args); | |
int len = strlen(buffer); | |
int scale = 4; | |
for(int i = 0; i < len; i++) { | |
char c = buffer[i]; | |
int index = indexOfChar(c); | |
if(index < 0) { | |
printf("Invalid char! Skipping!\n"); | |
continue; | |
} | |
int tx = (index % 16) * 8; | |
int ty = (index / 16) * 8; | |
SDL_Rect src = {tx, ty, 8, 8}; | |
SDL_Rect dst = {x, y, 8*scale, 8*scale}; | |
SDL_SetTextureColorMod(fontTexture, color.r, color.g, color.b); | |
SDL_RenderCopy(renderer, fontTexture, &src, &dst); | |
x += 8*scale; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment