|
@@ -1,54 +0,0 @@
|
|
|
-#include "./font.h"
|
|
|
-#include "./sdl_extra.h"
|
|
|
-
|
|
|
-Font font_load_from_file(SDL_Renderer *renderer, const char *file_path)
|
|
|
-{
|
|
|
- Font font = {0};
|
|
|
-
|
|
|
- SDL_Surface *font_surface = surface_from_file(file_path);
|
|
|
- scc(SDL_SetColorKey(font_surface, SDL_TRUE, 0xFF000000));
|
|
|
- font.spritesheet = scp(SDL_CreateTextureFromSurface(renderer, font_surface));
|
|
|
- SDL_FreeSurface(font_surface);
|
|
|
-
|
|
|
- for (size_t ascii = ASCII_DISPLAY_LOW; ascii <= ASCII_DISPLAY_HIGH; ++ascii) {
|
|
|
- const size_t index = ascii - ASCII_DISPLAY_LOW;
|
|
|
- const size_t col = index % FONT_COLS;
|
|
|
- const size_t row = index / FONT_COLS;
|
|
|
- font.glyph_table[index] = (SDL_Rect) {
|
|
|
- .x = (int) col * FONT_CHAR_WIDTH,
|
|
|
- .y = (int) row * FONT_CHAR_HEIGHT,
|
|
|
- .w = FONT_CHAR_WIDTH,
|
|
|
- .h = FONT_CHAR_HEIGHT,
|
|
|
- };
|
|
|
- }
|
|
|
-
|
|
|
- return font;
|
|
|
-}
|
|
|
-
|
|
|
-void render_char(SDL_Renderer *renderer, const Font *font, char c, Vec2f pos, float scale)
|
|
|
-{
|
|
|
- const SDL_Rect dst = {
|
|
|
- .x = (int) floorf(pos.x),
|
|
|
- .y = (int) floorf(pos.y),
|
|
|
- .w = (int) floorf(FONT_CHAR_WIDTH * scale),
|
|
|
- .h = (int) floorf(FONT_CHAR_HEIGHT * scale),
|
|
|
- };
|
|
|
-
|
|
|
- size_t index = '?' - ASCII_DISPLAY_LOW;
|
|
|
- if (ASCII_DISPLAY_LOW <= c && c <= ASCII_DISPLAY_HIGH) {
|
|
|
- index = c - ASCII_DISPLAY_LOW;
|
|
|
- }
|
|
|
-
|
|
|
- scc(SDL_RenderCopy(renderer, font->spritesheet, &font->glyph_table[index], &dst));
|
|
|
-}
|
|
|
-
|
|
|
-void render_text_sized(SDL_Renderer *renderer, Font *font, const char *text, size_t text_size, Vec2f pos, Uint32 color, float scale)
|
|
|
-{
|
|
|
- set_texture_color(font->spritesheet, color);
|
|
|
-
|
|
|
- Vec2f pen = pos;
|
|
|
- for (size_t i = 0; i < text_size; ++i) {
|
|
|
- render_char(renderer, font, text[i], pen, scale);
|
|
|
- pen.x += FONT_CHAR_WIDTH * scale;
|
|
|
- }
|
|
|
-}
|