1 /* $Id: movie.c,v 1.18 2003-02-18 07:37:19 btb Exp $ */
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.
20 static char rcsid[] = "$Id: movie.c,v 1.18 2003-02-18 07:37:19 btb Exp $";
23 #define DEBUG_LEVEL CON_NORMAL
26 #include <sys/types.h>
52 extern int MenuHiresAvailable;
53 extern char CDROM_dir[];
63 short first_frame,last_frame;
67 #define MAX_SUBTITLES 500
68 #define MAX_ACTIVE_SUBTITLES 3
69 subtitle Subtitles[MAX_SUBTITLES];
75 char name[FILENAME_LEN];
80 #define MAX_MOVIES_PER_LIB 50 //determines size of malloc
83 char name[100]; //[FILENAME_LEN];
90 char movielib_files[][FILENAME_LEN] = {"intro-l.mvl","other-l.mvl","robots-l.mvl","oem-l.mvl"};
92 char movielib_files[][FILENAME_LEN] = {"intro-l.mvl","other-l.mvl","robots-l.mvl"};
95 #define N_BUILTIN_MOVIE_LIBS (sizeof(movielib_files)/sizeof(*movielib_files))
96 #define N_MOVIE_LIBS (N_BUILTIN_MOVIE_LIBS+1)
97 #define EXTRA_ROBOT_LIB N_BUILTIN_MOVIE_LIBS
98 movielib *movie_libs[N_MOVIE_LIBS];
101 //do we have the robot movies available
102 int robot_movies = 0; //0 means none, 1 means lowres, 2 means hires
104 int MovieHires = 0; //default for now is lores
106 int RoboFile = 0, RoboFilePos = 0;
108 // Function Prototypes
109 int RunMovie(char *filename, int highres_flag, int allow_abort,int dx,int dy);
111 int open_movie_file(char *filename,int must_have);
112 int reset_movie_file(int handle);
114 void change_filename_ext( char *dest, char *src, char *ext );
115 void decode_text_line(char *p);
116 void draw_subtitles(int frame_num);
119 //-----------------------------------------------------------------------
122 //filename will actually get modified to be either low-res or high-res
123 //returns status. see values in movie.h
124 int PlayMovie(const char *filename, int must_have)
126 char name[FILENAME_LEN],*p;
130 if (FindArg("-nomovies"))
131 return MOVIE_NOT_PLAYED;
134 strcpy(name,filename);
136 if ((p=strchr(name,'.')) == NULL) //add extension, if missing
139 //check for escape already pressed & abort if so
140 while ((c=key_inkey()) != 0)
142 return MOVIE_ABORTED;
144 // Stop all digital sounds currently playing.
152 ret = RunMovie(name,MovieHires,must_have,-1,-1);
154 if (!FindArg("-nosound"))
157 Screen_mode = -1; //force screen reset
165 short x, y, w, h; // The location of the menu.
166 grs_bitmap * bmp; // The background under the menu.
169 bkg movie_bg = {0,0,0,0,NULL};
172 #define BOX_BORDER (MenuHires?40:20)
175 void show_pause_message(char *msg)
180 gr_set_current_canvas(NULL);
181 gr_set_curfont( SMALL_FONT );
183 gr_get_string_size(msg,&w,&h,&aw);
185 x = (grd_curscreen->sc_w-w)/2;
186 y = (grd_curscreen->sc_h-h)/2;
190 gr_free_bitmap(movie_bg.bmp);
194 // Save the background of the display
195 movie_bg.x=x; movie_bg.y=y; movie_bg.w=w; movie_bg.h=h;
197 movie_bg.bmp = gr_create_bitmap( w+BOX_BORDER, h+BOX_BORDER );
199 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 );
203 gr_rect(x-BOX_BORDER/2,y-BOX_BORDER/2,x+w+BOX_BORDER/2-1,y+h+BOX_BORDER/2-1);
205 gr_set_fontcolor( 255, -1 );
207 gr_ustring( 0x8000, y, msg );
212 void clear_pause_message()
217 gr_bitmap(movie_bg.x-BOX_BORDER/2, movie_bg.y-BOX_BORDER/2, movie_bg.bmp);
219 gr_free_bitmap(movie_bg.bmp);
226 //returns status. see movie.h
227 int RunMovie(char *filename, int hires_flag, int must_have,int dx,int dy)
230 int result=1,aborted=0;
237 // Open Movie file. If it doesn't exist, no movie, just return.
239 filehndl = open_movie_file(filename,must_have);
241 if (filehndl == -1) {
243 Warning("movie: RunMovie: Cannot open movie <%s>\n",filename);
244 return MOVIE_NOT_PLAYED;
248 gr_set_mode(SM(640,480));
250 gr_set_mode(SM(320,200));
253 if (MVE_rmPrepMovie(filehndl, dx, dy, track)) {
255 return MOVIE_NOT_PLAYED;
260 FontHires = FontHiresAvailable && hires_flag;
262 while((result = MVE_rmStepMovie()) == 0) {
264 draw_subtitles(frame_num);
270 // If ESCAPE pressed, then quit movie.
271 if (key == KEY_ESC) {
272 result = aborted = 1;
276 // If PAUSE pressed, then pause movie
277 if (key == KEY_PAUSE) {
279 show_pause_message(TXT_PAUSE);
280 while (!key_inkey()) ;
281 clear_pause_message();
284 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
285 if ((key == KEY_CTRLED+KEY_SHIFTED+KEY_PADENTER) ||
286 (key == KEY_ALTED+KEY_CTRLED+KEY_PADENTER) ||
287 (key == KEY_ALTED+KEY_SHIFTED+KEY_PADENTER))
288 gr_toggle_fullscreen();
294 Assert(aborted || result == MVE_ERR_EOF); ///movie should be over
298 close(filehndl); // Close Movie File
300 // Restore old graphic state
302 Screen_mode=-1; //force reset of screen mode
304 return (aborted?MOVIE_ABORTED:MOVIE_PLAYED_FULL);
308 int InitMovieBriefing()
311 gr_set_mode(SM(640,480));
313 gr_set_mode(SM(320,200));
319 //returns 1 if frame updated ok
324 err = MVE_rmStepMovie();
326 if (err == MVE_ERR_EOF) //end of movie, so reset
328 reset_movie_file(RoboFile);
329 if (MVE_rmPrepMovie(RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
343 void DeInitRobotMovie(void)
346 close(RoboFile); // Close Movie File
350 int InitRobotMovie(char *filename)
352 if (FindArg("-nomovies"))
355 con_printf(DEBUG_LEVEL, "RoboFile=%s\n", filename);
357 RoboFile = open_movie_file(filename, 1);
359 if (RoboFile == -1) {
360 Warning("movie: InitRobotMovie: Cannot open movie file <%s>",filename);
361 return MOVIE_NOT_PLAYED;
364 Vid_State = VID_PLAY;
366 if (MVE_rmPrepMovie(RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
371 RoboFilePos=lseek (RoboFile,0L,SEEK_CUR);
373 con_printf(DEBUG_LEVEL, "RoboFilePos=%d!\n", RoboFilePos);
380 * Subtitle system code
383 ubyte *subtitle_raw_data;
386 //search for next field following whitespace
387 ubyte *next_field(ubyte *p)
389 while (*p && !isspace(*p))
395 while (*p && isspace(*p))
405 int init_subtitles(char *filename)
414 if (! FindArg("-subtitles"))
417 ifile = cfopen(filename,"rb"); //try text version
419 if (!ifile) { //no text version, try binary version
420 char filename2[FILENAME_LEN];
421 change_filename_ext(filename2,filename,".TXB");
422 ifile = cfopen(filename2,"rb");
428 size = cfilelength(ifile);
430 MALLOC (subtitle_raw_data, ubyte, size+1);
432 read_count = cfread(subtitle_raw_data, 1, size, ifile);
436 subtitle_raw_data[size] = 0;
438 if (read_count != size) {
439 d_free(subtitle_raw_data);
443 p = subtitle_raw_data;
445 while (p && p < subtitle_raw_data+size) {
448 endp = strchr(p,'\n');
450 if (endp[-1] == '\r')
451 endp[-1] = 0; //handle 0d0a pair
452 *endp = 0; //string termintor
459 Subtitles[Num_subtitles].first_frame = atoi(p);
460 p = next_field(p); if (!p) continue;
461 Subtitles[Num_subtitles].last_frame = atoi(p);
462 p = next_field(p); if (!p) continue;
463 Subtitles[Num_subtitles].msg = p;
465 Assert(Num_subtitles==0 || Subtitles[Num_subtitles].first_frame >= Subtitles[Num_subtitles-1].first_frame);
466 Assert(Subtitles[Num_subtitles].last_frame >= Subtitles[Num_subtitles].first_frame);
479 void close_subtitles()
481 if (subtitle_raw_data)
482 d_free(subtitle_raw_data);
483 subtitle_raw_data = NULL;
488 //draw the subtitles for this frame
489 void draw_subtitles(int frame_num)
491 static int active_subtitles[MAX_ACTIVE_SUBTITLES];
492 static int num_active_subtitles,next_subtitle,line_spacing;
496 if (frame_num == 0) {
497 num_active_subtitles = 0;
499 gr_set_curfont( GAME_FONT );
500 line_spacing = grd_curcanv->cv_font->ft_h + (grd_curcanv->cv_font->ft_h >> 2);
501 gr_set_fontcolor(255,-1);
504 //get rid of any subtitles that have expired
505 for (t=0;t<num_active_subtitles;)
506 if (frame_num > Subtitles[active_subtitles[t]].last_frame) {
508 for (t2=t;t2<num_active_subtitles-1;t2++)
509 active_subtitles[t2] = active_subtitles[t2+1];
510 num_active_subtitles--;
516 //get any subtitles new for this frame
517 while (next_subtitle < Num_subtitles && frame_num >= Subtitles[next_subtitle].first_frame) {
518 if (num_active_subtitles >= MAX_ACTIVE_SUBTITLES)
519 Error("Too many active subtitles!");
520 active_subtitles[num_active_subtitles++] = next_subtitle;
524 //find y coordinate for first line of subtitles
525 y = grd_curcanv->cv_bitmap.bm_h-((line_spacing+1)*MAX_ACTIVE_SUBTITLES+2);
527 //erase old subtitles if necessary
530 gr_rect(0,y,grd_curcanv->cv_bitmap.bm_w-1,grd_curcanv->cv_bitmap.bm_h-1);
533 //now draw the current subtitles
534 for (t=0;t<num_active_subtitles;t++)
535 if (active_subtitles[t] != -1) {
536 gr_string(0x8000,y,Subtitles[active_subtitles[t]].msg);
542 movielib *init_new_movie_lib(char *filename,FILE *fp)
548 //read movie file header
550 nfiles = file_read_int(fp); //get number of files
552 //table = d_malloc(sizeof(*table) + sizeof(ml_entry)*nfiles);
553 MALLOC(table, movielib, 1);
554 MALLOC(table->movies, ml_entry, nfiles);
556 strcpy(table->name,filename);
557 table->n_movies = nfiles;
559 offset = 4+4+nfiles*(13+4); //id + nfiles + nfiles * (filename + size)
561 for (i=0;i<nfiles;i++) {
564 n = fread( table->movies[i].name, 13, 1, fp );
566 break; //end of file (probably)
568 len = file_read_int(fp);
570 table->movies[i].len = len;
571 table->movies[i].offset = offset;
573 offset += table->movies[i].len;
586 movielib *init_old_movie_lib(char *filename,FILE *fp)
590 movielib *table,*table2;
595 table = d_malloc(sizeof(*table) + sizeof(ml_entry)*MAX_MOVIES_PER_LIB);
600 i = fread( table->movies[nfiles].name, 13, 1, fp );
602 break; //end of file (probably)
604 i = fread( &len, 4, 1, fp );
606 Error("error reading movie library <%s>",filename);
608 table->movies[nfiles].len = INTEL_INT(len);
609 table->movies[nfiles].offset = ftell( fp );
611 fseek( fp, INTEL_INT(len), SEEK_CUR ); //skip data
616 //allocate correct-sized table
617 size = sizeof(*table) + sizeof(ml_entry)*nfiles;
618 table2 = d_malloc(size);
619 memcpy(table2,table,size);
623 strcpy(table->name,filename);
625 table->n_movies = nfiles;
636 //find the specified movie library, and read in list of movies in it
637 movielib *init_movie_lib(char *filename)
639 //note: this based on cfile_init_hogfile()
644 fp = fopen( filename, "rb" );
646 if ((fp == NULL) && (AltHogdir_initialized)) {
648 strcpy(temp, AltHogDir);
650 strcat(temp, filename);
651 fp = fopen(temp, "rb");
657 fread( id, 4, 1, fp );
658 if ( !strncmp( id, "DMVL", 4 ) )
659 return init_new_movie_lib(filename,fp);
660 else if ( !strncmp( id, "DHF", 3 ) ) {
661 fseek(fp,-1,SEEK_CUR); //old file had 3 char id
662 return init_old_movie_lib(filename,fp);
671 void close_movie(int i)
674 d_free(movie_libs[i]->movies);
675 d_free(movie_libs[i]);
684 for (i=0;i<N_MOVIE_LIBS;i++)
689 //ask user to put the D2 CD in.
690 //returns -1 if ESC pressed, 0 if OK chosen
691 //CD may not have been inserted
695 ubyte save_pal[256*3];
696 grs_canvas *save_canv,*tcanv;
697 int ret,was_faded=gr_palette_faded_out;
701 save_canv = grd_curcanv;
702 tcanv = gr_create_canvas(grd_curcanv->cv_w,grd_curcanv->cv_h);
704 gr_set_current_canvas(tcanv);
705 gr_ubitmap(0,0,&save_canv->cv_bitmap);
706 gr_set_current_canvas(save_canv);
708 gr_clear_canvas(BM_XRGB(0,0,0));
710 memcpy(save_pal,gr_palette,sizeof(save_pal));
712 memcpy(gr_palette,last_palette_for_color_fonts,sizeof(gr_palette));
716 ret = nm_messagebox( "CD ERROR", 1, "Ok", "Please insert your Descent II CD");
721 ret2 = nm_messagebox( "CD ERROR", 2, "Try Again", "Leave Game", "You must insert your\nDescent II CD to Continue");
723 if (ret2 == -1 || ret2 == 0)
727 force_rb_register = 1; //disc has changed; force register new CD
731 memcpy(gr_palette,save_pal,sizeof(save_pal));
733 gr_ubitmap(0,0,&tcanv->cv_bitmap);
736 gr_palette_load(gr_palette);
738 gr_free_canvas(tcanv);
742 con_printf(DEBUG_LEVEL, "STUB: movie: request_cd\n");
748 void init_movie(char *filename,int libnum,int is_robots,int required)
754 if (FindArg("-nomovies")) {
755 movie_libs[libnum] = NULL;
760 //for robots, load highres versions if highres menus set
762 high_res = MenuHiresAvailable;
764 high_res = MovieHires;
767 strchr(filename,'.')[-1] = 'h';
771 if ((movie_libs[libnum] = init_movie_lib(filename)) == NULL) {
774 strcpy(name2,CDROM_dir);
775 strcat(name2,filename);
776 movie_libs[libnum] = init_movie_lib(name2);
778 if (movie_libs[libnum] != NULL)
779 movie_libs[libnum]->flags |= MLF_ON_CD;
782 Warning("Cannot open movie file <%s>\n",filename);
785 if (!try) { // first try
786 if (strchr(filename, '.')[-1] == 'h') { // try again with lowres
787 strchr(filename, '.')[-1] = 'l';
789 Warning("Trying to open movie file <%s> instead\n", filename);
792 } else if (strchr(filename, '.')[-1] == 'l') { // try again with highres
793 strchr(filename, '.')[-1] = 'h';
795 Warning("Trying to open movie file <%s> instead\n", filename);
803 if (is_robots && movie_libs[libnum]!=NULL)
804 robot_movies = high_res?2:1;
808 //find and initialize the movie libraries
814 for (i=0;i<N_BUILTIN_MOVIE_LIBS;i++) {
816 if (!strnicmp(movielib_files[i],"robot",5))
821 init_movie(movielib_files[i],i,is_robots,1);
824 movie_libs[EXTRA_ROBOT_LIB] = NULL;
826 atexit(close_movies);
830 void init_extra_robot_movie(char *filename)
832 close_movie(EXTRA_ROBOT_LIB);
833 init_movie(filename,EXTRA_ROBOT_LIB,1,0);
837 int movie_handle,movie_start;
839 //looks through a movie library for a movie file
840 //returns filehandle, with fileposition at movie, or -1 if can't find
841 int search_movie_lib(movielib *lib,char *filename,int must_have)
849 for (i=0;i<lib->n_movies;i++)
850 if (!stricmp(filename,lib->movies[i].name)) { //found the movie in a library
853 from_cd = (lib->flags & MLF_ON_CD);
856 songs_stop_redbook(); //ready to read from CD
858 do { //keep trying until we get the file handle
861 movie_handle = filehandle = open(lib->name, O_RDONLY | O_BINARY);
863 movie_handle = filehandle = open(lib->name, O_RDONLY);
866 if ((filehandle == -1) && (AltHogdir_initialized)) {
868 strcpy(temp, AltHogDir);
870 strcat(temp, lib->name);
872 movie_handle = filehandle = open(temp, O_RDONLY | O_BINARY);
874 movie_handle = filehandle = open(temp, O_RDONLY);
878 if (must_have && from_cd && filehandle == -1) { //didn't get file!
880 if (request_cd() == -1) //ESC from requester
881 break; //bail from here. will get error later
884 } while (must_have && from_cd && filehandle == -1);
886 if (filehandle != -1)
887 lseek(filehandle,(movie_start=lib->movies[i].offset),SEEK_SET);
896 //returns file handle
897 int open_movie_file(char *filename,int must_have)
901 for (i=0;i<N_MOVIE_LIBS;i++) {
902 if ((filehandle = search_movie_lib(movie_libs[i],filename,must_have)) != -1)
906 return -1; //couldn't find it
909 //sets the file position to the start of this already-open file
910 int reset_movie_file(int handle)
912 Assert(handle == movie_handle);
914 lseek(handle,movie_start,SEEK_SET);
916 return 0; //everything is cool