Debian Codegraph @

prboom2/src/dsda/stretch.c

raw
1 //
2 // Copyright(C) 2022 by Ryan Krafnick
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // DESCRIPTION:
15 // DSDA Stretch
16 //
17
18 #include "doomdef.h"
19 #include "doomtype.h"
20 #include "hu_stuff.h"
21 #include "r_main.h"
22 #include "st_stuff.h"
23 #include "v_video.h"
24
25 #include "dsda/configuration.h"
26 #include "dsda/font.h"
27
28 #include "stretch.h"
29
30 int wide_offsetx;
31 int wide_offset2x;
32 int wide_offsety;
33 int wide_offset2y;
34
35 int render_stretch_hud;
36
37 int patches_scalex;
38 int patches_scaley;
39
40 static cb_video_t video;
41 static cb_video_t video_stretch;
42 static cb_video_t video_full;
43 static cb_video_t video_ex_text;
44
45 static stretch_param_t* stretch_params;
46 static stretch_param_t stretch_params_table[patch_stretch_max][VPT_ALIGN_MAX];
47
48 static int ex_text_screenwidth;
49 static int ex_text_screenheight;
50 static int ex_text_st_scaled_height;
51 static double ex_text_scale_x;
52 static double ex_text_scale_y;
53
54
55 static void GenLookup(short* lookup1, short* lookup2, int size, int max, int step) {
56 int i;
57 fixed_t frac, lastfrac;
58
59 memset(lookup1, 0, max * sizeof(lookup1[0]));
60 memset(lookup2, 0, max * sizeof(lookup2[0]));
61
62 lastfrac = frac = 0;
63
64 // lookup1[0] = 0;
65 // for (i = 1; i < max; ++i) {
66 // lookup1[i] = (float) i * size / max;
67 // lookup2[i - 1] = lookup1[i] - 1;
68 // }
69
70 // lookup2[max - 1] = size - 1;
71 // lookup1[max] = lookup2[max] = size;
72
73 for(i = 0; i < size; i++) {
74 if(frac >> FRACBITS > lastfrac >> FRACBITS) {
75 lookup1[frac >> FRACBITS] = i;
76 lookup2[lastfrac >> FRACBITS] = i - 1;
77
78 lastfrac = frac;
79 }
80
81 frac += step;
82 }
83
84 lookup2[max - 1] = size - 1;
85 lookup1[max] = lookup2[max] = size;
86
87 for(i = 1; i < max; i++) {
88 if (lookup1[i] == 0 && lookup1[i - 1] != 0)
89 lookup1[i] = lookup1[i - 1];
90
91 if (lookup2[i] == 0 && lookup2[i - 1] != 0)
92 lookup2[i] = lookup2[i - 1];
93 }
94 }
95
96 static void EvaluateExTextScale(void) {
97 ex_text_scale_x = dsda_IntConfig(dsda_config_ex_text_scale_x) / 100.0;
98 ex_text_scale_y = dsda_IntConfig(dsda_config_ex_text_ratio_y) / 100.0;
99
100 if (!ex_text_scale_x)
101 ex_text_scale_x = (double) WIDE_SCREENWIDTH / 320;
102
103 if (!ex_text_scale_y)
104 ex_text_scale_y = 1.0;
105
106 ex_text_scale_y *= ex_text_scale_x;
107
108 ex_text_screenwidth = 320 * ex_text_scale_x;
109 ex_text_screenheight = 200 * ex_text_scale_y;
110 ex_text_st_scaled_height = g_st_height * ex_text_scale_y;
111 }
112
113 stretch_param_t* dsda_StretchParams(int flags) {
114 if (flags & VPT_EX_TEXT)
115 return &stretch_params_table[patch_stretch_ex_text][flags & VPT_ALIGN_MASK];
116
117 return &stretch_params[flags & VPT_ALIGN_MASK];
118 }
119
120 static void InitExTextParam(stretch_param_t* offsets, enum patch_translation_e flags) {
121 int offset2x, offset2y;
122
123 offset2x = SCREENWIDTH - ex_text_screenwidth;
124 offset2y = (SCREENHEIGHT - ex_text_screenheight) -
125 R_PartialView() * (ST_SCALED_HEIGHT - ex_text_st_scaled_height);
126
127 memset(offsets, 0, sizeof(*offsets));
128
129 offsets->video = &video_ex_text;
130
131 if (flags == VPT_ALIGN_LEFT || flags == VPT_ALIGN_LEFT_BOTTOM || flags == VPT_ALIGN_LEFT_TOP) {
132 offsets->deltax1 = 0;
133 offsets->deltax2 = 0;
134 }
135
136 if (flags == VPT_ALIGN_RIGHT || flags == VPT_ALIGN_RIGHT_BOTTOM || flags == VPT_ALIGN_RIGHT_TOP) {
137 offsets->deltax1 = offset2x;
138 offsets->deltax2 = offset2x;
139 }
140
141 if (flags == VPT_ALIGN_BOTTOM || flags == VPT_ALIGN_LEFT_BOTTOM || flags == VPT_ALIGN_RIGHT_BOTTOM)
142 offsets->deltay1 = offset2y;
143 }
144
145 void dsda_UpdateExTextOffset(enum patch_translation_e flags, int offset) {
146 stretch_params_table[patch_stretch_ex_text][flags].deltay1 +=
147 (ST_SCALED_HEIGHT - ex_text_st_scaled_height) * offset * hud_font.line_height / g_st_height +
148 (hud_font.line_height - exhud_font.line_height) * ex_text_scale_y * (offset > 0 ? 1 : -1);
149 }
150
151 void dsda_ResetExTextOffsets(void) {
152 int k;
153
154 for (k = 0; k < VPT_ALIGN_MAX; k++)
155 InitExTextParam(&stretch_params_table[patch_stretch_ex_text][k], k);
156 }
157
158 static void InitStretchParam(stretch_param_t* offsets, int stretch, enum patch_translation_e flags) {
159 memset(offsets, 0, sizeof(*offsets));
160
161 switch (stretch_hud(stretch)) {
162 case patch_stretch_not_adjusted:
163 if (flags == VPT_ALIGN_WIDE) {
164 offsets->video = &video_stretch;
165 offsets->deltax1 = (SCREENWIDTH - WIDE_SCREENWIDTH) / 2;
166 offsets->deltax2 = (SCREENWIDTH - WIDE_SCREENWIDTH) / 2;
167 }
168 else {
169 offsets->video = &video;
170 offsets->deltax1 = wide_offsetx;
171 offsets->deltax2 = wide_offsetx;
172 }
173 break;
174 case patch_stretch_doom_format:
175 offsets->video = &video_stretch;
176 offsets->deltax1 = (SCREENWIDTH - WIDE_SCREENWIDTH) / 2;
177 offsets->deltax2 = (SCREENWIDTH - WIDE_SCREENWIDTH) / 2;
178 break;
179 case patch_stretch_fit_to_width:
180 offsets->video = &video_full;
181 offsets->deltax1 = 0;
182 offsets->deltax2 = 0;
183 break;
184 }
185
186 if (flags == VPT_ALIGN_LEFT || flags == VPT_ALIGN_LEFT_BOTTOM || flags == VPT_ALIGN_LEFT_TOP) {
187 offsets->deltax1 = 0;
188 offsets->deltax2 = 0;
189 }
190
191 if (flags == VPT_ALIGN_RIGHT || flags == VPT_ALIGN_RIGHT_BOTTOM || flags == VPT_ALIGN_RIGHT_TOP) {
192 offsets->deltax1 *= 2;
193 offsets->deltax2 *= 2;
194 }
195
196 offsets->deltay1 = wide_offsety;
197
198 if (flags == VPT_ALIGN_BOTTOM || flags == VPT_ALIGN_LEFT_BOTTOM || flags == VPT_ALIGN_RIGHT_BOTTOM)
199 offsets->deltay1 = wide_offset2y;
200
201 if (flags == VPT_ALIGN_TOP || flags == VPT_ALIGN_LEFT_TOP || flags == VPT_ALIGN_RIGHT_TOP)
202 offsets->deltay1 = 0;
203
204 if (flags == VPT_ALIGN_WIDE && !tallscreen)
205 offsets->deltay1 = 0;
206 }
207
208 void dsda_SetupStretchParams(void) {
209 int i, k;
210
211 EvaluateExTextScale();
212
213 for (k = 0; k < VPT_ALIGN_MAX; k++)
214 for (i = 0; i < patch_stretch_max_config; i++)
215 InitStretchParam(&stretch_params_table[i][k], i, k);
216
217 dsda_ResetExTextOffsets();
218
219 stretch_params = stretch_params_table[render_stretch_hud];
220
221 video.xstep = ((320 << FRACBITS) / 320 / patches_scalex) + 1;
222 video.ystep = ((200 << FRACBITS) / 200 / patches_scaley) + 1;
223 video_stretch.xstep = ((320 << FRACBITS) / WIDE_SCREENWIDTH) + 1;
224 video_stretch.ystep = ((200 << FRACBITS) / WIDE_SCREENHEIGHT) + 1;
225 video_full.xstep = ((320 << FRACBITS) / SCREENWIDTH) + 1;
226 video_full.ystep = ((200 << FRACBITS) / SCREENHEIGHT) + 1;
227 video_ex_text.xstep = ((320 << FRACBITS) / ex_text_screenwidth) + 1;
228 video_ex_text.ystep = ((200 << FRACBITS) / ex_text_screenheight) + 1;
229
230 video.width = 320 * patches_scalex;
231 video.height = 200 * patches_scaley;
232 GenLookup(video.x1lookup, video.x2lookup, video.width, 320, video.xstep);
233 GenLookup(video.y1lookup, video.y2lookup, video.height, 200, video.ystep);
234
235 video_stretch.width = WIDE_SCREENWIDTH;
236 video_stretch.height = WIDE_SCREENHEIGHT;
237 GenLookup(video_stretch.x1lookup, video_stretch.x2lookup, video_stretch.width, 320, video_stretch.xstep);
238 GenLookup(video_stretch.y1lookup, video_stretch.y2lookup, video_stretch.height, 200, video_stretch.ystep);
239
240 video_full.width = SCREENWIDTH;
241 video_full.height = SCREENHEIGHT;
242 GenLookup(video_full.x1lookup, video_full.x2lookup, video_full.width, 320, video_full.xstep);
243 GenLookup(video_full.y1lookup, video_full.y2lookup, video_full.height, 200, video_full.ystep);
244
245 video_ex_text.width = ex_text_screenwidth;
246 video_ex_text.height = ex_text_screenheight;
247 GenLookup(video_ex_text.x1lookup, video_ex_text.x2lookup, video_ex_text.width, 320, video_ex_text.xstep);
248 GenLookup(video_ex_text.y1lookup, video_ex_text.y2lookup, video_ex_text.height, 200, video_ex_text.ystep);
249 }
250
251 void dsda_EvaluatePatchScale(void) {
252 int render_patches_scalex;
253 int render_patches_scaley;
254
255 render_patches_scalex = dsda_IntConfig(dsda_config_render_patches_scalex);
256 render_patches_scaley = dsda_IntConfig(dsda_config_render_patches_scaley);
257
258 patches_scalex = MIN(SCREENWIDTH / 320, SCREENHEIGHT / 200);
259 patches_scalex = MAX(1, patches_scalex);
260 patches_scaley = patches_scalex;
261
262 if (render_patches_scalex > 0)
263 patches_scalex = MIN(render_patches_scalex, patches_scalex);
264
265 if (render_patches_scaley > 0)
266 patches_scaley = MIN(render_patches_scaley, patches_scaley);
267
268 ST_SCALED_HEIGHT = g_st_height * patches_scaley;
269
270 if (SCREENWIDTH < 320 || WIDE_SCREENWIDTH < 320 ||
271 SCREENHEIGHT < 200 || WIDE_SCREENHEIGHT < 200)
272 render_stretch_hud = patch_stretch_fit_to_width;
273
274 switch (stretch_hud(render_stretch_hud)) {
275 case patch_stretch_not_adjusted:
276 wide_offset2x = SCREENWIDTH - patches_scalex * 320;
277 wide_offset2y = SCREENHEIGHT - patches_scaley * 200;
278 break;
279 case patch_stretch_doom_format:
280 ST_SCALED_HEIGHT = g_st_height * WIDE_SCREENHEIGHT / 200;
281
282 wide_offset2x = SCREENWIDTH - WIDE_SCREENWIDTH;
283 wide_offset2y = SCREENHEIGHT - WIDE_SCREENHEIGHT;
284 break;
285 case patch_stretch_fit_to_width:
286 ST_SCALED_HEIGHT = g_st_height * SCREENHEIGHT / 200;
287
288 wide_offset2x = 0;
289 wide_offset2y = 0;
290 break;
291 }
292
293 wide_offsetx = wide_offset2x / 2;
294 wide_offsety = wide_offset2y / 2;
295 }