Wed, 06 Mar 2024 23:13:06 +0100
fix name of demo binary
| 11 | 1 | /* |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
| 3 | * Copyright 2023 Mike Becker. All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions are met: | |
| 7 | * | |
| 8 | * 1. Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * | |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 12 | * notice, this list of conditions and the following disclaimer in the | |
| 13 | * documentation and/or other materials provided with the distribution. | |
| 14 | * | |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
| 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 25 | * POSSIBILITY OF SUCH DAMAGE. | |
| 26 | */ | |
| 27 | ||
| 28 | #include "ascension/font.h" | |
| 29 | #include "ascension/context.h" | |
| 30 | #include "ascension/error.h" | |
| 31 | ||
| 32 | static char const *asc_font_filename(enum AscFontStyle style) { | |
| 33 | // TODO: do not assume we are running from the program dir | |
| 34 | switch (style) { | |
| 35 | case ASC_FONT_REGULAR: | |
| 36 | return "fonts/OpenSans-Regular.ttf"; | |
| 37 | case ASC_FONT_BOLD: | |
| 38 | return "fonts/OpenSans-Bold.ttf"; | |
| 39 | case ASC_FONT_ITALIC: | |
| 40 | return "fonts/OpenSans-Italic.ttf"; | |
| 41 | case ASC_FONT_BOLD_ITALIC: | |
| 42 | return "fonts/OpenSans-BoldItalic.ttf"; | |
| 43 | } | |
| 44 | } | |
| 45 | ||
| 46 | AscFont const *asc_font(enum AscFontStyle style, int size) { | |
| 47 | for (unsigned int i = 0 ; i < asc_context.fonts_loaded ; i++) { | |
| 48 | AscFont *font = &asc_context.fonts[i]; | |
| 49 | if (font->size == size && font->style == style) { | |
| 50 | return font; | |
| 51 | } | |
| 52 | } | |
| 53 | ||
| 54 | if (asc_context.fonts_loaded == ASC_MAX_FONTS) { | |
| 55 | asc_dprintf("WARNING: Maximum number of fonts reached, wiping cache!"); | |
| 56 | asc_font_cache_clear(); | |
| 57 | } | |
| 58 | ||
| 59 | unsigned int slot = asc_context.fonts_loaded++; | |
| 60 | AscFont *font = &asc_context.fonts[slot]; | |
| 61 | font->size = size; | |
| 62 | font->style = style; | |
| 63 | font->ptr = TTF_OpenFont(asc_font_filename(style), size); | |
| 64 | if (font->ptr == NULL) { | |
| 65 | asc_context.fonts_loaded--; | |
| 66 | asc_error(TTF_GetError()); | |
| 67 | return NULL; | |
| 68 | } | |
| 69 | asc_dprintf("Loaded font size %u, style %u in slot %u", size, style, slot); | |
| 70 | return font; | |
| 71 | } | |
| 72 | ||
| 73 | void asc_font_cache_clear(void) { | |
| 74 | asc_dprintf("Fonts in cache that are being unloaded: %u", asc_context.fonts_loaded); | |
| 75 | while (asc_context.fonts_loaded > 0) { | |
| 76 | unsigned int i = --asc_context.fonts_loaded; | |
| 77 | AscFont *font = &asc_context.fonts[i]; | |
| 78 | TTF_CloseFont(font->ptr); | |
| 79 | font->ptr = NULL; | |
| 80 | } | |
| 81 | } | |
| 82 | ||
| 83 | AscFont const *asc_font_cache_validate(AscFont const *font) { | |
| 84 | if (font->ptr) { | |
| 85 | return font; | |
| 86 | } else { | |
| 87 | asc_dprintf("Cache miss for font size %u, style %u", font->size, font->style); | |
| 88 | return asc_font(font->style, font->size); | |
| 89 | } | |
| 90 | } |