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