Most texure work
This commit is contained in:
72
util/font.c
72
util/font.c
@@ -7,58 +7,78 @@
|
||||
BitmapFont fonts[fontCount];
|
||||
|
||||
|
||||
BitmapFont
|
||||
prepText(SDL_Renderer *renderer, unsigned char pxSize, const char *file, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
|
||||
BitmapFont prepText(SDL_Renderer *renderer, unsigned char pxSize, const char *file) {
|
||||
TTF_Font *gFont = TTF_OpenFont(file, pxSize);
|
||||
BitmapFont out;
|
||||
out.size = pxSize;
|
||||
out.color = (SDL_Color) {r, g, b, a};
|
||||
unsigned int i = 1;
|
||||
do {
|
||||
if (i == 173) { //specifically this char is 0 width (IDK why)
|
||||
out.surface[i] = SDL_CreateRGBSurface(0, pxSize, pxSize, 32, 0, 0, 0, 0);
|
||||
out.color = (SDL_Color) {255, 255, 255, 255};
|
||||
|
||||
const int glyphsPerRow = 16;
|
||||
const int glyphsPerCol = 16;
|
||||
const int atlasWidth = glyphsPerRow * (pxSize + 1);
|
||||
const int atlasHeight = glyphsPerCol * (pxSize + 1);
|
||||
|
||||
SDL_Surface *atlasSurface = SDL_CreateRGBSurface(0, atlasWidth, atlasHeight, 32,
|
||||
0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
|
||||
out.atlasRect.x = 0;
|
||||
out.atlasRect.y = 0;
|
||||
out.atlasRect.w = atlasWidth;
|
||||
out.atlasRect.h = atlasHeight;
|
||||
SDL_FillRect(atlasSurface, NULL, SDL_MapRGBA(atlasSurface->format, 0, 0, 0, 0)); // transparent
|
||||
|
||||
for (uint16_t i = 1; i < 256; i++) {
|
||||
SDL_Surface *surf;
|
||||
|
||||
if (i == 173) {
|
||||
surf = SDL_CreateRGBSurface(0, pxSize, pxSize, 32,
|
||||
0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
|
||||
SDL_FillRect(surf, NULL, SDL_MapRGBA(surf->format, 0, 0, 0, 0)); // transparent
|
||||
} else {
|
||||
char tmpOut[2] = {i, 0};
|
||||
out.surface[i] = TTF_RenderText_Solid(gFont, tmpOut, out.color);
|
||||
surf = TTF_RenderText_Solid(gFont, tmpOut, out.color);
|
||||
}
|
||||
out.texture[i] = SDL_CreateTextureFromSurface(renderer, out.surface[i]);
|
||||
i++;
|
||||
} while (i < 256);
|
||||
|
||||
int x = (i % glyphsPerRow) * (pxSize + 1);
|
||||
int y = (i / glyphsPerRow) * (pxSize + 1);
|
||||
|
||||
SDL_Rect dest = {x, y, pxSize, pxSize};
|
||||
SDL_BlitSurface(surf, NULL, atlasSurface, &dest);
|
||||
|
||||
out.glyphs[i] = dest;
|
||||
|
||||
SDL_FreeSurface(surf);
|
||||
}
|
||||
|
||||
out.atlas = SDL_CreateTextureFromSurface(renderer, atlasSurface);
|
||||
SDL_SetTextureBlendMode(out.atlas, SDL_BLENDMODE_BLEND);
|
||||
SDL_FreeSurface(atlasSurface);
|
||||
TTF_CloseFont(gFont);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
void renderText(SDL_Renderer *renderer, BitmapFont font, char *string, uint16_t x, uint16_t y) {
|
||||
SDL_Rect charRect;
|
||||
charRect.x = 0;
|
||||
charRect.y = 0;
|
||||
charRect.w = font.size;
|
||||
charRect.h = font.size;
|
||||
SDL_Rect outRect = charRect;
|
||||
SDL_Rect outRect;
|
||||
outRect.x = x;
|
||||
outRect.y = y;
|
||||
outRect.w = font.size;
|
||||
outRect.h = font.size;
|
||||
|
||||
while (*string) {
|
||||
if (*string == '\n') {
|
||||
outRect.x = x;
|
||||
outRect.y += charRect.h + 4;
|
||||
outRect.y += outRect.h + 4;
|
||||
string++;
|
||||
continue;
|
||||
}
|
||||
SDL_RenderCopy(renderer, font.texture[*string], &charRect, &outRect); //TODO CONSIDER FONTS IN ONE ATLAS
|
||||
outRect.x += charRect.w + 1;
|
||||
SDL_RenderCopy(renderer, font.atlas, &font.glyphs[*string], &outRect); //TODO CONSIDER FONTS IN ONE ATLAS
|
||||
outRect.x += outRect.w + 1;
|
||||
string++;
|
||||
}
|
||||
}
|
||||
|
||||
void destroyFont(BitmapFont *font) {
|
||||
for (uint16_t i = 1; i < 256; i++) {
|
||||
if (font->texture[i]) {
|
||||
SDL_DestroyTexture(font->texture[i]);
|
||||
}
|
||||
if (font->surface[i]) {
|
||||
SDL_FreeSurface(font->surface[i]);
|
||||
}
|
||||
SDL_DestroyTexture(font->atlas);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user