Basic font atlas loading

This commit is contained in:
Casey 2022-10-13 21:13:44 +03:00
parent adac248f65
commit 72ff206acd
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
3 changed files with 42 additions and 3 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
obj/* obj/*
raylib-ascii-render raylib-ascii-render
unscii-16-full.ttf

View File

@ -1,10 +1,13 @@
CFLAGS += CFLAGS +=
LDFLAGS := -lm LDFLAGS := -lraylib
OBJECTS := OBJECTS :=
raylib-ascii-render: lib raylib-ascii-render: lib
$(CC) $(CFLAGS) $(OBJECTS) src/main.c $(LDFLAGS) -o raylib-ascii-render $(CC) $(CFLAGS) $(OBJECTS) src/main.c $(LDFLAGS) -o raylib-ascii-render
run: raylib-ascii-render
./raylib-ascii-render
all: raylib-ascii-render all: raylib-ascii-render
lib: $(OBJECTS) lib: $(OBJECTS)

View File

@ -1,6 +1,41 @@
// x-run: make -C.. run
#include <raylib.h>
#include <stdio.h> #include <stdio.h>
#include <math.h>
int main(void) { int main(void) {
printf("Hi!\n"); InitWindow(320, 240, "Loading...");
SetTargetFPS(30);
int font_size = 16, chars_x = 80, chars_y = 24;
Font font = LoadFontEx("unscii-16-full.ttf", font_size, NULL, 256);
Vector2 glyphSize = MeasureTextEx(font, "A", font_size, 0);
Image img_atlas = GenImageColor(glyphSize.x * 16, glyphSize.y * 8, BLACK);
for (int sym = ' '; sym < '\x7f'; sym++) {
Vector2 pos = { (sym & 0xf) * glyphSize.x, (sym >> 4) * glyphSize.y };
ImageDrawTextEx(&img_atlas, font, TextFormat("%c", sym), pos, font_size, 0, WHITE);
GlyphInfo glyph = font.glyphs[GetGlyphIndex(font, sym)];
ImageDraw(&img_atlas, glyph.image, (Rectangle){
0, 0,
glyph.image.width, glyph.image.height
}, (Rectangle){
pos.x + glyph.offsetX, pos.y + glyph.offsetY,
glyph.image.width, glyph.image.height
}, WHITE);
}
Texture2D tex_atlas = LoadTextureFromImage(img_atlas);
SetWindowSize(chars_x * glyphSize.x, chars_y * glyphSize.y);
for (int frame = 0; !WindowShouldClose(); frame++) {
BeginDrawing();
ClearBackground(BLACK);
DrawTexture(tex_atlas, 0, 0, WHITE);
EndDrawing();
}
UnloadFont(font);
UnloadImage(img_atlas);
UnloadTexture(tex_atlas);
} }