]> icculus.org git repositories - btb/d2x.git/blob - main/movie.c
added fullscreen toggle while playing movies
[btb/d2x.git] / main / movie.c
1 /* $Id: movie.c,v 1.13 2002-09-04 08:13:59 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.13 2002-09-04 08:13:59 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 "mvelib.h"
49 #include "text.h"
50 #include "fileutil.h"
51 #include "mveplay.h"
52
53 extern int MenuHiresAvailable;
54 extern char CDROM_dir[];
55
56 #define VID_PLAY 0
57 #define VID_PAUSE 1
58
59 int Vid_State;
60
61
62 // Subtitle data
63 typedef struct {
64         short first_frame,last_frame;
65         char *msg;
66 } subtitle;
67
68 #define MAX_SUBTITLES 500
69 #define MAX_ACTIVE_SUBTITLES 3
70 subtitle Subtitles[MAX_SUBTITLES];
71 int Num_subtitles;
72
73
74 typedef struct {
75         char name[FILENAME_LEN];
76         int offset,len;
77 } ml_entry;
78
79 #define MLF_ON_CD    1
80 #define MAX_MOVIES_PER_LIB    50    //determines size of malloc
81
82
83 typedef struct {
84         char     name[100]; //[FILENAME_LEN];
85         int      n_movies;
86         ubyte    flags,pad[3];
87         ml_entry *movies;
88 } movielib;
89
90 #ifdef D2_OEM
91 char movielib_files[][FILENAME_LEN] = {"intro-l.mvl","other-l.mvl","robots-l.mvl","oem-l.mvl"};
92 #else
93 char movielib_files[][FILENAME_LEN] = {"intro-l.mvl","other-l.mvl","robots-l.mvl"};
94 #endif
95
96 #define N_BUILTIN_MOVIE_LIBS (sizeof(movielib_files)/sizeof(*movielib_files))
97 #define N_MOVIE_LIBS (N_BUILTIN_MOVIE_LIBS+1)
98 #define EXTRA_ROBOT_LIB N_BUILTIN_MOVIE_LIBS
99 movielib *movie_libs[N_MOVIE_LIBS];
100
101 int MVEPaletteCalls = 0;
102
103 //do we have the robot movies available
104 int robot_movies = 0; //0 means none, 1 means lowres, 2 means hires
105
106 int MovieHires = 0;   //default for now is lores
107
108 int RoboFile = 0;
109 off_t Robo_filepos;
110 MVESTREAM *Robo_mve;
111 grs_bitmap *Robo_bitmap;
112
113
114 //      Function Prototypes
115 int RunMovie(char *filename, int highres_flag, int allow_abort,int dx,int dy);
116
117 int open_movie_file(char *filename,int must_have);
118
119 void change_filename_ext( char *dest, char *src, char *ext );
120 void decode_text_line(char *p);
121 void draw_subtitles(int frame_num);
122
123
124 //filename will actually get modified to be either low-res or high-res
125 //returns status.  see values in movie.h
126 int PlayMovie(const char *filename, int must_have)
127 {
128         char name[FILENAME_LEN],*p;
129         int c, ret;
130 #if 0
131         int save_sample_rate;
132 #endif
133
134 #ifndef RELEASE
135         if (FindArg("-nomovies"))
136                 return MOVIE_NOT_PLAYED;
137 #endif
138
139         strcpy(name,filename);
140
141         if ((p=strchr(name,'.')) == NULL)               //add extension, if missing
142                 strcat(name,".mve");
143
144         //check for escape already pressed & abort if so
145         while ((c=key_inkey()) != 0)
146                 if (c == KEY_ESC)
147                         return MOVIE_ABORTED;
148
149         // Stop all digital sounds currently playing.
150         digi_stop_all();
151
152         // Stop all songs
153         songs_stop_all();
154
155 #if 0
156         save_sample_rate = digi_sample_rate;
157         digi_sample_rate = SAMPLE_RATE_22K;             //always 22K for movies
158         digi_reset(); digi_reset();
159 #else
160         digi_close();
161 #endif
162
163         ret = RunMovie(name,MovieHires,must_have,-1,-1);
164
165 #if 0
166         gr_palette_clear();             //clear out palette in case movie aborted
167 #endif
168
169 #if 0
170         digi_sample_rate = save_sample_rate;            //restore rate for game
171         digi_reset(); digi_reset();
172 #else
173         digi_init();
174 #endif
175
176         Screen_mode = -1;               //force screen reset
177
178         return ret;
179 }
180
181 #if 0
182 typedef struct bkg {
183         short x, y, w, h;           // The location of the menu.
184         grs_bitmap * bmp;               // The background under the menu.
185 } bkg;
186
187 bkg movie_bg = {0,0,0,0,NULL};
188 #endif
189
190 #define BOX_BORDER (MenuHires?40:20)
191
192 void show_pause_message(char *msg)
193 {
194         int w,h,aw;
195         int x,y;
196
197         gr_set_current_canvas(NULL);
198         gr_set_curfont( SMALL_FONT );
199
200         gr_get_string_size(msg,&w,&h,&aw);
201
202         x = (grd_curscreen->sc_w-w)/2;
203         y = (grd_curscreen->sc_h-h)/2;
204
205 #if 0
206         if (movie_bg.bmp) {
207                 gr_free_bitmap(movie_bg.bmp);
208                 movie_bg.bmp = NULL;
209         }
210
211         // Save the background of the display
212         movie_bg.x=x; movie_bg.y=y; movie_bg.w=w; movie_bg.h=h;
213
214         movie_bg.bmp = gr_create_bitmap( w+BOX_BORDER, h+BOX_BORDER );
215
216         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 );
217 #endif
218
219         gr_setcolor(0);
220         gr_rect(x-BOX_BORDER/2,y-BOX_BORDER/2,x+w+BOX_BORDER/2-1,y+h+BOX_BORDER/2-1);
221
222         gr_set_fontcolor( 255, -1 );
223
224         gr_ustring( 0x8000, y, msg );
225
226         gr_update();
227 }
228
229 void clear_pause_message()
230 {
231 #if 0
232         if (movie_bg.bmp) {
233
234                 gr_bitmap(movie_bg.x-BOX_BORDER/2, movie_bg.y-BOX_BORDER/2, movie_bg.bmp);
235
236                 gr_free_bitmap(movie_bg.bmp);
237                 movie_bg.bmp = NULL;
238         }
239 #endif
240 }
241
242 //returns status.  see movie.h
243 int RunMovie(char *filename, int hires_flag, int must_have,int dx,int dy)
244 {
245         int filehndl;
246         int result=1,aborted=0;
247         int frame_num;
248         MVESTREAM *mve;
249         grs_bitmap *mve_bitmap;
250         int key;
251         int x, y, w, h;
252
253         result=1;
254
255         // Open Movie file.  If it doesn't exist, no movie, just return.
256
257         filehndl = open_movie_file(filename,must_have);
258
259         if (filehndl == -1) {
260 #ifndef EDITOR
261                 if (must_have)
262                         Warning("movie: RunMovie: Cannot open movie <%s>\n",filename);
263 #endif
264                 return MOVIE_NOT_PLAYED;
265         }
266
267         if (hires_flag) {
268                 gr_set_mode(SM(640,480));
269                 x = 24;
270                 y = 84;
271                 w = 592;
272                 h = 312;
273         } else {
274                 gr_set_mode(SM(320,200));
275                 x = 0;
276                 y = 32;
277                 w = 320;
278                 h = 136;
279         }
280
281         frame_num = 0;
282
283         FontHires = FontHiresAvailable && hires_flag;
284
285     mve = mve_open(filehndl);
286
287         mve_bitmap = gr_create_bitmap(w, h); // w, h must match the mve exactly!
288
289         mveplay_initializeMovie(mve, mve_bitmap);
290
291         while(result) {
292
293                 result = mveplay_stepMovie(mve);
294
295                 gr_bitmap(x, y, mve_bitmap);
296
297                 draw_subtitles(frame_num);
298
299                 key = key_inkey();
300
301                 switch (key) {
302                 case KEY_ESC:
303                         // If ESCAPE pressed, then quit movie.
304                         result = 0;
305                         aborted = 1;
306                         break;
307                 case KEY_PAUSE:
308                         // If PAUSE pressed, then pause movie
309                         show_pause_message(TXT_PAUSE);
310                         while (!key_inkey()) ;
311                         mveplay_restartTimer(mve);
312                         clear_pause_message();
313                         break;
314                 case KEY_CTRLED+KEY_SHIFTED+KEY_PADENTER:
315                 case KEY_ALTED+KEY_CTRLED+KEY_PADENTER:
316                 case KEY_ALTED+KEY_SHIFTED+KEY_PADENTER:
317                         gr_toggle_fullscreen();
318                         break;
319                 }
320
321                 frame_num++;
322         }
323
324         Assert(aborted || !result);      ///movie should be over
325
326     mveplay_shutdownMovie(mve);
327
328         gr_free_bitmap(mve_bitmap);
329
330     mve_close(mve);
331
332         close(filehndl);                           // Close Movie File
333
334         // Restore old graphic state
335
336         Screen_mode=-1;  //force reset of screen mode
337
338         return (aborted?MOVIE_ABORTED:MOVIE_PLAYED_FULL);
339 }
340
341
342 int InitMovieBriefing()
343 {
344         if (MenuHires)
345                 gr_set_mode(SM(640,480));
346         else
347                 gr_set_mode(SM(320,200));
348
349         return 1;
350 }
351
352
353 //returns 1 if frame updated ok
354 int RotateRobot()
355 {
356         int ret;
357
358         ret = mveplay_stepMovie(Robo_mve);
359         gr_bitmap(MenuHires?280:140, MenuHires?200:80, Robo_bitmap);
360
361         if (!ret) {
362                 mveplay_shutdownMovie(Robo_mve);
363                 mve_close(Robo_mve);
364                 lseek(RoboFile, Robo_filepos, SEEK_SET);
365                 Robo_mve = mve_open(RoboFile);
366                 mveplay_initializeMovie(Robo_mve, Robo_bitmap);
367         }
368
369         return 1;
370 }
371
372
373 void DeInitRobotMovie(void)
374 {
375         mveplay_shutdownMovie(Robo_mve);
376         mve_close(Robo_mve);
377
378         gr_free_bitmap(Robo_bitmap);
379
380         close(RoboFile);
381 }
382
383
384 int InitRobotMovie(char *filename)
385 {
386         RoboFile = open_movie_file(filename, 1);
387
388         if (RoboFile == -1) {
389                 Warning("movie: InitRobotMovie: Cannot open movie file <%s>",filename);
390                 return MOVIE_NOT_PLAYED;
391         }
392
393         Robo_filepos = lseek(RoboFile, 0, SEEK_CUR);
394
395         Robo_mve = mve_open(RoboFile);
396
397         Robo_bitmap = gr_create_bitmap(MenuHires?320:160, MenuHires?200:88);
398
399         mveplay_initializeMovie(Robo_mve, Robo_bitmap);
400
401         return 1;
402 }
403
404
405 /*
406  *              Subtitle system code
407  */
408
409 ubyte *subtitle_raw_data;
410
411
412 //search for next field following whitespace 
413 ubyte *next_field(ubyte *p)
414 {
415         while (*p && !isspace(*p))
416                 p++;
417
418         if (!*p)
419                 return NULL;
420
421         while (*p && isspace(*p))
422                 p++;
423
424         if (!*p)
425                 return NULL;
426
427         return p;
428 }
429
430
431 int init_subtitles(char *filename)
432 {
433         CFILE *ifile;
434         int size,read_count;
435         ubyte *p;
436         int have_binary = 0;
437
438         Num_subtitles = 0;
439
440         if (! FindArg("-subtitles"))
441                 return 0;
442
443         ifile = cfopen(filename,"rb");          //try text version
444
445         if (!ifile) {                                                           //no text version, try binary version
446                 char filename2[FILENAME_LEN];
447                 change_filename_ext(filename2,filename,".TXB");
448                 ifile = cfopen(filename2,"rb");
449                 if (!ifile)
450                         return 0;
451                 have_binary = 1;
452         }
453
454         size = cfilelength(ifile);
455    
456         MALLOC (subtitle_raw_data, ubyte, size+1);
457
458         read_count = cfread(subtitle_raw_data, 1, size, ifile);
459
460         cfclose(ifile);
461
462         subtitle_raw_data[size] = 0;
463
464         if (read_count != size) {
465                 d_free(subtitle_raw_data);
466                 return 0;
467         }
468
469         p = subtitle_raw_data;
470
471         while (p && p < subtitle_raw_data+size) {
472                 char *endp;
473
474                 endp = strchr(p,'\n'); 
475                 if (endp) {
476                         if (endp[-1] == '\r')
477                                 endp[-1] = 0;           //handle 0d0a pair
478                         *endp = 0;                      //string termintor
479                 }
480
481                 if (have_binary)
482                         decode_text_line(p);
483
484                 if (*p != ';') {
485                         Subtitles[Num_subtitles].first_frame = atoi(p);
486                         p = next_field(p); if (!p) continue;
487                         Subtitles[Num_subtitles].last_frame = atoi(p);
488                         p = next_field(p); if (!p) continue;
489                         Subtitles[Num_subtitles].msg = p;
490
491                         Assert(Num_subtitles==0 || Subtitles[Num_subtitles].first_frame >= Subtitles[Num_subtitles-1].first_frame);
492                         Assert(Subtitles[Num_subtitles].last_frame >= Subtitles[Num_subtitles].first_frame);
493
494                         Num_subtitles++;
495                 }
496
497                 p = endp+1;
498
499         }
500
501         return 1;
502 }
503
504
505 void close_subtitles()
506 {
507         if (subtitle_raw_data)
508                 d_free(subtitle_raw_data);
509         subtitle_raw_data = NULL;
510         Num_subtitles = 0;
511 }
512
513
514 //draw the subtitles for this frame
515 void draw_subtitles(int frame_num)
516 {
517         static int active_subtitles[MAX_ACTIVE_SUBTITLES];
518         static int num_active_subtitles,next_subtitle,line_spacing;
519         int t,y;
520         int must_erase=0;
521
522         if (frame_num == 0) {
523                 num_active_subtitles = 0;
524                 next_subtitle = 0;
525                 gr_set_curfont( GAME_FONT );
526                 line_spacing = grd_curcanv->cv_font->ft_h + (grd_curcanv->cv_font->ft_h >> 2);
527                 gr_set_fontcolor(255,-1);
528         }
529
530         //get rid of any subtitles that have expired
531         for (t=0;t<num_active_subtitles;)
532                 if (frame_num > Subtitles[active_subtitles[t]].last_frame) {
533                         int t2;
534                         for (t2=t;t2<num_active_subtitles-1;t2++)
535                                 active_subtitles[t2] = active_subtitles[t2+1];
536                         num_active_subtitles--;
537                         must_erase = 1;
538                 }
539                 else
540                         t++;
541
542         //get any subtitles new for this frame 
543         while (next_subtitle < Num_subtitles && frame_num >= Subtitles[next_subtitle].first_frame) {
544                 if (num_active_subtitles >= MAX_ACTIVE_SUBTITLES)
545                         Error("Too many active subtitles!");
546                 active_subtitles[num_active_subtitles++] = next_subtitle;
547                 next_subtitle++;
548         }
549
550         //find y coordinate for first line of subtitles
551         y = grd_curcanv->cv_bitmap.bm_h-((line_spacing+1)*MAX_ACTIVE_SUBTITLES+2);
552
553         //erase old subtitles if necessary
554         if (must_erase) {
555                 gr_setcolor(0);
556                 gr_rect(0,y,grd_curcanv->cv_bitmap.bm_w-1,grd_curcanv->cv_bitmap.bm_h-1);
557         }
558
559         //now draw the current subtitles
560         for (t=0;t<num_active_subtitles;t++)
561                 if (active_subtitles[t] != -1) {
562                         gr_string(0x8000,y,Subtitles[active_subtitles[t]].msg);
563                         y += line_spacing+1;
564                 }
565
566         gr_update();
567 }
568
569
570 int request_cd(void)
571 {
572         con_printf(DEBUG_LEVEL, "STUB: movie: request_cd\n");
573         return 0;
574 }
575
576
577 movielib *init_new_movie_lib(char *filename,FILE *fp)
578 {
579         int nfiles,offset;
580         int i,n;
581         movielib *table;
582
583         //read movie file header
584
585         nfiles = file_read_int(fp);             //get number of files
586
587         //table = d_malloc(sizeof(*table) + sizeof(ml_entry)*nfiles);
588         MALLOC(table, movielib, 1);
589         MALLOC(table->movies, ml_entry, nfiles);
590
591         strcpy(table->name,filename);
592         table->n_movies = nfiles;
593
594         offset = 4+4+nfiles*(13+4);     //id + nfiles + nfiles * (filename + size)
595
596         for (i=0;i<nfiles;i++) {
597                 int len;
598
599                 n = fread( table->movies[i].name, 13, 1, fp );
600                 if ( n != 1 )
601                         break;          //end of file (probably)
602
603                 len = file_read_int(fp);
604
605                 table->movies[i].len = len;
606                 table->movies[i].offset = offset;
607
608                 offset += table->movies[i].len;
609
610         }
611
612         fclose(fp);
613
614         table->flags = 0;
615
616         return table;
617
618 }
619
620
621 movielib *init_old_movie_lib(char *filename,FILE *fp)
622 {
623         int nfiles,size;
624         int i;
625         movielib *table,*table2;
626
627         nfiles = 0;
628
629         //allocate big table
630         table = d_malloc(sizeof(*table) + sizeof(ml_entry)*MAX_MOVIES_PER_LIB);
631
632         while( 1 ) {
633                 int len;
634
635                 i = fread( table->movies[nfiles].name, 13, 1, fp );
636                 if ( i != 1 )
637                         break;          //end of file (probably)
638
639                 i = fread( &len, 4, 1, fp );
640                 if ( i != 1 )
641                         Error("error reading movie library <%s>",filename);
642
643                 table->movies[nfiles].len = INTEL_INT(len);
644                 table->movies[nfiles].offset = ftell( fp );
645
646                 fseek( fp, INTEL_INT(len), SEEK_CUR );          //skip data
647
648                 nfiles++;
649         }
650
651         //allocate correct-sized table
652         size = sizeof(*table) + sizeof(ml_entry)*nfiles;
653         table2 = d_malloc(size);
654         memcpy(table2,table,size);
655         d_free(table);
656         table = table2;
657
658         strcpy(table->name,filename);
659
660         table->n_movies = nfiles;
661
662         fclose(fp);
663
664         table->flags = 0;
665
666         return table;
667
668 }
669
670
671 //find the specified movie library, and read in list of movies in it
672 movielib *init_movie_lib(char *filename)
673 {
674         //note: this based on cfile_init_hogfile()
675
676         char id[4];
677         FILE * fp;
678
679         fp = fopen( filename, "rb" );
680
681         if ((fp == NULL) && (AltHogdir_initialized)) {
682                 char temp[128];
683                 strcpy(temp, AltHogDir);
684                 strcat(temp, "/");
685                 strcat(temp, filename);
686                 fp = fopen(temp, "rb");
687         }
688
689         if ( fp == NULL )
690                 return NULL;
691
692         fread( id, 4, 1, fp );
693         if ( !strncmp( id, "DMVL", 4 ) )
694                 return init_new_movie_lib(filename,fp);
695         else if ( !strncmp( id, "DHF", 3 ) ) {
696                 fseek(fp,-1,SEEK_CUR);          //old file had 3 char id
697                 return init_old_movie_lib(filename,fp);
698         }
699         else {
700                 fclose(fp);
701                 return NULL;
702         }
703 }
704
705
706 void close_movie(int i)
707 {
708         if (movie_libs[i]) {
709                 d_free(movie_libs[i]->movies);
710                 d_free(movie_libs[i]);
711         }
712 }
713
714
715 void close_movies()
716 {
717         int i;
718
719         for (i=0;i<N_MOVIE_LIBS;i++)
720                 close_movie(i);
721 }
722
723
724 void init_movie(char *filename,int libnum,int is_robots,int required)
725 {
726         int high_res;
727         int try = 0;
728
729 #ifndef RELEASE
730         if (FindArg("-nomovies")) {
731                 movie_libs[libnum] = NULL;
732                 return;
733         }
734 #endif
735
736         //for robots, load highres versions if highres menus set
737         if (is_robots)
738                 high_res = MenuHiresAvailable;
739         else
740                 high_res = MovieHires;
741
742         if (high_res)
743                 strchr(filename,'.')[-1] = 'h';
744
745 try_again:;
746
747         if ((movie_libs[libnum] = init_movie_lib(filename)) == NULL) {
748                 char name2[100];
749
750                 strcpy(name2,CDROM_dir);
751                 strcat(name2,filename);
752                 movie_libs[libnum] = init_movie_lib(name2);
753
754                 if (movie_libs[libnum] != NULL)
755                         movie_libs[libnum]->flags |= MLF_ON_CD;
756                 else {
757                         if (required) {
758                                 Warning("Cannot open movie file <%s>\n",filename);
759                         }
760
761                         if (!try) {                                         // first try
762                                 if (strchr(filename, '.')[-1] == 'h') {         // try again with lowres
763                                         strchr(filename, '.')[-1] = 'l';
764                                         Warning("Trying to open movie file <%s> instead\n", filename);
765                                         try++;
766                                         goto try_again;
767                                 } else if (strchr(filename, '.')[-1] == 'l') {  // try again with highres
768                                         strchr(filename, '.')[-1] = 'h';
769                                         Warning("Trying to open movie file <%s> instead\n", filename);
770                                         try++;
771                                         goto try_again;
772                                 }
773                         }
774                 }
775         }
776
777         if (is_robots && movie_libs[libnum]!=NULL)
778                 robot_movies = high_res?2:1;
779 }
780
781
782 //find and initialize the movie libraries
783 void init_movies()
784 {
785         int i;
786         int is_robots;
787
788         for (i=0;i<N_BUILTIN_MOVIE_LIBS;i++) {
789
790                 if (!strnicmp(movielib_files[i],"robot",5))
791                         is_robots = 1;
792                 else
793                         is_robots = 0;
794
795                 init_movie(movielib_files[i],i,is_robots,1);
796         }
797
798         movie_libs[EXTRA_ROBOT_LIB] = NULL;
799
800         atexit(close_movies);
801 }
802
803
804 void init_extra_robot_movie(char *filename)
805 {
806         close_movie(EXTRA_ROBOT_LIB);
807         init_movie(filename,EXTRA_ROBOT_LIB,1,0);
808 }
809
810
811 //looks through a movie library for a movie file
812 //returns filehandle, with fileposition at movie, or -1 if can't find
813 int search_movie_lib(movielib *lib,char *filename,int must_have)
814 {
815         int i;
816         int filehandle;
817
818         if (lib == NULL)
819                 return -1;
820
821         for (i=0;i<lib->n_movies;i++)
822                 if (!stricmp(filename,lib->movies[i].name)) {   //found the movie in a library 
823                         int from_cd;
824
825                         from_cd = (lib->flags & MLF_ON_CD);
826
827                         if (from_cd)
828                                 songs_stop_redbook();           //ready to read from CD
829
830                         do {            //keep trying until we get the file handle
831
832                                 /* movie_handle = */ filehandle = open(lib->name, O_RDONLY);
833
834                                 if ((filehandle == -1) && (AltHogdir_initialized)) {
835                                         char temp[128];
836                                         strcpy(temp, AltHogDir);
837                                         strcat(temp, "/");
838                                         strcat(temp, lib->name);
839                                         filehandle = open(temp, O_RDONLY);
840                                 }
841
842                                 if (must_have && from_cd && filehandle == -1) {         //didn't get file!
843
844                                         if (request_cd() == -1)         //ESC from requester
845                                                 break;                                          //bail from here. will get error later
846                                 }
847
848                         } while (must_have && from_cd && filehandle == -1);
849
850                         if (filehandle != -1)
851                                 lseek(filehandle,(/* movie_start = */ lib->movies[i].offset),SEEK_SET);
852
853                         return filehandle;
854                 }
855
856         return -1;
857 }
858
859
860 //returns file handle
861 int open_movie_file(char *filename,int must_have)
862 {
863         int filehandle,i;
864
865         for (i=0;i<N_MOVIE_LIBS;i++) {
866                 if ((filehandle = search_movie_lib(movie_libs[i],filename,must_have)) != -1)
867                         return filehandle;
868         }
869
870         return -1;              //couldn't find it
871 }
872
873