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