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