1 /*
2 Copyright (C) 1994-1995 Apogee Software, Ltd.
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.
12 
13 See the GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 
19 */
20 
21 #include <stdarg.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <ctype.h>
27 
28 #ifdef DOS
29 #include <malloc.h>
30 #include <dos.h>
31 #include <conio.h>
32 #include <io.h>
33 #endif
34 
35 #include <stdlib.h>
36 #include <sys/stat.h>
37 #include "modexlib.h"
38 //MED
39 #include "memcheck.h"
40 #include "rt_util.h"
41 
42 
43 // GLOBAL VARIABLES
44 
45 
46 int    linewidth;
47 int    ylookup[MAXSCREENHEIGHT];
48 int    page1start;
49 int    page2start;
50 int    page3start;
51 int    screensize;
52 unsigned bufferofs;
53 unsigned displayofs;
54 boolean graphicsmode=false;
55 
56 #ifdef DOS
57 
58 /*
59 ====================
60 =
61 = GraphicsMode
62 =
63 ====================
64 */
GraphicsMode(void)65 void GraphicsMode ( void )
66 {
67 union REGS regs;
68 
69 regs.w.ax = 0x13;
70 int386(0x10,&regs,&regs);
71 graphicsmode=true;
72 }
73 
74 /*
75 ====================
76 =
77 = SetTextMode
78 =
79 ====================
80 */
SetTextMode(void)81 void SetTextMode ( void )
82 {
83 
84 union REGS regs;
85 
86 regs.w.ax = 0x03;
87 int386(0x10,&regs,&regs);
88 graphicsmode=false;
89 
90 }
91 
92 /*
93 ====================
94 =
95 = TurnOffTextCursor
96 =
97 ====================
98 */
TurnOffTextCursor(void)99 void TurnOffTextCursor ( void )
100 {
101 
102 union REGS regs;
103 
104 regs.w.ax = 0x0100;
105 regs.w.cx = 0x2000;
106 int386(0x10,&regs,&regs);
107 
108 }
109 
110 #if 0
111 /*
112 ====================
113 =
114 = TurnOnTextCursor
115 =
116 ====================
117 */
118 void TurnOnTextCursor ( void )
119 {
120 
121 union REGS regs;
122 
123 regs.w.ax = 0x03;
124 int386(0x10,&regs,&regs);
125 
126 }
127 #endif
128 
129 /*
130 ====================
131 =
132 = WaitVBL
133 =
134 ====================
135 */
WaitVBL(void)136 void WaitVBL( void )
137 {
138    unsigned char i;
139 
140    i=inp(0x03da);
141    while ((i&8)==0)
142       i=inp(0x03da);
143    while ((i&8)==1)
144       i=inp(0x03da);
145 }
146 
147 
148 /*
149 ====================
150 =
151 = VL_SetLineWidth
152 =
153 = Line witdh is in WORDS, 40 words is normal width for vgaplanegr
154 =
155 ====================
156 */
157 
VL_SetLineWidth(unsigned width)158 void VL_SetLineWidth (unsigned width)
159 {
160    int i,offset;
161 
162 //
163 // set wide virtual screen
164 //
165    outpw (CRTC_INDEX,CRTC_OFFSET+width*256);
166 
167 //
168 // set up lookup tables
169 //
170    linewidth = width*2;
171 
172    offset = 0;
173 
174    for (i=0;i<MAXSCANLINES;i++)
175       {
176       ylookup[i]=offset;
177       offset += linewidth;
178       }
179 }
180 
181 /*
182 =======================
183 =
184 = VL_SetVGAPlaneMode
185 =
186 =======================
187 */
188 
VL_SetVGAPlaneMode(void)189 void VL_SetVGAPlaneMode ( void )
190 {
191     GraphicsMode();
192     VL_DePlaneVGA ();
193     VL_SetLineWidth (48);
194     screensize=208*SCREENBWIDE;
195     page1start=0xa0200;
196     page2start=0xa0200+screensize;
197     page3start=0xa0200+(2u*screensize);
198     displayofs = page1start;
199     bufferofs = page2start;
200     XFlipPage ();
201 }
202 
203 /*
204 =======================
205 =
206 = VL_CopyPlanarPage
207 =
208 =======================
209 */
VL_CopyPlanarPage(byte * src,byte * dest)210 void VL_CopyPlanarPage ( byte * src, byte * dest )
211 {
212    int plane;
213 
214    for (plane=0;plane<4;plane++)
215       {
216       VGAREADMAP(plane);
217       VGAWRITEMAP(plane);
218       memcpy(dest,src,screensize);
219       }
220 }
221 
222 /*
223 =======================
224 =
225 = VL_CopyPlanarPageToMemory
226 =
227 =======================
228 */
VL_CopyPlanarPageToMemory(byte * src,byte * dest)229 void VL_CopyPlanarPageToMemory ( byte * src, byte * dest )
230 {
231    byte * ptr;
232    int plane,a,b;
233 
234    for (plane=0;plane<4;plane++)
235       {
236       ptr=dest+plane;
237       VGAREADMAP(plane);
238       for (a=0;a<200;a++)
239          for (b=0;b<80;b++,ptr+=4)
240             *(ptr)=*(src+(a*linewidth)+b);
241       }
242 }
243 
244 /*
245 =======================
246 =
247 = VL_CopyBufferToAll
248 =
249 =======================
250 */
VL_CopyBufferToAll(unsigned buffer)251 void VL_CopyBufferToAll ( unsigned buffer )
252 {
253    int plane;
254 
255    for (plane=0;plane<4;plane++)
256       {
257       VGAREADMAP(plane);
258       VGAWRITEMAP(plane);
259       if (page1start!=buffer)
260          memcpy((byte *)page1start,(byte *)buffer,screensize);
261       if (page2start!=buffer)
262          memcpy((byte *)page2start,(byte *)buffer,screensize);
263       if (page3start!=buffer)
264          memcpy((byte *)page3start,(byte *)buffer,screensize);
265       }
266 }
267 
268 /*
269 =======================
270 =
271 = VL_CopyDisplayToHidden
272 =
273 =======================
274 */
VL_CopyDisplayToHidden(void)275 void VL_CopyDisplayToHidden ( void )
276 {
277    VL_CopyBufferToAll ( displayofs );
278 }
279 
280 /*
281 =================
282 =
283 = VL_ClearBuffer
284 =
285 = Fill the entire video buffer with a given color
286 =
287 =================
288 */
289 
VL_ClearBuffer(unsigned buf,byte color)290 void VL_ClearBuffer (unsigned buf, byte color)
291 {
292   VGAMAPMASK(15);
293   memset((byte *)buf,color,screensize);
294 }
295 
296 /*
297 =================
298 =
299 = VL_ClearVideo
300 =
301 = Fill the entire video buffer with a given color
302 =
303 =================
304 */
305 
VL_ClearVideo(byte color)306 void VL_ClearVideo (byte color)
307 {
308   VGAMAPMASK(15);
309   memset((byte *)(0xa000<<4),color,0x10000);
310 }
311 
312 /*
313 =================
314 =
315 = VL_DePlaneVGA
316 =
317 =================
318 */
319 
VL_DePlaneVGA(void)320 void VL_DePlaneVGA (void)
321 {
322 
323 //
324 // change CPU addressing to non linear mode
325 //
326 
327 //
328 // turn off chain 4 and odd/even
329 //
330         outp (SC_INDEX,SC_MEMMODE);
331         outp (SC_DATA,(inp(SC_DATA)&~8)|4);
332 
333         outp (SC_INDEX,SC_MAPMASK);         // leave this set throughout
334 
335 //
336 // turn off odd/even and set write mode 0
337 //
338         outp (GC_INDEX,GC_MODE);
339         outp (GC_DATA,inp(GC_DATA)&~0x13);
340 
341 //
342 // turn off chain
343 //
344         outp (GC_INDEX,GC_MISCELLANEOUS);
345         outp (GC_DATA,inp(GC_DATA)&~2);
346 
347 //
348 // clear the entire buffer space, because int 10h only did 16 k / plane
349 //
350         VL_ClearVideo (0);
351 
352 //
353 // change CRTC scanning from doubleword to byte mode, allowing >64k scans
354 //
355         outp (CRTC_INDEX,CRTC_UNDERLINE);
356         outp (CRTC_DATA,inp(CRTC_DATA)&~0x40);
357 
358         outp (CRTC_INDEX,CRTC_MODE);
359         outp (CRTC_DATA,inp(CRTC_DATA)|0x40);
360 }
361 
362 
363 /*
364 =================
365 =
366 = XFlipPage
367 =
368 =================
369 */
370 
XFlipPage(void)371 void XFlipPage ( void )
372 {
373    displayofs=bufferofs;
374 
375 //   _disable();
376 
377    outp(CRTC_INDEX,CRTC_STARTHIGH);
378    outp(CRTC_DATA,((displayofs&0x0000ffff)>>8));
379 
380 //   _enable();
381 
382    bufferofs += screensize;
383    if (bufferofs > page3start)
384       bufferofs = page1start;
385 }
386 
387 #else
388 
389 #include "SDL.h"
390 
391 /* rt_def.h isn't included, so I just put this here... */
392 #if !defined(ANSIESC)
393 #ifdef _WIN32 /* VC6 */
394 #define STUB_FUNCTION fprintf(stderr,"STUB: at " __FILE__ ", line %d, thread %d\n",__LINE__,_getpid())
395 #else
396 #define STUB_FUNCTION fprintf(stderr,"STUB: %s at " __FILE__ ", line %d, thread %d\n",__FUNCTION__,__LINE__,getpid())
397 #endif
398 #else
399 #define STUB_FUNCTION
400 #endif
401 
402 /*
403 ====================
404 =
405 = GraphicsMode
406 =
407 ====================
408 */
409 static SDL_Surface *sdl_surface = NULL;
410 static SDL_Surface *sdl_backbuf = NULL;
411 
GraphicsMode(void)412 void GraphicsMode ( void )
413 {
414     Uint32 flags = 0;
415 
416 	if (SDL_InitSubSystem (SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
417 	{
418 	    Error ("Could not initialize SDL\n");
419 	}
420 
421     #if defined(PLATFORM_WIN32) || defined(PLATFORM_MACOSX) || defined(PLATFORM_UNIX)
422         // FIXME: remove this.  --ryan.
423         flags = SDL_FULLSCREEN;
424         SDL_WM_GrabInput(SDL_GRAB_ON);
425     #endif
426 
427     SDL_WM_SetCaption ("Rise of the Triad", "ROTT");
428     SDL_ShowCursor (0);
429     sdl_surface = SDL_SetVideoMode (320, 200, 8, flags);
430 
431 	if (sdl_surface == NULL)
432 	{
433 		Error ("Could not set video mode\n");
434 	}
435 }
436 
437 /*
438 ====================
439 =
440 = SetTextMode
441 =
442 ====================
443 */
SetTextMode(void)444 void SetTextMode ( void )
445 {
446 	if (SDL_WasInit(SDL_INIT_VIDEO) == SDL_INIT_VIDEO) {
447 		if (sdl_surface != NULL) {
448 			SDL_FreeSurface(sdl_surface);
449 
450 			sdl_surface = NULL;
451 		}
452 
453 		SDL_QuitSubSystem (SDL_INIT_VIDEO);
454 	}
455 }
456 
457 /*
458 ====================
459 =
460 = TurnOffTextCursor
461 =
462 ====================
463 */
TurnOffTextCursor(void)464 void TurnOffTextCursor ( void )
465 {
466 }
467 
468 /*
469 ====================
470 =
471 = WaitVBL
472 =
473 ====================
474 */
WaitVBL(void)475 void WaitVBL( void )
476 {
477 	SDL_Delay (16667/1000);
478 }
479 
480 /*
481 =======================
482 =
483 = VL_SetVGAPlaneMode
484 =
485 =======================
486 */
487 
VL_SetVGAPlaneMode(void)488 void VL_SetVGAPlaneMode ( void )
489 {
490    int i,offset;
491 
492     GraphicsMode();
493 
494 //
495 // set up lookup tables
496 //
497    linewidth = 320;
498 
499    offset = 0;
500 
501    for (i=0;i<MAXSCANLINES;i++)
502       {
503       ylookup[i]=offset;
504       offset += linewidth;
505       }
506 
507     screensize=MAXSCREENHEIGHT*MAXSCREENWIDTH;
508 
509     page1start=(int)sdl_surface->pixels;
510     page2start=(int)sdl_surface->pixels;
511     page3start=(int)sdl_surface->pixels;
512     displayofs = page1start;
513     bufferofs = page2start;
514     XFlipPage ();
515 }
516 
517 /*
518 =======================
519 =
520 = VL_CopyPlanarPage
521 =
522 =======================
523 */
VL_CopyPlanarPage(byte * src,byte * dest)524 void VL_CopyPlanarPage ( byte * src, byte * dest )
525 {
526 #ifdef DOS
527    int plane;
528 
529    for (plane=0;plane<4;plane++)
530       {
531       VGAREADMAP(plane);
532       VGAWRITEMAP(plane);
533       memcpy(dest,src,screensize);
534       }
535 #else
536       memcpy(dest,src,screensize);
537 #endif
538 }
539 
540 /*
541 =======================
542 =
543 = VL_CopyPlanarPageToMemory
544 =
545 =======================
546 */
VL_CopyPlanarPageToMemory(byte * src,byte * dest)547 void VL_CopyPlanarPageToMemory ( byte * src, byte * dest )
548 {
549 #ifdef DOS
550    byte * ptr;
551    int plane,a,b;
552 
553    for (plane=0;plane<4;plane++)
554       {
555       ptr=dest+plane;
556       VGAREADMAP(plane);
557       for (a=0;a<200;a++)
558          for (b=0;b<80;b++,ptr+=4)
559             *(ptr)=*(src+(a*linewidth)+b);
560       }
561 #else
562       memcpy(dest,src,screensize);
563 #endif
564 }
565 
566 /*
567 =======================
568 =
569 = VL_CopyBufferToAll
570 =
571 =======================
572 */
VL_CopyBufferToAll(unsigned buffer)573 void VL_CopyBufferToAll ( unsigned buffer )
574 {
575 #ifdef DOS
576    int plane;
577 
578    for (plane=0;plane<4;plane++)
579       {
580       VGAREADMAP(plane);
581       VGAWRITEMAP(plane);
582       if (page1start!=buffer)
583          memcpy((byte *)page1start,(byte *)buffer,screensize);
584       if (page2start!=buffer)
585          memcpy((byte *)page2start,(byte *)buffer,screensize);
586       if (page3start!=buffer)
587          memcpy((byte *)page3start,(byte *)buffer,screensize);
588       }
589 #endif
590 }
591 
592 /*
593 =======================
594 =
595 = VL_CopyDisplayToHidden
596 =
597 =======================
598 */
VL_CopyDisplayToHidden(void)599 void VL_CopyDisplayToHidden ( void )
600 {
601    VL_CopyBufferToAll ( displayofs );
602 }
603 
604 /*
605 =================
606 =
607 = VL_ClearBuffer
608 =
609 = Fill the entire video buffer with a given color
610 =
611 =================
612 */
613 
VL_ClearBuffer(unsigned buf,byte color)614 void VL_ClearBuffer (unsigned buf, byte color)
615 {
616 #ifdef DOS
617   VGAMAPMASK(15);
618   memset((byte *)buf,color,screensize);
619 #else
620   memset((byte *)buf,color,screensize);
621 #endif
622 }
623 
624 /*
625 =================
626 =
627 = VL_ClearVideo
628 =
629 = Fill the entire video buffer with a given color
630 =
631 =================
632 */
633 
VL_ClearVideo(byte color)634 void VL_ClearVideo (byte color)
635 {
636 #ifdef DOS
637   VGAMAPMASK(15);
638   memset((byte *)(0xa000<<4),color,0x10000);
639 #else
640   memset (sdl_surface->pixels, color, MAXSCREENWIDTH*MAXSCREENHEIGHT);
641 #endif
642 }
643 
644 /*
645 =================
646 =
647 = VL_DePlaneVGA
648 =
649 =================
650 */
651 
VL_DePlaneVGA(void)652 void VL_DePlaneVGA (void)
653 {
654 }
655 
656 
657 /* C version of rt_vh_a.asm */
658 
VH_UpdateScreen(void)659 void VH_UpdateScreen (void)
660 {
661 	SDL_UpdateRect (SDL_GetVideoSurface (), 0, 0, 0, 0);
662 }
663 
664 /*
665 =================
666 =
667 = XFlipPage
668 =
669 =================
670 */
671 
XFlipPage(void)672 void XFlipPage ( void )
673 {
674 #ifdef DOS
675    displayofs=bufferofs;
676 
677 //   _disable();
678 
679    outp(CRTC_INDEX,CRTC_STARTHIGH);
680    outp(CRTC_DATA,((displayofs&0x0000ffff)>>8));
681 
682 //   _enable();
683 
684    bufferofs += screensize;
685    if (bufferofs > page3start)
686       bufferofs = page1start;
687 #else
688    SDL_UpdateRect (sdl_surface, 0, 0, 0, 0);
689 #endif
690 }
691 
692 #endif
693