From 2fe40f7624b92a85d022b1f5c9209f0ea24b5be1 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Tue, 12 Apr 2022 19:29:19 -0700 Subject: [PATCH] bugfixing --- include/Wellspring.h | 2 ++ src/Wellspring.c | 40 +++++++++++++++++++++++++++++----------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/include/Wellspring.h b/include/Wellspring.h index c12831c..c622572 100644 --- a/include/Wellspring.h +++ b/include/Wellspring.h @@ -161,6 +161,7 @@ WELLSPRINGAPI void Wellspring_DestroyPacker(Wellspring_Packer *packer); #define Wellspring_fabs SDL_fabs #define Wellspring_assert SDL_assert #define Wellspring_strlen SDL_strlen +#define Wellspring_sort SDL_qsort #else @@ -178,6 +179,7 @@ WELLSPRINGAPI void Wellspring_DestroyPacker(Wellspring_Packer *packer); #define Wellspring_fabs fabs #define Wellspring_assert assert #define Wellspring_strlen strlen +#define Wellspring_sort qsort #endif diff --git a/src/Wellspring.c b/src/Wellspring.c index 1f1d7a3..d9cf070 100644 --- a/src/Wellspring.c +++ b/src/Wellspring.c @@ -25,7 +25,6 @@ */ #include "Wellspring.h" -#include "stb_rect_pack.h" #define STBTT_malloc(x,u) ((void)(u),Wellspring_malloc(x)) #define STBTT_free(x,u) ((void)(u),Wellspring_free(x)) @@ -42,6 +41,9 @@ #define STBTT_assert Wellspring_assert #define STBTT_strlen Wellspring_strlen +#define STBRP_SORT Wellspring_sort +#define STBRP_ASSERT Wellspring_assert + typedef uint8_t stbtt_uint8; typedef int8_t stbtt_int8; typedef uint16_t stbtt_uint16; @@ -49,11 +51,18 @@ typedef int16_t stbtt_int16; typedef uint32_t stbtt_uint32; typedef int32_t stbtt_int32; +#pragma GCC diagnostic ignored "-Wunused-function" + +#define STBRP_STATIC +#define STB_RECT_PACK_IMPLEMENTATION +#include "stb_rect_pack.h" + #define STBTT_STATIC #define STB_TRUETYPE_IMPLEMENTATION -#pragma GCC diagnostic ignored "-Wunused-function" #include "stb_truetype.h" +#pragma GCC diagnostic warning "-Wunused-function" + #define INITIAL_QUAD_CAPACITY 128 /* Structs */ @@ -200,8 +209,7 @@ uint32_t Wellspring_PackFontRanges( return 0; } - myPacker->rangeCount += numRanges; - myPacker->ranges = Wellspring_realloc(myPacker->ranges, sizeof(CharRange) * myPacker->rangeCount); + myPacker->ranges = Wellspring_realloc(myPacker->ranges, sizeof(CharRange) * (myPacker->rangeCount + numRanges)); for (i = 0; i < numRanges; i += 1) { @@ -212,7 +220,6 @@ uint32_t Wellspring_PackFontRanges( } myPacker->rangeCount += numRanges; - return 1; } @@ -267,7 +274,7 @@ uint8_t Wellspring_Draw( stbtt_aligned_quad charQuad; uint32_t vertexBufferIndex; uint32_t indexBufferIndex; - uint32_t i; + uint32_t i, j; for (i = 0; i < strLength; i += 1) { @@ -282,18 +289,27 @@ uint8_t Wellspring_Draw( continue; } + rangeData = NULL; + /* Find the packed char data */ - for (i = 0; i < myPacker->rangeCount; i += 1) + for (j = 0; j < myPacker->rangeCount; j += 1) { if ( - codepoint >= myPacker->ranges[i].firstCodepoint && - codepoint < myPacker->ranges[i].firstCodepoint + myPacker->ranges[i].charCount + codepoint >= myPacker->ranges[j].firstCodepoint && + codepoint < myPacker->ranges[j].firstCodepoint + myPacker->ranges[j].charCount ) { - rangeData = myPacker->ranges[i].data; - glyphIndex = codepoint - myPacker->ranges[i].firstCodepoint; + rangeData = myPacker->ranges[j].data; + glyphIndex = codepoint - myPacker->ranges[j].firstCodepoint; + break; } } + if (rangeData == NULL) + { + /* Requested char wasn't packed! */ + return 0; + } + stbtt_GetPackedQuad( rangeData, myPacker->width, @@ -403,6 +419,8 @@ void Wellspring_DestroyPacker(Wellspring_Packer *packer) Packer* myPacker = (Packer*) packer; uint32_t i; + stbtt_PackEnd(myPacker->context); + for (i = 0; i < myPacker->rangeCount; i += 1) { Wellspring_free(myPacker->ranges[i].data);