set the include directory
[AROS-Contrib.git] / Games / Doom / v_video.c
blob6a5b513d71c48c263424a691fdcac46961b5f6c2
1 // Emacs style mode select -*- C++ -*-
2 //-----------------------------------------------------------------------------
3 //
4 // $Id$
5 //
6 // Copyright (C) 1993-1996 by id Software, Inc.
7 //
8 // This source is available for distribution and/or modification
9 // only under the terms of the DOOM Source Code License as
10 // published by id Software. All rights reserved.
12 // The source is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License
15 // for more details.
17 // $Log$
18 // Revision 1.1 2000/02/29 18:21:05 stegerg
19 // Doom port based on ADoomPPC. Read README.AROS!
22 // DESCRIPTION:
23 // Gamma correction LUT stuff.
24 // Functions to draw patches (by post) directly to screen.
25 // Functions to blit a block to the screen.
27 //-----------------------------------------------------------------------------
29 static const char
30 rcsid[] = "$Id$";
32 #include <exec/exec.h>
34 #ifdef AROS
35 #define MEMF_NOCACHESYNCPPC 0
36 #define MEMF_NOCACHESYNCM68K 0
37 #define PPCAllocVec(size,flags) I_AllocLow(size)
38 #else
39 #include <powerup/clib/ppc_protos.h>
40 #include <powerup/ppclib/memory.h>
41 #endif
43 #include "i_system.h"
44 #include "i_video.h"
45 #include "r_local.h"
47 #include "doomdef.h"
48 #include "doomstat.h"
49 #include "doomdata.h"
51 #include "m_bbox.h"
52 #include "m_swap.h"
53 #include "m_argv.h"
54 #include "am_map.h"
55 #include "r_draw.h"
57 #include "v_video.h"
59 #include "st_stuff.h"
61 extern int weirdaspect;
63 // Screen buffer memory
64 byte *vid_mem;
66 // Each screen is [SCREENWIDTH*SCREENHEIGHT];
67 byte* screens[5];
69 int dirtybox[4];
73 // Now where did these came from?
74 byte gammatable[5][256] =
76 {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,
77 17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,
78 33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,
79 49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,
80 65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,
81 81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,
82 97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,
83 113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,
84 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
85 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
86 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
87 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
88 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
89 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
90 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
91 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255},
93 {2,4,5,7,8,10,11,12,14,15,16,18,19,20,21,23,24,25,26,27,29,30,31,
94 32,33,34,36,37,38,39,40,41,42,44,45,46,47,48,49,50,51,52,54,55,
95 56,57,58,59,60,61,62,63,64,65,66,67,69,70,71,72,73,74,75,76,77,
96 78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,
97 99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,
98 115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,129,
99 130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,
100 146,147,148,148,149,150,151,152,153,154,155,156,157,158,159,160,
101 161,162,163,163,164,165,166,167,168,169,170,171,172,173,174,175,
102 175,176,177,178,179,180,181,182,183,184,185,186,186,187,188,189,
103 190,191,192,193,194,195,196,196,197,198,199,200,201,202,203,204,
104 205,205,206,207,208,209,210,211,212,213,214,214,215,216,217,218,
105 219,220,221,222,222,223,224,225,226,227,228,229,230,230,231,232,
106 233,234,235,236,237,237,238,239,240,241,242,243,244,245,245,246,
107 247,248,249,250,251,252,252,253,254,255},
109 {4,7,9,11,13,15,17,19,21,22,24,26,27,29,30,32,33,35,36,38,39,40,42,
110 43,45,46,47,48,50,51,52,54,55,56,57,59,60,61,62,63,65,66,67,68,69,
111 70,72,73,74,75,76,77,78,79,80,82,83,84,85,86,87,88,89,90,91,92,93,
112 94,95,96,97,98,100,101,102,103,104,105,106,107,108,109,110,111,112,
113 113,114,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,
114 129,130,131,132,133,133,134,135,136,137,138,139,140,141,142,143,144,
115 144,145,146,147,148,149,150,151,152,153,153,154,155,156,157,158,159,
116 160,160,161,162,163,164,165,166,166,167,168,169,170,171,172,172,173,
117 174,175,176,177,178,178,179,180,181,182,183,183,184,185,186,187,188,
118 188,189,190,191,192,193,193,194,195,196,197,197,198,199,200,201,201,
119 202,203,204,205,206,206,207,208,209,210,210,211,212,213,213,214,215,
120 216,217,217,218,219,220,221,221,222,223,224,224,225,226,227,228,228,
121 229,230,231,231,232,233,234,235,235,236,237,238,238,239,240,241,241,
122 242,243,244,244,245,246,247,247,248,249,250,251,251,252,253,254,254,
123 255},
125 {8,12,16,19,22,24,27,29,31,34,36,38,40,41,43,45,47,49,50,52,53,55,
126 57,58,60,61,63,64,65,67,68,70,71,72,74,75,76,77,79,80,81,82,84,85,
127 86,87,88,90,91,92,93,94,95,96,98,99,100,101,102,103,104,105,106,107,
128 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,
129 125,126,127,128,129,130,131,132,133,134,135,135,136,137,138,139,140,
130 141,142,143,143,144,145,146,147,148,149,150,150,151,152,153,154,155,
131 155,156,157,158,159,160,160,161,162,163,164,165,165,166,167,168,169,
132 169,170,171,172,173,173,174,175,176,176,177,178,179,180,180,181,182,
133 183,183,184,185,186,186,187,188,189,189,190,191,192,192,193,194,195,
134 195,196,197,197,198,199,200,200,201,202,202,203,204,205,205,206,207,
135 207,208,209,210,210,211,212,212,213,214,214,215,216,216,217,218,219,
136 219,220,221,221,222,223,223,224,225,225,226,227,227,228,229,229,230,
137 231,231,232,233,233,234,235,235,236,237,237,238,238,239,240,240,241,
138 242,242,243,244,244,245,246,246,247,247,248,249,249,250,251,251,252,
139 253,253,254,254,255},
141 {16,23,28,32,36,39,42,45,48,50,53,55,57,60,62,64,66,68,69,71,73,75,76,
142 78,80,81,83,84,86,87,89,90,92,93,94,96,97,98,100,101,102,103,105,106,
143 107,108,109,110,112,113,114,115,116,117,118,119,120,121,122,123,124,
144 125,126,128,128,129,130,131,132,133,134,135,136,137,138,139,140,141,
145 142,143,143,144,145,146,147,148,149,150,150,151,152,153,154,155,155,
146 156,157,158,159,159,160,161,162,163,163,164,165,166,166,167,168,169,
147 169,170,171,172,172,173,174,175,175,176,177,177,178,179,180,180,181,
148 182,182,183,184,184,185,186,187,187,188,189,189,190,191,191,192,193,
149 193,194,195,195,196,196,197,198,198,199,200,200,201,202,202,203,203,
150 204,205,205,206,207,207,208,208,209,210,210,211,211,212,213,213,214,
151 214,215,216,216,217,217,218,219,219,220,220,221,221,222,223,223,224,
152 224,225,225,226,227,227,228,228,229,229,230,230,231,232,232,233,233,
153 234,234,235,235,236,236,237,237,238,239,239,240,240,241,241,242,242,
154 243,243,244,244,245,245,246,246,247,247,248,248,249,249,250,250,251,
155 251,252,252,253,254,254,255,255}
160 int usegamma;
163 // V_MarkRect
166 void
167 V_MarkRect
168 ( int x,
169 int y,
170 int width,
171 int height )
173 M_AddToBox (dirtybox, x, y);
174 M_AddToBox (dirtybox, x+width-1, y+height-1);
180 // V_CopyRect
182 void
183 V_CopyRect
184 ( int srcx,
185 int srcy,
186 int srcscrn,
187 int width,
188 int height,
189 int destx,
190 int desty,
191 int destscrn )
193 byte* src;
194 byte* dest;
196 #ifdef RANGECHECK
197 if (srcx<0
198 ||srcx+width >SCREENWIDTH
199 || srcy<0
200 || srcy+height>SCREENHEIGHT
201 ||destx<0||destx+width >SCREENWIDTH
202 || desty<0
203 || desty+height>SCREENHEIGHT
204 || (unsigned)srcscrn>4
205 || (unsigned)destscrn>4)
207 I_Error ("Bad V_CopyRect");
209 #endif
210 src = screens[srcscrn]+SCREENWIDTH*srcy+srcx;
211 dest = screens[destscrn]+SCREENWIDTH*desty+destx;
213 for ( ; height>0 ; height--)
215 memcpy (dest, src, width);
216 src += SCREENWIDTH;
217 dest += SCREENWIDTH;
220 if (destscrn == 0)
221 I_MarkRect (destx, desty, width, height);
226 // V_DrawPatch
227 // Masks a column based masked pic to the screen.
229 void
230 V_DrawPatch
231 ( int x,
232 int y,
233 int scrn,
234 patch_t* patch )
237 int count;
238 int col;
239 column_t* column;
240 byte* desttop;
241 byte* dest;
242 byte* source;
243 int w;
244 int x0;
246 y -= SWAPSHORT(patch->topoffset);
247 x -= SWAPSHORT(patch->leftoffset);
248 #ifdef RANGECHECK
249 if (x<0
250 ||x+SWAPSHORT(patch->width) >SCREENWIDTH
251 || y<0
252 || y+SWAPSHORT(patch->height)>SCREENHEIGHT
253 || (unsigned)scrn>4)
255 fprintf( stderr, "Patch at %d,%d exceeds LFB\n", x,y );
256 // No I_Error abort - what is up with TNT.WAD?
257 fprintf( stderr, "V_DrawPatch: bad patch (ignored)\n");
258 return;
260 #endif
262 x0 = x;
263 col = 0;
264 desttop = screens[scrn]+y*SCREENWIDTH+x;
266 w = SWAPSHORT(patch->width);
268 for ( ; col<w ; x++, col++, desttop++)
270 column = (column_t *)((byte *)patch + SWAPLONG(patch->columnofs[col]));
272 // step through the posts in a column
273 while (column->topdelta != 0xff )
275 source = (byte *)column + 3;
276 dest = desttop + column->topdelta*SCREENWIDTH;
277 count = column->length;
279 while (count--)
281 *dest = *source++;
282 dest += SCREENWIDTH;
284 column = (column_t *)( (byte *)column + column->length
285 + 4 );
288 if (scrn == 0)
289 I_MarkRect (x0, y, SWAPSHORT(patch->width), SWAPSHORT(patch->height));
293 // V_DrawPatchFlipped
294 // Masks a column based masked pic to the screen.
295 // Flips horizontally, e.g. to mirror face.
297 void
298 V_DrawPatchFlipped
299 ( int x,
300 int y,
301 int scrn,
302 patch_t* patch )
305 int count;
306 int col;
307 column_t* column;
308 byte* desttop;
309 byte* dest;
310 byte* source;
311 int w;
312 int x0;
314 y -= SWAPSHORT(patch->topoffset);
315 x -= SWAPSHORT(patch->leftoffset);
316 #ifdef RANGECHECK
317 if (x<0
318 ||x+SWAPSHORT(patch->width) >SCREENWIDTH
319 || y<0
320 || y+SWAPSHORT(patch->height)>SCREENHEIGHT
321 || (unsigned)scrn>4)
323 fprintf( stderr, "Patch origin %d,%d exceeds LFB\n", x,y );
324 I_Error ("Bad V_DrawPatch in V_DrawPatchFlipped");
326 #endif
328 x0 = x;
329 col = 0;
330 desttop = screens[scrn]+y*SCREENWIDTH+x;
332 w = SWAPSHORT(patch->width);
334 for ( ; col<w ; x++, col++, desttop++)
336 column = (column_t *)((byte *)patch + SWAPLONG(patch->columnofs[w-1-col]));
338 // step through the posts in a column
339 while (column->topdelta != 0xff )
341 source = (byte *)column + 3;
342 dest = desttop + column->topdelta*SCREENWIDTH;
343 count = column->length;
345 while (count--)
347 *dest = *source++;
348 dest += SCREENWIDTH;
350 column = (column_t *)( (byte *)column + column->length
351 + 4 );
354 if (scrn == 0)
355 I_MarkRect (x0, y, SWAPSHORT(patch->width), SWAPSHORT(patch->height));
361 // V_DrawPatchDirect
362 // Draws directly to the screen on the pc.
364 void
365 V_DrawPatchDirect
366 ( int x,
367 int y,
368 int scrn,
369 patch_t* patch )
371 V_DrawPatch (x,y,scrn, patch);
374 void //stretches bitmap to fill screen
375 V_DrawPatchInDirect
376 ( int x,
377 int y,
378 int scrn,
379 patch_t* patch )
381 int count;
382 int col;
383 column_t* column;
384 byte* desttop;
385 byte* dest;
386 byte* source;
387 int w;
388 int x0;
390 int deltax,deltay,deltaxi,deltayi,stretchx,stretchy;
391 int srccol,collen;
393 y -= SWAPSHORT(patch->topoffset);
394 x -= SWAPSHORT(patch->leftoffset);
396 #ifdef RANGECHECK
397 if (x<0
398 ||x+SWAPSHORT(patch->width)>320
399 || y<0
400 || y+SWAPSHORT(patch->height)>200
401 || (unsigned)scrn>4)
403 fprintf( stderr, "Patch at %d,%d exceeds LFB\n", x,y );
404 // No I_Error abort - what is up with TNT.WAD?
405 fprintf( stderr, "V_DrawPatch: bad patch (ignored)\n");
406 return;
408 #endif
410 deltax=(SCREENWIDTH<<16)/320;
411 deltaxi=(320<<16)/SCREENWIDTH;
412 deltay=(SCREENHEIGHT<<16)/200;
413 deltayi=(200<<16)/SCREENHEIGHT;
414 stretchx=(x*deltax)>>16;
415 stretchy=(y*deltay)>>16;
417 x0 = x;
418 col = 0;
419 desttop = screens[scrn]+stretchy*SCREENWIDTH+stretchx;
421 w = SWAPSHORT(patch->width)<<16;
423 for ( ; col < w; x++, col += deltaxi, desttop++) {
424 column = (column_t *)((byte *)patch + SWAPLONG(patch->columnofs[col>>16]));
426 // step through the posts in a column
427 while (column->topdelta != 0xff ) {
428 source = (byte *)column + 3;
429 dest = desttop+((column->topdelta*deltay)>>16)*SCREENWIDTH;
430 collen = count = (column->length*deltay)>>16;
431 srccol = 0;
432 while (count--) {
433 *dest = source[srccol>>16];
434 dest += SCREENWIDTH;
435 srccol += deltayi;
437 column = (column_t *)((byte *)column + (column->length)+4);
440 if (scrn == 0)
441 I_MarkRect (x0, y, SWAPSHORT(patch->width), SWAPSHORT(patch->height));
445 void //stretches bitmap to fill screen
446 V_DrawPatchInDirectFlipped
447 ( int x,
448 int y,
449 int scrn,
450 patch_t* patch )
452 int count;
453 int col;
454 column_t* column;
455 byte* desttop;
456 byte* dest;
457 byte* source;
458 int w;
460 int deltax,deltay,deltaxi,deltayi,stretchx,stretchy;
461 int srccol,collen;
463 y -= SWAPSHORT(patch->topoffset);
464 x -= SWAPSHORT(patch->leftoffset);
465 #ifdef RANGECHECK
466 if (x<0
467 ||x+SWAPSHORT(patch->width) >320
468 || y<0
469 || y+SWAPSHORT(patch->height)>200
470 || (unsigned)scrn>4)
472 fprintf( stderr, "Patch at %d,%d exceeds LFB\n", x,y );
473 // No I_Error abort - what is up with TNT.WAD?
474 fprintf( stderr, "V_DrawPatch: bad patch (ignored)\n");
475 return;
477 #endif
479 deltax=(SCREENWIDTH<<16)/320;
480 deltaxi=(320<<16)/SCREENWIDTH;
481 deltay=(SCREENHEIGHT<<16)/200;
482 deltayi=(200<<16)/SCREENHEIGHT;
483 stretchx=(x*deltax)>>16;
484 stretchy=(y*deltay)>>16;
486 col = 0;
487 desttop = screens[scrn]+stretchy*SCREENWIDTH+stretchx;
489 w = SWAPSHORT(patch->width)<<16;
491 for ( ; col < w; x++, col += deltaxi, desttop++) {
492 column = (column_t *)((byte *)patch +
493 SWAPLONG(patch->columnofs[SWAPSHORT(patch->width)-1-(col>>16)]));
495 // step through the posts in a column
496 while (column->topdelta != 0xff) {
497 source = (byte *)column + 3;
498 dest = desttop+((column->topdelta*deltay)>>16)*SCREENWIDTH;
499 collen = count = (column->length*deltay)>>16;
500 srccol=0;
501 while (count--) {
502 *dest=source[srccol>>16];
503 dest += SCREENWIDTH;
504 srccol+=deltayi;
506 column = (column_t *)((byte *)column+(column->length)+4);
509 if (scrn == 0)
510 I_MarkRect (stretchx,stretchy,(SWAPSHORT(patch->width)*deltax)>>16,
511 (SWAPSHORT(patch->height)*deltax)>>16);
517 // V_DrawBlock
518 // Draw a linear block of pixels into the view buffer.
520 void
521 V_DrawBlock
522 ( int x,
523 int y,
524 int scrn,
525 int width,
526 int height,
527 byte* src )
529 byte* dest;
530 int height0;
532 #ifdef RANGECHECK
533 if (x<0
534 ||x+width >SCREENWIDTH
535 || y<0
536 || y+height>SCREENHEIGHT
537 || (unsigned)scrn>4 )
539 I_Error ("Bad V_DrawBlock");
541 #endif
543 height0 = height;
544 dest = screens[scrn] + y*SCREENWIDTH+x;
546 while (height--)
548 memcpy (dest, src, width);
549 src += width;
550 dest += SCREENWIDTH;
552 if (scrn == 0)
553 I_MarkRect (x, y, width, height0);
559 // V_GetBlock
560 // Gets a linear block of pixels from the view buffer.
562 void
563 V_GetBlock
564 ( int x,
565 int y,
566 int scrn,
567 int width,
568 int height,
569 byte* dest )
571 byte* src;
573 #ifdef RANGECHECK
574 if (x<0
575 ||x+width >SCREENWIDTH
576 || y<0
577 || y+height>SCREENHEIGHT
578 || (unsigned)scrn>4 )
580 I_Error ("Bad V_DrawBlock");
582 #endif
584 src = screens[scrn] + y*SCREENWIDTH+x;
586 while (height--)
588 memcpy (dest, src, width);
589 src += SCREENWIDTH;
590 dest += width;
595 #ifdef GRAFFITI
596 extern int video_graffiti;
597 #endif
601 // V_Init
603 void V_Init (void)
605 int i;
606 int p;
607 int vid_flags;
608 int vid_size;
610 #ifdef GRAFFITI
611 video_graffiti = 0;
612 if (M_CheckParm ("-graffitilo")) {
613 video_graffiti = 1;
614 SCREENWIDTH = 320;
615 SCREENHEIGHT = 200;
616 } else if (M_CheckParm ("-graffitihi")) {
617 video_graffiti = 2;
618 SCREENWIDTH = 640;
619 SCREENHEIGHT = 400;
620 } else {
621 #endif
623 p = M_CheckParm ("-width");
624 if (p && p < myargc-1)
625 SCREENWIDTH = atoi(myargv[p+1]);
626 else
627 SCREENWIDTH = 320;
629 p = M_CheckParm ("-height");
630 if (p && p < myargc-1)
631 SCREENHEIGHT = atoi(myargv[p+1]);
632 else
633 SCREENHEIGHT = 200;
635 #ifdef GRAFFITI
637 #endif
639 printf ("Resolution: %d x %d\n", SCREENWIDTH, SCREENHEIGHT);
641 if (SCREENWIDTH < 320 || SCREENWIDTH > MAXSCREENWIDTH ||
642 SCREENWIDTH % 32 != 0 ||
643 SCREENHEIGHT < 200 || SCREENHEIGHT > MAXSCREENHEIGHT)
644 I_Error ("Resolution out of range or width is not a multiple of 32");
646 if ((SCREENHEIGHT*8) == (SCREENWIDTH*5)) // 640x400, 320x200
647 weirdaspect = 1;
648 else if ((SCREENHEIGHT*4) == (SCREENWIDTH*3)) // 640x480, 320x240
649 weirdaspect = 0;
650 else {
651 weirdaspect = 0;
652 printf("Warning: Bad aspect ratio, things might look screwy\n");
656 //allocate all the res-dependant vars
657 resinit_r_plane ();
658 resinit_am_map ();
659 resinit_r_draw ();
660 xtoviewangle = (angle_t *)I_calloc (SCREENWIDTH+1, sizeof(angle_t));
661 negonearray = (short *)I_calloc (SCREENWIDTH, sizeof(short));
662 screenheightarray = (short *)I_calloc (SCREENWIDTH, sizeof(short));
664 // stick these in low dos memory on PCs
666 vid_flags = MEMF_CLEAR+MEMF_FAST+MEMF_PUBLIC;
667 if (M_CheckParm ("-mmu"))
668 vid_flags += MEMF_NOCACHESYNCPPC+MEMF_NOCACHESYNCM68K;
669 vid_size = SCREENWIDTH*SCREENHEIGHT*4+SCREENWIDTH*ST_HEIGHT;
670 vid_mem = PPCAllocVec(vid_size + 100, vid_flags);
671 if (vid_mem == NULL)
672 I_Error ("Couldn't allocate memory for screens\n");
673 printf ("Screen memory located at $%08lx\n", (long)vid_mem);
675 for (i=0 ; i<4 ; i++)
676 screens[i] = vid_mem + 100 + i*SCREENWIDTH*SCREENHEIGHT;
677 screens[4] = vid_mem + SCREENWIDTH*SCREENHEIGHT*4;