-
Notifications
You must be signed in to change notification settings - Fork 37
/
PCRLIB_C.C
1625 lines (1315 loc) · 30 KB
/
PCRLIB_C.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* The Catacomb Source Code
* Copyright (C) 1993-2014 Flat Rock Software
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#define CATALOG
#include "pcrlib.h"
#include <conio.h>
char ch,str[80]; // scratch space
/*========================================================================*/
inputtype playermode[3] = {0,keyboard,joystick1};
boolean keydown[128];
int JoyXlow [3], JoyXhigh [3], JoyYlow [3], JoyYhigh [3];
int MouseSensitivity;
char key[8],keyB1,keyB2;
void interrupt (*oldint9) ()=NULL;
char demobuffer[5000];
char *demoptr;
int democount;
int lastdemoval; // so demo can be RLE compressed
enum demoenum indemo;
/*=======================================================================*/
/*
=======================
=
= SetupKBD
= Clears the keydown array and installs the INT 9 ISR if it isn't allready
= hooked up.
=
=======================
*/
void SetupKBD ()
{
void far *vect = getvect (9);
int i;
for (i=0;i<128;i++) /* clear our key down table */
keydown[i]= false;
poke (0x40,0x1c,peek(0x40,0x1a)); /* clear the bios key buffer */
if ( &Int9ISR != vect ) /* is our handler allready set up? */
{
oldint9 = vect;
setvect (9,Int9ISR);
}
}
/*
=========================
=
= Int9ISR
= Called for every keypress. Keeps track of which keys are down, and passes
= the key on to DOS after clearing the dos buffer (max 1 char in buffer).
=
=========================
*/
void interrupt Int9ISR ()
{
int key = inportb (0x60); /* get the key pressed */
if (key>127)
keydown [key-128] = false; /* break scan code */
else
{
keydown [key] = true; /* make scan code */
poke (0x40,0x1c,peek(0x40,0x1a)); /* clear the bios key buffer */
}
asm {
push ax
push bx
push cx
push dx
push si
push di
push bp
}
oldint9 (); /* give it to DOS */
asm {
pop bp
pop di
pop si
pop dx
pop cx
pop bx
pop ax
}
outport (0x20,0x20); /* tell the int manager we got it */
}
/*
===========================
=
= ShutdownKBD
= Sets the int 9 vector back to oldint 9
=
===========================
*/
void ShutdownKBD ()
{
if (oldint9 != NULL)
setvect (9,oldint9);
}
/*
===========================
=
= ControlKBD
=
===========================
*/
ControlStruct ControlKBD ()
{
int xmove=0,
ymove=0;
ControlStruct action;
if (keydown [key[north]])
ymove=-1;
if (keydown [key[east]])
xmove=1;
if (keydown [key[south]])
ymove=1;
if (keydown [key[west]])
xmove=-1;
if (keydown [key[northeast]])
{
ymove=-1;
xmove=1;
}
if (keydown [key[northwest]])
{
ymove=-1;
xmove=-1;
}
if (keydown [key[southeast]])
{
ymove=1;
xmove=1;
}
if (keydown [key[southwest]])
{
ymove=1;
xmove=-1;
}
switch (ymove*3+xmove)
{
case -4: action.dir = northwest; break;
case -3: action.dir = north; break;
case -2: action.dir = northeast; break;
case -1: action.dir = west; break;
case 0: action.dir = nodir; break;
case 1: action.dir = east; break;
case 2: action.dir = southwest; break;
case 3: action.dir = south; break;
case 4: action.dir = southeast; break;
}
action.button1 = keydown [keyB1];
action.button2 = keydown [keyB2];
return (action);
}
/*
============================
=
= ControlMouse
=
============================
*/
ControlStruct ControlMouse ()
{
int newx,newy, /* mickeys the mouse has moved */
xmove = 0,
ymove = 0;
ControlStruct action;
_AX = 3;
geninterrupt (0x33); /* mouse status */
newx = _CX;
newy = _DX;
action.button1 = _BX & 1;
action.button2 = (_BX & 2) >> 1;
if ((newx-320)/2>MouseSensitivity)
{
xmove = 1;
newx = newx - MouseSensitivity*2;
}
else if ((newx-320)/2<-MouseSensitivity)
{
xmove = -1;
newx = newx + MouseSensitivity*2;
}
if ((newy-100)>MouseSensitivity)
{
ymove = 1;
newy = newy - MouseSensitivity;
}
else if ((newy-100)<-MouseSensitivity)
{
ymove = -1;
newy = newy + MouseSensitivity;
}
_AX = 4;
_CX=newx;
_DX=newy;
geninterrupt (0x33); /* set mouse status */
switch (ymove*3+xmove)
{
case -4: action.dir = northwest; break;
case -3: action.dir = north; break;
case -2: action.dir = northeast; break;
case -1: action.dir = west; break;
case 0: action.dir = nodir; break;
case 1: action.dir = east; break;
case 2: action.dir = southwest; break;
case 3: action.dir = south; break;
case 4: action.dir = southeast; break;
}
return (action);
}
/*
===============================
=
= ReadJoystick
= Just return the resistance count of the joystick
=
===============================
*/
void ReadJoystick (int joynum,int *xcount,int *ycount)
{
int portval,a1,a2,xbit,ybit;
if (joynum==1)
{
xbit=1;
ybit=2;
}
else
{
xbit=4;
ybit=8;
}
*xcount = 0;
*ycount = 0;
outportb (0x201,inportb (0x201)); /* start the signal pulse */
asm cli;
do
{
portval = inportb (0x201);
a1 = (portval & xbit) != 0;
a2 = (portval & ybit) != 0;
*xcount+=a1;
*ycount+=a2;
} while ((a1+a2!=0) && (*xcount<500) && (*ycount<500));
asm sti;
}
/*
=============================
=
= ControlJoystick (joy# = 1 / 2)
=
=============================
*/
ControlStruct ControlJoystick (int joynum)
{
int joyx = 0,joyy = 0, /* resistance in joystick */
xmove = 0,
ymove = 0,
buttons;
ControlStruct action;
ReadJoystick (joynum,&joyx,&joyy);
if ( (joyx>500) | (joyy>500) )
{
joyx=JoyXlow [joynum] + 1; /* no joystick connected, do nothing */
joyy=JoyYlow [joynum] + 1;
}
if (joyx > JoyXhigh [joynum])
xmove = 1;
else if (joyx < JoyXlow [joynum])
xmove = -1;
if (joyy > JoyYhigh [joynum])
ymove = 1;
else if (joyy < JoyYlow [joynum])
ymove = -1;
switch (ymove*3+xmove)
{
case -4: action.dir = northwest; break;
case -3: action.dir = north; break;
case -2: action.dir = northeast; break;
case -1: action.dir = west; break;
case 0: action.dir = nodir; break;
case 1: action.dir = east; break;
case 2: action.dir = southwest; break;
case 3: action.dir = south; break;
case 4: action.dir = southeast; break;
}
buttons = inportb (0x201); /* get all four button status */
if (joynum == 1)
{
action.button1 = ((buttons & 0x10) == 0);
action.button2 = ((buttons & 0x20) == 0);
}
else
{
action.button1 = ((buttons & 0x40) == 0);
action.button2 = ((buttons & 0x80) == 0);
}
return (action);
}
/*
=============================
=
= ControlPlayer
=
= Expects a 1 or a 2
=
=============================
*/
ControlStruct ControlPlayer (int player)
{
ControlStruct ret;
int val;
if (indemo == notdemo || indemo == recording)
{
switch (playermode[player])
{
case keyboard : ret = ControlKBD (); break;
case mouse : ret = ControlMouse (); break;
case joystick1: ret = ControlJoystick(1); break;
case joystick2: ret = ControlJoystick(2); break;
}
//
// recording a demo?
//
if (indemo == recording)
{
val = (ret.dir << 2) | (ret.button2 << 1) | ret.button1;
*demoptr++=val;
}
}
else
//
// get the command from the demo buffer
//
{
val = *demoptr++;
ret.button1 = val & 1;
ret.button2 = (val & 2) >> 1;
ret.dir = (dirtype) ( (val & (4+8+16+32) ) >> 2);
}
return (ret);
}
////////////////////////
//
// RecordDemo
// Clears the demo buffer and starts capturing events
//
////////////////////////
void RecordDemo (void)
{
demobuffer[0]=level;
demoptr = &demobuffer[1];
indemo = recording;
}
////////////////////////
//
// LoadDemo / SaveDemo
// Loads a demo from disk or
// saves the accumulated demo command string to disk
//
////////////////////////
void LoadDemo (int demonum)
{
char st2[5];
strcpy (str,"DEMO");
itoa (demonum,st2,10);
strcat (str,st2);
strcat (str,".");
strcat (str,_extension);
LoadFile (str,MK_FP(_DS,&demobuffer));
level=demobuffer[0];
demoptr = &demobuffer[1];
indemo = demoplay;
}
void SaveDemo (int demonum)
{
char st2[5];
strcpy (str,"DEMO");
itoa (demonum,st2,10);
strcat (str,st2);
strcat (str,".");
strcat (str,_extension);
SaveFile (str,MK_FP(_DS,&demobuffer),(demoptr-&demobuffer[0]));
indemo = notdemo;
}
////////////////////////
//
// StartDemo
//
////////////////////////
/*=========================================================================*/
/*
** Miscellanious library routines
*/
///////////////////////////////
//
// clearkeys
// Clears out the bios buffer and zeros out the keydown array
//
///////////////////////////////
void clearkeys (void)
{
int i;
while (bioskey (1))
bioskey(0);
for (i=0;i<128;i++)
keydown [i]=0;
}
/*
===========================================
=
= Allocate a block aligned on a paragraph
=
===========================================
*/
void far *lastparalloc; // global variable of the EXACT (not paralign)
// last block, so it can be freed right
void huge *paralloc (long size)
{
void huge *temp;
word seg,ofs;
/* allocate a block with extra space */
lastparalloc = (void far*)temp = farmalloc (size+15);
if (temp == NULL)
//
// not enough memory!
//
{
setscreenmode (text);
_quit ("Out of memory!");
}
ofs=FP_OFF(temp);
if (ofs!=0) /* set offset to 0 and bump segment */
{
seg=FP_SEG(temp);
seg++;
ofs=0;
temp=MK_FP (seg,ofs);
}
return (void huge *) temp;
}
//==========================================================================
/*
==============================================
=
= Load a *LARGE* file into a FAR buffer!
= by John Romero (C) 1990 PCRcade
=
==============================================
*/
unsigned long LoadFile(char *filename,char huge *buffer)
{
unsigned int handle,flength1=0,flength2=0,buf1,buf2,foff1,foff2;
buf1=FP_OFF(buffer);
buf2=FP_SEG(buffer);
asm mov WORD PTR foff1,0 // file offset = 0 (start)
asm mov WORD PTR foff2,0
asm mov dx,filename
asm mov ax,3d00h // OPEN w/handle (read only)
asm int 21h
asm jc out
asm mov handle,ax
asm mov bx,ax
asm xor cx,cx
asm xor dx,dx
asm mov ax,4202h
asm int 21h // SEEK (find file length)
asm jc out
asm mov flength1,ax
asm mov flength2,dx
asm mov cx,flength2
asm inc cx // <- at least once!
L_1:
asm push cx
asm mov cx,foff2
asm mov dx,foff1
asm mov ax,4200h
asm int 21h // SEEK from start
asm push ds
asm mov bx,handle
asm mov cx,-1
asm mov dx,buf1
asm mov ax,buf2
asm mov ds,ax
asm mov ah,3fh // READ w/handle
asm int 21h
asm pop ds
asm pop cx
asm jc out
asm cmp ax,-1
asm jne out
asm push cx // need to read the last byte
asm push ds // into the segment! IMPORTANT!
asm mov bx,handle
asm mov cx,1
asm mov dx,buf1
asm add dx,-1
asm mov ax,buf2
asm mov ds,ax
asm mov ah,3fh
asm int 21h
asm pop ds
asm pop cx
asm add buf2,1000h
asm inc WORD PTR foff2
asm loop L_1
out:
asm mov bx,handle // CLOSE w/handle
asm mov ah,3eh
asm int 21h
return (flength2*0x10000+flength1);
}
//===========================================================================
/*
==============================================
=
= Save a *LARGE* file far a FAR buffer!
= by John Romero (C) 1990 PCRcade
=
==============================================
*/
void SaveFile(char *filename,char huge *buffer, long size)
{
unsigned int handle,buf1,buf2,foff1,foff2;
buf1=FP_OFF(buffer);
buf2=FP_SEG(buffer);
asm mov WORD PTR foff1,0 // file offset = 0 (start)
asm mov WORD PTR foff2,0
asm mov dx,filename
asm mov ax,3c00h // CREATE w/handle (read only)
asm xor cx,cx
asm int 21h
asm jc out
asm mov handle,ax
asm cmp word ptr size+2,0 // larger than 1 segment?
asm je L2
L1:
asm push ds
asm mov bx,handle
asm mov cx,8000h
asm mov dx,buf1
asm mov ax,buf2
asm mov ds,ax
asm mov ah,40h // WRITE w/handle
asm int 21h
asm pop ds
asm add buf2,800h // bump ptr up 1/2 segment
asm sub WORD PTR size,8000h // done yet?
asm sbb WORD PTR size+2,0
asm cmp WORD PTR size+2,0
asm ja L1
asm cmp WORD PTR size,8000h
asm jae L1
L2:
asm push ds
asm mov bx,handle
asm mov cx,WORD PTR size
asm mov dx,buf1
asm mov ax,buf2
asm mov ds,ax
asm mov ah,40h // WRITE w/handle
asm int 21h
asm pop ds
asm jmp out
out:
asm mov bx,handle // CLOSE w/handle
asm mov ah,3eh
asm int 21h
}
//==========================================================================
/*
====================================
=
= bloadin
= Paraligns just enough space and bloads in the
= specified file, returning a pointer to the start
=
====================================
*/
void huge *bloadin (char *filename)
{
int handle;
long length;
char huge *location;
if ( (handle = open (filename,O_BINARY)) != -1 )
{
length = filelength (handle);
location = paralloc (length);
close (handle);
LoadFile (filename,location);
return location;
}
else
return NULL;
}
/*==================================================================================*/
/*
** Graphic routines
*/
grtype grmode;
cardtype _videocard;
void huge *charptr; // 8*8 tileset
void huge *tileptr; // 16*16 tileset
void huge *picptr; // any size picture set
void huge *spriteptr; // any size masked and hit rect sprites
void huge *egaspriteptr[4]; // spriteptr for each ega plane's data
unsigned crtcaddr;
int sx,sy,leftedge;
/*
========================
=
= setscreenmode
= Call BIOS to set TEXT / CGAgr / EGAgr / VGAgr
=
========================
*/
void setscreenmode (grtype mode)
{
char extern VGAPAL; // deluxepaint vga pallet .OBJ file
void far *vgapal = &VGAPAL;
switch (mode)
{
case text: _AX = 3;
geninterrupt (0x10);
screenseg=0xb800;
break;
case CGAgr: _AX = 4;
geninterrupt (0x10);
screenseg=0xb800;
break;
case EGAgr: _AX = 0xd;
geninterrupt (0x10);
screenseg=0xa000;
EGAmove ();
moveega ();
break;
case VGAgr: _AX = 0x13;
geninterrupt (0x10);
screenseg=0xa000;
_ES = FP_SEG(vgapal);
_DX = FP_OFF(vgapal);
_BX = 0;
_CX = 0x100;
_AX = 0x1012;
geninterrupt(0x10); // set the deluxepaint pallet
break;
}
crtcaddr = 0x3d4; //peek (0x40,0x63) if not for two monitors...
}
/*
========================
=
= egasplitscreen
=
========================
*/
void egasplitscreen (int linenum)
{
WaitVBL ();
if (_videocard==VGAcard)
linenum*=2;
outportb (crtcaddr,CRTCLINECOMPARE);
outportb (crtcaddr+1,linenum % 256);
outportb (crtcaddr,CRTCOVERFLOW);
outportb (crtcaddr+1, 1+16*(linenum/256));
if (_videocard==VGAcard)
{
outportb (crtcaddr,CRTCMAXSCANLINE);
outportb (crtcaddr+1,inportb(crtcaddr+1) & (255-64));
}
}
/*
========================
=
= crtcstart
=
========================
*/
void crtcstart (unsigned start)
{
WaitVBL ();
outportb (crtcaddr,CRTCSTARTL);
outportb (crtcaddr+1,start % 256);
outportb (crtcaddr,CRTCSTARTH);
outportb (crtcaddr+1,start / 256);
}
int win_xl,win_yl,win_xh,win_yh;
int screencenterx = 19,screencentery = 11;
//////////////////////////
//
// drawwindow
// draws a bordered window and homes the cursor
//
//////////////////////////
void drawwindow (int xl, int yl, int xh, int yh)
{
int x,y;
win_xl=xl;
win_yl=yl;
win_xh=xh;
win_yh=yh; // so the window can be erased
drawchar (xl,yl,1);
for (x=xl+1;x<xh;x++)
drawchar (x,yl,2);
drawchar (xh,yl,3);
for (y=yl+1;y<yh;y++)
{
drawchar (xl,y,4);
for (x=xl+1;x<xh;x++)
drawchar (x,y,' ');
drawchar (xh,y,5);
}
drawchar (xl,yh,6);
for (x=xl+1;x<xh;x++)
drawchar (x,yh,7);
drawchar (xh,yh,8);
sx = leftedge = xl+1;
sy = yl+1;
}
////////////////////////////
//
// erasewindow
// clears out the last window and it's border to spaces
//
///////////////////////////
void bar (int xl, int yl, int xh, int yh, int ch)
{
int x,y;
for (y=yl;y<=yh;y++)
for (x=xl;x<=xh;x++)
drawchar (x,y,ch);
}
void erasewindow (void)
{
bar (win_xl,win_yl,win_xh,win_yh,' ');
}
/////////////////////////////
//
// centerwindow
// Centers a drawwindow of the given size
//
/////////////////////////////
void centerwindow (int width, int height)
{
int xl = screencenterx-width/2;
int yl = screencentery-height/2;
drawwindow (xl,yl,xl+width+1,yl+height+1);
}
///////////////////////////////
//
// expwin {h / v}
// Grows the window outward
//
///////////////////////////////
void expwin (int width, int height)
{
if (width > 2)
{
if (height >2)
expwin (width-2,height-2);
else
expwinh (width-2,height);
}
else
if (height >2)
expwinv (width,height-2);
WaitVBL ();
centerwindow (width,height);
}
void expwinh (int width, int height)
{
if (width > 2)
expwinh (width-2,height);
WaitVBL ();
centerwindow (width,height);
}
void expwinv (int width, int height)
{
if (height >2)
expwinv (width,height-2);
WaitVBL ();
centerwindow (width,height);
}
/////////////////////////
//
// get
// Flash a cursor at sx,sy and waits for a user bioskey
//
/////////////////////////
int get (void)
{
int cycle,key;
do
{
cycle = 9;
while (!(key = bioskey(1)) && cycle<13)
{
drawchar (sx,sy,cycle++);
WaitVBL ();
WaitVBL ();
WaitVBL ();
WaitVBL ();
WaitVBL ();
}
} while (key == 0);
drawchar (sx,sy,' ');
return bioskey(0); // take it out of the buffer
}
/////////////////////////
//
// print
// Prints a string at sx,sy. No clipping!!!
//