Debian Codegraph @

terps/agility/os_dos16.c

raw symbols SCIP
1 /* os_msdos.c -- interface routines for MS-DOS */
2 /* Copyright (C) 1996,1997,1998 Robert Masenten */
3 /* */
4 /* This is part of the source for the (Mostly) Universal */
5 /* AGT Interpreter */
6
7 /* This was written to work under Borland C; it hasn't been
8 tested with other compilers. */
9
10
11 /* NOTE: The library functions for screen location put the origin
12 at (1,1), but curr_x, curr_y, box_startx, and box_starty have
13 origin (0,0). */
14
15 #define USE_EDITLINE
16 #define DEBUG_KEY 0 /* Turns on debugging of key functions */
17
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <dos.h>
21 #include <conio.h>
22 #include <time.h>
23 #include <ctype.h>
24 #include <limits.h>
25 #include <assert.h>
26
27 #include "agility.h"
28 #include "interp.h"
29
30 #define cleareol clreol
31
32
33 rbool use_bios; /* If true, use the BIOS routines for text output.
34 (for the sake of blind users.) */
35
36 static int colorset[5]={7,14,0,15,1};
37
38
39 /* cs_ stands for "color scheme" */
40 enum {cs_normal=0, cs_bold, cs_back, cs_statfore, cs_statback} color_scheme;
41 /* normal/highlight/background/statfore/statback */
42
43 int scroll_count; /* Count of how many lines we have printed since
44 last player input. */
45 int status_height;
46 rbool have_compass;
47 int top_row; /* The absolute y value of the current top row. */
48
49 static char *gamefile_name;
50 static rbool font_enable;
51 static rbool nav_arrow;
52 static rbool block_cursor;
53 static rbool tall_screen; /* 43/50 line mode? */
54
55 static char *active_font; /* Currently active font; NULL if none */
56 static int active_font_height;
57
58 typedef enum {MDA,CGA,MCGA,EGA64,EGA,VGA} gh_type;
59 static gh_type graph_hardware;
60
61
62 static char *fkey_text[24]={ /* Text for function keys */
63 "help\n",
64 "get ","drop ",
65 "examine ","read ",
66 "open ","close ",
67 "inventory\n","look\n",
68 "score\n",
69 "save\n","restore\n",
70 "exit\n",
71 "w\n","e\n","n\n","s\n",
72 "nw\n","sw\n","ne\n","se\n",
73 "enter\n","up\n","down\n"
74 };
75
76
77 #if 0
78 {
79 "undo\n",
80 "get ","drop ",
81 "read ","unlock ",
82 "open ","close ",
83 "inventory\n","look\n",
84 "wait\n",
85 "save\n","restore\n"};
86 #endif
87
88 /* Other candidates:
89 view
90 help
91 examine (x)
92 again (g) */
93
94
95
96 /*------------------------------------------------------------------*/
97 /* Text Style */
98 /*------------------------------------------------------------------*/
99
100 #define STATUS_STATE 0x3800
101 #define BOX_STATE 0x2800
102 #define START_STATE 0x0800
103
104 /* For state:
105 currently low 4 bits are color.
106 bit 11 (0x0800) indicates we should use "normal" color
107 bit 12 (0x1000) indicates we are printing the status line, so
108 supress blinking
109 bit 13 (0x2000) indicates whether we are in reverse video mode or not
110 bit 14 (0x4000) indicates whether we are blinking or not
111 bit 15 (0x8000) indicates whether bold is on or not
112 */
113
114 #define BOLD_BIT 0x8000
115 #define BLINK_BIT 0x4000
116 #define RVID_BIT 0x2000
117 #define STAT_BIT 0x1000
118 #define NORM_BIT 0x0800
119
120 typedef unsigned short vstate;
121 static vstate state_stack[16];
122 int stateptr=0; /* Points into state stack */
123
124 #define cstate state_stack[stateptr] /* Current state */
125
126 static void set_state(vstate state)
127 {
128 int c; /* Color */
129 int bkgd; /* Background and blink bits */
130
131 /* 0=normal/1=highlight/2=background/3=statfore/4=statback */
132
133 if (state & NORM_BIT)
134 c=colorset[cs_normal];
135 else
136 c=state & 0xF; /* Extract color */
137
138 if (state & RVID_BIT) { /* Reverse video */
139 bkgd=colorset[cs_statback];
140 if (state & NORM_BIT)
141 c=colorset[cs_statfore];
142 } else
143 bkgd=colorset[cs_back];
144
145 if (state & BOLD_BIT)
146 if (state & NORM_BIT)
147 c=colorset[cs_bold];
148 else c^=0x08; /* Toggle bold bit */
149
150 if ( (state & BLINK_BIT) && !(state & STAT_BIT) )
151 bkgd|=0x08; /* Blinking */
152
153 bkgd=(bkgd<<4)|c;
154 textattr(bkgd);
155 cstate=state;
156 }
157
158 static void reset_state(void)
159 {
160 set_state(cstate);
161 }
162
163 static void push_state(vstate state)
164 {
165 if (stateptr==15) fatal("State stack overflow!");
166 stateptr++;
167 set_state(state);
168 }
169
170 static void pop_state(void)
171 {
172 if (stateptr==0) fatal("State stack error: POP without PUSH!");
173 stateptr--;
174 set_state(cstate);
175 }
176
177 void agt_textcolor(int c)
178 /* Set text color to color #c, where the colors are as follows: */
179 /* 0=Black, 1=Blue, 2=Green, 3=Cyan, 4=Red, 5=Magenta, 6=Brown, */
180 /* 7=White("normal"), 8=Blinking. */
181 /* 9= *Just* White (not neccessarily "normal" and no need to turn off */
182 /* blinking) */
183 /* Also used to set other text attributes: */
184 /* -1=emphasized text, used (e.g.) for room titles */
185 /* -2=end emphasized text */
186 {
187 vstate nstate;
188
189 nstate=cstate;
190 if (c==-1)
191 nstate=nstate|0x8000;
192 else if (c==-2)
193 nstate=nstate & ~0x8000; /* BOLD off */
194 else if (c==8)
195 nstate=nstate | 0x4000; /* BLINK on */
196 else if (c==7)
197 nstate=START_STATE; /* "Normal" */
198 else if (c==9)
199 nstate=(nstate & ~0x080F) | 7; /* Set color to 7: white */
200 else if (c>=0 && c<7)
201 nstate=(nstate & ~0x080F) | c; /* Set color to c */
202 set_state(nstate);
203 }
204
205
206
207 /*------------------------------------------------------------------*/
208 /* Misc. Functions */
209 /*------------------------------------------------------------------*/
210
211
212 void agt_delay(int n)
213 {
214 if (BATCH_MODE) return;
215 print_statline();
216 sleep(n);
217 }
218
219
220 void agt_tone(int hz,int ms)
221 /* Produce a hz-Hertz sound for ms milliseconds */
222 {
223 if (!sound_on) return;
224 sound(hz);
225 delay(ms);
226 nosound();
227 }
228
229 int agt_rand(int a,int b)
230 /* Return random number from a to b inclusive */
231 {
232 return a+(rand()>>2)%(b-a+1);
233 }
234
235
236
237 /*------------------------------------------------------------------*/
238 /* Key Tables */
239 /*------------------------------------------------------------------*/
240
241
242 /*
243 1000= backspace
244 1001= delete
245 1002= left
246 1003= right
247 1004= up
248 1005= down
249 1006= home
250 1007= end
251 1008= page up
252 1009= page down
253 1010= ins
254 1011= gray plus
255 1012= gray minus
256 10mm= Function key F<n> with mm=n+13 (F0==F10)
257 1500= ^K
258 1501= ^Y
259 1502= ^L
260 2000= unknown control character
261 */
262
263 #define KEY_CNT 25
264 #define FKEY_BASE 13
265 #define FKEY_CNT (12+8+4)
266
267 static char keytrans[KEY_CNT]={
268 0x00,0x53, /* Delete and backspace */
269 0x4b,0x4d,0x48,0x50, /* Arrow keys */
270 0x47,0x4F, /* Home and End */
271 0x49,0x51, /* PgUp, PgDown */
272 0x52,0x4E,0x4A, /* Ins, Plus, Minus */
273 0x44, /* F10 */
274 0x3b,0x3c,0x3d,0x3e,0x3f, /* F1-F5 */
275 0x40,0x41,0x42,0x43, /* F6-F9 */
276 0x85,0x86}; /* F11-F12 */
277
278
279 static const char *keylist[12]={
280 "del","left","right","up","down","home","end","pgup","pgdn",
281 "ins", "plus", "minus"};
282
283
284
285
286
287 /*------------------------------------------------------------------*/
288 /* Line Editor */
289 /*------------------------------------------------------------------*/
290
291 static int saved_tab=0;
292
293 unsigned xgetch(void)
294 {
295 struct REGPACK regs;
296
297 regs.r_ax=0;
298 intr(0x16,&regs);
299 return regs.r_ax;
300 }
301
302
303 int read_a_key(void)
304 {
305 unsigned xc;
306 char c;
307 int i;
308
309 scroll_count=0;
310 if (saved_tab>0) {
311 saved_tab--;
312 return ' ';
313 }
314 xc=xgetch();
315
316 if (xc==0x4e2b) xc=0x4e00; /* Gray plus */
317 if (xc==0x4a2d) xc=0x4a00; /* Gray minus */
318 c=xc&0xFF;
319 if ( c!=0 )
320 if (isprint(c)) return c;
321 else if (c=='\n' || c=='\r') return '\n';
322 else if (c=='\t') {
323 saved_tab=4;
324 return ' ';
325 }
326 else if (c=='\001') /* Ctrl-A ==> Home */
327 return 1006;
328 else if (c=='\005') /* Ctrl-E ==> End */
329 return 1007;
330 else if (c=='\010') /* Ctrl-H ==> Backspace */
331 return 1000;
332 else if (c=='\013') /* Ctrl-K */
333 return 1500;
334 else if (c=='\031') /* Ctrl-Y */
335 return 1501;
336 else if (c=='\014') /* Ctrl-L */
337 return 1502;
338 else return 2000;
339
340 /* Get extended code */
341 c=(xc>>8)&0xFF;
342 for(i=0;i<KEY_CNT;i++) {
343 if (c==keytrans[i])
344 if (!nav_arrow || i==0 || i>=FKEY_BASE)
345 return 1000+i;
346 else return 1000+FKEY_BASE+12+i-1;
347 }
348 return 2000;
349 }
350
351
352
353
354 #ifdef USE_EDITLINE
355
356 void agt_putc(char c)
357 {
358 #ifdef UNIX
359 fix_loc(); /* Fixup if returning from being suspended */
360 #endif
361 printf("%c",c);
362 curr_x+=1;
363 }
364
365 static rbool old_yheight=0;
366 static int base_x,base_y; /* Base X value of first character of input */
367
368 /* This assumes the cursor is always positioned on the string
369 being edited */
370 static void update_cursor(int cursor)
371 {
372 int curr_y;
373
374 curr_x=base_x+cursor;
375 curr_y=base_y;
376 while (curr_x>=screen_width) {
377 curr_x-=screen_width;
378 curr_y++;
379 }
380 gotoxy(curr_x+1,curr_y+1);
381 }
382
383 /* static rbool save_accum_text=0;*/
384
385 static void update_line(int cursor,char *buff)
386 {
387 int yval, yheight, xpos, i, curr_y;
388
389 yheight=(base_x+strlen(buff)+screen_width-1)/screen_width; /* Round up */
390 /* printf("<%d+%d>",base_y,yheight);*/
391 gotoxy(screen_width,screen_height);
392 while (base_y+yheight+status_height>screen_height && base_y>1) {
393 base_y--;
394 /* save_accum_text=1;*/
395 cputs("\r\n");
396 /* save_accum_text=0;*/
397 }
398 for(yval=yheight;yval<old_yheight;yval++) {
399 gotoxy(1,base_y+yval+1);
400 cleareol();
401 }
402 old_yheight=yheight;
403 xpos=cursor-1;
404 if (xpos<0) xpos=0;
405 update_cursor(xpos);
406 curr_x=wherex()-1;
407 curr_y=wherey()-1;
408 for(yval=curr_y-base_y;yval<yheight;yval++) {
409 cleareol();
410 for(i=curr_x;i<screen_width;i++) {
411 if (buff[xpos]==0) break;
412 putchar(buff[xpos++]);
413 }
414 curr_x=0;
415 if (base_y+yval+status_height<screen_height)
416 gotoxy(curr_x+1,base_y+yval+2);
417 else break;
418 }
419 update_cursor(cursor);
420 }
421
422 static void redisplay_line(char *buff)
423 {
424 update_line(0,buff);
425 }
426
427 #define ENABLE_HIST
428 static char **hist=NULL;
429 static long histcount=0;
430 static long histmax=10;
431
432 static char *line_edit(int state)
433 /* state=0, input a line.
434 state=1, input character with echo
435 state=2, input character with no echo */
436 {
437 static char *yank_text=NULL;
438 rbool editmode, exitflag;
439 int key;
440 int buffleng, buffspace; /* buffspace is space allocated for buffer,
441 buffleng is space really used */
442 static int cursor; /* Where the insertion point is in buffer:
443 insert at this location */
444 char *buff, *savebuff;
445 int i, curr_hist;
446
447 buff=rmalloc(2);
448 savebuff=NULL;
449 buff[0]=buff[1]=0;
450 cursor=buffleng=0;buffspace=2;
451 base_x=wherex()-1; base_y=wherey()-1;
452 editmode=(state==0);
453 saved_tab=0;
454 old_yheight=0;
455 scroll_count=0;
456 exitflag=0;
457
458 #ifdef UNIX
459 set_term_noncanon();
460 keyread=keyptr=0; /* Clear key-buffer */
461 #endif
462
463 for(;;) {
464 key=read_a_key();
465 if (DEBUG_KEY) printf("{%d}",key);
466 if (key<1000) { /* Real character entered */
467 if (state>0) { /* One key */
468 if (state==1) {
469 agt_putc(key);
470 agt_newline();
471 }
472 buff[0]=key; buff[1]=0;
473 break;
474 }
475
476 #ifdef ENABLE_HIST
477 if (savebuff!=NULL) {
478 rfree(savebuff);
479 buff=rstrdup(buff);
480 buffspace=buffleng+1;
481 }
482 #endif
483 if (key=='\n') {
484 update_cursor(buffleng);
485 break;
486 }
487 /* add character to buffer */
488 buffleng++;
489 if (buffleng+1>buffspace) buffspace+=20;
490 buff=rrealloc(buff,buffspace);
491 for(i=buffleng;i>cursor;i--)
492 buff[i]=buff[i-1];
493 buff[cursor++]=key;
494 update_line(cursor,buff);
495 }
496 else switch(key-1000)
497 { /* Special key */
498 case 0: /* Backspace */
499 if (!editmode || cursor==0) break;
500 cursor--;
501 /* Fall through... */
502 case 1: /* Delete */
503 if (!editmode || buffleng==cursor) break;
504 #ifdef ENABLE_HIST
505 if (savebuff!=NULL) {
506 rfree(savebuff);
507 buff=rstrdup(buff);
508 buffspace=buffleng+1;
509 }
510 #endif
511 for(i=cursor;i<buffleng;i++)
512 buff[i]=buff[i+1];
513 buffleng--;
514 update_line(cursor,buff);
515 break;
516 case 2: /* Left arrow */
517 if (editmode && cursor>0)
518 update_cursor(--cursor);
519 break;
520 case 3: /* Right arrow */
521 if (editmode && cursor<buffleng)
522 update_cursor(++cursor);
523 break;
524 #ifdef ENABLE_HIST
525 case 4: /* Up arrow: Command history */
526 if (!editmode || histcount==0) break;
527 if (savebuff==NULL) { /* Save current line */
528 savebuff=buff;
529 curr_hist=histcount;
530 }
531 curr_hist--;
532 if (curr_hist<0) {
533 curr_hist=0;
534 break;
535 }
536 buff=hist[curr_hist];
537 buffleng=strlen(buff);
538 cursor=0;
539 redisplay_line(buff);
540 break;
541 case 5: /* Down arrow: Command history */
542 if (!editmode || savebuff==NULL) break;
543 curr_hist++;
544 if (curr_hist>=histcount) {
545 /* Reached bottom: restore original line */
546 curr_hist=histcount;
547 buff=savebuff;
548 savebuff=NULL;
549 } else
550 buff=hist[curr_hist];
551 buffleng=strlen(buff);
552 cursor=0;
553 redisplay_line(buff);
554 break;
555 #endif
556 case 6: /* Home */
557 if (!editmode) break;
558 cursor=0;
559 update_cursor(cursor);
560 break;
561 case 7: /* End */
562 if (!editmode) break;
563 cursor=buffleng;
564 update_cursor(cursor);
565 break;
566 case 8: /* Page up : Scroll back */
567 case 9: /* Page down */
568 break;
569 case 500: /* Ctrl-K: Delete to EOL */
570 if (!editmode) break;
571 #ifdef ENABLE_HIST
572 if (savebuff!=NULL) {
573 rfree(savebuff);
574 buff=rstrdup(buff);
575 buffspace=buffleng+1;
576 }
577 #endif
578 rfree(yank_text);
579 yank_text=rstrdup(buff+cursor);
580 buffleng=cursor;
581 buff[buffleng]=0;
582 update_line(cursor,buff);
583 break;
584 case 501: /* Ctrl-Y: Yank */
585 if (!editmode) break;
586 {
587 int txtleng;
588 #ifdef ENABLE_HIST
589 if (savebuff!=NULL) {
590 rfree(savebuff);
591 buff=rstrdup(buff);
592 buffspace=buffleng+1;
593 }
594 #endif
595 if (yank_text==NULL)
596 txtleng=0;
597 else
598 txtleng=strlen(yank_text);
599 buffleng+=txtleng;
600 while(buffleng+1>buffspace) buffspace+=20;
601 buff=rrealloc(buff,buffspace);
602 for(i=buffleng;i>=cursor+txtleng;i--)
603 buff[i]=buff[i-txtleng];
604 for(i=0;i<txtleng;i++)
605 buff[cursor+i]=yank_text[i];
606 update_line(cursor,buff);
607 cursor+=txtleng;
608 update_cursor(cursor);
609 }
610 break;
611 case 502: /* Ctrl-L: Redraw screen */
612 #ifdef UNIX
613 agt_clrscr();
614 base_y=curr_y;
615 printf("%s",accum_text); /* Redraw prompt */
616 if (editmode)
617 redisplay_line(buff);
618 #endif
619 break;
620
621 default:
622 if (key<1000+FKEY_BASE || key>=1000+FKEY_BASE+FKEY_CNT)
623 break;
624 if (!editmode) break;
625 {
626 int txtleng;
627 #ifdef ENABLE_HIST
628 if (savebuff!=NULL) {
629 rfree(savebuff);
630 buff=rstrdup(buff);
631 buffspace=buffleng+1;
632 }
633 #endif
634 txtleng=strlen(fkey_text[key-1000-FKEY_BASE]);
635 if (fkey_text[key-1000-FKEY_BASE][txtleng-1]=='\n') {
636 /* Newlines should only appear at the end; they indicate
637 that this function key should cause the line to be entered. */
638 txtleng--;
639 exitflag=1; /* Finish entering this line */
640 }
641 buffleng+=txtleng;
642 while(buffleng+1>buffspace) buffspace+=20;
643 buff=rrealloc(buff,buffspace);
644 for(i=buffleng;i>=cursor+txtleng;i--)
645 buff[i]=buff[i-txtleng];
646 for(i=0;i<txtleng;i++)
647 buff[cursor+i]=fkey_text[key-1000-FKEY_BASE][i];
648 update_line(cursor,buff);
649 cursor+=txtleng;
650 update_cursor(cursor);
651 }
652 break;
653 }
654
655 if (exitflag) break;
656 }
657 #ifdef UNIX
658 set_term_normal();
659 #endif
660 buff=rrealloc(buff,(buffleng+1));
661 if (state==0) {
662 if (histmax==0 || histcount<histmax)
663 hist=rrealloc(hist,(++histcount)*sizeof(char*));
664 else
665 for(i=0;i<histcount-1;i++)
666 hist[i]=hist[i+1];
667 hist[histcount-1]=rstrdup(buff);
668 }
669 return buff;
670 }
671 #endif
672
673
674
675
676
677
678 /*------------------------------------------------------------------*/
679 /* Basic Input and Output Code */
680 /*------------------------------------------------------------------*/
681
682
683 static void script_out(const char *s)
684 {
685 if (DEBUG_OUT)
686 fprintf(debugfile,"%s",s);
687 if (script_on) fprintf(scriptfile,"%s",s);
688 }
689
690
691
692 int tmpcnt=0;
693
694 char *agt_input(int in_type)
695 /* read a line from the keyboard, allocating space for it using malloc */
696 /* in_type: 0=command, 1=number, 2=question, 3=userstr, 4=filename,*/
697 /* 5=RESTART,RESTORE,UNDO,QUIT */
698 /* Negative values are for internal use by the interface (i.e. this module) */
699 /* We completely ignore the value of in_type */
700 {
701 static int input_error=0;
702 char *s;
703
704 scroll_count=0;
705 print_statline();
706
707 #ifdef USE_EDITLINE
708 s=line_edit(0);
709 #else
710 s=rmalloc(300);
711 s[0]=297;
712 s[1]=0;
713 if (cgets(s)==NULL) { /* I have no idea if cgets _ever_ returns NULL */
714 if (++input_error>10) {
715 printf("Console read failure.\n");
716 exit(1);
717 }
718 } input_error=0;
719
720 s=rrealloc(s,s[1]+3);
721 memmove(s,s+2,s[1]+1);
722 printf("\n");
723 #endif
724
725 printf("\n");
726 script_out(s);script_out("\n");
727 curr_x=wherex()-1;
728 return s;
729 }
730
731 char agt_getkey(rbool echo_char)
732 /* Reads a character and returns it, possibly reading in a full line
733 depending on the platform */
734 /* If echo_char=1, echo character. If 0, then the character is not
735 required to be echoed (and ideally shouldn't be) */
736 {
737 char c;
738 char s[2];
739
740 scroll_count=0;
741 print_statline();
742
743 if (echo_char)
744 c=getche(); /* Get with echo */
745 else c=getch(); /* Get w/o echo */
746 if (c==0) getch(); /* Throw away extended character */
747 if (echo_char) {
748 s[0]=c;s[1]=0;
749 script_out(s);
750 agt_newline();
751 }
752 curr_x=wherex()-1;
753 return c;
754 }
755
756
757 static void print_compass(void)
758 {
759 int cwidth, i;
760
761 if (status_width< 9+4*12) return;
762 gotoxy(1,2);
763 cputs(" EXITS: ");cwidth=9;
764 for(i=0;i<12;i++)
765 if (compass_rose & (1<<i)) {
766 cputs(exitname[i]);
767 cputs(" ");
768 cwidth+=strlen(exitname[i])+1;
769 }
770 for(i=cwidth;i<status_width;i++) cputs(" ");
771 }
772
773
774 static rbool is_fullscreen; /* True until we print the first status line */
775
776 void agt_statline(const char *s)
777 /* Output a string at location (x,y), with attributes attr */
778 /* 0=normal, 1=background(e.g. status line) */
779 {
780 int savex, savey;
781
782 savex=wherex();savey=wherey()+top_row; /* Save old postion */
783 if (is_fullscreen) { /* Need to move text down a bit */
784 int i;
785 gotoxy(1,1);
786 for(i=0;i<status_height;i++)
787 insline();
788 }
789 window(1,1,screen_width,screen_height); /* Set window to whole screen */
790 gotoxy(1,1);
791 push_state(STATUS_STATE);
792 cputs(s); /* Console put string */
793 if (have_compass) {
794 status_height=2;
795 top_row=3;
796 print_compass();
797 } else {
798 top_row=2;
799 status_height=1;
800 }
801 pop_state();
802 window(1,status_height+1,screen_width,screen_height); is_fullscreen=0;
803 /* Exclude status line from window again */
804 gotoxy(savex,savey-top_row); /* Return to old postion */
805 return;
806 }
807
808
809 void agt_clrscr(void)
810 /* This should clear the screen and put the cursor at the upper left
811 corner (or one down if there is a status line) */
812 {
813 clrscr();
814 curr_x=0;
815 if (DEBUG_OUT) fprintf(stderr,"\n\n<CLRSCR>\n\n");
816 if (script_on) fprintf(scriptfile,"\n\n\n\n");
817 scroll_count=0;
818 }
819
820 rbool cursor_wrap;
821
822 void agt_puts(const char *s)
823 {
824 int old_x;
825 old_x=wherex()-1;
826 cputs(s);
827 curr_x=wherex()-1;
828 if (curr_x<old_x+strlen(s)) /* We wrapped */
829 cursor_wrap=1;
830 if (DEBUG_OUT) fprintf(stderr,"%s",s);
831 if (script_on) fprintf(scriptfile,"%s",s);
832 }
833
834 void agt_newline(void)
835 {
836 if (!cursor_wrap) cputs("\r\n");
837 curr_x=0;cursor_wrap=0;
838 if (DEBUG_OUT) fprintf(stderr,"\n");
839 if (script_on) fprintf(scriptfile,"\n");
840 scroll_count++;
841 if (scroll_count>=screen_height-status_height-1) {
842 cputs(" --MORE--"); /* Notice: no newline */
843 agt_waitkey();
844 gotoxy(1,wherey()); /* Move to beginning of line */
845 clreol(); /* Clear to end of line: erase the --MORE-- */
846 }
847 }
848
849
850
851
852 /*------------------------------------------------------------------*/
853 /* Text Box Routines */
854 /*------------------------------------------------------------------*/
855
856
857 static unsigned long boxflags;
858 static int box_startx; /* Starting x of box; starts from 0 */
859 static int box_width;
860 static int delta_scroll; /* Amount we are adding to vertical scroll
861 with the box */
862
863 static void box_border(char *c)
864 {
865 #ifdef DRAW_BORDER
866 agt_puts(c);
867 #else
868 agt_puts(" ");
869 #endif
870 }
871
872
873 #define VLINE_STR "\263" /* 0xB3 or 0xBA */
874
875 static void boxrule(int bottom)
876 /* Draw line at top or bottom of box */
877 {
878 int i;
879
880 if (bottom)
881 box_border("\300"); /* 0xC0 or 0xC8 */
882 else
883 box_border("\332"); /* 0xDA or 0xC9 */
884 for(i=0;i<box_width+2;i++) box_border("\304"); /* 0xC4 or 0xCD */
885 if (bottom)
886 box_border("\311"); /* 0xC9 or 0xBC */
887 else
888 box_border("\277"); /* 0xBF or 0xBB */
889 }
890
891
892 static void boxpos(void)
893 {
894 int curr_y;
895
896 curr_y=wherey(); /* == y location + 1 for status line */
897 if (curr_y>screen_height) curr_y=screen_height;
898 gotoxy(box_startx+1,curr_y+1);
899 curr_x=box_startx;
900 }
901
902 void agt_makebox(int width,int height,unsigned long flags)
903 /* Flags: TB_TTL, TB_BORDER, TB_NOCENT */
904 {
905 int box_starty;
906
907 boxflags=flags;
908 box_width=width;
909 if (boxflags&TB_BORDER) { /* Add space for border */
910 width+=4;
911 height+=2;
912 }
913 if (boxflags&TB_NOCENT) box_startx=0;
914 else box_startx=(screen_width-width)/2; /* Center the box horizontally */
915 if (box_startx<0) box_startx=0;
916
917 /* Now we need to compute the vertical position of the box */
918 if (flags & TB_TTL) { /* Title: centered horizontally */
919 box_starty=(screen_height-height)/2;
920 if (box_starty+height+8>screen_height) /* Make room for credits */
921 box_starty=screen_height-height-8;
922 if (box_starty<1) box_starty=1;
923 delta_scroll=0;
924 scroll_count=0;
925 window(1,1,screen_height,screen_width); /* Titles get the whole screen */
926 is_fullscreen=1;
927 } else { /* Compute vertical position of non-title box */
928 box_starty=wherey()-1-height;
929 if (box_starty<1) {
930 delta_scroll=1-box_starty;
931 box_starty=1;
932 } else delta_scroll=0;
933 }
934
935 curr_x=box_startx;
936 gotoxy(box_startx+1,box_starty+1);
937
938 if (boxflags&TB_BORDER) {
939 push_state(BOX_STATE);
940 boxrule(0);
941 boxpos();
942 box_border(VLINE_STR);
943 agt_puts(" ");
944 }
945 }
946
947 void agt_qnewline(void)
948 {
949 if (boxflags&TB_BORDER) {
950 agt_puts(" ");
951 box_border(VLINE_STR);
952 }
953 boxpos();
954 if (boxflags&TB_BORDER) {
955 box_border(VLINE_STR);
956 agt_puts(" ");
957 }
958 }
959
960 void agt_endbox(void)
961 {
962 if (boxflags&TB_BORDER) {
963 agt_puts(" ");
964 box_border(VLINE_STR);
965 boxpos();
966 boxrule(1);
967 pop_state();
968 }
969 scroll_count+=delta_scroll;
970 agt_newline(); /* NOT agt_qnewline() */
971 if ( (boxflags&TB_TTL) )
972 while(wherey()+1<screen_height-8) agt_newline();
973 }
974
975
976
977 /*------------------------------------------------------------------*/
978 /* CFG Editor (In development) */
979 /*------------------------------------------------------------------*/
980 #if 0
981
982 static char **readconf(fc_type fc)
983 {
984
985 }
986
987
988 static void writeconf(fc_type fc, char **conf)
989 {
990
991 }
992
993
994 static char *edops[]={"color","navarrow","tone","compass","input_bold"};
995
996
997 static rbool checkopt(int i)
998 /* Return true if option i is true */
999 {
1000 switch(i) {
1001 case 1:return nav_arrow;
1002 case 2:return PURE_TONE;
1003 case 3:return have_compass;
1004 case 4:return PURE_INPUT;
1005 default: return 1;
1006 }
1007 }
1008
1009
1010 static void savechange(fc_type fc, uchar changes)
1011 /* Call this to save changes to configuration */
1012 {
1013 char **conf, *s;
1014 int i;
1015 int lopt[5];
1016 /* lxxx holds the line number of xxx */
1017
1018 lcolor=lnavarrow=lcompass=ltone=linputbold=-1;
1019 conf=readconf(fc);
1020 for(numline=0;conf[numline]!=NULL;numline++) {
1021 /* Detect COLOR, NAVARROW, TONE, COMPASS, INPUT_BOLD */
1022 s=conf[numline];
1023 while(strncasecmp(s,"no_",3)==0) s+=3;
1024 for(i=0;i<5;i++)
1025 if (strncasecmp(s,edops[i],strlen(edops[i]))==0) {
1026 lopt[i]=numline;
1027 break;
1028 }
1029 }
1030
1031 /* Rebuild config file */
1032 for(i=0;i<5;i++, changes>>=1)
1033 if (changes & 1) { /* Check bit */
1034
1035 if (i==0) { /* COLOR */
1036 int j;
1037 s=rmalloc(5+5*15); /* More than enough space */
1038 strcpy(s,"color");
1039 for(j=0;j<5;j++) {
1040 strcat(s," ");
1041 strcat(s,colorname[colorset[j]]);
1042 }
1043
1044 } else { /* Non-COLOR options */
1045 s=rmalloc(strlen(edops[i])+4); /* Room for NO_ & \0 */
1046 if (!checkopt(i))
1047 strcpy(s,"no_");
1048 else s[0]=0;
1049 strcat(s,edops[i]);
1050 }
1051
1052 if (i>=0) { /* Change existing line */
1053 rfree(conf[numline]);
1054 conf[numline]=s;
1055 } else { /* Add new line */
1056 conf=rrealloc(++numline);
1057 conf[numline-1]=s;
1058 conf[numline]=NULL;
1059 }
1060
1061 }
1062 writeconf(fc, conf);
1063
1064 for(i=0;i<numline;i++)
1065 rfree(conf[i]);
1066 rfree(conf);
1067 }
1068
1069
1070 void editconf(void)
1071 {
1072
1073
1074
1075
1076 }
1077
1078
1079
1080
1081
1082 #endif
1083 /*------------------------------------------------------------------*/
1084 /* Parsing CONFIG Options */
1085 /*------------------------------------------------------------------*/
1086
1087 const char *colorname[16]=
1088 {"black","blue","green","cyan","red","magenta","brown","lightgray",
1089 "darkgray","lightblue","lightgreen","lightcyan","lightred","lightmagenta",
1090 "yellow","white"};
1091
1092 static int parse_color(int index,char *cname)
1093 {
1094 int i;
1095 for(i=0;i<16;i++)
1096 if(strcasecmp(colorname[i],cname)==0) return i;
1097 return colorset[index];
1098 }
1099
1100
1101 static void getcolors(int cnum,char *clist[])
1102 {
1103 int i;
1104
1105 if (cnum>5) cnum=5;
1106 for(i=0;i<cnum;i++)
1107 colorset[i]=parse_color(i,clist[i]);
1108 colorset[cs_back]&=0x7;
1109 colorset[cs_statback]&=0x7;
1110 }
1111
1112
1113 /* Duplicate s and add '/' if neccessary */
1114 char *makepathentry(char *s)
1115 {
1116 char *p;
1117 int n;
1118 rbool addslash;
1119
1120 n=strlen(s);
1121 if (s[n-1]!='\\') addslash=1;
1122 p=rmalloc(n+addslash+1);
1123 strcpy(p,s);
1124 if (addslash) {
1125 p[n]='\\';
1126 p[n+1]=0; /* Mark new end of string */
1127 }
1128 return p;
1129 }
1130
1131 void free_gamepath(void)
1132 {
1133 char **p;
1134
1135 if (gamepath==NULL) return;
1136
1137 /* We skip the first element which always points to "" */
1138 for(p=gamepath+1;*p!=NULL;p++)
1139 rfree(*p);
1140 rfree(gamepath);
1141 }
1142
1143
1144 #define opt(s) (!strcasecmp(optstr[0],s))
1145
1146 void set_fkey(char *keyname, char *words[], int numwords)
1147 {
1148 int n,i,leng;
1149 char *s;
1150 rbool err;
1151 static long keys_defined=0;
1152
1153 err=0;
1154 if (tolower(keyname[0])!='f') {
1155 err=1;
1156 for(i=0;i<12;i++)
1157 if (0==strcasecmp(keylist[i],keyname)) {
1158 err=0;
1159 n=i+12;
1160 }
1161 } else { /* Name of form 'Fnn' */
1162 n=strtol(keyname+1,&s,0); /* s will point to the first erroneous char */
1163 if (keyname[1]==0 || *s!=0) err=1;
1164 if (n>12) err=1;
1165 if (n==10) n=0;
1166 if (n>10) n--;
1167 }
1168 if (err) {
1169 writeln("Unrecognized KEY name.");
1170 return;
1171 }
1172
1173 leng=0;
1174 for(i=0;i<numwords;i++)
1175 leng+=strlen(words[i]);
1176 s=rmalloc(leng+numwords+1);
1177 s[0]=0;
1178
1179 /* This isn't very efficient, but it doesn't need to be. */
1180 for(i=0;i<numwords;i++) {
1181 strcat(s,words[i]);
1182 strcat(s," ");
1183 }
1184 i=strlen(s)-2;
1185 if (i>=0 && s[i]=='+') {
1186 s[i]='\n';
1187 s[i+1]=0;
1188 }
1189
1190 if (keys_defined & (1<<n)) rfree(fkey_text[n]);
1191 fkey_text[n]=s;
1192 keys_defined|=(1<<n);
1193 }
1194
1195
1196 #define opt(s) (!strcasecmp(optstr[0],s))
1197
1198 rbool agt_option(int optnum,char *optstr[],rbool setflag)
1199 /* If setflag is 0, then the option was prefixed with NO_ */
1200 {
1201 int i;
1202
1203 if (optnum==0) return 1;
1204 if (opt("COMPASS")) {
1205 have_compass=1;
1206 return 1;
1207 }
1208 if (opt("FONT")) {
1209 font_enable=setflag;
1210 return 1;
1211 }
1212 if (opt("NAVARROW")) {
1213 nav_arrow=setflag;
1214 return 1;
1215 }
1216 if (opt("BIOS")) {
1217 use_bios=setflag;
1218 return 1;
1219 }
1220 if (opt("BLOCK_CURSOR")) {
1221 block_cursor=setflag;
1222 return 1;
1223 }
1224 if (opt("COLORS") || opt("COLOURS")) {
1225 getcolors(optnum-1,optstr+1);
1226 return 1;}
1227 if (opt("HISTORY")) {
1228 histmax=strtol(optstr[1],NULL,10);
1229 if (histmax<0) histmax=0;
1230 return 1;
1231 }
1232 if (opt("50_LINE")) {
1233 tall_screen=setflag;
1234 return 1;
1235 }
1236 if (opt("KEY") && optnum>=2) {
1237 set_fkey(optstr[1],optstr+2,optnum-2);
1238 return 1;
1239 }
1240 if (opt("PATH") && optnum>=2) {
1241 free_gamepath(); /* Throw away previous settings */
1242 gamepath=rmalloc((optnum+1)*sizeof(char*)); /* optnum-1+1 */
1243 gamepath[0]=""; /* ==> The current directory */
1244 for(i=1;i<optnum;i++) /* Starting at the 2nd entry of both */
1245 gamepath[i]=makepathentry(optstr[i]);
1246 gamepath[optnum]=NULL;
1247 return 1;
1248 }
1249 return 0;
1250 }
1251
1252 #undef opt
1253
1254 static char *progname;
1255 static const char *cfgname="agil.cfg";
1256
1257 FILE *agt_globalfile(int fid)
1258 {
1259 char *s,*t;
1260 FILE *f;
1261
1262 if (fid==0 && progname!=NULL) {
1263 s=rstrdup(progname);
1264 t=s+strlen(s);
1265 while (t!=s && *t!='\\' && *t!=':') t--;
1266 if (*t!=0) t++;
1267 *t=0;
1268 s=rrealloc(s,strlen(s)+strlen(cfgname)+1);
1269 strcat(s,cfgname);
1270 f=fopen(s,"rb");
1271 rfree(s);
1272 return f;
1273 }
1274 return NULL;
1275 }
1276
1277
1278
1279
1280
1281 /*------------------------------------------------------------------*/
1282 /* Initialization and Shutdown */
1283 /*------------------------------------------------------------------*/
1284
1285 static void id_hardware(void)
1286 {
1287 union REGS r;
1288
1289 r.h.ah=0x12; /* EGA+ Misc functions */
1290 r.h.bl=0x10; /* ...get information */
1291 r.h.bh=0x37; /* Check value */
1292 r.x.cx=0x73F1; /* Random check value */
1293 int86(0x10,&r,&r);
1294 if ( (r.h.bh==0 || r.h.bh==1) &&
1295 (r.h.bl>=0 && r.h.bl<=3) &&
1296 (r.h.ch & 0xF0)==0 && (r.h.cl & 0xF0)==0) {
1297 /* Okay, the function works; we have an EGA/VGA */
1298
1299 if (r.h.bl==0) graph_hardware=EGA64;
1300 else graph_hardware=EGA;
1301
1302 r.x.ax=0x1A00;
1303 int86(0x10,&r,&r);
1304 if (r.h.al==0x1A) /* We have a VGA */
1305 graph_hardware=VGA;
1306
1307 } else { /* MDA/CGA/MCGA */
1308 r.x.ax=0x1A00;
1309 int86(0x10,&r,&r);
1310 if (r.h.al==0x1A) { /* We have an MCGA */
1311 graph_hardware=MCGA;
1312 return;
1313 }
1314
1315 graph_hardware=CGA;
1316
1317 r.h.ah=0x0F; /* Get current video mode */
1318 int86(0x10,&r,&r);
1319 if ((r.h.al & 0x7F)==7)
1320 graph_hardware=MDA;
1321 }
1322 }
1323
1324
1325 static int save_mode;
1326 static int save_attr;
1327
1328 void init_interface(int argc,char *argv[])
1329 {
1330 struct text_info term_data;
1331
1332 active_font=NULL;
1333
1334 id_hardware();
1335
1336 if (argc>0) progname=argv[0]; else progname=NULL;
1337 script_on=0;scriptfile=NULL;
1338 scroll_count=0;
1339 cursor_wrap=0;
1340 font_enable=1;
1341 nav_arrow=0;
1342 have_compass=0;status_height=0;
1343 block_cursor=0; tall_screen=0;
1344 center_on=par_fill_on=0;
1345 DEBUG_OUT=0;debugfile=stderr;
1346 gettextinfo(&term_data);
1347 save_mode=term_data.currmode;
1348 save_attr=term_data.attribute;
1349 directvideo=0; /* During startup, go through BIOS. */
1350 use_bios=0;
1351 textmode(C80); /* Change to color, 80 column mode */
1352 screen_height=25; /* Assume PC dimensions */
1353 status_width=screen_width=80;
1354 clrscr();
1355 window(1,1,screen_width,screen_height); is_fullscreen=1;
1356 top_row=1;
1357 gotoxy(1,1); /* Upper-left hand corner of just defined window */
1358 }
1359
1360 void start_interface(fc_type fc)
1361 {
1362 if (tall_screen) {
1363 struct text_info term_data;
1364 textmode(C4350); /* Change to color, 80 column mode */
1365 gettextinfo(&term_data);
1366 screen_height=term_data.screenheight; /* Assume PC dimensions */
1367 clrscr();
1368 gotoxy(1,1); /* Upper-left hand corner of just defined window */
1369 }
1370 directvideo=!use_bios; /* Set directvideo according to options */
1371 free_gamepath();
1372 if (stable_random)
1373 srand(6);
1374 else
1375 srand(time(0));
1376 gamefile_name=fc->gamename;
1377 if (block_cursor)
1378 _setcursortype(_SOLIDCURSOR);
1379 else
1380 _setcursortype(_NORMALCURSOR);
1381 clrscr();
1382 curr_x=0;scroll_count=0;
1383 if (have_compass)
1384 status_height=2;
1385 else
1386 status_height=1;
1387 window(1,1,screen_width,screen_height); /* Set window to whole screen */
1388 is_fullscreen=1; /* We use the whole screen until the first status line
1389 is printed */
1390 top_row=status_height+1;
1391 set_state(7);
1392 }
1393
1394 void close_interface(void)
1395 {
1396 if (scriptfile!=NULL)
1397 fclose(scriptfile);
1398 agt_newline();agt_newline();
1399 if (block_cursor) _setcursortype(_NORMALCURSOR);
1400 textmode(save_mode);
1401 textattr(save_attr);
1402 gotoxy(1,25);
1403 clreol();
1404 gotoxy(1,24);
1405 clreol();
1406 /* clrscr();*/
1407 }
1408
1409
1410
1411
1412
1413
1414 #ifdef REPLACE_BNW
1415
1416 /*------------------------------------------------------------------*/
1417 /* GRAPHICS AND FONT SUPPORT */
1418 /*------------------------------------------------------------------*/
1419
1420
1421 /* This cheats a bit, using "hidden" data from filename.c */
1422
1423 static FILE *linopen(char *name, char *ext)
1424 {
1425 FILE *f;
1426 char *fname;
1427
1428 fname=assemble_filename(hold_fc->path,name,ext);
1429 f=fopen(fname,"rb");
1430 rfree(fname);
1431 return f;
1432 }
1433
1434 /*------------------------------------------------------------------*/
1435 /* FONT Routines */
1436 /*------------------------------------------------------------------*/
1437
1438 static char *save_font;
1439 static int save_font_height;
1440
1441 static int set_font(char *fontdef, int font_height)
1442 {
1443 struct REGPACK r;
1444
1445 r.r_ax=0x1100;
1446 r.r_bx=font_height<<8; /* 8 bytes per character in BH; 0 in BL */
1447 r.r_cx=256; /* Number of characters defined */
1448 r.r_dx=0; /* Start at ASCII 0. */
1449 r.r_es=((long)fontdef)>>16;
1450 r.r_bp=((long)fontdef)&0xFFFF;
1451 intr(0x10,&r);
1452 return 0;
1453 }
1454
1455
1456 static void save_old_font(void)
1457 {
1458 union REGS r;
1459
1460 if (graph_hardware<MCGA) {
1461 /* User-defined fonts aren't supported */
1462 font_enable=0;
1463 return;
1464 }
1465
1466 r.x.ax=0x1130;
1467 r.h.bh=0;
1468 int86(0x10,&r,&r);
1469 save_font_height=r.x.cx;
1470 }
1471
1472
1473 static void restore_font(void)
1474 {
1475 union REGS r;
1476
1477 if (save_font_height==14)
1478 r.x.ax=0x1101; /* 8x14 */
1479 else if (save_font_height==16)
1480 r.x.ax=0x1104; /* 8x16 */
1481 else
1482 r.x.ax=0x1102; /* 8x8 */
1483 r.h.bl=0;
1484 int86(0x10,&r,&r);
1485 /* ... if MCGA ... */
1486 /* if (graph_hardware==MCGA) {*/
1487 r.x.ax=0x1103;
1488 r.h.bl=0;
1489 int86(0x10,&r,&r);
1490 /* } */
1491 }
1492
1493
1494
1495 void fontcmd(int cmd,int font)
1496 /* 0=Load font, name is fontlist[font]
1497 1=Restore original font
1498 2=Set startup font. (<gamename>.FNT)
1499 */
1500 {
1501 FILE *fontfile;
1502 char *buff, *fontname;
1503 int height;
1504
1505 if (!font_enable) return;
1506
1507 if (cmd==2)
1508 save_old_font();
1509 if (!font_enable) return;
1510
1511
1512 if (cmd==0 || cmd==2) {
1513 if (cmd==0) {
1514 fontname=fontlist[font];
1515 }
1516 else fontname=gamefile_name;
1517
1518 fontfile=linopen(fontname,".fnt");
1519 if (fontfile==NULL) return;
1520 fseek(fontfile,0,SEEK_END);
1521 height=ftell(fontfile);
1522 if (height%256!=0) {
1523 /* Error message */
1524 return;
1525 }
1526 height=height/256;
1527 buff=rmalloc(256*height);
1528 memset(buff,0,256*height);
1529
1530 fseek(fontfile,0,SEEK_SET);
1531 if (fread(buff,height,256,fontfile)!=256) {
1532 fclose(fontfile);
1533 rfree(buff);
1534 return;
1535 }
1536 fclose(fontfile);
1537 if (set_font(buff,height)) {
1538 /* Print error message */
1539 }
1540 rfree(active_font);
1541 active_font=buff;
1542 active_font_height=height;
1543 return;
1544 } else if (cmd==1) {
1545 rfree(active_font);
1546 restore_font();
1547 }
1548 }
1549
1550
1551
1552
1553
1554 /*------------------------------------------------------------------*/
1555 /* Graphics Support */
1556 /*------------------------------------------------------------------*/
1557
1558 static rbool in_gmode=0;
1559
1560 static char *gfxext[]={".P06",
1561 ".P40",".P41",".P42",".P43", /* 0-4: CGA */
1562 ".P13", /* 5: EGA */
1563 ".P19", /* 6: MCGA */
1564 ".P14",".P16", /* 7-8: EGA */
1565 ".P18"}; /* 9: VGA */
1566
1567 /* Modes 1-4 differ only in pallette */
1568
1569 /* 0 1 2 3 4 5 6 7 8 9 */
1570 static int paltype[]={0, 1, 1, 1, 1, 2, 3, 2, 2, 2};
1571 static int mode_x[]={640,320,320,320,320, 320,320, 640,640, 640};
1572 static int mode_y[]={200,200,200,200,200, 200,200, 200,350, 480};
1573 static int mode[] ={ 6, 4, 4, 4, 4, 13, 19, 14, 16, 18};
1574
1575 /* paltype is the log_2 of the bit depth */
1576 /* Mode 16 with lowmem EGA only has 2bpp */
1577 /* Mode to start from for each video type */
1578 /* MDA, CGA, MCGA, EGA65, EGA, VGA */
1579 static int start_mode[]={-1,4,6,7,8,9};
1580
1581
1582 int pick_gmode(int gmode, int pmode, int xsize, int ysize)
1583 {
1584 int save_gmode, work_gmode;
1585
1586 if (gmode!=-1) /* Check that gmode is consitent with file */
1587 if (mode_x[gmode]>=xsize && mode_y[gmode]>=ysize &&
1588 paltype[gmode]==pmode)
1589 return gmode;
1590
1591 save_gmode=gmode; work_gmode=-1;
1592 /* Okay, we don't know what mode we are; try to pick the best for
1593 this image */
1594 /* work_gmode will contain the highest resolution mode with the
1595 neccessary color depth; if we can't find a perfect mode, we'll
1596 use this and clip. */
1597 for(gmode=0;gmode<=start_mode[graph_hardware];gmode++) {
1598 if (gmode==6 && (graph_hardware==EGA || graph_hardware==EGA64))
1599 continue;
1600 if (gmode==5 && graph_hardware==MCGA) continue;
1601
1602 if (paltype[gmode]!=pmode) continue;
1603 work_gmode=gmode;
1604 if (mode_x[gmode]<xsize || mode_y[gmode]<ysize) continue;
1605 return gmode;
1606 }
1607 return work_gmode; /* May need to be clipped */
1608 }
1609
1610
1611 int pix_per_byte[]={8,4,8,1};
1612
1613 /* 8421 */
1614 /* IRGB */
1615
1616 #define BPI 8
1617 #define BPR 4
1618 #define BPG 2
1619 #define BPB 1
1620
1621 void draw_scanline(uchar *linebuff,int gmode,int y_start,
1622 int linesize,int sizex)
1623 {
1624 static const char mmr[4]=
1625 {BPB,BPG,BPR,BPI}; /* Seq Mask Map Register values */
1626 static int mode_bpl, writesize;
1627 static uchar end_mask;
1628 static uchar *target;
1629 int i;
1630
1631 if (linebuff==NULL) { /* Set up before first line */
1632 mode_bpl=mode_x[gmode]/pix_per_byte[paltype[gmode]];
1633 i=(mode_x[gmode]-sizex)/(2*pix_per_byte[paltype[gmode]]); /* Center it */
1634
1635 if (gmode<=4) target=(uchar*)0xB8000000L;
1636 else target=(uchar*)0xA0000000L;
1637
1638 target+=i+y_start*mode_bpl;
1639
1640 /* Now set i to the number of "extra" pixels */
1641 i=linesize*pix_per_byte[paltype[gmode]]-sizex;
1642 writesize=linesize-i/pix_per_byte[paltype[gmode]];
1643 i=i%pix_per_byte[paltype[gmode]];
1644 i=i*(1<<8/pix_per_byte[paltype[gmode]]); /* i=# bits of excess */
1645 end_mask=(0xFF<<i)&0xFF; /* ...and construct a bit mask */
1646
1647 if (paltype[gmode]==2) { /* Bit plane graphics */
1648 /* 3CE/3CF: Graphic Control Registers */
1649 /* Graphic Ctrl Reg (3CE/3CF):
1650 1: 0x00 (Enable Set/reset: CPU => bitplanes)
1651 3: 0x00 (Function select: replace)
1652 5: 0x00 (Write and read mode 0)
1653 8: 0xFF (Bit Mask) */
1654
1655 /* First, basic initalization. Most of these are default values,
1656 but it doesn't hurt to be careful */
1657 outp(0x3CE,0x01); /* Enable Set/Reset Register */
1658 outp(0x3CF,0x00); /* ... ignore Set/Reset register */
1659 outp(0x3CE,0x03); /* Function Select Register */
1660 outp(0x3CF,0x00); /* ... replace and don't rotate */
1661 outp(0x3CE,0x05); /* Mode Register */
1662 outp(0x3CF,0x00); /* ... write and read mode 0 */
1663 outp(0x3CE,0x08); /* Bit Mask Register */
1664 outp(0x3CF,0xFF); /* ... write to everything */
1665 }
1666 return;
1667 }
1668
1669 target+=mode_bpl; /* Move to beginning of next line */
1670
1671 if (paltype[gmode]!=2) { /* The easy case: just one plane */
1672 linebuff[writesize-1]&=end_mask;
1673 memcpy(target,linebuff,writesize);
1674 } else { /* 4 planes, 1 bit/pixel/plane */
1675 /* 3C4/3C5: Video Sequencer Registers
1676 2: Map Mask Register */
1677 for(i=0;i<4;i++) {
1678 outp(0x3C4,2); /* Map Mask Register */
1679 outp(0x3C5,mmr[i]); /* ... select the given bitplane */
1680 linebuff[linesize*i+writesize-1]&=end_mask;
1681 memcpy(target,linebuff+linesize*i,writesize);
1682 }
1683 }
1684 }
1685
1686
1687
1688 /* This flips the bits and separates them by three bits */
1689 static const uchar paltrans[]={0,8,1,9};
1690
1691
1692 static uchar build_ega_pal(uchar red, uchar green, uchar blue)
1693 /* Construct 6-bit color code, on pattern
1694 00rgbRGB, from Rrxxxxxx, Ggxxxxxx, Bbxxxxxx */
1695 {
1696 uchar palentry;
1697
1698 red>>=6;
1699 green>>=6;
1700 blue>>=6;
1701
1702 return paltrans[blue]+(paltrans[green]<<1)+(paltrans[red]<<2);
1703 }
1704
1705
1706 #define ival(n) (header[n]+(header[n+1]<<8))
1707
1708
1709
1710 static rbool setup_card(FILE *pcxfile,uchar *header,int gmode, int pmode)
1711 {
1712 int i;
1713 uchar *tmppal, *pal;
1714 int havepal; /* 0=No, 1=Yes, but not VGA, 2=Yes, maybe VGA */
1715 union REGS r;
1716 struct SREGS segreg;
1717
1718 if (header[1]==5) havepal=2;
1719 else if (header[1]==2) havepal=1;
1720 else havepal=0;
1721
1722 if (pmode==3 && havepal!=2) {
1723 writeln("ERROR: PCX file corrupted: bad color depth.");
1724 return 0;
1725 }
1726
1727 /*-------------------------------------*/
1728 /* Read in VGA palette */
1729 /*-------------------------------------*/
1730
1731 /* Next, read in the VGA palette if neccessary */
1732 pal=NULL;
1733 if (pmode==3) { /* VGA palette */
1734 uchar c;
1735 long n;
1736
1737 pal=rmalloc(256*3);
1738 fseek(pcxfile,0,SEEK_END);
1739 n=ftell(pcxfile);
1740 if (fseek(pcxfile,n-256*3-1,SEEK_SET)
1741 || fread(&c,1,1,pcxfile)!=1) {
1742 writeln("GAME ERROR: Errors reading picture file.");
1743 rfree(pal);
1744 return 0;
1745 }
1746 if (c!=12) {
1747 writeln("ERROR: PCX file corrupted: bad palette.");
1748 rfree(pal);
1749 return 0;
1750 }
1751 if (fread(pal,768,1,pcxfile)!=1) {
1752 writeln("GAME ERROR: Errors reading palette from picture file.");
1753 rfree(pal);
1754 return 0;
1755 }
1756 }
1757
1758
1759 /*-------------------------------------*/
1760 /* Set up video mode */
1761 /*-------------------------------------*/
1762
1763 /* -- Need to save old video state here -- */
1764
1765 /* Set video mode */
1766 r.x.ax=mode[gmode];
1767 int86(0x10,&r,&r);
1768 in_gmode=1;
1769
1770 /*-------------------------------------*/
1771 /* Set up Palette */
1772 /*-------------------------------------*/
1773
1774 #if 0
1775 if (pmode==2 && paltype[gmode]==3) {
1776 /* Set up fake EGA palette */
1777 pal=rmalloc(256*3);
1778 for(i=0;i<3*16;i++)
1779 pal[i]=header[16+i];
1780 for(i=3*16;i<3*256;i++)
1781 pal[i]=0;
1782 } else if (pmode<=1 && paltype[gmode]==2) {
1783 /* This could occur with a high resolution 2 or 4 color image */
1784
1785 fatal("Unsupported image class.");
1786
1787 } else assert(pmode==paltype[gmode]);
1788 #endif
1789
1790 assert(pmode==paltype[gmode]);
1791
1792 /* Now to set the palette */
1793 if (paltype[gmode]==1) { /* CGA palette */
1794 r.h.ah=0x0B;
1795 r.h.bh=1;
1796 if (pmode==1 && havepal!=0)
1797 r.h.bl=(header[19]>>6)&0x01;
1798 else
1799 r.h.bl=(gmode-1)>>1; /* Top bit==> palette */
1800 int86(0x10,&r,&r);
1801
1802 r.h.ah=0x0B; /* Set background and intensity bits */
1803 r.h.bh=0;
1804 if (pmode==1 && havepal!=0)
1805 r.h.bl= ( (header[19]>>1) & 0x10 ) | (header[16]>>4);
1806 else
1807 r.h.bl=((gmode-1)&1)<<4; /* Background will be 0. */
1808 }
1809 else if (paltype[gmode]==2 && havepal!=0) { /* EGA Palette */
1810 tmppal=rmalloc(17);
1811 if (graph_hardware!=VGA)
1812 for(i=0;i<16;i++)
1813 tmppal[i]=build_ega_pal(header[16+3*i],
1814 header[17+3*i],
1815 header[18+3*i]);
1816 else for(i=0;i<16;i++) tmppal[i]=i+1; /* For VGA hardware, point to DAC */
1817 tmppal[16]=0; /* Set overscan to 0 */
1818
1819 r.x.ax=0x1002;
1820 segreg.es=((long)tmppal)>>16;
1821 r.x.dx=((long)tmppal)&0xFFFF;
1822 int86x(0x10,&r,&r,&segreg); /* Set EGA palette registers */
1823
1824 rfree(tmppal);
1825
1826 if (graph_hardware==VGA) { /* Now reprogram the DAC */
1827 tmppal=rmalloc(17*3);
1828 for(i=0;i<16;i++) {
1829 tmppal[3+3*i]=header[16+3*i]>>2;
1830 tmppal[4+3*i]=header[17+3*i]>>2;
1831 tmppal[5+3*i]=header[18+3*i]>>2;
1832 }
1833 tmppal[0]=tmppal[1]=tmppal[2]=0; /* Overscan color is black */
1834 r.x.ax=0x1012;
1835 r.x.bx=0;
1836 r.x.cx=17;
1837 r.x.dx=((long)tmppal)&0xFFFF;
1838 segreg.es=((long)tmppal)>>16;
1839 int86x(0x10,&r,&r,&segreg);
1840 }
1841 } else if (paltype[gmode]==3 && havepal!=0) { /* MCGA Palette */
1842 for(i=0;i<256*3;i++)
1843 pal[i]>>=2;
1844 r.x.ax=0x1012;
1845 r.x.bx=0;
1846 r.x.cx=256;
1847 r.x.dx=((long)pal)&0xFFFF;
1848 segreg.es=((long)pal)>>16;
1849 int86x(0x10,&r,&r,&segreg);
1850 }
1851 return 1;
1852 }
1853
1854
1855 #ifdef DEBUG_VIDCARD
1856 static int test_pattern(int y, int p)
1857 {
1858 if (p==1) return (1<<(y%8));
1859 return 0;
1860 }
1861 #endif
1862
1863 static int save_pmode; /* Debugging only */
1864
1865
1866
1867 #define PCX_BUFFSIZE 16000
1868 static uchar *fbuff;
1869 static int fb_index;
1870
1871 static int readbyte(FILE *f)
1872 {
1873 int n;
1874
1875 if (fb_index==PCX_BUFFSIZE) {
1876 n=fread(fbuff,1,PCX_BUFFSIZE,f);
1877 if (n==0) return EOF;
1878 if (n<PCX_BUFFSIZE) memset(fbuff+n,0,PCX_BUFFSIZE-n);
1879 fb_index=0;
1880 }
1881 return fbuff[fb_index++];
1882 }
1883
1884
1885
1886
1887 static void display_PCX(FILE *pcxfile, int gmode)
1888 /* pcxfile= the file to display.
1889 gmode= suggested video mode. */
1890 {
1891 int i,j, k;
1892 int pmode; /* PCX palette mode: 0=CGA hires 1=CGA, 2=EGA, 3=MCGA */
1893 uchar header[128];
1894 int xsize,ysize;
1895 uchar *linebuff; /* Up to four planes */
1896 int linesize; /* Number of bytes in a line */
1897 int y_ofs;
1898 int c;
1899
1900 /*-------------------------------------*/
1901 /* Read in header */
1902 /*-------------------------------------*/
1903
1904 if (fread(header,128,1,pcxfile)!=1) {
1905 writeln("GAME ERROR: Picture file corrupted.");
1906 return;
1907 }
1908
1909 /* Verify header */
1910 if (header[0]!=10 || header[2]!=1 ||
1911 (header[1]!=1 && header[1]!=2 && header[1]!=3 && header[1]!=5))
1912 {writeln("ERROR: Unrecognized PCX version.");return;}
1913
1914 /* Determine which palette type we're using */
1915
1916 if (header[3]==1 && header[65]==1) pmode=0; /* CGA 2-color */
1917 else if (header[3]==2 && header[65]==1) pmode=1; /* CGA 4-color */
1918 else if (header[3]==1 && header[65]==4) pmode=2; /* EGA/VGA 16-color*/
1919 else if (header[3]==8 && header[65]==1) pmode=3; /* MCGA 256-color */
1920 else {
1921 writeln("ERROR: Unsupported graphics mode.");
1922 return;
1923 }
1924
1925 xsize=ival(8)-ival(4)+1;
1926 ysize=ival(10)-ival(6)+1;
1927
1928 gmode=pick_gmode(gmode,pmode,xsize,ysize);
1929 if (gmode==-1) {pmode=-4; return;}
1930
1931 assert(pmode==paltype[gmode]); /* Temporary */
1932
1933 /* Clip if neccessary */
1934 if (xsize>mode_x[gmode]) xsize=mode_x[gmode];
1935 if (ysize>mode_y[gmode]) ysize=mode_y[gmode];
1936
1937 save_pmode=pmode;
1938
1939 if (!setup_card(pcxfile,header,gmode,pmode)) return;
1940
1941
1942 /*-------------------------------------*/
1943 /* Draw picture */
1944 /*-------------------------------------*/
1945
1946 fseek(pcxfile,128,SEEK_SET); /* Beginning of picture data */
1947
1948 linesize=ival(66);
1949 linebuff=rmalloc(linesize*header[65]);
1950
1951 y_ofs=(mode_y[gmode]-ysize)/2;
1952
1953 /* This will set up the scanline drawing routines */
1954 draw_scanline(NULL,gmode,y_ofs,linesize,xsize);
1955
1956 #ifdef DEBUG_VIDCARD
1957 for(i=0;i<ysize;i++) {
1958 memset(linebuff,0,linesize*header[65]);
1959 if (pmode==2) {
1960 int j;
1961 for(j=0;j<4;j++)
1962 memset(linebuff+j*linesize,
1963 test_pattern(i,j),linesize);
1964 } else if (pmode==3) {
1965 memset(linebuff,0,linesize);
1966 } else if (pmode==1) {
1967 memset(linebuff,0,linesize);
1968 } else memset(linebuff,0,linesize); /* Vertical lines */
1969
1970 draw_scanline(linebuff,gmode,y_ofs+i,linesize,xsize);
1971 }
1972 #endif
1973
1974 fbuff=rmalloc(PCX_BUFFSIZE); /* The input buffer */
1975 fb_index=PCX_BUFFSIZE;
1976
1977 /* Draw picture */
1978 for(i=0;i<ysize;i++) {
1979 memset(linebuff,0,linesize*header[65]);
1980 for(j=0;j<linesize*header[65];) {
1981 c=readbyte(pcxfile);
1982 if (c==EOF) {save_pmode=-1;return;}
1983 if ( (c&0xC0)==0xC0) {
1984 k=c&0x3F; /* Count=lower 6 bits */
1985 c=readbyte(pcxfile);
1986 assert(c!=EOF);
1987 if (c==EOF) {save_pmode=-2;return;}
1988 for(;k>0 && j<linesize*header[65];k--)
1989 linebuff[j++]=c;
1990 if (k>0) {save_pmode=-3;return;}
1991 } else
1992 linebuff[j++]=c;
1993 }
1994 draw_scanline(linebuff,gmode,y_ofs+i,linesize,xsize);
1995 }
1996 rfree(fbuff);
1997 rfree(linebuff);
1998 }
1999
2000
2001
2002
2003 void pictcmd(int cmd,int pict)
2004 /* 1=show global picture, name is pictlist[pict]
2005 2=show room picture, name is pixlist[pict]
2006 3=Show title picture.
2007 */
2008 {
2009 union REGS r;
2010 char *base;
2011 int gmode;
2012 FILE *pcxfile;
2013
2014 int savex,savey;
2015 char *save_vidmem;
2016
2017 if (graph_hardware==MDA) return; /* MDA is out */
2018
2019 if (cmd==1) base=pictlist[pict];
2020 else if (cmd==2) base=pixlist[pict];
2021 else if (cmd==3) base=gamefile_name;
2022 else return;
2023
2024 while(kbhit()) getch(); /* Discard any buffered keypresses */
2025
2026 /* Find graphics file; determine mode from extension... */
2027 for(gmode=start_mode[graph_hardware];gmode>=0;gmode--) {
2028 if (gmode==6 && (graph_hardware==EGA || graph_hardware==EGA64))
2029 continue; /* EGA should skip MCGA mode */
2030 if (gmode==5 && graph_hardware==MCGA) continue; /* ... and conversely */
2031 pcxfile=linopen(base,gfxext[gmode]);
2032 if (pcxfile!=NULL) break;
2033 }
2034 if (gmode==-1) {
2035 pcxfile=linopen(base,".pcx");
2036 if (pcxfile==NULL) return;
2037 /* Leave gmode as -1 */
2038 }
2039
2040 in_gmode=0;
2041 savex=wherex();
2042 savey=wherey();
2043 save_vidmem=rmalloc(2*25*80);
2044 memcpy(save_vidmem,(void*)0xB8000000L,2*25*80);
2045
2046 display_PCX(pcxfile,gmode);
2047
2048 fclose(pcxfile);
2049
2050 /*-------------------------------------*/
2051 /* Wait for key and restore text mode */
2052 /*-------------------------------------*/
2053
2054 if (in_gmode) {
2055
2056 if (!BATCH_MODE) getch(); /* Wait for a key-press */
2057
2058 r.x.ax=0x0003; /* Restore text mode */
2059 int86(0x10,&r,&r);
2060
2061 memcpy((void*)0xB8000000L,save_vidmem,2*25*80); /* Restore video mem */
2062 gotoxy(savex,savey); /* Restore cursor position */
2063 if (active_font!=NULL) /* Restore font, if neccessary */
2064 set_font(active_font,active_font_height);
2065 reset_state();
2066
2067 /* -- Need to restore old video state here -- */
2068 /* -- Primary font (16*256=4K) */
2069 /* -- Screen memory (2*25*80=4K) */
2070 /* -- Cursor position and current screen attributes */
2071
2072 }
2073 /* rprintf("Mode: %d\n",save_pmode); */
2074 rfree(save_vidmem);
2075 }
2076
2077
2078
2079 int musiccmd(int cmd,int song)
2080 /* For cmd=1 or 2, the name of the song is songlist[song]
2081 The other commands don't take an additional argument.
2082 1=play song
2083 2=repeat song
2084 3=end repeat
2085 4=end song
2086 5=suspend song
2087 6=resume song
2088 7=clean-up
2089 8=turn sound on
2090 9=turn sound off
2091 -1=Is a song playing? (0=false, -1=true)
2092 -2=Is the sound on? (0=false, -1=true)
2093 */
2094 {
2095 if (cmd==8) sound_on=1;
2096 else if (cmd==9) sound_on=0;
2097 if (cmd==-1) return sound_on;
2098 return 0;
2099 }
2100
2101 #endif /* REPLACE_BNW */