1 /* $Id: movie.c,v 1.20 2003-02-24 07:03:47 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.20 2003-02-24 07:03:47 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.
153 if (!FindArg("-nosound"))
158 ret = RunMovie(name,MovieHires,must_have,-1,-1);
160 if (!FindArg("-nosound"))
163 Screen_mode = -1; //force screen reset
171 short x, y, w, h; // The location of the menu.
172 grs_bitmap * bmp; // The background under the menu.
175 bkg movie_bg = {0,0,0,0,NULL};
178 #define BOX_BORDER (MenuHires?40:20)
181 void show_pause_message(char *msg)
186 gr_set_current_canvas(NULL);
187 gr_set_curfont( SMALL_FONT );
189 gr_get_string_size(msg,&w,&h,&aw);
191 x = (grd_curscreen->sc_w-w)/2;
192 y = (grd_curscreen->sc_h-h)/2;
196 gr_free_bitmap(movie_bg.bmp);
200 // Save the background of the display
201 movie_bg.x=x; movie_bg.y=y; movie_bg.w=w; movie_bg.h=h;
203 movie_bg.bmp = gr_create_bitmap( w+BOX_BORDER, h+BOX_BORDER );
205 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 );
209 gr_rect(x-BOX_BORDER/2,y-BOX_BORDER/2,x+w+BOX_BORDER/2-1,y+h+BOX_BORDER/2-1);
211 gr_set_fontcolor( 255, -1 );
213 gr_ustring( 0x8000, y, msg );
218 void clear_pause_message()
223 gr_bitmap(movie_bg.x-BOX_BORDER/2, movie_bg.y-BOX_BORDER/2, movie_bg.bmp);
225 gr_free_bitmap(movie_bg.bmp);
232 //returns status. see movie.h
233 int RunMovie(char *filename, int hires_flag, int must_have,int dx,int dy)
236 int result=1,aborted=0;
243 // Open Movie file. If it doesn't exist, no movie, just return.
245 filehndl = open_movie_file(filename,must_have);
247 if (filehndl == -1) {
249 Warning("movie: RunMovie: Cannot open movie <%s>\n",filename);
250 return MOVIE_NOT_PLAYED;
254 gr_set_mode(SM(640,480));
256 gr_set_mode(SM(320,200));
259 if (MVE_rmPrepMovie(filehndl, dx, dy, track)) {
261 return MOVIE_NOT_PLAYED;
266 FontHires = FontHiresAvailable && hires_flag;
268 while((result = MVE_rmStepMovie()) == 0) {
270 draw_subtitles(frame_num);
276 // If ESCAPE pressed, then quit movie.
277 if (key == KEY_ESC) {
278 result = aborted = 1;
282 // If PAUSE pressed, then pause movie
283 if (key == KEY_PAUSE) {
285 show_pause_message(TXT_PAUSE);
286 while (!key_inkey()) ;
287 clear_pause_message();
290 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
291 if ((key == KEY_CTRLED+KEY_SHIFTED+KEY_PADENTER) ||
292 (key == KEY_ALTED+KEY_CTRLED+KEY_PADENTER) ||
293 (key == KEY_ALTED+KEY_SHIFTED+KEY_PADENTER))
294 gr_toggle_fullscreen();
300 Assert(aborted || result == MVE_ERR_EOF); ///movie should be over
304 close(filehndl); // Close Movie File
306 // Restore old graphic state
308 Screen_mode=-1; //force reset of screen mode
310 return (aborted?MOVIE_ABORTED:MOVIE_PLAYED_FULL);
314 int InitMovieBriefing()
320 gr_set_mode(SM(640,480));
322 gr_set_mode(SM(320,200));
328 //returns 1 if frame updated ok
333 err = MVE_rmStepMovie();
335 if (err == MVE_ERR_EOF) //end of movie, so reset
337 reset_movie_file(RoboFile);
338 if (MVE_rmPrepMovie(RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
352 void DeInitRobotMovie(void)
355 close(RoboFile); // Close Movie File
359 int InitRobotMovie(char *filename)
361 if (FindArg("-nomovies"))
364 con_printf(DEBUG_LEVEL, "RoboFile=%s\n", filename);
366 MVE_sndInit(-1); //tell movies to play no sound for robots
368 RoboFile = open_movie_file(filename, 1);
370 if (RoboFile == -1) {
371 Warning("movie: InitRobotMovie: Cannot open movie file <%s>",filename);
372 return MOVIE_NOT_PLAYED;
375 Vid_State = VID_PLAY;
377 if (MVE_rmPrepMovie(RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
382 RoboFilePos=lseek (RoboFile,0L,SEEK_CUR);
384 con_printf(DEBUG_LEVEL, "RoboFilePos=%d!\n", RoboFilePos);
391 * Subtitle system code
394 ubyte *subtitle_raw_data;
397 //search for next field following whitespace
398 ubyte *next_field(ubyte *p)
400 while (*p && !isspace(*p))
406 while (*p && isspace(*p))
416 int init_subtitles(char *filename)
425 if (! FindArg("-subtitles"))
428 ifile = cfopen(filename,"rb"); //try text version
430 if (!ifile) { //no text version, try binary version
431 char filename2[FILENAME_LEN];
432 change_filename_ext(filename2,filename,".TXB");
433 ifile = cfopen(filename2,"rb");
439 size = cfilelength(ifile);
441 MALLOC (subtitle_raw_data, ubyte, size+1);
443 read_count = cfread(subtitle_raw_data, 1, size, ifile);
447 subtitle_raw_data[size] = 0;
449 if (read_count != size) {
450 d_free(subtitle_raw_data);
454 p = subtitle_raw_data;
456 while (p && p < subtitle_raw_data+size) {
459 endp = strchr(p,'\n');
461 if (endp[-1] == '\r')
462 endp[-1] = 0; //handle 0d0a pair
463 *endp = 0; //string termintor
470 Subtitles[Num_subtitles].first_frame = atoi(p);
471 p = next_field(p); if (!p) continue;
472 Subtitles[Num_subtitles].last_frame = atoi(p);
473 p = next_field(p); if (!p) continue;
474 Subtitles[Num_subtitles].msg = p;
476 Assert(Num_subtitles==0 || Subtitles[Num_subtitles].first_frame >= Subtitles[Num_subtitles-1].first_frame);
477 Assert(Subtitles[Num_subtitles].last_frame >= Subtitles[Num_subtitles].first_frame);
490 void close_subtitles()
492 if (subtitle_raw_data)
493 d_free(subtitle_raw_data);
494 subtitle_raw_data = NULL;
499 //draw the subtitles for this frame
500 void draw_subtitles(int frame_num)
502 static int active_subtitles[MAX_ACTIVE_SUBTITLES];
503 static int num_active_subtitles,next_subtitle,line_spacing;
507 if (frame_num == 0) {
508 num_active_subtitles = 0;
510 gr_set_curfont( GAME_FONT );
511 line_spacing = grd_curcanv->cv_font->ft_h + (grd_curcanv->cv_font->ft_h >> 2);
512 gr_set_fontcolor(255,-1);
515 //get rid of any subtitles that have expired
516 for (t=0;t<num_active_subtitles;)
517 if (frame_num > Subtitles[active_subtitles[t]].last_frame) {
519 for (t2=t;t2<num_active_subtitles-1;t2++)
520 active_subtitles[t2] = active_subtitles[t2+1];
521 num_active_subtitles--;
527 //get any subtitles new for this frame
528 while (next_subtitle < Num_subtitles && frame_num >= Subtitles[next_subtitle].first_frame) {
529 if (num_active_subtitles >= MAX_ACTIVE_SUBTITLES)
530 Error("Too many active subtitles!");
531 active_subtitles[num_active_subtitles++] = next_subtitle;
535 //find y coordinate for first line of subtitles
536 y = grd_curcanv->cv_bitmap.bm_h-((line_spacing+1)*MAX_ACTIVE_SUBTITLES+2);
538 //erase old subtitles if necessary
541 gr_rect(0,y,grd_curcanv->cv_bitmap.bm_w-1,grd_curcanv->cv_bitmap.bm_h-1);
544 //now draw the current subtitles
545 for (t=0;t<num_active_subtitles;t++)
546 if (active_subtitles[t] != -1) {
547 gr_string(0x8000,y,Subtitles[active_subtitles[t]].msg);
553 movielib *init_new_movie_lib(char *filename,FILE *fp)
559 //read movie file header
561 nfiles = file_read_int(fp); //get number of files
563 //table = d_malloc(sizeof(*table) + sizeof(ml_entry)*nfiles);
564 MALLOC(table, movielib, 1);
565 MALLOC(table->movies, ml_entry, nfiles);
567 strcpy(table->name,filename);
568 table->n_movies = nfiles;
570 offset = 4+4+nfiles*(13+4); //id + nfiles + nfiles * (filename + size)
572 for (i=0;i<nfiles;i++) {
575 n = fread( table->movies[i].name, 13, 1, fp );
577 break; //end of file (probably)
579 len = file_read_int(fp);
581 table->movies[i].len = len;
582 table->movies[i].offset = offset;
584 offset += table->movies[i].len;
597 movielib *init_old_movie_lib(char *filename,FILE *fp)
601 movielib *table,*table2;
606 table = d_malloc(sizeof(*table) + sizeof(ml_entry)*MAX_MOVIES_PER_LIB);
611 i = fread( table->movies[nfiles].name, 13, 1, fp );
613 break; //end of file (probably)
615 i = fread( &len, 4, 1, fp );
617 Error("error reading movie library <%s>",filename);
619 table->movies[nfiles].len = INTEL_INT(len);
620 table->movies[nfiles].offset = ftell( fp );
622 fseek( fp, INTEL_INT(len), SEEK_CUR ); //skip data
627 //allocate correct-sized table
628 size = sizeof(*table) + sizeof(ml_entry)*nfiles;
629 table2 = d_malloc(size);
630 memcpy(table2,table,size);
634 strcpy(table->name,filename);
636 table->n_movies = nfiles;
647 //find the specified movie library, and read in list of movies in it
648 movielib *init_movie_lib(char *filename)
650 //note: this based on cfile_init_hogfile()
655 fp = fopen( filename, "rb" );
657 if ((fp == NULL) && (AltHogdir_initialized)) {
659 strcpy(temp, AltHogDir);
661 strcat(temp, filename);
662 fp = fopen(temp, "rb");
668 fread( id, 4, 1, fp );
669 if ( !strncmp( id, "DMVL", 4 ) )
670 return init_new_movie_lib(filename,fp);
671 else if ( !strncmp( id, "DHF", 3 ) ) {
672 fseek(fp,-1,SEEK_CUR); //old file had 3 char id
673 return init_old_movie_lib(filename,fp);
682 void close_movie(int i)
685 d_free(movie_libs[i]->movies);
686 d_free(movie_libs[i]);
695 for (i=0;i<N_MOVIE_LIBS;i++)
700 //ask user to put the D2 CD in.
701 //returns -1 if ESC pressed, 0 if OK chosen
702 //CD may not have been inserted
706 ubyte save_pal[256*3];
707 grs_canvas *save_canv,*tcanv;
708 int ret,was_faded=gr_palette_faded_out;
712 save_canv = grd_curcanv;
713 tcanv = gr_create_canvas(grd_curcanv->cv_w,grd_curcanv->cv_h);
715 gr_set_current_canvas(tcanv);
716 gr_ubitmap(0,0,&save_canv->cv_bitmap);
717 gr_set_current_canvas(save_canv);
719 gr_clear_canvas(BM_XRGB(0,0,0));
721 memcpy(save_pal,gr_palette,sizeof(save_pal));
723 memcpy(gr_palette,last_palette_for_color_fonts,sizeof(gr_palette));
727 ret = nm_messagebox( "CD ERROR", 1, "Ok", "Please insert your Descent II CD");
732 ret2 = nm_messagebox( "CD ERROR", 2, "Try Again", "Leave Game", "You must insert your\nDescent II CD to Continue");
734 if (ret2 == -1 || ret2 == 0)
738 force_rb_register = 1; //disc has changed; force register new CD
742 memcpy(gr_palette,save_pal,sizeof(save_pal));
744 gr_ubitmap(0,0,&tcanv->cv_bitmap);
747 gr_palette_load(gr_palette);
749 gr_free_canvas(tcanv);
753 con_printf(DEBUG_LEVEL, "STUB: movie: request_cd\n");
759 void init_movie(char *filename,int libnum,int is_robots,int required)
765 if (FindArg("-nomovies")) {
766 movie_libs[libnum] = NULL;
771 //for robots, load highres versions if highres menus set
773 high_res = MenuHiresAvailable;
775 high_res = MovieHires;
778 strchr(filename,'.')[-1] = 'h';
782 if ((movie_libs[libnum] = init_movie_lib(filename)) == NULL) {
785 strcpy(name2,CDROM_dir);
786 strcat(name2,filename);
787 movie_libs[libnum] = init_movie_lib(name2);
789 if (movie_libs[libnum] != NULL)
790 movie_libs[libnum]->flags |= MLF_ON_CD;
793 Warning("Cannot open movie file <%s>\n",filename);
796 if (!try) { // first try
797 if (strchr(filename, '.')[-1] == 'h') { // try again with lowres
798 strchr(filename, '.')[-1] = 'l';
800 Warning("Trying to open movie file <%s> instead\n", filename);
803 } else if (strchr(filename, '.')[-1] == 'l') { // try again with highres
804 strchr(filename, '.')[-1] = 'h';
806 Warning("Trying to open movie file <%s> instead\n", filename);
814 if (is_robots && movie_libs[libnum]!=NULL)
815 robot_movies = high_res?2:1;
819 //find and initialize the movie libraries
825 for (i=0;i<N_BUILTIN_MOVIE_LIBS;i++) {
827 if (!strnicmp(movielib_files[i],"robot",5))
832 init_movie(movielib_files[i],i,is_robots,1);
835 movie_libs[EXTRA_ROBOT_LIB] = NULL;
837 atexit(close_movies);
841 void init_extra_robot_movie(char *filename)
843 close_movie(EXTRA_ROBOT_LIB);
844 init_movie(filename,EXTRA_ROBOT_LIB,1,0);
848 int movie_handle,movie_start;
850 //looks through a movie library for a movie file
851 //returns filehandle, with fileposition at movie, or -1 if can't find
852 int search_movie_lib(movielib *lib,char *filename,int must_have)
860 for (i=0;i<lib->n_movies;i++)
861 if (!stricmp(filename,lib->movies[i].name)) { //found the movie in a library
864 from_cd = (lib->flags & MLF_ON_CD);
867 songs_stop_redbook(); //ready to read from CD
869 do { //keep trying until we get the file handle
872 movie_handle = filehandle = open(lib->name, O_RDONLY | O_BINARY);
874 movie_handle = filehandle = open(lib->name, O_RDONLY);
877 if ((filehandle == -1) && (AltHogdir_initialized)) {
879 strcpy(temp, AltHogDir);
881 strcat(temp, lib->name);
883 movie_handle = filehandle = open(temp, O_RDONLY | O_BINARY);
885 movie_handle = filehandle = open(temp, O_RDONLY);
889 if (must_have && from_cd && filehandle == -1) { //didn't get file!
891 if (request_cd() == -1) //ESC from requester
892 break; //bail from here. will get error later
895 } while (must_have && from_cd && filehandle == -1);
897 if (filehandle != -1)
898 lseek(filehandle,(movie_start=lib->movies[i].offset),SEEK_SET);
907 //returns file handle
908 int open_movie_file(char *filename,int must_have)
912 for (i=0;i<N_MOVIE_LIBS;i++) {
913 if ((filehandle = search_movie_lib(movie_libs[i],filename,must_have)) != -1)
917 return -1; //couldn't find it
920 //sets the file position to the start of this already-open file
921 int reset_movie_file(int handle)
923 Assert(handle == movie_handle);
925 lseek(handle,movie_start,SEEK_SET);
927 return 0; //everything is cool