Sun, 11 Aug 2024 16:29:09 +0200
redesign FPS counter
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 | ||
48 | 28 | #include "ascension/ui/font.h" |
11 | 29 | #include "ascension/context.h" |
30 | #include "ascension/error.h" | |
31 | ||
66
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
32 | #include <assert.h> |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
33 | #include <cx/array_list.h> |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
34 | |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
35 | void asc_font(enum AscFontStyle style, int size) { |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
36 | asc_context.active_font.style = style; |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
37 | asc_context.active_font.size = size; |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
38 | } |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
39 | |
11 | 40 | static char const *asc_font_filename(enum AscFontStyle style) { |
41 | // TODO: do not assume we are running from the program dir | |
42 | switch (style) { | |
43 | case ASC_FONT_REGULAR: | |
44 | return "fonts/OpenSans-Regular.ttf"; | |
45 | case ASC_FONT_BOLD: | |
46 | return "fonts/OpenSans-Bold.ttf"; | |
47 | case ASC_FONT_ITALIC: | |
48 | return "fonts/OpenSans-Italic.ttf"; | |
49 | case ASC_FONT_BOLD_ITALIC: | |
50 | return "fonts/OpenSans-BoldItalic.ttf"; | |
51 | } | |
52 | } | |
53 | ||
66
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
54 | struct asc_font_cache_entry { |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
55 | AscFont font; |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
56 | TTF_Font *ttf; |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
57 | }; |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
58 | |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
59 | static CxList *asc_font_cache; |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
60 | |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
61 | static void asc_font_unload(struct asc_font_cache_entry *entry) { |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
62 | TTF_CloseFont(entry->ttf); |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
63 | asc_dprintf("Closed font size %u, style %u", entry->font.size, entry->font.style); |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
64 | } |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
65 | |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
66 | void asc_font_cache_init(void) { |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
67 | assert(asc_font_cache == NULL); |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
68 | asc_font_cache = cxArrayListCreateSimple( |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
69 | sizeof(struct asc_font_cache_entry), 16 |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
70 | ); |
67
0b96fe6d6b5e
update to recent snapshot of UCX 3.1
Mike Becker <universe@uap-core.de>
parents:
66
diff
changeset
|
71 | cxDefineDestructor(asc_font_cache, asc_font_unload); |
66
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
72 | } |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
73 | |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
74 | void asc_font_cache_destroy(void) { |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
75 | assert(asc_font_cache != NULL); |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
76 | cxListDestroy(asc_font_cache); |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
77 | } |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
78 | |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
79 | |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
80 | TTF_Font *asc_font_load(AscFont font) { |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
81 | CxIterator iter = cxListIterator(asc_font_cache); |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
82 | cx_foreach(struct asc_font_cache_entry*, cache, iter) { |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
83 | if (cache->font.style == font.style && cache->font.size == font.size) { |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
84 | return cache->ttf; |
11 | 85 | } |
86 | } | |
87 | ||
66
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
88 | struct asc_font_cache_entry entry; |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
89 | entry.font = font; |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
90 | entry.ttf = TTF_OpenFont(asc_font_filename(font.style), font.size); |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
91 | if (entry.ttf == NULL) { |
11 | 92 | asc_error(TTF_GetError()); |
66
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
93 | return NULL; |
65
9c44c55d327a
consistently refer to windows by ID - fixes #381
Mike Becker <universe@uap-core.de>
parents:
48
diff
changeset
|
94 | } else { |
66
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
95 | cxListAdd(asc_font_cache, &entry); |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
96 | asc_dprintf("Loaded font size %d, style %d from disk", font.size, font.style); |
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
97 | return entry.ttf; |
11 | 98 | } |
99 | } |