Debian Codegraph @

similar/main/gamefont.cpp

raw
1 /*
2 * Portions of this file are copyright Rebirth contributors and licensed as
3 * described in COPYING.txt.
4 * Portions of this file are copyright Parallax Software and licensed
5 * according to the Parallax license below.
6 * See COPYING.txt for license details.
7
8 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
9 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
10 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
11 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
12 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
13 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
14 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
15 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
16 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
17 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 */
19
20 /*
21 *
22 * Fonts for the game.
23 *
24 */
25
26 #include <stdlib.h>
27 #include <stdio.h>
28
29 #include "gr.h"
30 #include "dxxerror.h"
31 #include <string.h>
32 #include "args.h"
33 #include "physfsx.h"
34 #include "gamefont.h"
35 #include "mission.h"
36 #if DXX_USE_OGL
37 #include "ogl_init.h"
38 #endif
39 #include "config.h"
40 #include "console.h"
41
42 #include "d_enumerate.h"
43 #include "partial_range.h"
44
45 namespace dcx {
46
47 enumerated_array<loaded_game_font, MAX_FONTS, gamefont_index> Gamefonts;
48
49 namespace {
50
51 /* Low-resolution fonts need 11 characters for the name, plus one for the
52 * null. High-resolution fonts need 12 characters for the name, plus one
53 * for the null. Round up to 16 for alignment and easy indexing math.
54 */
55 using font_filename = std::array<char, 16>;
56
57 /* Both Descent 1 and Descent 2 ship fonts with these filenames.
58 * For Descent 1 PC and for Descent 2, these fonts are low-resolution files.
59 * For Descent 1 Mac, `font1-1.fnt` is low-resolution, and the rest are high
60 * resolution.
61 */
62 constexpr enumerated_array<font_filename, MAX_FONTS, gamefont_index> Gamefont_filenames_l{{{
63 {{"font1-1.fnt"}}, // Font 0
64 {{"font2-1.fnt"}}, // Font 1
65 {{"font2-2.fnt"}}, // Font 2
66 {{"font2-3.fnt"}}, // Font 3
67 {{"font3-1.fnt"}} // Font 4
68 }}};
69
70 /* Only Descent 2 ships fonts with these filenames. However, D1X-Rebirth has
71 * the ability to read high resolution fonts, and should prefer them when the
72 * user's window dimensions are 640x480 or higher. Therefore, these filenames
73 * are defined in both games, and are probed in both games, even though
74 * D1X-Rebirth will only find these files if the user has provided them outside
75 * the game's original data files. D2X-Rebirth should always have access to
76 * these files.
77 *
78 * In both games, the user can choose not to load these files, via
79 * `CGameArg.GfxSkipHiresFNT`.
80 */
81 constexpr enumerated_array<font_filename, MAX_FONTS, gamefont_index> Gamefont_filenames_h{{{
82 {{"font1-1h.fnt"}}, // Font 0
83 {{"font2-1h.fnt"}}, // Font 1
84 {{"font2-2h.fnt"}}, // Font 2
85 {{"font2-3h.fnt"}}, // Font 3
86 {{"font3-1h.fnt"}} // Font 4
87 }}};
88
89 }
90
91 font_x_scale_proportion FNTScaleX;
92 font_y_scale_proportion FNTScaleY;
93
94 }
95
96 void gamefont_choose_game_font(int scrx,int scry){
97 for (const auto &&[gf, fc] : enumerate(Gamefonts))
98 {
99 int close{-1};
100 const loaded_game_font::a_gamefont_conf *best_gamefont{nullptr};
101 for (const auto &f : partial_range(fc.fontconf, fc.total_fonts_loaded))
102 if ((scrx >= f.expected_screen_resolution_x && close < f.expected_screen_resolution_x) && (scry >= f.expected_screen_resolution_y && close < f.expected_screen_resolution_y))
103 {
104 close = f.expected_screen_resolution_x;
105 best_gamefont = &f;
106 }
107 if (best_gamefont == nullptr)
108 {
109 FNTScaleX = {};
110 FNTScaleY = {};
111 con_printf(CON_CRITICAL, "No suitable font file found for font %u for window dimensions %ix%i; using default scaling", static_cast<unsigned>(gf), scrx, scry);
112 }
113 else
114 {
115
116 #if DXX_USE_OGL
117 if (!CGameArg.OglFixedFont)
118 {
119 // if there's no texture filtering, scale by int
120 auto &f{*best_gamefont};
121 if (CGameCfg.TexFilt == opengl_texture_filter::classic)
122 {
123 FNTScaleX.reset(scrx / f.expected_screen_resolution_x);
124 FNTScaleY.reset(scry / f.expected_screen_resolution_y);
125 }
126 else
127 {
128 FNTScaleX.reset(static_cast<float>(scrx) / f.expected_screen_resolution_x);
129 FNTScaleY.reset(static_cast<float>(scry) / f.expected_screen_resolution_y);
130 }
131
132 // keep proportions
133 #if DXX_BUILD_DESCENT == 1
134 #define DXX_FONT_SCALE_MULTIPLIER 1
135 #elif DXX_BUILD_DESCENT == 2
136 #define DXX_FONT_SCALE_MULTIPLIER 100
137 #endif
138 if (FNTScaleY*DXX_FONT_SCALE_MULTIPLIER < FNTScaleX*DXX_FONT_SCALE_MULTIPLIER)
139 FNTScaleX.reset(FNTScaleY.operator float());
140 else if (FNTScaleX*DXX_FONT_SCALE_MULTIPLIER < FNTScaleY*DXX_FONT_SCALE_MULTIPLIER)
141 FNTScaleY.reset(FNTScaleX.operator float());
142 }
143 #endif
144 }
145 fc.font = gr_init_font(*grd_curcanv, best_gamefont->name);
146 }
147 }
148
149 namespace dsx {
150
151 namespace {
152
153 /* For Descent 1, one caller needs to detect whether the load succeeded, so
154 * return a `bool` reporting success versus failure.
155 * For Descent 2, no caller needs to detect whether the load succeeded, so
156 * return `void`.
157 */
158 #if DXX_BUILD_DESCENT == 1
159 #define DXX_addfontconf_return_type bool
160 #define DXX_addfontconf_return_value
161 #elif DXX_BUILD_DESCENT == 2
162 #define DXX_addfontconf_return_type void
163 #define DXX_addfontconf_return_value (void)
164 #endif
165 static DXX_addfontconf_return_type addfontconf(loaded_game_font &fc, const uint16_t expected_screen_resolution_x, const uint16_t expected_screen_resolution_y, const font_filename &fn)
166 #undef DXX_addfontconf_return_type
167 {
168 if (!PHYSFS_exists(fn.data()))
169 /* File not found - load failed */
170 return DXX_addfontconf_return_value(false);
171
172 auto &f{fc.fontconf[fc.total_fonts_loaded]};
173 ++ fc.total_fonts_loaded;
174 f.expected_screen_resolution_x = {expected_screen_resolution_x};
175 f.expected_screen_resolution_y = {expected_screen_resolution_y};
176 f.name = fn;
177 /* File found - presume the load will succeed when the file is eventually
178 * opened and read.
179 */
180 return DXX_addfontconf_return_value(true);
181 #undef DXX_addfontconf_return_value
182 }
183
184 }
185
186 void gamefont_init()
187 {
188 for (auto &&[i, gf] : enumerate(Gamefonts))
189 {
190 gf.font = nullptr;
191 constexpr uint16_t high_resolution_x{640};
192 constexpr uint16_t high_resolution_y{480};
193 constexpr uint16_t low_resolution_x{320};
194 constexpr uint16_t low_resolution_y{200};
195
196 #if DXX_BUILD_DESCENT == 1
197 if (MacHog && (i != gamefont_index::big))
198 {
199 /* Mac fonts are high-resolution (except for the "big" one), even
200 * when stored in the low-resolution filename. Try to add the Mac
201 * font first. If successful, continue, since adding the standard
202 * high-resolution font would be redundant, and there is no
203 * low-resolution font available.
204 *
205 * On failure, fall back to trying to add a high-resolution font if
206 * the user has provided one. These are not included in the normal
207 * game data, but users could supply them via an add-on.
208 */
209 if (addfontconf(gf, high_resolution_x, high_resolution_y, Gamefont_filenames_l[i]))
210 continue;
211 if (!CGameArg.GfxSkipHiresFNT)
212 addfontconf(gf, high_resolution_x, high_resolution_y, Gamefont_filenames_h[i]); // ZICO - addition to use D2 fonts if available
213 /* Both filenames have now been tried, so continue.
214 */
215 continue;
216 }
217 #endif
218 /* If the above special case does not apply, add the Descent 2
219 * high-resolution font first, then add the low-resolution font.
220 */
221 if (!CGameArg.GfxSkipHiresFNT)
222 addfontconf(gf, high_resolution_x, high_resolution_y, Gamefont_filenames_h[i]);
223 addfontconf(gf, low_resolution_x, low_resolution_y, Gamefont_filenames_l[i]);
224 }
225 }
226 }