Got rid of all compiler warnings, for non-OpenGL on linux, anyway...
[btb/d2x.git] / main / titles.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 #include <conf.h>
15
16 #ifdef WINDOWS
17 #include "desw.h"
18 #endif
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23
24 #ifdef MACINTOSH
25 #include <Events.h>
26 #endif
27
28 #include "pa_enabl.h"                   //$$POLY_ACC
29 #include "pstypes.h"
30 #include "timer.h"
31 #include "key.h"
32 #include "gr.h"
33 #include "palette.h"
34 #include "iff.h"
35 #include "pcx.h"
36 #include "u_mem.h"
37 #include "joy.h"
38 #include "mono.h"
39 #include "gamefont.h"
40 #include "cfile.h"
41 #include "error.h"
42 #include "polyobj.h"
43 #include "textures.h"
44 #include "screens.h"
45 #include "multi.h"
46 #include "player.h"
47 #include "digi.h"
48 #include "compbit.h"
49 #include "text.h"
50 #include "kmatrix.h"
51 #include "piggy.h"
52 #include "songs.h"
53 #include "newmenu.h"
54 #include "state.h"
55 #include "movie.h"
56 #include "menu.h"
57
58 #if defined(POLY_ACC)
59 #include "poly_acc.h"
60 #endif
61
62 extern void RotateRobot();
63
64 void DoBriefingColorStuff ();
65 int get_new_message_num(char **message);
66 int DefineBriefingBox (char **buf);
67
68 extern unsigned RobSX,RobSY,RobDX,RobDY; // Robot movie coords
69
70 extern int MVEPaletteCalls;
71
72 ubyte New_pal[768];
73 int     New_pal_254_bash;
74
75 char CurBriefScreenName[15]="brief03.pcx";
76 char    * Briefing_text;
77 char RobotPlaying=0;
78
79 #define MAX_BRIEFING_COLORS     3
80
81 #define SHAREWARE_ENDING_FILENAME       "ending.tex"
82
83 //      Can be set by -noscreens command line option.  Causes bypassing of all briefing screens.
84 int     Skip_briefing_screens=0;
85 int     Briefing_foreground_colors[MAX_BRIEFING_COLORS], Briefing_background_colors[MAX_BRIEFING_COLORS];
86 int     Current_color = 0;
87 int     Erase_color;
88
89 extern int check_button_press();
90
91 #ifdef MACINTOSH
92 extern void macintosh_quit(void);
93 #endif
94
95 #ifndef MACINTOSH
96 int local_key_inkey(void)
97 {
98         int     rval;
99
100 #ifdef WINDOWS
101         MSG msg;
102         
103         DoMessageStuff(&msg);
104 #endif
105
106         rval = key_inkey();
107
108         if (rval == KEY_PRINT_SCREEN) {
109                 #ifdef POLY_ACC
110                 if (RobotPlaying) {
111                         gr_palette_read(gr_palette);
112                         gr_copy_palette(gr_palette,gr_palette,0);       //reset color lookup cache
113                 }
114                 #endif
115                 save_screen_shot(0);
116                 return 0;                               //say no key pressed
117         }
118
119         if (check_button_press())               //joystick or mouse button pressed?
120                 rval = KEY_SPACEBAR;
121
122         #ifdef MACINTOSH
123         if ( rval == KEY_Q+KEY_COMMAND )
124                 macintosh_quit();
125         #endif
126
127         return rval;
128 }
129 #else
130 int local_key_inkey(void)
131 {
132         EventRecord event;
133         int     rval;
134
135         if (!GetOSEvent(everyEvent, &event))
136                 return 0;
137
138         if (event.what != keyDown)
139                 return 0;
140                 
141         rval = (int)((event.message & keyCodeMask) >> 8);
142
143         if (rval == KEY_PRINT_SCREEN) {
144                 save_screen_shot(0);
145                 return 0;                               //say no key pressed
146         }
147
148         if (check_button_press())               //joystick or mouse button pressed?
149                 rval = KEY_SPACEBAR;
150
151         #ifdef MACINTOSH
152         if ( rval == KEY_Q+KEY_COMMAND )
153                 macintosh_quit();
154         #endif
155
156         return rval;
157 }
158 #endif
159
160 int show_title_screen( char * filename, int allow_keys, int from_hog_only )
161 {
162         fix timer;
163         int pcx_error;
164         grs_bitmap title_bm;
165         ubyte   palette_save[768];
166         char new_filename[FILENAME_LEN+1] = "";
167
168         #ifdef RELEASE
169         if (from_hog_only)
170                 strcpy(new_filename,"\x01");    //only read from hog file
171         #endif
172
173         strcat(new_filename,filename);
174         filename = new_filename;
175
176         title_bm.bm_data=NULL;
177         if ((pcx_error=pcx_read_bitmap( filename, &title_bm, BM_LINEAR, New_pal ))!=PCX_ERROR_NONE)     {
178                 printf( "File '%s', PCX load error: %s (%i)\n  (No big deal, just no title screen.)\n",filename, pcx_errormsg(pcx_error), pcx_error);
179                 mprintf((0, "File '%s', PCX load error: %s (%i)\n  (No big deal, just no title screen.)\n",filename, pcx_errormsg(pcx_error), pcx_error));
180                 Error( "Error loading briefing screen <%s>, PCX load error: %s (%i)\n",filename, pcx_errormsg(pcx_error), pcx_error);
181         }
182
183         memcpy(palette_save,gr_palette,sizeof(palette_save));
184
185 #if defined(POLY_ACC)
186     pa_save_clut();
187     pa_update_clut(New_pal, 0, 256, 0);
188 #endif
189
190         //vfx_set_palette_sub( New_pal );
191 #ifdef OGL
192         gr_palette_load( New_pal );
193 #else
194         gr_palette_clear();     
195 #endif
196
197         WINDOS( 
198                 dd_gr_set_current_canvas(NULL),
199                 gr_set_current_canvas( NULL )
200         );
201         WIN(DDGRLOCK(dd_grd_curcanv));
202                 gr_bitmap( 0, 0, &title_bm );
203         WIN(DDGRUNLOCK(dd_grd_curcanv));
204
205         WIN(DDGRRESTORE);
206
207 #if defined(POLY_ACC)
208     pa_restore_clut();
209 #endif
210
211         if (gr_palette_fade_in( New_pal, 32, allow_keys ))      
212                 return 1;
213         gr_copy_palette(gr_palette, New_pal, sizeof(gr_palette));
214
215         gr_palette_load( New_pal );
216         timer   = timer_get_fixed_seconds() + i2f(3);
217         while (1)       {
218                 if ( local_key_inkey() && allow_keys ) break;
219                 if ( timer_get_fixed_seconds() > timer ) break;
220         }                       
221         if (gr_palette_fade_out( New_pal, 32, allow_keys ))
222                 return 1;
223         gr_copy_palette(gr_palette, palette_save, sizeof(palette_save));
224         d_free(title_bm.bm_data);
225         return 0;
226 }
227
228 typedef struct {
229         char    bs_name[14];                                            //      filename, eg merc01.  Assumes .lbm suffix.
230         byte    level_num;
231         byte    message_num;
232         short   text_ulx, text_uly;                     //      upper left x,y of text window
233         short   text_width, text_height;        //      width and height of text window
234 } briefing_screen;
235
236 #define BRIEFING_SECRET_NUM     31                      //      This must correspond to the first secret level which must come at the end of the list.
237 #define BRIEFING_OFFSET_NUM     4                       // This must correspond to the first level screen (ie, past the bald guy briefing screens)
238
239 #define SHAREWARE_ENDING_LEVEL_NUM              0x7f
240 #define REGISTERED_ENDING_LEVEL_NUM     0x7e
241
242 #ifdef SHAREWARE
243 #define ENDING_LEVEL_NUM        SHAREWARE_ENDING_LEVEL_NUM
244 #else
245 #define ENDING_LEVEL_NUM        REGISTERED_ENDING_LEVEL_NUM
246 #endif
247
248 #define MAX_BRIEFING_SCREENS 60
249
250 briefing_screen Briefing_screens[MAX_BRIEFING_SCREENS]=
251  {{"brief03.pcx",0,3,8,8,257,177}}; // default=0!!!
252
253 int     Briefing_text_x, Briefing_text_y;
254
255 void init_char_pos(int x, int y)
256 {
257         Briefing_text_x = x;
258         Briefing_text_y = y;
259    mprintf ((0,"Setting init x=%d y=%d\n",x,y));
260 }
261
262 grs_canvas      *Robot_canv = NULL;
263 vms_angvec      Robot_angles;
264
265 char    Bitmap_name[32] = "";
266 #define EXIT_DOOR_MAX   14
267 #define OTHER_THING_MAX 10              //      Adam: This is the number of frames in your new animating thing.
268 #define DOOR_DIV_INIT   6
269 byte    Door_dir=1, Door_div_count=0, Animating_bitmap_type=0;
270
271 //      -----------------------------------------------------------------------------
272 void show_bitmap_frame(void)
273 {
274 #ifdef WINDOWS
275         dd_grs_canvas *curcanv_save, *bitmap_canv=0;
276 #else
277         grs_canvas      *curcanv_save, *bitmap_canv=0;
278 #endif
279
280         grs_bitmap      *bitmap_ptr;
281
282         //      Only plot every nth frame.
283         if (Door_div_count) {
284                 Door_div_count--;
285                 return;
286         }
287
288         Door_div_count = DOOR_DIV_INIT;
289
290         if (Bitmap_name[0] != 0) {
291                 char            *pound_signp;
292                 int             num, dig1, dig2;
293
294                 //      Set supertransparency color to black
295                 if (!New_pal_254_bash) {
296                         New_pal_254_bash = 1;
297                         New_pal[254*3] = 0;
298                         New_pal[254*3+1] = 0;
299                         New_pal[254*3+2] = 0;
300                         gr_palette_load( New_pal );
301                 }
302
303                 switch (Animating_bitmap_type) {
304                         case 0:
305                         WINDOS( 
306                                 bitmap_canv = dd_gr_create_sub_canvas(dd_grd_curcanv, 220, 45, 64, 64); break,
307                                 bitmap_canv = gr_create_sub_canvas(grd_curcanv, 220, 45, 64, 64);       break
308                         );
309                         case 1: 
310                         WINDOS(
311                                 bitmap_canv = dd_gr_create_sub_canvas(dd_grd_curcanv, 220, 45, 94, 94); break,
312                                 bitmap_canv = gr_create_sub_canvas(grd_curcanv, 220, 45, 94, 94);       break
313                         );      
314                         
315                         //      Adam: Change here for your new animating bitmap thing. 94, 94 are bitmap size.
316                         default:        Int3(); //      Impossible, illegal value for Animating_bitmap_type
317                 }
318
319                 WINDOS(
320                         curcanv_save = dd_grd_curcanv; dd_grd_curcanv = bitmap_canv,
321                         curcanv_save = grd_curcanv; grd_curcanv = bitmap_canv
322                 );
323
324                 pound_signp = strchr(Bitmap_name, '#');
325                 Assert(pound_signp != NULL);
326
327                 dig1 = *(pound_signp+1);
328                 dig2 = *(pound_signp+2);
329                 if (dig2 == 0)
330                         num = dig1-'0';
331                 else
332                         num = (dig1-'0')*10 + (dig2-'0');
333
334                 switch (Animating_bitmap_type) {
335                         case 0:
336                                 num += Door_dir;
337                                 if (num > EXIT_DOOR_MAX) {
338                                         num = EXIT_DOOR_MAX;
339                                         Door_dir = -1;
340                                 } else if (num < 0) {
341                                         num = 0;
342                                         Door_dir = 1;
343                                 }
344                                 break;
345                         case 1:
346                                 num++;
347                                 if (num > OTHER_THING_MAX)
348                                         num = 0;
349                                 break;
350                 }
351
352                 Assert(num < 100);
353                 if (num >= 10) {
354                         *(pound_signp+1) = (num / 10) + '0';
355                         *(pound_signp+2) = (num % 10) + '0';
356                         *(pound_signp+3) = 0;
357                 } else {
358                         *(pound_signp+1) = (num % 10) + '0';
359                         *(pound_signp+2) = 0;
360                 }
361
362                 {
363                         bitmap_index bi;
364                         bi = piggy_find_bitmap(Bitmap_name);
365                         bitmap_ptr = &GameBitmaps[bi.index];
366                         PIGGY_PAGE_IN( bi );
367                 }
368
369                 WIN(DDGRLOCK(dd_grd_curcanv));
370                         gr_bitmapm(0, 0, bitmap_ptr);
371                 WIN(DDGRUNLOCK(dd_grd_curcanv));
372
373                 WINDOS(
374                         dd_grd_curcanv = curcanv_save,
375                         grd_curcanv = curcanv_save
376                 );
377                 d_free(bitmap_canv);
378
379                 switch (Animating_bitmap_type) {
380                         case 0:
381                                 if (num == EXIT_DOOR_MAX) {
382                                         Door_dir = -1;
383                                         Door_div_count = 64;
384                                 } else if (num == 0) {
385                                         Door_dir = 1;
386                                         Door_div_count = 64;
387                                 }
388                                 break;
389                         case 1:
390                                 break;
391                 }
392         }
393
394 }
395
396 //      -----------------------------------------------------------------------------
397 void show_briefing_bitmap(grs_bitmap *bmp)
398 {
399 #ifdef WINDOWS
400         dd_grs_canvas *bitmap_canv, *curcanv_save;
401   
402         bitmap_canv = dd_gr_create_sub_canvas(dd_grd_curcanv, 220, 45, bmp->bm_w, bmp->bm_h); 
403         curcanv_save = dd_grd_curcanv;
404         dd_gr_set_current_canvas(bitmap_canv);
405         DDGRLOCK(dd_grd_curcanv);
406         gr_bitmapm(0,0,bmp);
407         DDGRUNLOCK(dd_grd_curcanv);
408         dd_gr_set_current_canvas(curcanv_save);
409 #else
410         grs_canvas      *curcanv_save, *bitmap_canv;
411
412         bitmap_canv = gr_create_sub_canvas(grd_curcanv, 220, 45, bmp->bm_w, bmp->bm_h);
413         curcanv_save = grd_curcanv;
414         gr_set_current_canvas(bitmap_canv);
415         gr_bitmapm(0, 0, bmp);
416         gr_set_current_canvas(curcanv_save);
417 #endif
418
419         d_free(bitmap_canv);
420 }
421
422 #ifndef WINDOWS
423 //      -----------------------------------------------------------------------------
424 void show_spinning_robot_frame(int robot_num)
425 {
426         grs_canvas      *curcanv_save;
427
428         if (robot_num != -1) {
429                 Robot_angles.h += 150;
430
431                 curcanv_save = grd_curcanv;
432                 grd_curcanv = Robot_canv;
433                 Assert(Robot_info[robot_num].model_num != -1);
434                 draw_model_picture(Robot_info[robot_num].model_num, &Robot_angles);
435                 grd_curcanv = curcanv_save;
436         }
437
438 }
439
440 //  -----------------------------------------------------------------------------
441 void init_spinning_robot(int x,int y,int w,int h)
442  {
443         Robot_angles.p += 0;
444         Robot_angles.b += 0;
445         Robot_angles.h += 0;
446
447         Robot_canv = gr_create_sub_canvas(grd_curcanv, x, y, w, h);
448         // 138, 55, 166, 138
449  }
450 #endif
451
452 //      -----------------------------------------------------------------------------
453 //      Returns char width.
454 //      If show_robot_flag set, then show a frame of the spinning robot.
455 int show_char_delay(char the_char, int delay, int robot_num, int cursor_flag)
456 {
457         int     w, h, aw;
458         char    message[2];
459         static fix      start_time=0;
460
461    robot_num=0;
462         message[0] = the_char;
463         message[1] = 0;
464
465    if (start_time==0 && timer_get_fixed_seconds()<0)
466     start_time=timer_get_fixed_seconds(); 
467  
468         gr_get_string_size(message, &w, &h, &aw );
469
470         Assert((Current_color >= 0) && (Current_color < MAX_BRIEFING_COLORS));
471
472         //      Draw cursor if there is some delay and caller says to draw cursor
473         if (cursor_flag && delay) {
474         WIN(DDGRLOCK(dd_grd_curcanv));
475                 gr_set_fontcolor(Briefing_foreground_colors[Current_color], -1);
476                 gr_printf(Briefing_text_x+1, Briefing_text_y, "_" );
477         WIN(DDGRUNLOCK(dd_grd_curcanv));
478         }
479
480                 if (delay)
481                  delay=fixdiv (F1_0,i2f(15));
482
483                 if (delay != 0)
484                         show_bitmap_frame();
485
486       if (RobotPlaying && (delay != 0))
487          RotateRobot();
488       
489       
490                 while (timer_get_fixed_seconds() < (start_time + delay))
491                  {
492         if (RobotPlaying && delay != 0)
493          RotateRobot();
494                  }      
495
496                 start_time = timer_get_fixed_seconds();
497
498 WIN(DDGRLOCK(dd_grd_curcanv));
499         //      Erase cursor
500         if (cursor_flag && delay) {
501                 gr_set_fontcolor(Erase_color, -1);
502                 gr_printf(Briefing_text_x+1, Briefing_text_y, "_" );
503         }
504
505         //      Draw the character
506         gr_set_fontcolor(Briefing_background_colors[Current_color], -1);
507         gr_printf(Briefing_text_x, Briefing_text_y, message );
508
509         gr_set_fontcolor(Briefing_foreground_colors[Current_color], -1);
510         gr_printf(Briefing_text_x+1, Briefing_text_y, message );
511 WIN(DDGRUNLOCK(dd_grd_curcanv));
512
513 //      if (the_char != ' ')
514 //              if (!digi_is_sound_playing(SOUND_MARKER_HIT))
515 //                      digi_play_sample( SOUND_MARKER_HIT, F1_0 );
516
517         return w;
518 }
519
520 //      -----------------------------------------------------------------------------
521 int load_briefing_screen( int screen_num )
522 {
523         int     pcx_error;
524
525         WIN(DDGRLOCK(dd_grd_curcanv));
526       if ((pcx_error=pcx_read_bitmap( CurBriefScreenName, &grd_curcanv->cv_bitmap, grd_curcanv->cv_bitmap.bm_type, New_pal ))!=PCX_ERROR_NONE)     {
527                 printf( "File '%s', PCX load error: %s\n  (It's a briefing screen.  Does this cause you pain?)\n",Briefing_screens[screen_num].bs_name, pcx_errormsg(pcx_error));
528                 printf( "File '%s', PCX load error: %s (%i)\n  (It's a briefing screen.  Does this cause you pain?)\n",Briefing_screens[screen_num].bs_name, pcx_errormsg(pcx_error), pcx_error);
529                 WIN(DDGRUNLOCK(dd_grd_curcanv));
530                 Error( "Error loading briefing screen <%s>, PCX load error: %s (%i)\n",CurBriefScreenName, pcx_errormsg(pcx_error), pcx_error);
531         }
532         WIN(DDGRUNLOCK(dd_grd_curcanv));
533
534         WIN(DDGRRESTORE);
535
536         return 0;
537 }
538
539 int load_new_briefing_screen( char *fname )
540 {
541         int     pcx_error;
542
543    mprintf ((0,"Loading new briefing %s!\n",fname));
544    strcpy (CurBriefScreenName,fname);
545
546 // WIN(DEFINE_SCREEN(CurBriefScreenName));
547
548         if (gr_palette_fade_out( New_pal, 32, 0 ))      
549                 return 0;
550
551 WIN(DDGRLOCK(dd_grd_curcanv));
552    if ((pcx_error=pcx_read_bitmap( fname, &grd_curcanv->cv_bitmap, grd_curcanv->cv_bitmap.bm_type, New_pal ))!=PCX_ERROR_NONE)     {
553                 printf( "File '%s', PCX load error: %s (%i)\n  (It's a briefing screen.  Does this cause you pain?)\n",fname, pcx_errormsg(pcx_error), pcx_error);
554                         WIN(DDGRUNLOCK(dd_grd_curcanv));
555                 Error( "Error loading briefing screen <%s>, PCX load error: %s (%i)\n",fname, pcx_errormsg(pcx_error), pcx_error);
556         }
557 WIN(DDGRUNLOCK(dd_grd_curcanv));
558
559 WIN(DDGRRESTORE);
560
561         gr_copy_palette(gr_palette, New_pal, sizeof(gr_palette));
562
563         if (gr_palette_fade_in( New_pal, 32, 0 ))       
564                 return 0;
565    DoBriefingColorStuff();
566
567         return 1;
568 }
569
570
571
572 #define KEY_DELAY_DEFAULT       ((F1_0*20)/1000)
573
574 //      -----------------------------------------------------------------------------
575 int get_message_num(char **message)
576 {
577         int     num=0;
578
579         while (**message == ' ')
580                 (*message)++;
581
582         while ((**message >= '0') && (**message <= '9')) {
583                 num = 10*num + **message-'0';
584                 (*message)++;
585         }
586
587         while (*(*message)++ != 10)             //      Get and drop eoln
588                 ;
589
590         return num;
591 }
592
593 //      -----------------------------------------------------------------------------
594 void get_message_name(char **message, char *result)
595 {
596         while (**message == ' ')
597                 (*message)++;
598
599         while ((**message != ' ') && (**message != 10)) {
600                 if (**message != '\n')
601                         *result++ = **message;
602                 (*message)++;
603         }
604
605         if (**message != 10)
606                 while (*(*message)++ != 10)             //      Get and drop eoln
607                         ;
608
609         *result = 0;
610 }
611
612 //      -----------------------------------------------------------------------------
613 void flash_cursor(int cursor_flag)
614 {
615         if (cursor_flag == 0)
616                 return;
617
618 WIN(DDGRLOCK(dd_grd_curcanv));
619         if ((timer_get_fixed_seconds() % (F1_0/2) ) > (F1_0/4))
620                 gr_set_fontcolor(Briefing_foreground_colors[Current_color], -1);
621         else
622                 gr_set_fontcolor(Erase_color, -1);
623
624         gr_printf(Briefing_text_x+1, Briefing_text_y, "_" );
625 WIN(DDGRUNLOCK(dd_grd_curcanv));
626 }
627
628 extern int InitMovieBriefing();
629
630 //      -----------------------------------------------------------------------------
631 //      Return true if message got aborted by user (pressed ESC), else return false.
632 int show_briefing_message(int screen_num, char *message)
633 {
634         int     prev_ch=-1;
635         int     ch, done=0,i;
636         briefing_screen *bsp = &Briefing_screens[screen_num];
637         int     delay_count = KEY_DELAY_DEFAULT;
638         int     key_check;
639         int     robot_num=-1;
640         int     rval=0;
641         static int tab_stop=0;
642         int     flashing_cursor=0;
643         int     new_page=0,GotZ=0;
644         char *spinRobotName="rba.mve",kludge;  // matt don't change this!  
645         char fname[15];
646    char DumbAdjust=0;
647         char chattering=0;
648         int hum_channel=-1,printing_channel=-1;
649         int LineAdjustment=0;   
650         WIN(int wpage_done=0);
651
652         Bitmap_name[0] = 0;
653    Current_color = 0;
654    RobotPlaying=0;
655
656         InitMovieBriefing();
657
658         #ifndef SHAREWARE
659         hum_channel  = digi_start_sound( digi_xlat_sound(SOUND_BRIEFING_HUM), F1_0/2, 0xFFFF/2, 1, -1, -1, -1 );
660         #endif
661  
662         // mprintf((0, "Going to print message [%s] at x=%i, y=%i\n", message, x, y));
663         gr_set_curfont( GAME_FONT );
664
665    bsp=&Briefing_screens[0];
666    init_char_pos(bsp->text_ulx, bsp->text_uly-(8*(1+MenuHires)));
667
668         while (!done) {
669                 ch = *message++;
670                 if (ch == '$') {
671                         ch = *message++;
672               if (ch=='D')
673                {
674                       screen_num=DefineBriefingBox (&message);
675                         //load_new_briefing_screen (Briefing_screens[screen_num].bs_name);
676                                 
677            bsp = &Briefing_screens[screen_num];
678             init_char_pos(bsp->text_ulx, bsp->text_uly);
679                                 LineAdjustment=0;
680             prev_ch = 10;                                     //      read to eoln
681           }
682          else if (ch=='U')
683           {
684                 screen_num=get_message_num(&message);
685            bsp = &Briefing_screens[screen_num];
686            init_char_pos(bsp->text_ulx, bsp->text_uly);
687            prev_ch = 10;                                                   //      read to eoln
688           }
689                        
690          else if (ch == 'C') {
691                                 Current_color = get_message_num(&message)-1;
692                                 Assert((Current_color >= 0) && (Current_color < MAX_BRIEFING_COLORS));
693                                 prev_ch = 10;
694                         } else if (ch == 'F') {         //      toggle flashing cursor
695                                 flashing_cursor = !flashing_cursor;
696                                 prev_ch = 10;
697                                 while (*message++ != 10)
698                                         ;
699                         } else if (ch == 'T') {
700                                 tab_stop = get_message_num(&message);
701                                 tab_stop*=(1+MenuHires);
702                                 prev_ch = 10;                                                   //      read to eoln
703                         } else if (ch == 'R') {
704                                 if (Robot_canv != NULL)
705                                 {
706                                         d_free(Robot_canv); 
707                                         Robot_canv=NULL;
708                                 }
709                            if (RobotPlaying)
710                                  {
711                                   DeInitRobotMovie();
712                                   RobotPlaying=0;
713                                  }
714                                 
715                 kludge=*message++;
716             spinRobotName[2]=kludge; // ugly but proud
717
718             RobotPlaying=InitRobotMovie(spinRobotName);
719
720                         // gr_remap_bitmap_good( &grd_curcanv->cv_bitmap, pal, -1, -1 );
721
722                            if (RobotPlaying)
723                                 {                       
724               DoBriefingColorStuff ();
725                         mprintf ((0,"Robot playing is %d!!!",RobotPlaying));
726                                 }
727
728             prev_ch = 10;                                                       //      read to eoln
729                         } else if (ch == 'N') {
730                                 //--grs_bitmap  *bitmap_ptr;
731                                 if (Robot_canv != NULL)
732                                         {d_free(Robot_canv); Robot_canv=NULL;}
733
734                                 get_message_name(&message, Bitmap_name);
735                                 strcat(Bitmap_name, "#0");
736                                 Animating_bitmap_type = 0;
737                                 prev_ch = 10;
738                         } else if (ch == 'O') {
739                                 if (Robot_canv != NULL)
740                                         {d_free(Robot_canv); Robot_canv=NULL;}
741
742                                 get_message_name(&message, Bitmap_name);
743                                 strcat(Bitmap_name, "#0");
744                                 Animating_bitmap_type = 1;
745                                 prev_ch = 10;
746                         } else if (ch=='A') {
747                                 LineAdjustment=1-LineAdjustment;
748                         } else if (ch=='Z') {
749                                 mprintf ((0,"Got a Z!\n"));
750                                 GotZ=1;
751                                 #if defined (D2_OEM) || defined(COMPILATION) || (defined(MACINTOSH) && defined(SHAREWARE))
752                                         DumbAdjust=1;
753                                 #else
754                                         if (LineAdjustment==1)
755                                                 DumbAdjust=1;
756                                         else
757                                                 DumbAdjust=2;
758                                 #endif
759         
760                                 i=0;
761                                 while ((fname[i]=*message) != '\n') {
762                                         i++;
763                                         message++;
764                                 }
765                                 fname[i]=0;
766   
767                                 if (MenuHires)
768                                  {
769                                    i=0; 
770                                    while (fname[i]!='.')
771                                         i++;
772                                    fname[i]='b';                        
773                                    fname[i+1]='.';      
774                                    fname[i+2]='p';      
775                                    fname[i+3]='c';      
776                                    fname[i+4]='x';      
777                                    fname[i+5]=0;        
778
779                                    load_new_briefing_screen (fname);
780                                  }
781                                 else
782                                    load_new_briefing_screen (fname);
783
784                                 //load_new_briefing_screen (MenuHires?"end01b.pcx":"end01.pcx");        
785
786                         } else if (ch == 'B') {
787                                 char                    bitmap_name[32];
788                                 grs_bitmap      guy_bitmap;
789                                 ubyte                   temp_palette[768];
790                                 int                     iff_error;
791
792                                 if (Robot_canv != NULL)
793                                         {d_free(Robot_canv); Robot_canv=NULL;}
794
795                                 get_message_name(&message, bitmap_name);
796                                 strcat(bitmap_name, ".bbm");
797                                 guy_bitmap.bm_data = NULL;
798                                 iff_error = iff_read_bitmap(bitmap_name, &guy_bitmap, BM_LINEAR, temp_palette);
799                                 Assert(iff_error == IFF_NO_ERROR);
800                                 gr_remap_bitmap_good( &guy_bitmap, temp_palette, -1, -1 );
801
802                                 show_briefing_bitmap(&guy_bitmap);
803                                 d_free(guy_bitmap.bm_data);
804                                 prev_ch = 10;
805 //                 } else if (ch==EOF) {
806 //                              done=1;
807 //                      } else if (ch == 'B') {
808 //                              if (Robot_canv != NULL)
809 //                                      {d_free(Robot_canv); Robot_canv=NULL;}
810 //
811 //                              bitmap_num = get_message_num(&message);
812 //                              if (bitmap_num != -1)
813 //                                      show_briefing_bitmap(Textures[bitmap_num]);
814 //                              prev_ch = 10;                                                   //      read to eoln
815                         } else if (ch == 'S') {
816                                 int     keypress;
817                                 fix     start_time;
818                         
819                                 chattering=0;
820                                 if (printing_channel>-1)
821                                    digi_stop_sound( printing_channel );
822                                 printing_channel=-1;
823
824                         #ifdef WINDOWS
825                                 if (!wpage_done) {
826                                         DDGRRESTORE;
827                                         wpage_done =1;
828                                 }
829                         #endif
830
831                                 start_time = timer_get_fixed_seconds();
832                                 while ( (keypress = local_key_inkey()) == 0 ) {         //      Wait for a key
833                                 #ifdef WINDOWS
834                                         if (_RedrawScreen) {
835                                                 _RedrawScreen = FALSE;
836                                                 hum_channel  = digi_start_sound( digi_xlat_sound(SOUND_BRIEFING_HUM), F1_0/2, 0xFFFF/2, 1, -1, -1, -1 );
837                                                 keypress = KEY_ESC;
838                                                 break;
839                                         }
840                                 #endif
841
842                                         while (timer_get_fixed_seconds() < start_time + KEY_DELAY_DEFAULT/2)
843                                                 ;
844                                         flash_cursor(flashing_cursor);
845
846                if (RobotPlaying)        RotateRobot ();
847
848                                         show_bitmap_frame();
849                                         start_time += KEY_DELAY_DEFAULT/2;
850                                 }
851
852 #ifndef NDEBUG
853                                 if (keypress == KEY_BACKSP)
854                                         Int3();
855 #endif
856                                 if (keypress == KEY_ESC)
857                                         rval = 1;
858
859                                 flashing_cursor = 0;
860                                 done = 1;
861                                 WIN(wpage_done = 0);
862                         } else if (ch == 'P') {         //      New page.
863                                 if (!GotZ)
864                                  {
865                                          Int3(); // Hey ryan!!!! You gotta load a screen before you start 
866                                                                 // printing to it! You know, $Z !!!
867                                     load_new_briefing_screen (MenuHires?"end01b.pcx":"end01.pcx");
868                                  }
869                                                                         
870                 
871                                 new_page = 1;
872                                 while (*message != 10) {
873                                         message++;      //      drop carriage return after special escape sequence
874                                 }
875                                 message++;
876                                 prev_ch = 10;
877                         }
878                 } else if (ch == '\t') {                //      Tab
879                         if (Briefing_text_x - bsp->text_ulx < tab_stop)
880                                 Briefing_text_x = bsp->text_ulx + tab_stop;
881                 } else if ((ch == ';') && (prev_ch == 10)) {
882                         while (*message++ != 10)
883                                 ;
884                         prev_ch = 10;
885                 } else if (ch == '\\') {
886                         prev_ch = ch;
887                 } else if (ch == 10) {
888                         if (prev_ch != '\\') {
889                                 prev_ch = ch;
890                                 if (DumbAdjust==0)
891                                         Briefing_text_y += (8*(MenuHires+1));
892                                 else
893                     DumbAdjust--;
894                                 Briefing_text_x = bsp->text_ulx;
895                                 if (Briefing_text_y > bsp->text_uly + bsp->text_height) {
896                                         load_briefing_screen(screen_num);
897                                         Briefing_text_x = bsp->text_ulx;
898                                         Briefing_text_y = bsp->text_uly;
899                                 }
900                         } else {
901                                 if (ch == 13)           //Can this happen? Above says ch==10
902                                         Int3();
903                                 prev_ch = ch;
904                         }
905
906                 } else {
907                         if (!GotZ)
908                          {
909                                  Int3(); // Hey ryan!!!! You gotta load a screen before you start 
910                                                         // printing to it! You know, $Z !!!
911                                  load_new_briefing_screen (MenuHires?"end01b.pcx":"end01.pcx");
912                          }
913                                  
914                         prev_ch = ch;
915
916          if (!chattering)
917                          {
918                                 printing_channel  = digi_start_sound( digi_xlat_sound(SOUND_BRIEFING_PRINTING), F1_0, 0xFFFF/2, 1, -1, -1, -1 );
919                            chattering=1;
920                          }
921                 
922                         WIN(if (GRMODEINFO(emul)) delay_count = 0);
923
924                         Briefing_text_x += show_char_delay(ch, delay_count, robot_num, flashing_cursor);
925
926                 }
927
928                 //      Check for Esc -> abort.
929                 key_check = local_key_inkey();
930
931         #ifdef WINDOWS
932                 if (_RedrawScreen) {
933                         _RedrawScreen = FALSE;
934                         hum_channel  = digi_start_sound( digi_xlat_sound(SOUND_BRIEFING_HUM), F1_0/2, 0xFFFF/2, 1, -1, -1, -1 );
935                         key_check = KEY_ESC;
936                 }
937         #endif
938                 if ( key_check == KEY_ESC ) {
939                         rval = 1;
940                         done = 1;
941                 }
942
943                 if ((key_check == KEY_SPACEBAR) || (key_check == KEY_ENTER))
944                         delay_count = 0;
945
946                 if (Briefing_text_x > bsp->text_ulx + bsp->text_width) {
947                         Briefing_text_x = bsp->text_ulx;
948                         Briefing_text_y += bsp->text_uly;
949                 }
950
951                 if ((new_page) || (Briefing_text_y > bsp->text_uly + bsp->text_height)) {
952                         fix     start_time = 0;
953                         int     keypress;
954
955                         new_page = 0;
956
957                         if (printing_channel>-1)
958                            digi_stop_sound( printing_channel );
959                         printing_channel=-1;
960                         
961                         chattering=0;
962
963                         #ifdef WINDOWS
964                                 if (!wpage_done) {
965                                         DDGRRESTORE;
966                                         wpage_done =1;
967                                 }
968                         #endif
969
970                         start_time = timer_get_fixed_seconds();
971                         while ( (keypress = local_key_inkey()) == 0 ) {         //      Wait for a key
972                         #ifdef WINDOWS
973                                 if (_RedrawScreen) {
974                                         _RedrawScreen = FALSE;
975                                         hum_channel  = digi_start_sound( digi_xlat_sound(SOUND_BRIEFING_HUM), F1_0/2, 0xFFFF/2, 1, -1, -1, -1 );
976                                         keypress = KEY_ESC;
977                                         break;
978                                 }
979                         #endif
980
981                                 while (timer_get_fixed_seconds() < start_time + KEY_DELAY_DEFAULT/2)
982                                         ;
983                                 flash_cursor(flashing_cursor);
984          
985                                 if (RobotPlaying) RotateRobot();
986                         
987                                 show_bitmap_frame();
988                                 start_time += KEY_DELAY_DEFAULT/2;
989                         }
990
991                         if (RobotPlaying) DeInitRobotMovie();
992          RobotPlaying=0;
993                         robot_num = -1;
994
995 #ifndef NDEBUG
996                         if (keypress == KEY_BACKSP)
997                                 Int3();
998 #endif
999                         if (keypress == KEY_ESC) {
1000                                 rval = 1;
1001                                 done = 1;
1002                         }
1003
1004                         load_briefing_screen(screen_num);
1005                         Briefing_text_x = bsp->text_ulx;
1006                         Briefing_text_y = bsp->text_uly;
1007          delay_count = KEY_DELAY_DEFAULT;
1008
1009                         WIN(wpage_done = 0);
1010                 }
1011         }
1012
1013
1014         if (RobotPlaying)
1015    {
1016         DeInitRobotMovie();
1017       RobotPlaying=0;
1018    }
1019
1020         if (Robot_canv != NULL)
1021                 {d_free(Robot_canv); Robot_canv=NULL;}
1022
1023         if (hum_channel>-1)
1024                 digi_stop_sound( hum_channel );
1025         if (printing_channel>-1)
1026            digi_stop_sound( printing_channel );
1027
1028         return rval;
1029 }
1030
1031 //      -----------------------------------------------------------------------------
1032 //      Return a pointer to the start of text for screen #screen_num.
1033 char * get_briefing_message(int screen_num)
1034 {
1035         char    *tptr = Briefing_text;
1036         int     cur_screen=0;
1037         int     ch;
1038
1039         Assert(screen_num >= 0);
1040
1041         while ( (*tptr != 0 ) && (screen_num != cur_screen)) {
1042                 ch = *tptr++;
1043                 if (ch == '$') {
1044                         ch = *tptr++;
1045                         if (ch == 'S')
1046                                 cur_screen = get_message_num(&tptr);
1047                 }
1048         }
1049
1050    if (screen_num!=cur_screen)
1051          return (NULL);
1052   
1053         return tptr;
1054 }
1055
1056 // -----------------------------------------------------------------------------
1057 //      Load Descent briefing text.
1058 int load_screen_text(char *filename, char **buf)
1059 {
1060         CFILE   *tfile;
1061         CFILE *ifile;
1062         int     len, i,x;
1063         int     have_binary = 0;
1064
1065         if ((tfile = cfopen(filename,"rb")) == NULL) {
1066                 char nfilename[30], *ptr;
1067
1068                 strcpy(nfilename, filename);
1069                 ptr = strrchr(nfilename, '.');
1070                 *ptr = '\0';
1071                 strcat(nfilename, ".txb");
1072                 if ((ifile = cfopen(nfilename, "rb")) == NULL)
1073                 { 
1074                         mprintf ((0,"can't open %s!\n",nfilename));
1075                         return (0); 
1076                         //Error("Cannot open file %s or %s", filename, nfilename); 
1077                 }       
1078    
1079                 mprintf ((0,"reading...\n"));
1080                 have_binary = 1;
1081
1082                 len = cfilelength(ifile);
1083                 MALLOC(*buf, char, len+500); 
1084       mprintf ((0,"len=%d\n",len));
1085                 for (x=0,i=0;i<len;i++,x++)
1086                  {
1087              cfread (*buf+x,1,1,ifile);
1088                 //  mprintf ((0,"%c",*(*buf+x)));
1089                   if (*(*buf+x)==13)
1090                         x--;
1091        }
1092       
1093                 cfclose(ifile);
1094         } else {
1095                 len = cfilelength(tfile);
1096                 MALLOC(*buf, char, len+500); 
1097                 for (x=0,i=0;i<len;i++,x++)
1098                  {
1099              cfread (*buf+x,1,1,tfile);
1100                  // mprintf ((0,"%c",*(*buf+x)));
1101                   if (*(*buf+x)==13)
1102                         x--;
1103        }
1104           
1105       
1106                 //cfread(*buf, 1, len, tfile);
1107                 cfclose(tfile);
1108         }
1109
1110         if (have_binary) {
1111                 char *ptr;
1112
1113                 for (i = 0, ptr = *buf; i < len; i++, ptr++) {
1114                         if (*ptr != '\n') {
1115                                 encode_rotate_left(ptr);
1116                                 *ptr = *ptr ^ BITMAP_TBL_XOR;
1117                                 encode_rotate_left(ptr);
1118                         }
1119                 }
1120         }
1121
1122  return (1);
1123 }
1124
1125 //      -----------------------------------------------------------------------------
1126 //      Return true if message got aborted, else return false.
1127 int show_briefing_text(int screen_num)
1128 {
1129         char    *message_ptr;
1130
1131    message_ptr = get_briefing_message(screen_num);
1132    if (message_ptr==NULL)
1133          return (0);
1134     
1135    DoBriefingColorStuff();
1136
1137         return show_briefing_message(screen_num, message_ptr);
1138 }
1139 void DoBriefingColorStuff ()
1140  {
1141    Briefing_foreground_colors[0] = gr_find_closest_color_current( 0, 40, 0);
1142    Briefing_background_colors[0] = gr_find_closest_color_current( 0, 6, 0);
1143
1144    Briefing_foreground_colors[1] = gr_find_closest_color_current( 40, 33, 35);
1145    Briefing_background_colors[1] = gr_find_closest_color_current( 5, 5, 5);
1146
1147    Briefing_foreground_colors[2] = gr_find_closest_color_current( 8, 31, 54);
1148    Briefing_background_colors[2] = gr_find_closest_color_current( 1, 4, 7);
1149
1150    Erase_color = gr_find_closest_color_current(0, 0, 0);
1151  } 
1152
1153 //      -----------------------------------------------------------------------------
1154 //      Return true if screen got aborted by user, else return false.
1155 int show_briefing_screen( int screen_num, int allow_keys)
1156 {
1157         int     rval=0;
1158         ubyte   palette_save[768];
1159
1160         New_pal_254_bash = 0;
1161
1162         if (Skip_briefing_screens) {
1163         mprintf((0, "Skipping briefing screen [brief03.pcx]\n"));
1164                 return 0;
1165         }
1166
1167 //      briefing_bm.bm_data=NULL;       
1168 //   if ((pcx_error=pcx_read_bitmap( "brief03.pcx", &briefing_bm, BM_LINEAR, New_pal ))!=PCX_ERROR_NONE)     {
1169 //        mprintf((0, "File '%s', PCX load error: %s (%i)\n  (It's a briefing screen.  Does this cause you pain?)\n","Brief03.pcx", pcx_errormsg(pcx_error), pcx_error));
1170 //              Int3();
1171 //              return 0;
1172 //      }
1173
1174         memcpy(palette_save,gr_palette,sizeof(palette_save));
1175         memcpy(New_pal,gr_palette,sizeof(gr_palette));
1176    
1177
1178 //      vfx_set_palette_sub( New_pal );
1179 //      gr_palette_clear();
1180 //      gr_bitmap( 0, 0, &briefing_bm );
1181 #ifdef OGL
1182         gr_palette_load(New_pal);
1183 #endif
1184
1185 //      if (gr_palette_fade_in( New_pal, 32, allow_keys ))      
1186 //              return 1;
1187 //      memcpy(gr_palette,New_pal,sizeof(gr_palette));
1188
1189         #ifdef MACINTOSH
1190         key_close();            // kill the keyboard handler during briefing screens for movies
1191         #endif
1192         rval = show_briefing_text(screen_num);
1193         #ifdef MACINTOSH
1194         key_init();
1195         #endif
1196         
1197         #if defined (MACINTOSH) || defined(WINDOWS)
1198         memcpy(New_pal,gr_palette,sizeof(gr_palette));          // attempt to get fades after briefing screens done correctly.
1199         #endif
1200
1201
1202         #ifndef WINDOWS 
1203         if (gr_palette_fade_out( New_pal, 32, allow_keys ))
1204                 return 1;
1205    #else
1206                 DEFINE_SCREEN(NULL);
1207                 WIN(DDGRLOCK(dd_grd_curcanv));
1208                 gr_clear_canvas (0);
1209                 WIN(DDGRUNLOCK(dd_grd_curcanv));
1210                 if (gr_palette_fade_out( New_pal, 32, allow_keys ))
1211                         return 1;
1212         #endif
1213
1214         gr_copy_palette(gr_palette, palette_save, sizeof(palette_save));
1215
1216 //      d_free(briefing_bm.bm_data);
1217
1218         return rval;
1219 }
1220
1221
1222 //      -----------------------------------------------------------------------------
1223 void do_briefing_screens(char *filename,int level_num)
1224 {
1225
1226    MVEPaletteCalls=0;
1227
1228         if (Skip_briefing_screens) {
1229                 mprintf((0, "Skipping all briefing screens.\n"));
1230                 return;
1231         }
1232         
1233         #ifdef APPLE_DEMO
1234         return;                 // no briefing screens at all for demo
1235         #endif
1236
1237         mprintf ((0,"Trying briefing screen! %s\n",filename));
1238
1239         if (!filename)
1240                 return;
1241
1242         if (!load_screen_text(filename, &Briefing_text))
1243          return;
1244
1245         #ifdef SHAREWARE
1246         songs_play_song( SONG_BRIEFING, 1 );
1247         #else
1248         songs_stop_all();
1249         #endif
1250
1251         set_screen_mode( SCREEN_MENU );
1252         
1253         WINDOS(
1254                 dd_gr_set_current_canvas(NULL),
1255                 gr_set_current_canvas(NULL)
1256         );
1257
1258         mprintf ((0,"Playing briefing screen! %s %d\n",filename,level_num));
1259
1260         key_flush();
1261         
1262         show_briefing_screen(level_num,0);
1263
1264         d_free (Briefing_text);
1265         key_flush();
1266
1267         return;
1268
1269 }
1270
1271 int DefineBriefingBox (char **buf)
1272  {
1273   int n,i=0;
1274   char name[20];
1275
1276   n=get_new_message_num (buf);
1277
1278         Assert(n < MAX_BRIEFING_SCREENS);
1279
1280   while (**buf!=' ')
1281    {
1282     name[i++]=**buf;
1283     (*buf)++;
1284    }
1285
1286   name[i]='\0';   // slap a delimiter on this guy
1287
1288   strcpy (Briefing_screens[n].bs_name,name);
1289   Briefing_screens[n].level_num=get_new_message_num (buf);
1290   Briefing_screens[n].message_num=get_new_message_num (buf);
1291   Briefing_screens[n].text_ulx=get_new_message_num (buf);
1292   Briefing_screens[n].text_uly=get_new_message_num (buf);
1293   Briefing_screens[n].text_width=get_new_message_num (buf);
1294   Briefing_screens[n].text_height=get_message_num (buf);  // NOTICE!!!
1295
1296   if (MenuHires)
1297         {
1298          Briefing_screens[n].text_ulx*=2;
1299          Briefing_screens[n].text_uly*=2.4;
1300          Briefing_screens[n].text_width*=2;
1301          Briefing_screens[n].text_height*=2.4;
1302         }
1303  
1304   return (n);
1305  }
1306
1307 int get_new_message_num(char **message)
1308 {
1309         int     num=0;
1310
1311         while (**message == ' ')
1312                 (*message)++;
1313
1314         while ((**message >= '0') && (**message <= '9')) {
1315                 num = 10*num + **message-'0';
1316                 (*message)++;
1317         }
1318
1319        (*message)++;
1320
1321         return num;
1322 }
1323