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