Skip to content

Commit

Permalink
fixed some problems with ttf loading and drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
PaoloMazzon committed Jan 1, 2024
1 parent 769266f commit 14df254
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.14)
project(Astro)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O3 -ffast-math -funroll-loops")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -ffast-math -funroll-loops")

find_package(Vulkan)
find_package(SDL2 REQUIRED)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,6 @@ In no particular order,
+ Quadtree-based collisions
+ Load hitboxes from json
+ External asset managing program to make jsons automatically
+ Better TTF support
+ Per-character kerning for ttf support
+ Spatial audio
+ Move `Util.wren` to C side for optimization purposes
42 changes: 39 additions & 3 deletions examples/testing/data/assets.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,47 @@
{
"sprites": [

],
"fonts": [

{
"file": "merriweather.ttf",
"name": "merriweather48",
"ustart": 32,
"uend": 128,
"size": 48
},
{
"file": "merriweather.ttf",
"name": "merriweather32",
"ustart": 32,
"uend": 128,
"size": 32
},
{
"file": "merriweather.ttf",
"name": "merriweather24",
"ustart": 32,
"uend": 128,
"size": 24
},
{
"file": "merriweather.ttf",
"name": "merriweather16",
"ustart": 32,
"uend": 128,
"size": 16
}
],
"bitmap_fonts": [

],
"buffers": [

],
"strings": [

],
"exclude": [
]
}
6 changes: 4 additions & 2 deletions examples/testing/data/game/Game.wren
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ class Game is Level {

update() {
//super.update()

Renderer.draw_font(null, Engine.fps.toString, 0, 0)

var mouse_pos = Mouse.position(null)
var dir = Math.point_angle(250, 250, mouse_pos[0], mouse_pos[1])
Expand All @@ -87,6 +85,10 @@ class Game is Level {
Renderer.draw_circle_outline(mouse_pos[0], mouse_pos[1], 5, 1)
Renderer.draw_rectangle_outline(250 - 50, 250 - 50, 100, 100, dir, 50, 50, 1)
Renderer.draw_circle_outline(250 + Math.cast_x(dist * Math.serp(Engine.time / 2, 0, 1), dir), 250 + Math.cast_y(dist * Math.serp(Engine.time / 2, 0, 1), dir), 5, 1)
Renderer.draw_font(Assets.fnt_merriweather48, "The quick brown fox jumps over the lazy dog.", 0, 0)
Renderer.draw_font(Assets.fnt_merriweather32, "The quick brown fox jumps over the lazy dog.", 0, 50)
Renderer.draw_font(Assets.fnt_merriweather24, "The quick brown fox jumps over the lazy dog.", 0, 50 + 34)
Renderer.draw_font(Assets.fnt_merriweather16, "The quick brown fox jumps over the lazy dog.", 0, 50 + 34 + 26)
}

destroy() {
Expand Down
Binary file added examples/testing/data/merriweather.ttf
Binary file not shown.
21 changes: 18 additions & 3 deletions src/InternalBindings.c
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,20 @@ void vksk_RuntimeFontAllocate(WrenVM *vm) {
font->bitmapFont->unicodeEnd = uniEnd;
float spaceSize = font->bitmapFont->newLineHeight / 2;

// Calculate space size
float average = 0;
int count = 0;
for (int i = 0; i <= (uniEnd - uniStart); i++) {
int codePoint = i + uniStart;
int x0, y0, x1, y1;
stbtt_GetCodepointBox(&info, codePoint, &x0, &y0, &x1, &y1);
if (stbtt_IsGlyphEmpty(&info, stbtt_FindGlyphIndex(&info, codePoint)) == 0) {
average += (x1 * scale) - (x0 * scale);
count++;
}
}
spaceSize = (average / count) * 0.5;

// Calculate the width and height of the image we'll need
font->bitmapFont->characters = calloc(uniEnd - uniStart + 1, sizeof(struct JUCharacter));
float w = 0;
Expand All @@ -1052,9 +1066,9 @@ void vksk_RuntimeFontAllocate(WrenVM *vm) {
int x0, y0, x1, y1;
JUCharacter *c = &font->bitmapFont->characters[i];
stbtt_GetCodepointBox(&info, codePoint, &x0, &y0, &x1, &y1);
if (x1 != 0) {
if (stbtt_IsGlyphEmpty(&info, stbtt_FindGlyphIndex(&info, codePoint)) == 0) {
c->w = (x1 * scale) - (x0 * scale);
c->h = (y1 * scale) - (y0 * scale);
c->h = (y1 * scale) - (y0 * scale) + 2;
c->x = w;
c->drawn = true;
} else {
Expand Down Expand Up @@ -1090,7 +1104,8 @@ void vksk_RuntimeFontAllocate(WrenVM *vm) {
VALIDATE_SDL(temp)
c->ykern = ((-(float) ascent * scale) + yoff) + ((ascent * scale) * 2);
SDL_Rect dst = {c->x, c->y, width, height};
SDL_BlitSurface(temp, NULL, bitmap, &dst);
if (SDL_BlitSurface(temp, NULL, bitmap, &dst) < 0)
vksk_Log("Failed to copy character u%i", codePoint);
SDL_FreeSurface(temp);
free(pixels);
}
Expand Down

0 comments on commit 14df254

Please sign in to comment.