Debian Codegraph @

prboom2/src/v_video.c

raw
1 /* Emacs style mode select -*- C -*-
2 *-----------------------------------------------------------------------------
3 *
4 *
5 * PrBoom: a Doom port merged with LxDoom and LSDLDoom
6 * based on BOOM, a modified and improved DOOM engine
7 * Copyright (C) 1999 by
8 * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
9 * Copyright (C) 1999-2000 by
10 * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
11 * Copyright 2005, 2006 by
12 * Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 * 02111-1307, USA.
28 *
29 * DESCRIPTION:
30 * Gamma correction LUT stuff.
31 * Color range translation support
32 * Functions to draw patches (by post) directly to screen.
33 * Functions to blit a block to the screen.
34 *
35 *-----------------------------------------------------------------------------
36 */
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42 #include <assert.h>
43 #include "SDL.h"
44
45 #include "doomdef.h"
46 #include "doomstat.h"
47 #include "r_main.h"
48 #include "r_draw.h"
49 #include "r_patch.h"
50 #include "m_bbox.h"
51 #include "w_wad.h" /* needed for color translation lump lookup */
52 #include "v_video.h"
53 #include "i_video.h"
54 #include "lprintf.h"
55 #include "st_stuff.h"
56 #include "e6y.h"
57
58 #include "dsda/configuration.h"
59 #include "dsda/cr_table.h"
60 #include "dsda/global.h"
61 #include "dsda/palette.h"
62 #include "dsda/stretch.h"
63 #include "dsda/text_color.h"
64
65 // DWF 2012-05-10
66 // SetRatio sets the following global variables based on window geometry and
67 // user preferences. The integer ratio is hardly used anymore, so further
68 // simplification may be in order.
69 dboolean tallscreen;
70 unsigned int ratio_multiplier, ratio_scale;
71 float gl_ratio;
72 int psprite_offset; // Needed for "tallscreen" modes
73
74
75 // Each screen is [SCREENWIDTH*SCREENHEIGHT];
76 screeninfo_t screens[NUM_SCREENS];
77
78 /* jff 4/24/98 initialize this at runtime */
79 const byte *colrngs[CR_LIMIT];
80
81 int usegamma;
82
83 int V_BloodColor(int blood)
84 {
85 if (blood < 1 || blood > 8)
86 return 0;
87
88 return CR_BLOOD + blood - 1;
89 }
90
91 // haleyjd: DOSDoom-style single translucency lookup-up table
92 // generation code. This code has a 32k (plus a bit more)
93 // footprint but allows a much wider range of translucency effects
94 // than BOOM-style translucency. This will be used for particles,
95 // for variable mapthing trans levels, and for screen patches.
96
97 // haleyjd: Updated 06/21/08 to use 32k lookup, mainly to fix
98 // additive translucency. Note this code is included in Odamex and
99 // so it can be considered GPL as used here, rather than BSD. But,
100 // I don't care either way. It is effectively dual-licensed I suppose.
101
102 unsigned int Col2RGB8[65][256];
103 byte RGB32k[32][32][32];
104
105 #define MAKECOLOR(a) (((a)<<3)|((a)>>2))
106
107 void V_InitFlexTranTable(void)
108 {
109 static int flexTranInit = false;
110
111 if (!flexTranInit)
112 {
113 int r, g, b, x, y, pos;
114 const unsigned char *palette = V_GetPlaypal();
115
116 // mark that we've initialized the flex tran table
117 flexTranInit = true;
118
119 // build RGB table
120 for(r = 0; r < 32; r++)
121 {
122 for(g = 0; g < 32; g++)
123 {
124 for(b = 0; b < 32; b++)
125 {
126 RGB32k[r][g][b] = V_BestColor(palette,
127 MAKECOLOR(r), MAKECOLOR(g), MAKECOLOR(b));
128 }
129 }
130 }
131
132 // build lookup table
133 for(x = 0; x < 65; x++)
134 {
135 pos = 0;
136 for(y = 0; y < 256; y++)
137 {
138 Col2RGB8[x][y] =
139 (((palette[pos + 0] * x) >> 4) << 20) |
140 ((palette[pos + 1] * x) >> 4) |
141 (((palette[pos + 2] * x) >> 4) << 10);
142
143 pos += 3;
144 }
145 }
146 }
147 }
148
149 void V_InitColorTranslation(void)
150 {
151 int i;
152 byte* full_table;
153
154 full_table = dsda_GenerateCRTable();
155
156 for (i = 0; i < CR_LIMIT; ++i)
157 colrngs[i] = full_table + 256 * i;
158
159 dsda_LoadTextColor();
160 }
161
162 //
163 // V_CopyRect
164 //
165 // Copies a source rectangle in a screen buffer to a destination
166 // rectangle in another screen buffer. Source origin in srcx,srcy,
167 // destination origin in destx,desty, common size in width and height.
168 // Source buffer specfified by srcscrn, destination buffer by destscrn.
169 //
170 // Marks the destination rectangle on the screen dirty.
171 //
172 // No return.
173 //
174 static void FUNC_V_CopyRect(int srcscrn, int destscrn,
175 int x, int y, int width, int height,
176 enum patch_translation_e flags)
177 {
178 byte *src;
179 byte *dest;
180
181 if (flags & VPT_STRETCH_MASK)
182 {
183 stretch_param_t *params;
184 int sx = x;
185 int sy = y;
186
187 params = dsda_StretchParams(flags);
188
189 x = params->video->x1lookup[x];
190 y = params->video->y1lookup[y];
191 width = params->video->x2lookup[sx + width - 1] - x + 1;
192 height = params->video->y2lookup[sy + height - 1] - y + 1;
193 x += params->deltax1;
194 y += params->deltay1;
195 }
196
197 if (x < 0)
198 {
199 width += x;
200 x = 0;
201 }
202
203 if (x + width > SCREENWIDTH)
204 {
205 width = SCREENWIDTH - x;
206 }
207
208 if (y < 0)
209 {
210 height += y;
211 y = 0;
212 }
213
214 if (y + height > SCREENHEIGHT)
215 {
216 height = SCREENHEIGHT - y;
217 }
218
219 if (width <= 0 || height <= 0)
220 {
221 return;
222 }
223
224 src = screens[srcscrn].data + screens[srcscrn].pitch * y + x;
225 dest = screens[destscrn].data + screens[destscrn].pitch * y + x;
226
227 for ( ; height>0 ; height--)
228 {
229 memcpy (dest, src, width);
230 src += screens[srcscrn].pitch;
231 dest += screens[destscrn].pitch;
232 }
233 }
234
235 #define FILL_FLAT(dest_type, dest_pitch, pal_func)\
236 {\
237 const byte *src, *src_p;\
238 dest_type *dest, *dest_p;\
239 for (sy = y ; sy < y + height; sy += 64)\
240 {\
241 h = (y + height - sy < 64 ? y + height - sy : 64);\
242 dest = (dest_type *)screens[scrn].data + dest_pitch * sy + x;\
243 src = data + 64 * ((sy - y) % 64);\
244 for (sx = x; sx < x + width; sx += 64)\
245 {\
246 src_p = src;\
247 dest_p = dest;\
248 w = (x + width - sx < 64 ? x + width - sx : 64);\
249 for (j = 0; j < h; j++)\
250 {\
251 for (i = 0; i < w; i++)\
252 {\
253 dest_p[i] = pal_func(src_p[i], VID_COLORWEIGHTMASK);\
254 }\
255 dest_p += dest_pitch;\
256 src_p += 64;\
257 }\
258 dest += 64;\
259 }\
260 }\
261 }\
262
263 static void FUNC_V_FillFlat(int lump, int scrn, int x, int y, int width, int height, enum patch_translation_e flags)
264 {
265 const byte *data;
266 byte *dest;
267 int sx, sy;
268 int pitch, src_x_offset, src_y_offset;
269 float ratio_x, ratio_y;
270 stretch_param_t* stretch;
271
272 stretch = dsda_StretchParams(flags);
273 pitch = screens[scrn].pitch;
274 data = W_LumpByNum(lump + firstflat);
275
276 ratio_x = stretch->video->width / 320.f;
277 ratio_y = stretch->video->height / 200.f;
278
279 for (sy = y; sy < y + height; ++sy)
280 {
281 src_y_offset = 64 * ((int) (sy / ratio_y) % 64);
282 dest = screens[scrn].data + pitch * sy + x;
283
284 for (sx = x; sx < x + width; ++sx)
285 {
286 src_x_offset = (int) ((sx - x) / ratio_x) % 64;
287 *dest++ = data[src_x_offset + src_y_offset];
288 }
289 }
290 }
291
292 static void FUNC_V_FillPatch(int lump, int scrn, int x, int y, int width, int height, enum patch_translation_e flags)
293 {
294 int sx, sy, w, h;
295
296 w = R_NumPatchWidth(lump);
297 h = R_NumPatchHeight(lump);
298
299 for (sy = y; sy < y + height; sy += h)
300 {
301 for (sx = x; sx < x + width; sx += w)
302 {
303 V_DrawNumPatch(sx, sy, scrn, lump, CR_DEFAULT, flags);
304 }
305 }
306 }
307
308 /*
309 * V_DrawBackground tiles a 64x64 patch over the entire screen, providing the
310 * background for the Help and Setup screens, and plot text betwen levels.
311 * cphipps - used to have M_DrawBackground, but that was used the framebuffer
312 * directly, so this is my code from the equivalent function in f_finale.c
313 */
314 static void FUNC_V_DrawBackground(const char* flatname, int scrn)
315 {
316 V_FillFlatName(flatname, scrn, 0, 0, SCREENWIDTH, SCREENHEIGHT, VPT_STRETCH);
317 }
318
319 //
320 // V_Init
321 //
322 // Allocates the 4 full screen buffers in low DOS memory
323 // No return
324 //
325
326 void V_Init (void)
327 {
328 int i;
329
330 // reset the all
331 for (i = 0; i<NUM_SCREENS; i++) {
332 screens[i].data = NULL;
333 screens[i].not_on_heap = false;
334 screens[i].width = 0;
335 screens[i].height = 0;
336 screens[i].pitch = 0;
337 }
338 }
339
340 //
341 // V_DrawMemPatch
342 //
343 // CPhipps - unifying patch drawing routine, handles all cases and combinations
344 // of stretching, flipping and translating
345 //
346 // This function is big, hopefully not too big that gcc can't optimise it well.
347 // In fact it packs pretty well, there is no big performance lose for all this merging;
348 // the inner loops themselves are just the same as they always were
349 // (indeed, laziness of the people who wrote the 'clones' of the original V_DrawPatch
350 // means that their inner loops weren't so well optimised, so merging code may even speed them).
351 //
352 static void V_DrawMemPatch(int x, int y, int scrn, const rpatch_t *patch,
353 dboolean center, int cm, enum patch_translation_e flags)
354 {
355 const byte *trans;
356
357 stretch_param_t *params;
358
359 if (cm == CR_DEFAULT)
360 trans = &colormaps[0][0];
361 else if (cm == CR_DARKEN)
362 trans = &colormaps[0][256 * 15];
363 else if (cm < CR_LIMIT)
364 trans = colrngs[cm];
365 else
366 trans = translationtables + 256*((cm - CR_LIMIT) - 1);
367
368 if (!(flags & VPT_NOOFFSET))
369 {
370 y -= patch->topoffset;
371 x -= patch->leftoffset;
372 }
373
374 // CPhipps - auto-no-stretch if not high-res
375 if ((flags & VPT_STRETCH_MASK) && SCREEN_320x200)
376 flags &= ~VPT_STRETCH_MASK;
377
378 params = dsda_StretchParams(flags);
379
380 // CPhipps - null translation pointer => no translation
381 if (!trans)
382 flags &= ~VPT_TRANS;
383
384 // [FG] automatically center wide patches without horizontal offset
385 if (center)
386 {
387 if (patch->width > 320 && patch->leftoffset == 0)
388 x -= (patch->width - 320) / 2;
389 }
390
391 if (!(flags & VPT_STRETCH_MASK)) {
392 int col;
393 byte *desttop = screens[scrn].data+y*screens[scrn].pitch+x;
394 int w = patch->width;
395
396 if (y<0 || y+patch->height > ((flags & VPT_STRETCH) ? 200 : SCREENHEIGHT)) {
397 // killough 1/19/98: improved error message:
398 lprintf(LO_WARN, "V_DrawMemPatch8: Patch (%d,%d)-(%d,%d) exceeds LFB in vertical direction (horizontal is clipped)\n"
399 "Bad V_DrawMemPatch8 (flags=%u)", x, y, x+patch->width, y+patch->height, flags);
400 return;
401 }
402
403 w--; // CPhipps - note: w = width-1 now, speeds up flipping
404
405 for (col=0 ; col<=w ; desttop++, col++, x++) {
406 int i;
407 const int colindex = (flags & VPT_FLIP) ? (w - col) : (col);
408 const rcolumn_t *column = R_GetPatchColumn(patch, colindex);
409
410 if (x < 0)
411 continue;
412 if (x >= SCREENWIDTH)
413 break;
414
415 // step through the posts in a column
416 for (i=0; i<column->numPosts; i++) {
417 const rpost_t *post = &column->posts[i];
418 // killough 2/21/98: Unrolled and performance-tuned
419
420 const byte *source = column->pixels + post->topdelta;
421 byte *dest = desttop + post->topdelta*screens[scrn].pitch;
422 int count = post->length;
423
424 if (!(flags & VPT_TRANS)) {
425 if ((count-=4)>=0)
426 do {
427 register byte s0,s1;
428 s0 = source[0];
429 s1 = source[1];
430 dest[0] = s0;
431 dest[screens[scrn].pitch] = s1;
432 dest += screens[scrn].pitch*2;
433 s0 = source[2];
434 s1 = source[3];
435 source += 4;
436 dest[0] = s0;
437 dest[screens[scrn].pitch] = s1;
438 dest += screens[scrn].pitch*2;
439 } while ((count-=4)>=0);
440 if (count+=4)
441 do {
442 *dest = *source++;
443 dest += screens[scrn].pitch;
444 } while (--count);
445 } else {
446 // CPhipps - merged translation code here
447 if ((count-=4)>=0)
448 do {
449 register byte s0,s1;
450 s0 = source[0];
451 s1 = source[1];
452 s0 = trans[s0];
453 s1 = trans[s1];
454 dest[0] = s0;
455 dest[screens[scrn].pitch] = s1;
456 dest += screens[scrn].pitch*2;
457 s0 = source[2];
458 s1 = source[3];
459 s0 = trans[s0];
460 s1 = trans[s1];
461 source += 4;
462 dest[0] = s0;
463 dest[screens[scrn].pitch] = s1;
464 dest += screens[scrn].pitch*2;
465 } while ((count-=4)>=0);
466 if (count+=4)
467 do {
468 *dest = trans[*source++];
469 dest += screens[scrn].pitch;
470 } while (--count);
471 }
472 }
473 }
474 }
475 else {
476 // CPhipps - move stretched patch drawing code here
477 // - reformat initialisers, move variables into inner blocks
478
479 int col;
480 int w = (patch->width << 16) - 1; // CPhipps - -1 for faster flipping
481 int left, right, top, bottom;
482 int DXI, DYI;
483 int deltay1;
484 R_DrawColumn_f colfunc;
485 draw_column_vars_t dcvars;
486 draw_vars_t olddrawvars = drawvars;
487
488 R_SetDefaultDrawColumnVars(&dcvars);
489
490 drawvars.topleft = screens[scrn].data;
491 drawvars.pitch = screens[scrn].pitch;
492
493 if (flags & VPT_TRANS) {
494 colfunc = R_GetDrawColumnFunc(RDC_PIPELINE_TRANSLATED, RDRAW_FILTER_NONE);
495 dcvars.translation = trans;
496 } else {
497 colfunc = R_GetDrawColumnFunc(RDC_PIPELINE_STANDARD, RDRAW_FILTER_NONE);
498 }
499
500 DXI = params->video->xstep;
501 DYI = params->video->ystep;
502
503 left = (x < 0 || x > 320 ? (x * params->video->width) / 320 : params->video->x1lookup[x]);
504 top = (y < 0 || y > 200 ? (y * params->video->height) / 200 : params->video->y1lookup[y]);
505
506 if (x + patch->width < 0 || x + patch->width > 320)
507 right = ( ((x + patch->width) * params->video->width - 1) / 320 );
508 else
509 right = params->video->x2lookup[x + patch->width - 1];
510
511 if (y + patch->height < 0 || y + patch->height > 200)
512 bottom = ( ((y + patch->height - 0) * params->video->height) / 200 );
513 else
514 bottom = params->video->y2lookup[y + patch->height - 1];
515
516 deltay1 = params->deltay1;
517
518 if (TOP_ALIGNMENT(flags & VPT_STRETCH_MASK))
519 deltay1 += global_patch_top_offset;
520
521 left += params->deltax1;
522 right += params->deltax2;
523 top += deltay1;
524 bottom += deltay1;
525
526 dcvars.texheight = patch->height;
527 dcvars.iscale = DYI;
528 dcvars.drawingmasked = MAX(patch->width, patch->height) > 8;
529
530 col = 0;
531
532 for (dcvars.x=left; dcvars.x<=right; dcvars.x++, col+=DXI) {
533 int i;
534 const int colindex = (flags & VPT_FLIP) ? ((w - col)>>16): (col>>16);
535 const rcolumn_t *column = R_GetPatchColumn(patch, colindex);
536 const rcolumn_t *prevcolumn = R_GetPatchColumn(patch, colindex-1);
537 const rcolumn_t *nextcolumn = R_GetPatchColumn(patch, colindex+1);
538
539 // ignore this column if it's to the left of our clampRect
540 if (dcvars.x < 0)
541 continue;
542 if (dcvars.x >= SCREENWIDTH)
543 break;
544
545 // step through the posts in a column
546 for (i=0; i<column->numPosts; i++) {
547 const rpost_t *post = &column->posts[i];
548 int yoffset = 0;
549
550 //e6y
551 if (!(flags & VPT_STRETCH_MASK))
552 {
553 dcvars.yl = y + post->topdelta;
554 dcvars.yh = ((((y + post->topdelta + post->length) << 16) - (FRACUNIT>>1))>>FRACBITS);
555 }
556 else
557 {
558 // e6y
559 // More accurate patch drawing from Eternity.
560 // Predefined arrays are used instead of dynamic calculation
561 // of the top and bottom screen coordinates of a column.
562
563 int tmpy;
564
565 tmpy = y + post->topdelta;
566 if (tmpy < 0 || tmpy > 200)
567 dcvars.yl = (tmpy * params->video->height) / 200 + deltay1;
568 else
569 dcvars.yl = params->video->y1lookup[tmpy] + deltay1;
570
571 tmpy = y + post->topdelta + post->length - 1;
572 if (tmpy < 0 || tmpy > 200)
573 dcvars.yh = (tmpy * params->video->height) / 200 + deltay1;
574 else
575 dcvars.yh = params->video->y2lookup[tmpy] + deltay1;
576 }
577 dcvars.edgeslope = post->slope;
578
579 if ((dcvars.yh < 0) || (dcvars.yh < top))
580 continue;
581 if ((dcvars.yl >= SCREENHEIGHT) || (dcvars.yl >= bottom))
582 continue;
583
584 if (dcvars.yh >= bottom) {
585 //dcvars.yh = bottom-1;
586 dcvars.edgeslope &= ~RDRAW_EDGESLOPE_BOT_MASK;
587 }
588 if (dcvars.yh >= SCREENHEIGHT) {
589 dcvars.yh = SCREENHEIGHT-1;
590 dcvars.edgeslope &= ~RDRAW_EDGESLOPE_BOT_MASK;
591 }
592
593 if (dcvars.yl < 0) {
594 yoffset = (0-dcvars.yl) * 200/params->video->height;
595 dcvars.yl = 0;
596 dcvars.edgeslope &= ~RDRAW_EDGESLOPE_TOP_MASK;
597 }
598 if (dcvars.yl < top) {
599 yoffset = (top-dcvars.yl) * 200/params->video->height;
600 dcvars.yl = top;
601 dcvars.edgeslope &= ~RDRAW_EDGESLOPE_TOP_MASK;
602 }
603
604 dcvars.source = column->pixels + post->topdelta + yoffset;
605 dcvars.prevsource = prevcolumn ? prevcolumn->pixels + post->topdelta + yoffset: dcvars.source;
606 dcvars.nextsource = nextcolumn ? nextcolumn->pixels + post->topdelta + yoffset: dcvars.source;
607
608 dcvars.texturemid = -((dcvars.yl-centery)*dcvars.iscale);
609
610 //e6y
611 dcvars.dy = deltay1;
612 dcvars.flags |= DRAW_COLUMN_ISPATCH;
613
614 colfunc(&dcvars);
615 }
616 }
617
618 R_ResetColumnBuffer();
619 drawvars = olddrawvars;
620 }
621 }
622
623
624 //
625 // FUNC_V_DrawShaded
626 //
627 // Adapted from Woof.
628 //
629 // This uses a dark colormap to create
630 // a dark faded background under menus.
631 //
632 static void FUNC_V_DrawShaded(int scrn, int x, int y, int width, int height, int shade)
633 {
634 const lighttable_t *darkcolormap;
635 extern dboolean LevelUseFullBright;
636 extern const byte* colormap_lump;
637 byte* dest;
638 int ix, iy;
639
640 // Compensate for Hexen FOGMAP
641 darkcolormap = (hexen && !LevelUseFullBright) ? (const lighttable_t *)colormap_lump : colormaps[scrn];
642
643 for (iy = y; iy < y + height; ++iy)
644 {
645 dest = screens[scrn].data + screens[scrn].pitch * iy + x;
646
647 for (ix = x; ix < x + width; ++ix)
648 {
649 *dest = darkcolormap[shade * 256 + dest[scrn]];
650 dest++;
651 }
652 }
653 }
654
655
656 // CPhipps - some simple, useful wrappers for that function, for drawing patches from wads
657
658 // CPhipps - GNU C only suppresses generating a copy of a function if it is
659 // static inline; other compilers have different behaviour.
660 // This inline is _only_ for the function below
661
662 static void FUNC_V_DrawNumPatch(int x, int y, int scrn, int lump,
663 dboolean center, int cm, enum patch_translation_e flags)
664 {
665 V_DrawMemPatch(x, y, scrn, R_PatchByNum(lump), center, cm, flags);
666 }
667
668 static void FUNC_V_DrawNumPatchPrecise(float x, float y, int scrn, int lump,
669 dboolean center, int cm, enum patch_translation_e flags)
670 {
671 V_DrawMemPatch((int)x, (int)y, scrn, R_PatchByNum(lump), center, cm, flags);
672 }
673
674 static int currentPaletteIndex = 0;
675
676 void V_TouchPalette(void)
677 {
678 V_SetPalette(currentPaletteIndex);
679 }
680
681 //
682 // V_SetPalette
683 //
684 // CPhipps - New function to set the palette to palette number pal.
685 // Handles loading of PLAYPAL and calls I_SetPalette
686
687 void V_SetPalette(int pal)
688 {
689 currentPaletteIndex = pal;
690
691 if (V_IsOpenGLMode()) {
692 gld_SetPalette(pal);
693 } else {
694 I_SetPalette(pal);
695 }
696 }
697
698 void V_SetPlayPal(int playpal_index)
699 {
700 dsda_SetPlayPal(playpal_index);
701 R_UpdatePlayPal();
702 V_SetPalette(currentPaletteIndex);
703
704 if (V_IsOpenGLMode())
705 {
706 gld_FlushTextures();
707 }
708 }
709
710 //
711 // V_FillRect
712 //
713 // CPhipps - New function to fill a rectangle with a given colour
714 static void V_FillRect8(int scrn, int x, int y, int width, int height, byte colour)
715 {
716 byte* dest = screens[scrn].data + x + y*screens[scrn].pitch;
717 while (height--) {
718 memset(dest, colour, width);
719 dest += screens[scrn].pitch;
720 }
721 }
722
723 static void WRAP_V_DrawLine(fline_t* fl, int color);
724 static void V_PlotPixel8(int scrn, int x, int y, byte color);
725
726 static void WRAP_V_DrawLineWu(fline_t* fl, int color);
727 static void V_PlotPixelWu8(int scrn, int x, int y, byte color, int weight);
728
729 static void WRAP_gld_BeginUIDraw(void)
730 {
731 gld_BeginUIDraw();
732 }
733 static void WRAP_gld_EndUIDraw(void)
734 {
735 gld_EndUIDraw();
736 }
737 static void WRAP_gld_BeginAutomapDraw(void)
738 {
739 gld_BeginAutomapDraw();
740 }
741 static void WRAP_gld_EndAutomapDraw(void)
742 {
743 gld_EndAutomapDraw();
744 }
745 static void WRAP_gld_BeginMenuDraw(void)
746 {
747 gld_BeginMenuDraw();
748 }
749 static void WRAP_gld_EndMenuDraw(void)
750 {
751 gld_EndMenuDraw();
752 }
753 static void WRAP_gld_FillRect(int scrn, int x, int y, int width, int height, byte colour)
754 {
755 gld_FillBlock(x,y,width,height,colour);
756 }
757 static void WRAP_gld_CopyRect(int srcscrn, int destscrn, int x, int y, int width, int height, enum patch_translation_e flags)
758 {
759 }
760 static void WRAP_gld_DrawBackground(const char *flatname, int n)
761 {
762 gld_FillFlatName(flatname, 0, 0, SCREENWIDTH, SCREENHEIGHT, VPT_STRETCH);
763 }
764 static void WRAP_gld_FillFlat(int lump, int n, int x, int y, int width, int height, enum patch_translation_e flags)
765 {
766 gld_FillFlat(lump, x, y, width, height, flags);
767 }
768 static void WRAP_gld_FillPatch(int lump, int n, int x, int y, int width, int height, enum patch_translation_e flags)
769 {
770 gld_FillPatch(lump, x, y, width, height, flags);
771 }
772 static void WRAP_gld_DrawNumPatch(int x, int y, int scrn, int lump, dboolean center, int cm, enum patch_translation_e flags)
773 {
774 gld_DrawNumPatch(x,y,lump,center,cm,flags);
775 }
776 static void WRAP_gld_DrawNumPatchPrecise(float x, float y, int scrn, int lump, dboolean center, int cm, enum patch_translation_e flags)
777 {
778 gld_DrawNumPatch_f(x,y,lump,center,cm,flags);
779 }
780 static void V_PlotPixelGL(int scrn, int x, int y, byte color) {
781 gld_DrawLine(x-1, y, x+1, y, color);
782 gld_DrawLine(x, y-1, x, y+1, color);
783 }
784 static void V_PlotPixelWuGL(int scrn, int x, int y, byte color, int weight) {
785 V_PlotPixelGL(scrn, x, y, color);
786 }
787 static void WRAP_gld_DrawLine(fline_t* fl, int color)
788 {
789 gld_DrawLine_f(fl->a.fx, fl->a.fy, fl->b.fx, fl->b.fy, color);
790 }
791 static void WRAP_gld_DrawShaded(int scrn, int x, int y, int width, int height, int shade)
792 {
793 gld_DrawShaded(x, y, width, height, shade);
794 }
795
796 static void NULL_BeginUIDraw(void) {}
797 static void NULL_EndUIDraw(void) {}
798 static void NULL_BeginAutomapDraw(void) {}
799 static void NULL_EndAutomapDraw(void) {}
800 static void NULL_BeginMenuDraw(void) {}
801 static void NULL_EndMenuDraw(void) {}
802 static void NULL_FillRect(int scrn, int x, int y, int width, int height, byte colour) {}
803 static void NULL_CopyRect(int srcscrn, int destscrn, int x, int y, int width, int height, enum patch_translation_e flags) {}
804 static void NULL_FillFlat(int lump, int n, int x, int y, int width, int height, enum patch_translation_e flags) {}
805 static void NULL_FillPatch(int lump, int n, int x, int y, int width, int height, enum patch_translation_e flags) {}
806 static void NULL_DrawBackground(const char *flatname, int n) {}
807 static void NULL_DrawNumPatch(int x, int y, int scrn, int lump, dboolean center, int cm, enum patch_translation_e flags) {}
808 static void NULL_DrawNumPatchPrecise(float x, float y, int scrn, int lump, dboolean center, int cm, enum patch_translation_e flags) {}
809 static void NULL_PlotPixel(int scrn, int x, int y, byte color) {}
810 static void NULL_PlotPixelWu(int scrn, int x, int y, byte color, int weight) {}
811 static void NULL_DrawLine(fline_t* fl, int color) {}
812 static void NULL_DrawLineWu(fline_t* fl, int color) {}
813 static void NULL_DrawShaded(int scrn, int x, int y, int width, int height, int shade) {}
814
815 static video_mode_t current_videomode = VID_MODESW;
816
817 V_BeginUIDraw_f V_BeginUIDraw = NULL_BeginUIDraw;
818 V_EndUIDraw_f V_EndUIDraw = NULL_EndUIDraw;
819 V_BeginUIDraw_f V_BeginAutomapDraw = NULL_BeginAutomapDraw;
820 V_EndUIDraw_f V_EndAutomapDraw = NULL_EndAutomapDraw;
821 V_BeginUIDraw_f V_BeginMenuDraw = NULL_BeginMenuDraw;
822 V_EndUIDraw_f V_EndMenuDraw = NULL_EndMenuDraw;
823 V_CopyRect_f V_CopyRect = NULL_CopyRect;
824 V_FillRect_f V_FillRect = NULL_FillRect;
825 V_DrawNumPatchGen_f V_DrawNumPatchGen = NULL_DrawNumPatch;
826 V_DrawNumPatchGenPrecise_f V_DrawNumPatchGenPrecise = NULL_DrawNumPatchPrecise;
827 V_FillFlat_f V_FillFlat = NULL_FillFlat;
828 V_FillPatch_f V_FillPatch = NULL_FillPatch;
829 V_DrawBackground_f V_DrawBackground = NULL_DrawBackground;
830 V_PlotPixel_f V_PlotPixel = NULL_PlotPixel;
831 V_PlotPixelWu_f V_PlotPixelWu = NULL_PlotPixelWu;
832 V_DrawLine_f V_DrawLine = NULL_DrawLine;
833 V_DrawLineWu_f V_DrawLineWu = NULL_DrawLineWu;
834 V_DrawShaded_f V_DrawShaded = NULL_DrawShaded;
835
836 //
837 // V_InitMode
838 //
839 void V_InitMode(video_mode_t mode) {
840 switch (mode) {
841 case VID_MODESW:
842 lprintf(LO_DEBUG, "V_InitMode: using software video mode\n");
843 V_BeginUIDraw = NULL_BeginUIDraw; // [XA] no-op in software
844 V_EndUIDraw = NULL_EndUIDraw; // [XA] ditto for the other begin/ends
845 V_BeginAutomapDraw = NULL_BeginAutomapDraw;
846 V_EndAutomapDraw = NULL_EndAutomapDraw;
847 V_BeginMenuDraw = NULL_BeginMenuDraw;
848 V_EndMenuDraw = NULL_EndMenuDraw;
849 V_CopyRect = FUNC_V_CopyRect;
850 V_FillRect = V_FillRect8;
851 V_DrawNumPatchGen = FUNC_V_DrawNumPatch;
852 V_DrawNumPatchGenPrecise = FUNC_V_DrawNumPatchPrecise;
853 V_FillFlat = FUNC_V_FillFlat;
854 V_FillPatch = FUNC_V_FillPatch;
855 V_DrawBackground = FUNC_V_DrawBackground;
856 V_PlotPixel = V_PlotPixel8;
857 V_PlotPixelWu = V_PlotPixelWu8;
858 V_DrawLine = WRAP_V_DrawLine;
859 V_DrawLineWu = WRAP_V_DrawLineWu;
860 V_DrawShaded = FUNC_V_DrawShaded;
861 current_videomode = VID_MODESW;
862 break;
863 case VID_MODEGL:
864 lprintf(LO_DEBUG, "V_InitMode: using OpenGL video mode\n");
865 V_BeginUIDraw = WRAP_gld_BeginUIDraw;
866 V_EndUIDraw = WRAP_gld_EndUIDraw;
867 V_BeginAutomapDraw = WRAP_gld_BeginAutomapDraw;
868 V_EndAutomapDraw = WRAP_gld_EndAutomapDraw;
869 V_BeginMenuDraw = WRAP_gld_BeginMenuDraw;
870 V_EndMenuDraw = WRAP_gld_EndMenuDraw;
871 V_CopyRect = WRAP_gld_CopyRect;
872 V_FillRect = WRAP_gld_FillRect;
873 V_DrawNumPatchGen = WRAP_gld_DrawNumPatch;
874 V_DrawNumPatchGenPrecise = WRAP_gld_DrawNumPatchPrecise;
875 V_FillFlat = WRAP_gld_FillFlat;
876 V_FillPatch = WRAP_gld_FillPatch;
877 V_DrawBackground = WRAP_gld_DrawBackground;
878 V_PlotPixel = V_PlotPixelGL;
879 V_PlotPixelWu = V_PlotPixelWuGL;
880 V_DrawLine = WRAP_gld_DrawLine;
881 V_DrawLineWu = WRAP_gld_DrawLine;
882 V_DrawShaded = WRAP_gld_DrawShaded;
883 current_videomode = VID_MODEGL;
884 break;
885 }
886 }
887
888 dboolean V_IsSoftwareMode(void) {
889 return current_videomode == VID_MODESW;
890 }
891
892 dboolean V_IsOpenGLMode(void) {
893 return current_videomode == VID_MODEGL;
894 }
895
896 dboolean V_IsUILightmodeIndexed(void) {
897 return gl_ui_lightmode_indexed;
898 }
899
900 dboolean V_IsAutomapLightmodeIndexed(void) {
901 return gl_automap_lightmode_indexed;
902 }
903
904 dboolean V_IsMenuLightmodeIndexed(void) {
905 return gl_menu_lightmode_indexed;
906 }
907
908 void V_CopyScreen(int srcscrn, int destscrn)
909 {
910 V_CopyRect(srcscrn, destscrn, 0, 0, SCREENWIDTH, SCREENHEIGHT, VPT_NONE);
911 }
912
913 //
914 // V_AllocScreen
915 //
916 void V_AllocScreen(screeninfo_t *scrn) {
917 if (!scrn->not_on_heap)
918 if ((scrn->pitch * scrn->height) > 0)
919 //e6y: Clear the screen to black.
920 scrn->data = Z_Calloc(scrn->pitch*scrn->height, 1);
921 }
922
923 //
924 // V_AllocScreens
925 //
926 void V_AllocScreens(void) {
927 int i;
928
929 for (i=0; i<NUM_SCREENS; i++)
930 V_AllocScreen(&screens[i]);
931 }
932
933 //
934 // V_FreeScreen
935 //
936 void V_FreeScreen(screeninfo_t *scrn) {
937 if (!scrn->not_on_heap) {
938 Z_Free(scrn->data);
939 scrn->data = NULL;
940 }
941 }
942
943 //
944 // V_FreeScreens
945 //
946 void V_FreeScreens(void) {
947 int i;
948
949 for (i=0; i<NUM_SCREENS; i++)
950 V_FreeScreen(&screens[i]);
951 }
952
953 static void V_PlotPixel8(int scrn, int x, int y, byte color) {
954 screens[scrn].data[x+screens[scrn].pitch*y] = color;
955 }
956
957 #define PUTDOT(xx,yy,cc) V_PlotPixel(0,xx,yy,(byte)cc)
958
959 //
960 // WRAP_V_DrawLine()
961 //
962 // Draw a line in the frame buffer.
963 // Classic Bresenham w/ whatever optimizations needed for speed
964 //
965 // Passed the frame coordinates of line, and the color to be drawn
966 // Returns nothing
967 //
968 static void WRAP_V_DrawLine(fline_t* fl, int color)
969 {
970 register int x;
971 register int y;
972 register int dx;
973 register int dy;
974 register int sx;
975 register int sy;
976 register int ax;
977 register int ay;
978 register int d;
979
980 #ifdef RANGECHECK // killough 2/22/98
981 static int fuck = 0;
982
983 // For debugging only
984 if
985 (
986 fl->a.x < 0 || fl->a.x >= SCREENWIDTH
987 || fl->a.y < 0 || fl->a.y >= SCREENHEIGHT
988 || fl->b.x < 0 || fl->b.x >= SCREENWIDTH
989 || fl->b.y < 0 || fl->b.y >= SCREENHEIGHT
990 )
991 {
992 //jff 8/3/98 use logical output routine
993 lprintf(LO_DEBUG, "fuck %d \r", fuck++);
994 return;
995 }
996 #endif
997
998 dx = fl->b.x - fl->a.x;
999 ax = 2 * (dx<0 ? -dx : dx);
1000 sx = dx<0 ? -1 : 1;
1001
1002 dy = fl->b.y - fl->a.y;
1003 ay = 2 * (dy<0 ? -dy : dy);
1004 sy = dy<0 ? -1 : 1;
1005
1006 x = fl->a.x;
1007 y = fl->a.y;
1008
1009 if (ax > ay)
1010 {
1011 d = ay - ax/2;
1012 while (1)
1013 {
1014 PUTDOT(x,y,color);
1015 if (x == fl->b.x) return;
1016 if (d>=0)
1017 {
1018 y += sy;
1019 d -= ax;
1020 }
1021 x += sx;
1022 d += ay;
1023 }
1024 }
1025 else
1026 {
1027 d = ax - ay/2;
1028 while (1)
1029 {
1030 PUTDOT(x, y, color);
1031 if (y == fl->b.y) return;
1032 if (d >= 0)
1033 {
1034 x += sx;
1035 d -= ay;
1036 }
1037 y += sy;
1038 d += ax;
1039 }
1040 }
1041 }
1042
1043 extern SDL_Surface *screen;
1044 #define RGB2COLOR(r, g, b)\
1045 ((r >> screen->format->Rloss) << screen->format->Rshift) |\
1046 ((g >> screen->format->Gloss) << screen->format->Gshift) |\
1047 ((b >> screen->format->Bloss) << screen->format->Bshift)\
1048
1049 // Given 65536, we need 2048; 65536 / 2048 == 32 == 2^5
1050 // Why 2048? ANG90 == 0x40000000 which >> 19 == 0x800 == 2048.
1051 // The trigonometric correction is based on an angle from 0 to 90.
1052 #define wu_fineshift 5
1053
1054 #define wu_weightbits 6
1055
1056 // Given 64 levels in the Col2RGB8 table, 65536 / 64 == 1024 == 2^10
1057 #define wu_fixedshift (16-wu_weightbits)
1058
1059 //
1060 // V_PlotPixelWu
1061 //
1062 // haleyjd 06/13/09: Pixel plotter for Wu line drawing.
1063 //
1064 static void V_PlotPixelWu8(int scrn, int x, int y, byte color, int weight)
1065 {
1066 unsigned int bg_color = screens[scrn].data[x+screens[scrn].pitch*y];
1067 unsigned int *fg2rgb = Col2RGB8[weight];
1068 unsigned int *bg2rgb = Col2RGB8[64 - weight];
1069 unsigned int fg = fg2rgb[color];
1070 unsigned int bg = bg2rgb[bg_color];
1071
1072 fg = (fg + bg) | 0x1f07c1f;
1073 V_PlotPixel(scrn, x, y, RGB32k[0][0][fg & (fg >> 15)]);
1074 }
1075
1076 //
1077 // WRAP_V_DrawLineWu
1078 //
1079 // haleyjd 06/12/09: Wu line drawing for the automap, with trigonometric
1080 // brightness correction by SoM. I call this the Wu-McGranahan line drawing
1081 // algorithm.
1082 //
1083 void WRAP_V_DrawLineWu(fline_t *fl, int color)
1084 {
1085 unsigned short erroracc, erroradj, erroracctmp;
1086 int dx, dy, xdir = 1;
1087 int x, y;
1088
1089 // swap end points if necessary
1090 if(fl->a.y > fl->b.y)
1091 {
1092 fpoint_t tmp = fl->a;
1093
1094 fl->a = fl->b;
1095 fl->b = tmp;
1096 }
1097
1098 // determine change in x, y and direction of travel
1099 dx = fl->b.x - fl->a.x;
1100 dy = fl->b.y - fl->a.y;
1101
1102 if(dx < 0)
1103 {
1104 dx = -dx;
1105 xdir = -xdir;
1106 }
1107
1108 // detect special cases -- horizontal, vertical, and 45 degrees;
1109 // revert to Bresenham
1110 if(dx == 0 || dy == 0 || dx == dy)
1111 {
1112 V_DrawLine(fl, color);
1113 return;
1114 }
1115
1116 // draw first pixel
1117 PUTDOT(fl->a.x, fl->a.y, color);
1118
1119 x = fl->a.x;
1120 y = fl->a.y;
1121
1122 if(dy > dx)
1123 {
1124 // line is y-axis major.
1125 erroracc = 0;
1126 erroradj = (unsigned short)(((unsigned int)dx << 16) / (unsigned int)dy);
1127
1128 while(--dy)
1129 {
1130 erroracctmp = erroracc;
1131
1132 erroracc += erroradj;
1133
1134 // if error has overflown, advance x coordinate
1135 if(erroracc <= erroracctmp)
1136 x += xdir;
1137
1138 y += 1; // advance y
1139
1140 // the trick is in the trig!
1141 V_PlotPixelWu(0, x, y, (byte)color,
1142 finecosine[erroracc >> wu_fineshift] >> wu_fixedshift);
1143 V_PlotPixelWu(0, x + xdir, y, (byte)color,
1144 finesine[erroracc >> wu_fineshift] >> wu_fixedshift);
1145 }
1146 }
1147 else
1148 {
1149 // line is x-axis major.
1150 erroracc = 0;
1151 erroradj = (unsigned short)(((unsigned int)dy << 16) / (unsigned int)dx);
1152
1153 while(--dx)
1154 {
1155 erroracctmp = erroracc;
1156
1157 erroracc += erroradj;
1158
1159 // if error has overflown, advance y coordinate
1160 if(erroracc <= erroracctmp)
1161 y += 1;
1162
1163 x += xdir; // advance x
1164
1165 // the trick is in the trig!
1166 V_PlotPixelWu(0, x, y, (byte)color,
1167 finecosine[erroracc >> wu_fineshift] >> wu_fixedshift);
1168 V_PlotPixelWu(0, x, y + 1, (byte)color,
1169 finesine[erroracc >> wu_fineshift] >> wu_fixedshift);
1170 }
1171 }
1172
1173 // draw last pixel
1174 PUTDOT(fl->b.x, fl->b.y, color);
1175 }
1176
1177
1178 const unsigned char* V_GetPlaypal(void)
1179 {
1180 dsda_playpal_t* playpal_data;
1181
1182 playpal_data = dsda_PlayPalData();
1183
1184 if (!playpal_data->lump)
1185 {
1186 int lump = W_GetNumForName(playpal_data->lump_name);
1187 const byte *data = W_LumpByNum(lump);
1188 playpal_data->length = W_LumpLength(lump);
1189 playpal_data->lump = Z_Malloc(playpal_data->length);
1190 memcpy(playpal_data->lump, data, playpal_data->length);
1191 }
1192
1193 return playpal_data->lump;
1194 }
1195
1196 void V_FreePlaypal(void)
1197 {
1198 dsda_FreePlayPal();
1199 }
1200
1201 int V_GetPlaypalCount(void)
1202 {
1203 V_GetPlaypal(); // ensure playpal data is initialized
1204 return (dsda_PlayPalData()->length / PALETTE_SIZE);
1205 }
1206
1207 //
1208 // V_GetPatchColor
1209 // Get the color of a Doom-Format
1210 // graphic via lumpnum
1211 //
1212
1213 SDL_Color V_GetPatchColor (int lumpnum)
1214 {
1215 SDL_Color col = {0,0,0,0};
1216 int r = 0, g = 0, b = 0;
1217 const unsigned char *playpal = V_GetPlaypal();
1218 int x, y, pixel_cnt = 0;
1219 const byte* lump;
1220 short width;
1221
1222 lump = W_LumpByNum(lumpnum);
1223
1224 width = *((const int16_t *) lump);
1225 width = LittleShort(width);
1226
1227 for (x = 0; x < width; ++x) {
1228 byte length;
1229 byte entry;
1230 const byte* p;
1231 int32_t offset;
1232
1233 // Only calculate for the leftmost and rightmost 16 columns
1234 if (width > 32 && x > 16 && x < width - 16)
1235 continue;
1236
1237 // Skip irrelevant data in the doom patch header
1238 p = lump + 8 + 4 * x;
1239 offset = *((const int32_t *) p);
1240 p = lump + LittleLong(offset);
1241
1242 while (*p != 0xff) {
1243 p++;
1244 length = *p++;
1245 p++;
1246
1247 // Get RGB values per pixel
1248 for (y = 0; y < length; ++y) {
1249 entry = *p++;
1250 r += playpal[3 * entry + 0];
1251 g += playpal[3 * entry + 1];
1252 b += playpal[3 * entry + 2];
1253 pixel_cnt++;
1254 }
1255 p++;
1256 }
1257 }
1258
1259 // Average RGB values
1260 col.r = r / pixel_cnt;
1261 col.g = g / pixel_cnt;
1262 col.b = b / pixel_cnt;
1263
1264 return col;
1265 }
1266
1267 void V_ClearBorder(void)
1268 {
1269 int bordtop, bordbottom, bordleft, bordright;
1270
1271 if (render_stretch_hud == patch_stretch_fit_to_width)
1272 return;
1273
1274 bordleft = wide_offsetx;
1275 bordright = wide_offset2x - wide_offsetx;
1276 bordtop = wide_offsety;
1277 bordbottom = wide_offset2y - wide_offsety;
1278
1279 if (bordtop > 0)
1280 {
1281 // Top
1282 V_FillRect(0, 0, 0, SCREENWIDTH, bordtop, 0);
1283 // Bottom
1284 V_FillRect(0, 0, SCREENHEIGHT - bordbottom, SCREENWIDTH, bordbottom, 0);
1285 }
1286
1287 if (bordleft > 0)
1288 {
1289 // Left
1290 V_FillRect(0, 0, bordtop, bordleft, SCREENHEIGHT - bordbottom - bordtop, 0);
1291 // Right
1292 V_FillRect(0, SCREENWIDTH - bordright, bordtop, bordright, SCREENHEIGHT - bordbottom - bordtop, 0);
1293 }
1294 }
1295
1296 // DWF 2012-05-10
1297 // Euclid's algorithm to find the greatest common divisor.
1298 static unsigned int gcd (unsigned int n, unsigned int d) { return (d ? gcd(d, n%d) : n); }
1299
1300 // DWF 2012-05-10
1301 // Reduce aspect ratio fractions to make the log messages nicer. Even if
1302 // integer math were still being used for FPS scaling, this would not
1303 // necessarily speed it up, but it does no harm.
1304 // Order of parameters (numerator, denominator) doesn't matter.
1305 static void ReduceFraction (unsigned *num1, unsigned *num2)
1306 {
1307 unsigned int g;
1308 assert(*num1 || *num2);
1309 g = gcd (*num1, *num2);
1310 *num1 /= g;
1311 *num2 /= g;
1312 }
1313
1314 // DWF 2012-05-01
1315 // C substitute for C++ std::swap.
1316 static void swap(unsigned int *num1, unsigned int *num2)
1317 {
1318 unsigned int temp = *num1;
1319 *num1 = *num2;
1320 *num2 = temp;
1321 }
1322
1323 // DWF 2012-05-01
1324 // Set global variables for video scaling.
1325 void SetRatio(int width, int height)
1326 {
1327 lprintf(LO_DEBUG, "SetRatio: width/height parameters %dx%d\n", width, height);
1328
1329 ratio_multiplier = width;
1330 ratio_scale = height;
1331 ReduceFraction(&ratio_multiplier, &ratio_scale);
1332
1333 // The terms storage aspect ratio, pixel aspect ratio, and display aspect
1334 // ratio came from Wikipedia. SAR x PAR = DAR
1335 lprintf(LO_DEBUG, "SetRatio: storage aspect ratio %u:%u\n", ratio_multiplier, ratio_scale);
1336 if (height == 200 || height == 400 || !dsda_IntConfig(dsda_config_aspect_ratio_correction))
1337 {
1338 lprintf(LO_DEBUG, "SetRatio: recognized VGA mode with pixel aspect ratio 5:6\n");
1339 ratio_multiplier = width * 5;
1340 ratio_scale = height * 6;
1341 ReduceFraction(&ratio_multiplier, &ratio_scale);
1342 }
1343 else
1344 {
1345 lprintf(LO_DEBUG, "SetRatio: assuming square pixels\n");
1346 }
1347 lprintf(LO_DEBUG, "SetRatio: display aspect ratio %u:%u\n", ratio_multiplier, ratio_scale);
1348
1349 // If user wants to force aspect ratio, let them.
1350 {
1351 unsigned int new_multiplier = ratio_multiplier;
1352 unsigned int new_scale = ratio_scale;
1353 int render_aspect = dsda_IntConfig(dsda_config_render_aspect);
1354
1355 // Hardcoded to match render_aspects_list
1356 switch (render_aspect)
1357 {
1358 case 0:
1359 break;
1360 case 1:
1361 new_multiplier = 16;
1362 new_scale = 9;
1363 break;
1364 case 2:
1365 new_multiplier = 16;
1366 new_scale = 10;
1367 break;
1368 case 3:
1369 new_multiplier = 4;
1370 new_scale = 3;
1371 break;
1372 case 4:
1373 new_multiplier = 5;
1374 new_scale = 4;
1375 break;
1376 default:
1377 lprintf(LO_ERROR, "SetRatio: render_aspect has invalid value %d\n", render_aspect);
1378 }
1379
1380 if (ratio_multiplier != new_multiplier || ratio_scale != new_scale)
1381 {
1382 lprintf(LO_DEBUG, "SetRatio: overruled by user configuration setting\n");
1383 ratio_multiplier = new_multiplier;
1384 ratio_scale = new_scale;
1385 lprintf(LO_DEBUG, "SetRatio: revised display aspect ratio %u:%u\n", ratio_multiplier, ratio_scale);
1386 }
1387 }
1388
1389 gl_ratio = RMUL * ratio_multiplier / ratio_scale;
1390 lprintf(LO_DEBUG, "SetRatio: gl_ratio %f\n", gl_ratio);
1391
1392 // Calculate modified multiplier following the pattern of the old
1393 // BaseRatioSizes table in PrBoom-Plus 2.5.1.3.
1394 swap(&ratio_multiplier, &ratio_scale);
1395 ratio_multiplier *= 4;
1396 ratio_scale *= 3;
1397 ReduceFraction(&ratio_multiplier, &ratio_scale);
1398
1399 tallscreen = (ratio_scale < ratio_multiplier);
1400 if (tallscreen)
1401 {
1402 float ratio_quotient = (float)ratio_multiplier/ratio_scale;
1403 float ratio_percentage = (ratio_quotient - 1) * 100.0;
1404 psprite_offset = (int)(ratio_percentage*FRACUNIT);
1405
1406 lprintf(LO_DEBUG, "SetRatio: tallscreen aspect recognized; flipping multiplier\n");
1407 swap(&ratio_multiplier, &ratio_scale);
1408 }
1409 else
1410 {
1411 psprite_offset = 0;
1412 }
1413 lprintf(LO_DEBUG, "SetRatio: multiplier %u/%u\n", ratio_multiplier, ratio_scale);
1414
1415 // The rest is carried over from CheckRatio in PrBoom-Plus 2.5.1.3.
1416 if (tallscreen)
1417 {
1418 WIDE_SCREENWIDTH = SCREENWIDTH;
1419 WIDE_SCREENHEIGHT = SCREENHEIGHT * ratio_multiplier / ratio_scale;
1420 }
1421 else
1422 {
1423 WIDE_SCREENWIDTH = SCREENWIDTH * ratio_multiplier / ratio_scale;
1424 WIDE_SCREENHEIGHT = SCREENHEIGHT;
1425 }
1426
1427 WIDE_SCREENWIDTH = MAX(1, WIDE_SCREENWIDTH);
1428 WIDE_SCREENHEIGHT = MAX(1, WIDE_SCREENHEIGHT);
1429
1430 yaspectmul = Scale((320<<FRACBITS), WIDE_SCREENHEIGHT, 200 * WIDE_SCREENWIDTH);
1431
1432 dsda_EvaluatePatchScale();
1433
1434 SCREEN_320x200 =
1435 (SCREENWIDTH == 320) && (SCREENHEIGHT == 200) &&
1436 (WIDE_SCREENWIDTH == 320) && (WIDE_SCREENHEIGHT == 200);
1437
1438 // [FG] support widescreen status bar backgrounds
1439 ST_SetScaledWidth();
1440 }
1441
1442 void V_GetWideRect(int *x, int *y, int *w, int *h, enum patch_translation_e flags)
1443 {
1444 stretch_param_t *params = dsda_StretchParams(flags);
1445 int sx = *x;
1446 int sy = *y;
1447
1448 *x = params->video->x1lookup[*x];
1449 *y = params->video->y1lookup[*y];
1450 *w = params->video->x2lookup[sx + *w - 1] - *x + 1;
1451 *h = params->video->y2lookup[sy + *h - 1] - *y + 1;
1452 *x += params->deltax1;
1453 *y += params->deltay1;
1454 }
1455
1456 //
1457 // V_BestColor
1458 //
1459 // Adapted from zdoom -- thanks to Randy Heit.
1460 //
1461 // This always assumes a 256-color palette;
1462 // it's intended for use in startup functions to match hard-coded
1463 // color values to the best fit in the game's palette (allows
1464 // cross-game usage among other things).
1465 //
1466 int V_BestColor(const unsigned char *palette, int r, int g, int b)
1467 {
1468 int color;
1469
1470 // use color 0 as a worst-case match for any color
1471 int bestcolor = 0;
1472 int bestdist = 257 * 257 + 257 * 257 + 257 * 257;
1473
1474 for (color = 0; color < 256; color++)
1475 {
1476 int dr, dg, db, dist;
1477
1478 dr = r - *palette++;
1479 dg = g - *palette++;
1480 db = b - *palette++;
1481
1482 dist = dr * dr + dg * dg + db * db;
1483
1484 if (dist < bestdist)
1485 {
1486 // exact match
1487 if (dist == 0)
1488 return color;
1489
1490 bestdist = dist;
1491 bestcolor = color;
1492 }
1493 }
1494
1495 return bestcolor;
1496 }
1497
1498 // Alt-Enter: fullscreen <-> windowed
1499 void V_ToggleFullscreen(void)
1500 {
1501 dsda_UpdateIntConfig(dsda_config_use_fullscreen, !desired_fullscreen, true);
1502 }
1503
1504 void V_ChangeScreenResolution(void)
1505 {
1506 I_UpdateVideoMode();
1507
1508 if (V_IsOpenGLMode())
1509 {
1510 gld_PreprocessLevel();
1511 }
1512 }
1513
1514 void V_FillRectVPT(int scrn, int x, int y, int width, int height, byte color, enum patch_translation_e flags)
1515 {
1516 V_GetWideRect(&x, &y, &width, &height, flags);
1517 V_FillRect(scrn, x, y, width, height, color);
1518 }
1519
1520 int V_FillHeightVPT(int scrn, int y, int height, byte color, enum patch_translation_e flags)
1521 {
1522 stretch_param_t *params = dsda_StretchParams(flags);
1523 int sy = y;
1524
1525 y = params->video->y1lookup[y];
1526 height = params->video->y2lookup[sy + height - 1] - y + 1;
1527 y += params->deltay1;
1528 V_FillRect(scrn, 0, y, SCREENWIDTH, height, color);
1529
1530 return height;
1531 }
1532
1533 // heretic
1534
1535 // heretic_note: is something already implemented to handle this?
1536 void V_DrawRawScreen(const char *lump_name)
1537 {
1538 V_DrawRawScreenSection(lump_name, 0, 0, 200);
1539 }
1540
1541 void V_DrawRawScreenSection(const char *lump_name, int source_offset, int dest_y_offset, int dest_y_limit)
1542 {
1543 int i, j;
1544 float x_factor = 0, y_factor = 0;
1545 int x_offset, y_offset;
1546 const byte* raw;
1547 int lump_num = W_CheckNumForName(lump_name);
1548 int lump_width = W_LumpLength(lump_num) / 200;
1549
1550 // e6y: wide-res
1551 // NOTE: the size isn't quite right on all resolutions,
1552 // which causes the black bars on the edge of heretic E3's
1553 // bottom endscreen to overlap the top screen during scrolling.
1554 // this happens in both software and GL at the time of writing.
1555 V_ClearBorder();
1556
1557 // custom widescreen assets are a different format
1558 if (R_IsPatchLump(lump_num))
1559 {
1560 V_DrawNamePatchFS(0, 0, 0, lump_name, CR_DEFAULT, VPT_STRETCH);
1561 return;
1562 }
1563
1564 // aspect ratio correction
1565 switch (render_stretch_hud) {
1566 case patch_stretch_not_adjusted:
1567 x_factor = (float)SCREENWIDTH / 320;
1568 y_factor = (float)SCREENHEIGHT / 200;
1569 if (y_factor < x_factor)
1570 x_factor = y_factor;
1571 break;
1572 case patch_stretch_doom_format:
1573 x_factor = (float)WIDE_SCREENWIDTH / 320;
1574 y_factor = (float)WIDE_SCREENHEIGHT / 200;
1575 if (y_factor < x_factor)
1576 x_factor = y_factor;
1577 break;
1578 case patch_stretch_fit_to_width:
1579 x_factor = (float)SCREENWIDTH / 320;
1580 y_factor = (float)SCREENHEIGHT / 200;
1581 break;
1582 }
1583
1584 x_offset = (int)((SCREENWIDTH - (x_factor * lump_width)) / 2);
1585 y_offset = (int)((dest_y_offset * y_factor) - (source_offset * y_factor / lump_width));
1586
1587 // TODO: create a V_FillRaw alias and call that instead of the gld_ func directly,
1588 // though that means there needs to be a software version too (that's ideally a
1589 // bit more efficient than the current code's thousands-of-little-boxes approach)
1590 if (V_IsOpenGLMode()) {
1591 gld_FillRawName(lump_name, x_offset, y_offset, lump_width, 200, lump_width * x_factor, 200 * y_factor, VPT_STRETCH_REAL);
1592 return;
1593 }
1594
1595 raw = (const byte *)W_LumpByName(lump_name) + source_offset;
1596
1597 for (j = dest_y_offset; j < dest_y_offset + dest_y_limit; ++j)
1598 for (i = 0; i < lump_width; ++i, ++raw)
1599 {
1600 int x, y, width, height, x_pos;
1601
1602 x = (int)(i * x_factor);
1603 y = (int)(j * y_factor);
1604 width = (int)((i + 1) * x_factor) - x;
1605 height = (int)((j + 1) * y_factor) - y;
1606
1607 x_pos = x_offset + x;
1608
1609 // Don't draw pixels outside screen
1610 if ((x_pos < 0) || (x_pos > SCREENWIDTH - width))
1611 continue;
1612
1613 V_FillRect(0, x_pos, y, width, height, *raw);
1614 }
1615 }
1616
1617 void V_DrawShadowedNumPatch(int x, int y, int lump)
1618 {
1619 V_DrawNumPatch(x, y, 0, lump, CR_DEFAULT, VPT_STRETCH);
1620 }
1621
1622 void V_DrawShadowedNamePatch(int x, int y, const char* name)
1623 {
1624 V_DrawNamePatch(x, y, 0, name, CR_DEFAULT, VPT_STRETCH);
1625 }
1626
1627 void V_DrawTLNumPatch(int x, int y, int lump)
1628 {
1629 V_DrawNumPatch(x, y, 0, lump, CR_DEFAULT, VPT_STRETCH);
1630 }
1631
1632 void V_DrawTLNamePatch(int x, int y, const char* name)
1633 {
1634 V_DrawNamePatch(x, y, 0, name, CR_DEFAULT, VPT_STRETCH);
1635 }
1636
1637 void V_DrawAltTLNumPatch(int x, int y, int lump)
1638 {
1639 V_DrawNumPatch(x, y, 0, lump, CR_DEFAULT, VPT_STRETCH);
1640 }
1641
1642 // void V_DrawShadowedPatch(int x, int y, patch_t *patch)
1643 // {
1644 // int count, col;
1645 // column_t *column;
1646 // pixel_t *desttop, *dest;
1647 // byte *source;
1648 // pixel_t *desttop2, *dest2;
1649 // int w;
1650 //
1651 // y -= SHORT(patch->topoffset);
1652 // x -= SHORT(patch->leftoffset);
1653 //
1654 // if (x < 0
1655 // || x + SHORT(patch->width) > ORIGWIDTH
1656 // || y < 0
1657 // || y + SHORT(patch->height) > ORIGHEIGHT)
1658 // {
1659 // I_Error("Bad V_DrawShadowedPatch");
1660 // }
1661 //
1662 // col = 0;
1663 // desttop = dest_screen + ((y * dy) >> FRACBITS) * SCREENWIDTH + ((x * dx) >> FRACBITS);
1664 // desttop2 = dest_screen + (((y + 2) * dy) >> FRACBITS) * SCREENWIDTH + (((x + 2) * dx) >> FRACBITS);
1665 //
1666 // w = SHORT(patch->width);
1667 // for (; col < w << FRACBITS; x++, col+=dxi, desttop++, desttop2++)
1668 // {
1669 // column = (column_t *) ((byte *) patch + LONG(patch->columnofs[col >> FRACBITS]));
1670 //
1671 // // step through the posts in a column
1672 //
1673 // while (column->topdelta != 0xff)
1674 // {
1675 // int srccol = 0;
1676 // source = (byte *) column + 3;
1677 // dest = desttop + ((column->topdelta * dy) >> FRACBITS) * SCREENWIDTH;
1678 // dest2 = desttop2 + ((column->topdelta * dy) >> FRACBITS) * SCREENWIDTH;
1679 // count = (column->length * dy) >> FRACBITS;
1680 //
1681 // while (count--)
1682 // {
1683 // *dest2 = tinttable[((*dest2) << 8)];
1684 // dest2 += SCREENWIDTH;
1685 // *dest = source[srccol >> FRACBITS];
1686 // srccol += dyi;
1687 // dest += SCREENWIDTH;
1688 //
1689 // }
1690 // column = (column_t *) ((byte *) column + column->length + 4);
1691 // }
1692 // }
1693 // }
1694
1695 // void V_DrawTLPatch(int x, int y, patch_t * patch)
1696 // {
1697 // int count, col;
1698 // column_t *column;
1699 // pixel_t *desttop, *dest;
1700 // byte *source;
1701 // int w;
1702 //
1703 // y -= SHORT(patch->topoffset);
1704 // x -= SHORT(patch->leftoffset);
1705 //
1706 // if (x < 0
1707 // || x + SHORT(patch->width) > ORIGWIDTH
1708 // || y < 0
1709 // || y + SHORT(patch->height) > ORIGHEIGHT)
1710 // {
1711 // I_Error("Bad V_DrawTLPatch");
1712 // }
1713 //
1714 // col = 0;
1715 // desttop = dest_screen + ((y * dy) >> FRACBITS) * SCREENWIDTH + ((x * dx) >> FRACBITS);
1716 //
1717 // w = SHORT(patch->width);
1718 // for (; col < w << FRACBITS; x++, col+=dxi, desttop++)
1719 // {
1720 // column = (column_t *) ((byte *) patch + LONG(patch->columnofs[col >> FRACBITS]));
1721 //
1722 // // step through the posts in a column
1723 //
1724 // while (column->topdelta != 0xff)
1725 // {
1726 // int srccol = 0;
1727 // source = (byte *) column + 3;
1728 // dest = desttop + ((column->topdelta * dy) >> FRACBITS) * SCREENWIDTH;
1729 // count = (column->length * dy) >> FRACBITS;
1730 //
1731 // while (count--)
1732 // {
1733 // *dest = tinttable[((*dest) << 8) + source[srccol >> FRACBITS]];
1734 // srccol += dyi;
1735 // dest += SCREENWIDTH;
1736 // }
1737 // column = (column_t *) ((byte *) column + column->length + 4);
1738 // }
1739 // }
1740 // }