]> icculus.org git repositories - btb/d2x.git/blob - main/movie.c
convert file i/o to cfile
[btb/d2x.git] / main / movie.c
1 /* $Id: movie.c,v 1.27 2003-06-16 06:57:34 btb 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 #ifdef HAVE_CONFIG_H
16 #include <conf.h>
17 #endif
18
19 #ifdef RCS
20 static char rcsid[] = "$Id: movie.c,v 1.27 2003-06-16 06:57:34 btb Exp $";
21 #endif
22
23 #define DEBUG_LEVEL CON_NORMAL
24
25 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <ctype.h>
31
32 #include "movie.h"
33 #include "console.h"
34 #include "args.h"
35 #include "key.h"
36 #include "digi.h"
37 #include "songs.h"
38 #include "inferno.h"
39 #include "palette.h"
40 #include "strutil.h"
41 #include "error.h"
42 #include "u_mem.h"
43 #include "byteswap.h"
44 #include "gr.h"
45 #include "gamefont.h"
46 #include "cfile.h"
47 #include "menu.h"
48 #include "libmve.h"
49 #include "text.h"
50 #include "screens.h"
51
52 extern int MenuHiresAvailable;
53 extern char CDROM_dir[];
54
55 #define VID_PLAY 0
56 #define VID_PAUSE 1
57
58 int Vid_State;
59
60
61 // Subtitle data
62 typedef struct {
63         short first_frame,last_frame;
64         char *msg;
65 } subtitle;
66
67 #define MAX_SUBTITLES 500
68 #define MAX_ACTIVE_SUBTITLES 3
69 subtitle Subtitles[MAX_SUBTITLES];
70 int Num_subtitles;
71
72 // Movielib data
73 typedef struct {
74         char name[FILENAME_LEN];
75         int offset,len;
76 } ml_entry;
77
78 #define MLF_ON_CD    1
79 #define MAX_MOVIES_PER_LIB    50    //determines size of malloc
80
81 typedef struct {
82         char     name[100]; //[FILENAME_LEN];
83         int      n_movies;
84         ubyte    flags,pad[3];
85         ml_entry *movies;
86 } movielib;
87
88 #ifdef D2_OEM
89 char movielib_files[][FILENAME_LEN] = {"intro-l.mvl","other-l.mvl","robots-l.mvl","oem-l.mvl"};
90 #else
91 char movielib_files[][FILENAME_LEN] = {"intro-l.mvl","other-l.mvl","robots-l.mvl"};
92 #endif
93
94 #define N_BUILTIN_MOVIE_LIBS (sizeof(movielib_files)/sizeof(*movielib_files))
95 #define N_MOVIE_LIBS (N_BUILTIN_MOVIE_LIBS+1)
96 #define EXTRA_ROBOT_LIB N_BUILTIN_MOVIE_LIBS
97 movielib *movie_libs[N_MOVIE_LIBS];
98
99
100 //do we have the robot movies available
101 int robot_movies = 0; //0 means none, 1 means lowres, 2 means hires
102
103 int MovieHires = 1;   //default is highres
104
105 int RoboFile = 0, RoboFilePos = 0;
106
107 // Function Prototypes
108 int RunMovie(char *filename, int highres_flag, int allow_abort,int dx,int dy);
109
110 int open_movie_file(char *filename,int must_have);
111 int reset_movie_file(int handle);
112
113 void change_filename_ext( char *dest, char *src, char *ext );
114 void decode_text_line(char *p);
115 void draw_subtitles(int frame_num);
116
117
118 // ----------------------------------------------------------------------
119 void* MPlayAlloc(unsigned size)
120 {
121     return d_malloc(size);
122 }
123
124 void MPlayFree(void *p)
125 {
126     d_free(p);
127 }
128
129
130 //-----------------------------------------------------------------------
131
132 unsigned int FileRead(void *handle, void *buf, unsigned int count)
133 {
134     unsigned numread;
135     numread = read((int)handle, buf, count);
136     return (numread == count);
137 }
138
139
140 //-----------------------------------------------------------------------
141
142
143 //filename will actually get modified to be either low-res or high-res
144 //returns status.  see values in movie.h
145 int PlayMovie(const char *filename, int must_have)
146 {
147         char name[FILENAME_LEN],*p;
148         int c, ret;
149
150 #ifndef RELEASE
151         if (FindArg("-nomovies"))
152                 return MOVIE_NOT_PLAYED;
153 #endif
154
155         strcpy(name,filename);
156
157         if ((p=strchr(name,'.')) == NULL)               //add extension, if missing
158                 strcat(name,".mve");
159
160         //check for escape already pressed & abort if so
161         while ((c=key_inkey()) != 0)
162                 if (c == KEY_ESC)
163                         return MOVIE_ABORTED;
164
165         // Stop all digital sounds currently playing.
166         digi_stop_all();
167
168         // Stop all songs
169         songs_stop_all();
170
171         digi_close();
172
173         // Start sound
174         if (!FindArg("-nosound"))
175                 MVE_sndInit(1);
176         else
177                 MVE_sndInit(-1);
178
179         ret = RunMovie(name,MovieHires,must_have,-1,-1);
180
181         if (!FindArg("-nosound"))
182                 digi_init();
183
184         Screen_mode = -1;               //force screen reset
185
186         return ret;
187 }
188
189
190 void MovieShowFrame(ubyte *buf, uint bufw, uint bufh, uint sx, uint sy,
191                                         uint w, uint h, uint dstx, uint dsty)
192 {
193         grs_bitmap source_bm;
194
195         //mprintf((0,"MovieShowFrame %d,%d  %d,%d  %d,%d  %d,%d\n",bufw,bufh,sx,sy,w,h,dstx,dsty));
196
197         Assert(bufw == w && bufh == h);
198
199         source_bm.bm_x = source_bm.bm_y = 0;
200         source_bm.bm_w = source_bm.bm_rowsize = bufw;
201         source_bm.bm_h = bufh;
202         source_bm.bm_type = BM_LINEAR;
203         source_bm.bm_flags = 0;
204         source_bm.bm_data = buf;
205
206         gr_bm_ubitblt(bufw,bufh,dstx,dsty,sx,sy,&source_bm,&grd_curcanv->cv_bitmap);
207 }
208
209 //our routine to set the pallete, called from the movie code
210 void MovieSetPalette(unsigned char *p, unsigned start, unsigned count)
211 {
212         if (count == 0)
213                 return;
214
215         //mprintf((0,"SetPalette p=%x, start=%d, count=%d\n",p,start,count));
216
217         //Color 0 should be black, and we get color 255
218         Assert(start>=1 && start+count-1<=254);
219
220         //Set color 0 to be black
221         gr_palette[0] = gr_palette[1] = gr_palette[2] = 0;
222
223         //Set color 255 to be our subtitle color
224         gr_palette[765] = gr_palette[766] = gr_palette[767] = 50;
225
226         //movie libs palette into our array
227         memcpy(gr_palette+start*3,p+start*3,count*3);
228
229         //finally set the palette in the hardware
230         //gr_palette_load(gr_palette);
231
232         //MVE_SetPalette(p, start, count);
233 }
234
235
236 #if 0
237 typedef struct bkg {
238         short x, y, w, h;           // The location of the menu.
239         grs_bitmap * bmp;               // The background under the menu.
240 } bkg;
241
242 bkg movie_bg = {0,0,0,0,NULL};
243 #endif
244
245 #define BOX_BORDER (MenuHires?40:20)
246
247
248 void show_pause_message(char *msg)
249 {
250         int w,h,aw;
251         int x,y;
252
253         gr_set_current_canvas(NULL);
254         gr_set_curfont( SMALL_FONT );
255
256         gr_get_string_size(msg,&w,&h,&aw);
257
258         x = (grd_curscreen->sc_w-w)/2;
259         y = (grd_curscreen->sc_h-h)/2;
260
261 #if 0
262         if (movie_bg.bmp) {
263                 gr_free_bitmap(movie_bg.bmp);
264                 movie_bg.bmp = NULL;
265         }
266
267         // Save the background of the display
268         movie_bg.x=x; movie_bg.y=y; movie_bg.w=w; movie_bg.h=h;
269
270         movie_bg.bmp = gr_create_bitmap( w+BOX_BORDER, h+BOX_BORDER );
271
272         gr_bm_ubitblt(w+BOX_BORDER, h+BOX_BORDER, 0, 0, x-BOX_BORDER/2, y-BOX_BORDER/2, &(grd_curcanv->cv_bitmap), movie_bg.bmp );
273 #endif
274
275         gr_setcolor(0);
276         gr_rect(x-BOX_BORDER/2,y-BOX_BORDER/2,x+w+BOX_BORDER/2-1,y+h+BOX_BORDER/2-1);
277
278         gr_set_fontcolor( 255, -1 );
279
280         gr_ustring( 0x8000, y, msg );
281
282         gr_update();
283 }
284
285 void clear_pause_message()
286 {
287 #if 0
288         if (movie_bg.bmp) {
289
290                 gr_bitmap(movie_bg.x-BOX_BORDER/2, movie_bg.y-BOX_BORDER/2, movie_bg.bmp);
291
292                 gr_free_bitmap(movie_bg.bmp);
293                 movie_bg.bmp = NULL;
294         }
295 #endif
296 }
297
298
299 //returns status.  see movie.h
300 int RunMovie(char *filename, int hires_flag, int must_have,int dx,int dy)
301 {
302         int filehndl;
303         int result=1,aborted=0;
304         int track = 0;
305         int frame_num;
306         int key;
307 #ifdef OGL
308         char pal_save[768];
309 #endif
310
311         result=1;
312
313         // Open Movie file.  If it doesn't exist, no movie, just return.
314
315         filehndl = open_movie_file(filename,must_have);
316
317         if (filehndl == -1) {
318                 if (must_have)
319                         Warning("movie: RunMovie: Cannot open movie <%s>\n",filename);
320                 return MOVIE_NOT_PLAYED;
321         }
322
323         MVE_memCallbacks(MPlayAlloc, MPlayFree);
324         MVE_ioCallbacks(FileRead);
325
326         if (hires_flag) {
327                 gr_set_mode(SM(640,480));
328         } else {
329                 gr_set_mode(SM(320,200));
330         }
331 #ifdef OGL
332         set_screen_mode(SCREEN_MENU);
333         gr_copy_palette(pal_save, gr_palette, 768);
334         memset(gr_palette, 0, 768);
335         gr_palette_load(gr_palette);
336 #endif
337
338 #if !defined(POLY_ACC)
339         MVE_sfCallbacks(MovieShowFrame);
340         MVE_palCallbacks(MovieSetPalette);
341 #endif
342
343         if (MVE_rmPrepMovie((void *)filehndl, dx, dy, track)) {
344                 Int3();
345                 return MOVIE_NOT_PLAYED;
346         }
347
348         frame_num = 0;
349
350         FontHires = FontHiresAvailable && hires_flag;
351
352         while((result = MVE_rmStepMovie()) == 0) {
353
354                 draw_subtitles(frame_num);
355
356                 gr_palette_load(gr_palette); // moved this here because of flashing
357
358                 gr_update();
359
360                 key = key_inkey();
361
362                 // If ESCAPE pressed, then quit movie.
363                 if (key == KEY_ESC) {
364                         result = aborted = 1;
365                         break;
366                 }
367
368                 // If PAUSE pressed, then pause movie
369                 if (key == KEY_PAUSE) {
370                         MVE_rmHoldMovie();
371                         show_pause_message(TXT_PAUSE);
372                         while (!key_inkey()) ;
373                         clear_pause_message();
374                 }
375
376 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
377                 if ((key == KEY_CTRLED+KEY_SHIFTED+KEY_PADENTER) ||
378                         (key == KEY_ALTED+KEY_CTRLED+KEY_PADENTER) ||
379                         (key == KEY_ALTED+KEY_SHIFTED+KEY_PADENTER))
380                         gr_toggle_fullscreen();
381 #endif
382
383                 frame_num++;
384         }
385
386         Assert(aborted || result == MVE_ERR_EOF);        ///movie should be over
387
388     MVE_rmEndMovie();
389
390         close(filehndl);                           // Close Movie File
391
392         // Restore old graphic state
393
394         Screen_mode=-1;  //force reset of screen mode
395 #ifdef OGL
396         gr_copy_palette(gr_palette, pal_save, 768);
397         gr_palette_load(pal_save);
398 #endif
399
400         return (aborted?MOVIE_ABORTED:MOVIE_PLAYED_FULL);
401 }
402
403
404 int InitMovieBriefing()
405 {
406 #if 0
407         if (MenuHires)
408                 gr_set_mode(SM(640,480));
409         else
410                 gr_set_mode(SM(320,200));
411
412         gr_init_sub_canvas( &VR_screen_pages[0], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
413         gr_init_sub_canvas( &VR_screen_pages[1], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
414 #endif
415
416         return 1;
417 }
418
419
420 //returns 1 if frame updated ok
421 int RotateRobot()
422 {
423         int err;
424
425         err = MVE_rmStepMovie();
426
427         gr_palette_load(gr_palette);
428
429         if (err == MVE_ERR_EOF)     //end of movie, so reset
430         {
431                 reset_movie_file(RoboFile);
432                 if (MVE_rmPrepMovie((void *)RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
433                         Int3();
434                         return 0;
435                 }
436         }
437         else if (err) {
438                 Int3();
439                 return 0;
440         }
441
442         return 1;
443 }
444
445
446 void DeInitRobotMovie(void)
447 {
448         MVE_rmEndMovie();
449         close(RoboFile);                           // Close Movie File
450 }
451
452
453 int InitRobotMovie(char *filename)
454 {
455         if (FindArg("-nomovies"))
456                 return 0;
457
458         con_printf(DEBUG_LEVEL, "RoboFile=%s\n", filename);
459
460         MVE_sndInit(-1);        //tell movies to play no sound for robots
461
462         RoboFile = open_movie_file(filename, 1);
463
464         if (RoboFile == -1) {
465                 Warning("movie: InitRobotMovie: Cannot open movie file <%s>",filename);
466                 return MOVIE_NOT_PLAYED;
467         }
468
469         Vid_State = VID_PLAY;
470
471         if (MVE_rmPrepMovie((void *)RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
472                 Int3();
473                 return 0;
474         }
475
476         RoboFilePos=lseek (RoboFile,0L,SEEK_CUR);
477
478         con_printf(DEBUG_LEVEL, "RoboFilePos=%d!\n", RoboFilePos);
479
480         return 1;
481 }
482
483
484 /*
485  *              Subtitle system code
486  */
487
488 ubyte *subtitle_raw_data;
489
490
491 //search for next field following whitespace 
492 ubyte *next_field(ubyte *p)
493 {
494         while (*p && !isspace(*p))
495                 p++;
496
497         if (!*p)
498                 return NULL;
499
500         while (*p && isspace(*p))
501                 p++;
502
503         if (!*p)
504                 return NULL;
505
506         return p;
507 }
508
509
510 int init_subtitles(char *filename)
511 {
512         CFILE *ifile;
513         int size,read_count;
514         ubyte *p;
515         int have_binary = 0;
516
517         Num_subtitles = 0;
518
519         if (! FindArg("-subtitles"))
520                 return 0;
521
522         ifile = cfopen(filename,"rb");          //try text version
523
524         if (!ifile) {                                                           //no text version, try binary version
525                 char filename2[FILENAME_LEN];
526                 change_filename_ext(filename2,filename,".TXB");
527                 ifile = cfopen(filename2,"rb");
528                 if (!ifile)
529                         return 0;
530                 have_binary = 1;
531         }
532
533         size = cfilelength(ifile);
534
535         MALLOC (subtitle_raw_data, ubyte, size+1);
536
537         read_count = cfread(subtitle_raw_data, 1, size, ifile);
538
539         cfclose(ifile);
540
541         subtitle_raw_data[size] = 0;
542
543         if (read_count != size) {
544                 d_free(subtitle_raw_data);
545                 return 0;
546         }
547
548         p = subtitle_raw_data;
549
550         while (p && p < subtitle_raw_data+size) {
551                 char *endp;
552
553                 endp = strchr(p,'\n'); 
554                 if (endp) {
555                         if (endp[-1] == '\r')
556                                 endp[-1] = 0;           //handle 0d0a pair
557                         *endp = 0;                      //string termintor
558                 }
559
560                 if (have_binary)
561                         decode_text_line(p);
562
563                 if (*p != ';') {
564                         Subtitles[Num_subtitles].first_frame = atoi(p);
565                         p = next_field(p); if (!p) continue;
566                         Subtitles[Num_subtitles].last_frame = atoi(p);
567                         p = next_field(p); if (!p) continue;
568                         Subtitles[Num_subtitles].msg = p;
569
570                         Assert(Num_subtitles==0 || Subtitles[Num_subtitles].first_frame >= Subtitles[Num_subtitles-1].first_frame);
571                         Assert(Subtitles[Num_subtitles].last_frame >= Subtitles[Num_subtitles].first_frame);
572
573                         Num_subtitles++;
574                 }
575
576                 p = endp+1;
577
578         }
579
580         return 1;
581 }
582
583
584 void close_subtitles()
585 {
586         if (subtitle_raw_data)
587                 d_free(subtitle_raw_data);
588         subtitle_raw_data = NULL;
589         Num_subtitles = 0;
590 }
591
592
593 //draw the subtitles for this frame
594 void draw_subtitles(int frame_num)
595 {
596         static int active_subtitles[MAX_ACTIVE_SUBTITLES];
597         static int num_active_subtitles,next_subtitle,line_spacing;
598         int t,y;
599         int must_erase=0;
600
601         if (frame_num == 0) {
602                 num_active_subtitles = 0;
603                 next_subtitle = 0;
604                 gr_set_curfont( GAME_FONT );
605                 line_spacing = grd_curcanv->cv_font->ft_h + (grd_curcanv->cv_font->ft_h >> 2);
606                 gr_set_fontcolor(255,-1);
607         }
608
609         //get rid of any subtitles that have expired
610         for (t=0;t<num_active_subtitles;)
611                 if (frame_num > Subtitles[active_subtitles[t]].last_frame) {
612                         int t2;
613                         for (t2=t;t2<num_active_subtitles-1;t2++)
614                                 active_subtitles[t2] = active_subtitles[t2+1];
615                         num_active_subtitles--;
616                         must_erase = 1;
617                 }
618                 else
619                         t++;
620
621         //get any subtitles new for this frame 
622         while (next_subtitle < Num_subtitles && frame_num >= Subtitles[next_subtitle].first_frame) {
623                 if (num_active_subtitles >= MAX_ACTIVE_SUBTITLES)
624                         Error("Too many active subtitles!");
625                 active_subtitles[num_active_subtitles++] = next_subtitle;
626                 next_subtitle++;
627         }
628
629         //find y coordinate for first line of subtitles
630         y = grd_curcanv->cv_bitmap.bm_h-((line_spacing+1)*MAX_ACTIVE_SUBTITLES+2);
631
632         //erase old subtitles if necessary
633         if (must_erase) {
634                 gr_setcolor(0);
635                 gr_rect(0,y,grd_curcanv->cv_bitmap.bm_w-1,grd_curcanv->cv_bitmap.bm_h-1);
636         }
637
638         //now draw the current subtitles
639         for (t=0;t<num_active_subtitles;t++)
640                 if (active_subtitles[t] != -1) {
641                         gr_string(0x8000,y,Subtitles[active_subtitles[t]].msg);
642                         y += line_spacing+1;
643                 }
644 }
645
646
647 movielib *init_new_movie_lib(char *filename, CFILE *fp)
648 {
649         int nfiles,offset;
650         int i,n;
651         movielib *table;
652
653         //read movie file header
654
655         nfiles = cfile_read_int(fp);        //get number of files
656
657         //table = d_malloc(sizeof(*table) + sizeof(ml_entry)*nfiles);
658         MALLOC(table, movielib, 1);
659         MALLOC(table->movies, ml_entry, nfiles);
660
661         strcpy(table->name,filename);
662         table->n_movies = nfiles;
663
664         offset = 4+4+nfiles*(13+4);     //id + nfiles + nfiles * (filename + size)
665
666         for (i=0;i<nfiles;i++) {
667                 int len;
668
669                 n = cfread(table->movies[i].name, 13, 1, fp);
670                 if ( n != 1 )
671                         break;          //end of file (probably)
672
673                 len = cfile_read_int(fp);
674
675                 table->movies[i].len = len;
676                 table->movies[i].offset = offset;
677
678                 offset += table->movies[i].len;
679
680         }
681
682         cfclose(fp);
683
684         table->flags = 0;
685
686         return table;
687
688 }
689
690
691 movielib *init_old_movie_lib(char *filename, CFILE *fp)
692 {
693         int nfiles,size;
694         int i;
695         movielib *table,*table2;
696
697         nfiles = 0;
698
699         //allocate big table
700         table = d_malloc(sizeof(*table) + sizeof(ml_entry)*MAX_MOVIES_PER_LIB);
701
702         while( 1 ) {
703                 int len;
704
705                 i = cfread(table->movies[nfiles].name, 13, 1, fp);
706                 if ( i != 1 )
707                         break;          //end of file (probably)
708
709                 i = cfread(&len, 4, 1, fp);
710                 if ( i != 1 )
711                         Error("error reading movie library <%s>",filename);
712
713                 table->movies[nfiles].len = INTEL_INT(len);
714                 table->movies[nfiles].offset = cftell(fp);
715
716                 cfseek(fp, INTEL_INT(len), SEEK_CUR);       //skip data
717
718                 nfiles++;
719         }
720
721         //allocate correct-sized table
722         size = sizeof(*table) + sizeof(ml_entry)*nfiles;
723         table2 = d_malloc(size);
724         memcpy(table2,table,size);
725         d_free(table);
726         table = table2;
727
728         strcpy(table->name,filename);
729
730         table->n_movies = nfiles;
731
732         cfclose(fp);
733
734         table->flags = 0;
735
736         return table;
737
738 }
739
740
741 //find the specified movie library, and read in list of movies in it
742 movielib *init_movie_lib(char *filename)
743 {
744         //note: this based on cfile_init_hogfile()
745
746         char id[4];
747         CFILE *fp;
748
749         fp = cfopen(filename, "rb");
750
751         if ( fp == NULL )
752                 return NULL;
753
754         cfread(id, 4, 1, fp);
755         if ( !strncmp( id, "DMVL", 4 ) )
756                 return init_new_movie_lib(filename,fp);
757         else if ( !strncmp( id, "DHF", 3 ) ) {
758                 cfseek(fp,-1,SEEK_CUR);         //old file had 3 char id
759                 return init_old_movie_lib(filename,fp);
760         }
761         else {
762                 cfclose(fp);
763                 return NULL;
764         }
765 }
766
767
768 void close_movie(int i)
769 {
770         if (movie_libs[i]) {
771                 d_free(movie_libs[i]->movies);
772                 d_free(movie_libs[i]);
773         }
774 }
775
776
777 void close_movies()
778 {
779         int i;
780
781         for (i=0;i<N_MOVIE_LIBS;i++)
782                 close_movie(i);
783 }
784
785
786 //ask user to put the D2 CD in.
787 //returns -1 if ESC pressed, 0 if OK chosen
788 //CD may not have been inserted
789 int request_cd(void)
790 {
791 #if 0
792         ubyte save_pal[256*3];
793         grs_canvas *save_canv,*tcanv;
794         int ret,was_faded=gr_palette_faded_out;
795
796         gr_palette_clear();
797
798         save_canv = grd_curcanv;
799         tcanv = gr_create_canvas(grd_curcanv->cv_w,grd_curcanv->cv_h);
800
801         gr_set_current_canvas(tcanv);
802         gr_ubitmap(0,0,&save_canv->cv_bitmap);
803         gr_set_current_canvas(save_canv);
804
805         gr_clear_canvas(BM_XRGB(0,0,0));
806
807         memcpy(save_pal,gr_palette,sizeof(save_pal));
808
809         memcpy(gr_palette,last_palette_for_color_fonts,sizeof(gr_palette));
810
811  try_again:;
812
813         ret = nm_messagebox( "CD ERROR", 1, "Ok", "Please insert your Descent II CD");
814
815         if (ret == -1) {
816                 int ret2;
817
818                 ret2 = nm_messagebox( "CD ERROR", 2, "Try Again", "Leave Game", "You must insert your\nDescent II CD to Continue");
819
820                 if (ret2 == -1 || ret2 == 0)
821                         goto try_again;
822         }
823
824         force_rb_register = 1;  //disc has changed; force register new CD
825
826         gr_palette_clear();
827
828         memcpy(gr_palette,save_pal,sizeof(save_pal));
829
830         gr_ubitmap(0,0,&tcanv->cv_bitmap);
831
832         if (!was_faded)
833                 gr_palette_load(gr_palette);
834
835         gr_free_canvas(tcanv);
836
837         return ret;
838 #else
839         con_printf(DEBUG_LEVEL, "STUB: movie: request_cd\n");
840         return 0;
841 #endif
842 }
843
844
845 void init_movie(char *filename,int libnum,int is_robots,int required)
846 {
847         int high_res;
848         int try = 0;
849
850 #ifndef RELEASE
851         if (FindArg("-nomovies")) {
852                 movie_libs[libnum] = NULL;
853                 return;
854         }
855 #endif
856
857         //for robots, load highres versions if highres menus set
858         if (is_robots)
859                 high_res = MenuHiresAvailable;
860         else
861                 high_res = MovieHires;
862
863         if (high_res)
864                 strchr(filename,'.')[-1] = 'h';
865
866 try_again:;
867
868         if ((movie_libs[libnum] = init_movie_lib(filename)) == NULL) {
869                 char name2[100];
870
871                 strcpy(name2,CDROM_dir);
872                 strcat(name2,filename);
873                 movie_libs[libnum] = init_movie_lib(name2);
874
875                 if (movie_libs[libnum] != NULL)
876                         movie_libs[libnum]->flags |= MLF_ON_CD;
877                 else {
878                         if (required) {
879                                 Warning("Cannot open movie file <%s>\n",filename);
880                         }
881
882                         if (!try) {                                         // first try
883                                 if (strchr(filename, '.')[-1] == 'h') {         // try again with lowres
884                                         strchr(filename, '.')[-1] = 'l';
885                                         high_res = 0;
886                                         Warning("Trying to open movie file <%s> instead\n", filename);
887                                         try++;
888                                         goto try_again;
889                                 } else if (strchr(filename, '.')[-1] == 'l') {  // try again with highres
890                                         strchr(filename, '.')[-1] = 'h';
891                                         high_res = 1;
892                                         Warning("Trying to open movie file <%s> instead\n", filename);
893                                         try++;
894                                         goto try_again;
895                                 }
896                         }
897                 }
898         }
899
900         if (is_robots && movie_libs[libnum]!=NULL)
901                 robot_movies = high_res?2:1;
902 }
903
904
905 //find and initialize the movie libraries
906 void init_movies()
907 {
908         int i;
909         int is_robots;
910
911         for (i=0;i<N_BUILTIN_MOVIE_LIBS;i++) {
912
913                 if (!strnicmp(movielib_files[i],"robot",5))
914                         is_robots = 1;
915                 else
916                         is_robots = 0;
917
918                 init_movie(movielib_files[i],i,is_robots,1);
919         }
920
921         movie_libs[EXTRA_ROBOT_LIB] = NULL;
922
923         atexit(close_movies);
924 }
925
926
927 void init_extra_robot_movie(char *filename)
928 {
929         close_movie(EXTRA_ROBOT_LIB);
930         init_movie(filename,EXTRA_ROBOT_LIB,1,0);
931 }
932
933
934 int movie_handle,movie_start;
935
936 //looks through a movie library for a movie file
937 //returns filehandle, with fileposition at movie, or -1 if can't find
938 int search_movie_lib(movielib *lib,char *filename,int must_have)
939 {
940         int i;
941         int filehandle;
942
943         if (lib == NULL)
944                 return -1;
945
946         for (i=0;i<lib->n_movies;i++)
947                 if (!stricmp(filename,lib->movies[i].name)) {   //found the movie in a library 
948                         int from_cd;
949
950                         from_cd = (lib->flags & MLF_ON_CD);
951
952                         if (from_cd)
953                                 songs_stop_redbook();           //ready to read from CD
954
955                         do {            //keep trying until we get the file handle
956
957 #ifdef O_BINARY
958                                 movie_handle = filehandle = open(lib->name, O_RDONLY | O_BINARY);
959 #else
960                                 movie_handle = filehandle = open(lib->name, O_RDONLY);
961 #endif
962
963                                 if (must_have && from_cd && filehandle == -1) {         //didn't get file!
964
965                                         if (request_cd() == -1)         //ESC from requester
966                                                 break;                                          //bail from here. will get error later
967                                 }
968
969                         } while (must_have && from_cd && filehandle == -1);
970
971                         if (filehandle != -1)
972                                 lseek(filehandle,(movie_start=lib->movies[i].offset),SEEK_SET);
973
974                         return filehandle;
975                 }
976
977         return -1;
978 }
979
980
981 //returns file handle
982 int open_movie_file(char *filename,int must_have)
983 {
984         int filehandle,i;
985
986         for (i=0;i<N_MOVIE_LIBS;i++) {
987                 if ((filehandle = search_movie_lib(movie_libs[i],filename,must_have)) != -1)
988                         return filehandle;
989         }
990
991         return -1;              //couldn't find it
992 }
993
994 //sets the file position to the start of this already-open file
995 int reset_movie_file(int handle)
996 {
997         Assert(handle == movie_handle);
998
999         lseek(handle,movie_start,SEEK_SET);
1000
1001         return 0;       //everything is cool
1002 }