1 /* $Id: movie.c,v 1.15 2003-02-12 08:58:38 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.15 2003-02-12 08:58:38 btb Exp $";
23 #define DEBUG_LEVEL CON_NORMAL
26 #include <sys/types.h>
53 extern int MenuHiresAvailable;
54 extern char CDROM_dir[];
64 short first_frame,last_frame;
68 #define MAX_SUBTITLES 500
69 #define MAX_ACTIVE_SUBTITLES 3
70 subtitle Subtitles[MAX_SUBTITLES];
75 char name[FILENAME_LEN];
80 #define MAX_MOVIES_PER_LIB 50 //determines size of malloc
84 char name[100]; //[FILENAME_LEN];
91 char movielib_files[][FILENAME_LEN] = {"intro-l.mvl","other-l.mvl","robots-l.mvl","oem-l.mvl"};
93 char movielib_files[][FILENAME_LEN] = {"intro-l.mvl","other-l.mvl","robots-l.mvl"};
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];
101 int MVEPaletteCalls = 0;
103 //do we have the robot movies available
104 int robot_movies = 0; //0 means none, 1 means lowres, 2 means hires
106 int MovieHires = 0; //default for now is lores
111 grs_bitmap *Robo_bitmap;
114 // Function Prototypes
115 int RunMovie(char *filename, int highres_flag, int allow_abort,int dx,int dy);
117 int open_movie_file(char *filename,int must_have);
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);
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)
128 char name[FILENAME_LEN],*p;
131 int save_sample_rate;
135 if (FindArg("-nomovies"))
136 return MOVIE_NOT_PLAYED;
139 strcpy(name,filename);
141 if ((p=strchr(name,'.')) == NULL) //add extension, if missing
144 //check for escape already pressed & abort if so
145 while ((c=key_inkey()) != 0)
147 return MOVIE_ABORTED;
149 // Stop all digital sounds currently playing.
156 save_sample_rate = digi_sample_rate;
157 digi_sample_rate = SAMPLE_RATE_22K; //always 22K for movies
158 digi_reset(); digi_reset();
163 ret = RunMovie(name,MovieHires,must_have,-1,-1);
166 gr_palette_clear(); //clear out palette in case movie aborted
170 digi_sample_rate = save_sample_rate; //restore rate for game
171 digi_reset(); digi_reset();
176 Screen_mode = -1; //force screen reset
183 short x, y, w, h; // The location of the menu.
184 grs_bitmap * bmp; // The background under the menu.
187 bkg movie_bg = {0,0,0,0,NULL};
190 #define BOX_BORDER (MenuHires?40:20)
192 void show_pause_message(char *msg)
197 gr_set_current_canvas(NULL);
198 gr_set_curfont( SMALL_FONT );
200 gr_get_string_size(msg,&w,&h,&aw);
202 x = (grd_curscreen->sc_w-w)/2;
203 y = (grd_curscreen->sc_h-h)/2;
207 gr_free_bitmap(movie_bg.bmp);
211 // Save the background of the display
212 movie_bg.x=x; movie_bg.y=y; movie_bg.w=w; movie_bg.h=h;
214 movie_bg.bmp = gr_create_bitmap( w+BOX_BORDER, h+BOX_BORDER );
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 );
220 gr_rect(x-BOX_BORDER/2,y-BOX_BORDER/2,x+w+BOX_BORDER/2-1,y+h+BOX_BORDER/2-1);
222 gr_set_fontcolor( 255, -1 );
224 gr_ustring( 0x8000, y, msg );
229 void clear_pause_message()
234 gr_bitmap(movie_bg.x-BOX_BORDER/2, movie_bg.y-BOX_BORDER/2, movie_bg.bmp);
236 gr_free_bitmap(movie_bg.bmp);
242 //returns status. see movie.h
243 int RunMovie(char *filename, int hires_flag, int must_have,int dx,int dy)
246 int result=1,aborted=0;
249 grs_bitmap *mve_bitmap;
255 // Open Movie file. If it doesn't exist, no movie, just return.
257 filehndl = open_movie_file(filename,must_have);
259 if (filehndl == -1) {
262 Warning("movie: RunMovie: Cannot open movie <%s>\n",filename);
264 return MOVIE_NOT_PLAYED;
268 gr_set_mode(SM(640,480));
274 gr_set_mode(SM(320,200));
283 FontHires = FontHiresAvailable && hires_flag;
285 mve = mve_open_filehandle(filehndl);
287 mve_bitmap = gr_create_bitmap(w, h); // w, h must match the mve exactly!
289 mveplay_initializeMovie(mve, mve_bitmap);
293 result = mveplay_stepMovie(mve);
295 gr_bitmap(x, y, mve_bitmap);
297 draw_subtitles(frame_num);
303 // If ESCAPE pressed, then quit movie.
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();
314 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
315 case KEY_CTRLED+KEY_SHIFTED+KEY_PADENTER:
316 case KEY_ALTED+KEY_CTRLED+KEY_PADENTER:
317 case KEY_ALTED+KEY_SHIFTED+KEY_PADENTER:
318 gr_toggle_fullscreen();
326 Assert(aborted || !result); ///movie should be over
328 mveplay_shutdownMovie(mve);
330 gr_free_bitmap(mve_bitmap);
334 close(filehndl); // Close Movie File
336 // Restore old graphic state
338 Screen_mode=-1; //force reset of screen mode
340 return (aborted?MOVIE_ABORTED:MOVIE_PLAYED_FULL);
344 int InitMovieBriefing()
347 gr_set_mode(SM(640,480));
349 gr_set_mode(SM(320,200));
355 //returns 1 if frame updated ok
360 ret = mveplay_stepMovie(Robo_mve);
361 gr_bitmap(MenuHires?280:140, MenuHires?200:80, Robo_bitmap);
364 mveplay_shutdownMovie(Robo_mve);
366 lseek(RoboFile, Robo_filepos, SEEK_SET);
367 Robo_mve = mve_open_filehandle(RoboFile);
368 mveplay_initializeMovie(Robo_mve, Robo_bitmap);
375 void DeInitRobotMovie(void)
377 mveplay_shutdownMovie(Robo_mve);
380 gr_free_bitmap(Robo_bitmap);
386 int InitRobotMovie(char *filename)
388 RoboFile = open_movie_file(filename, 1);
390 if (RoboFile == -1) {
391 Warning("movie: InitRobotMovie: Cannot open movie file <%s>",filename);
392 return MOVIE_NOT_PLAYED;
395 Robo_filepos = lseek(RoboFile, 0, SEEK_CUR);
397 Robo_mve = mve_open_filehandle(RoboFile);
399 Robo_bitmap = gr_create_bitmap(MenuHires?320:160, MenuHires?200:88);
401 mveplay_initializeMovie(Robo_mve, Robo_bitmap);
408 * Subtitle system code
411 ubyte *subtitle_raw_data;
414 //search for next field following whitespace
415 ubyte *next_field(ubyte *p)
417 while (*p && !isspace(*p))
423 while (*p && isspace(*p))
433 int init_subtitles(char *filename)
442 if (! FindArg("-subtitles"))
445 ifile = cfopen(filename,"rb"); //try text version
447 if (!ifile) { //no text version, try binary version
448 char filename2[FILENAME_LEN];
449 change_filename_ext(filename2,filename,".TXB");
450 ifile = cfopen(filename2,"rb");
456 size = cfilelength(ifile);
458 MALLOC (subtitle_raw_data, ubyte, size+1);
460 read_count = cfread(subtitle_raw_data, 1, size, ifile);
464 subtitle_raw_data[size] = 0;
466 if (read_count != size) {
467 d_free(subtitle_raw_data);
471 p = subtitle_raw_data;
473 while (p && p < subtitle_raw_data+size) {
476 endp = strchr(p,'\n');
478 if (endp[-1] == '\r')
479 endp[-1] = 0; //handle 0d0a pair
480 *endp = 0; //string termintor
487 Subtitles[Num_subtitles].first_frame = atoi(p);
488 p = next_field(p); if (!p) continue;
489 Subtitles[Num_subtitles].last_frame = atoi(p);
490 p = next_field(p); if (!p) continue;
491 Subtitles[Num_subtitles].msg = p;
493 Assert(Num_subtitles==0 || Subtitles[Num_subtitles].first_frame >= Subtitles[Num_subtitles-1].first_frame);
494 Assert(Subtitles[Num_subtitles].last_frame >= Subtitles[Num_subtitles].first_frame);
507 void close_subtitles()
509 if (subtitle_raw_data)
510 d_free(subtitle_raw_data);
511 subtitle_raw_data = NULL;
516 //draw the subtitles for this frame
517 void draw_subtitles(int frame_num)
519 static int active_subtitles[MAX_ACTIVE_SUBTITLES];
520 static int num_active_subtitles,next_subtitle,line_spacing;
524 if (frame_num == 0) {
525 num_active_subtitles = 0;
527 gr_set_curfont( GAME_FONT );
528 line_spacing = grd_curcanv->cv_font->ft_h + (grd_curcanv->cv_font->ft_h >> 2);
529 gr_set_fontcolor(255,-1);
532 //get rid of any subtitles that have expired
533 for (t=0;t<num_active_subtitles;)
534 if (frame_num > Subtitles[active_subtitles[t]].last_frame) {
536 for (t2=t;t2<num_active_subtitles-1;t2++)
537 active_subtitles[t2] = active_subtitles[t2+1];
538 num_active_subtitles--;
544 //get any subtitles new for this frame
545 while (next_subtitle < Num_subtitles && frame_num >= Subtitles[next_subtitle].first_frame) {
546 if (num_active_subtitles >= MAX_ACTIVE_SUBTITLES)
547 Error("Too many active subtitles!");
548 active_subtitles[num_active_subtitles++] = next_subtitle;
552 //find y coordinate for first line of subtitles
553 y = grd_curcanv->cv_bitmap.bm_h-((line_spacing+1)*MAX_ACTIVE_SUBTITLES+2);
555 //erase old subtitles if necessary
558 gr_rect(0,y,grd_curcanv->cv_bitmap.bm_w-1,grd_curcanv->cv_bitmap.bm_h-1);
561 //now draw the current subtitles
562 for (t=0;t<num_active_subtitles;t++)
563 if (active_subtitles[t] != -1) {
564 gr_string(0x8000,y,Subtitles[active_subtitles[t]].msg);
574 con_printf(DEBUG_LEVEL, "STUB: movie: request_cd\n");
579 movielib *init_new_movie_lib(char *filename,FILE *fp)
585 //read movie file header
587 nfiles = file_read_int(fp); //get number of files
589 //table = d_malloc(sizeof(*table) + sizeof(ml_entry)*nfiles);
590 MALLOC(table, movielib, 1);
591 MALLOC(table->movies, ml_entry, nfiles);
593 strcpy(table->name,filename);
594 table->n_movies = nfiles;
596 offset = 4+4+nfiles*(13+4); //id + nfiles + nfiles * (filename + size)
598 for (i=0;i<nfiles;i++) {
601 n = fread( table->movies[i].name, 13, 1, fp );
603 break; //end of file (probably)
605 len = file_read_int(fp);
607 table->movies[i].len = len;
608 table->movies[i].offset = offset;
610 offset += table->movies[i].len;
623 movielib *init_old_movie_lib(char *filename,FILE *fp)
627 movielib *table,*table2;
632 table = d_malloc(sizeof(*table) + sizeof(ml_entry)*MAX_MOVIES_PER_LIB);
637 i = fread( table->movies[nfiles].name, 13, 1, fp );
639 break; //end of file (probably)
641 i = fread( &len, 4, 1, fp );
643 Error("error reading movie library <%s>",filename);
645 table->movies[nfiles].len = INTEL_INT(len);
646 table->movies[nfiles].offset = ftell( fp );
648 fseek( fp, INTEL_INT(len), SEEK_CUR ); //skip data
653 //allocate correct-sized table
654 size = sizeof(*table) + sizeof(ml_entry)*nfiles;
655 table2 = d_malloc(size);
656 memcpy(table2,table,size);
660 strcpy(table->name,filename);
662 table->n_movies = nfiles;
673 //find the specified movie library, and read in list of movies in it
674 movielib *init_movie_lib(char *filename)
676 //note: this based on cfile_init_hogfile()
681 fp = fopen( filename, "rb" );
683 if ((fp == NULL) && (AltHogdir_initialized)) {
685 strcpy(temp, AltHogDir);
687 strcat(temp, filename);
688 fp = fopen(temp, "rb");
694 fread( id, 4, 1, fp );
695 if ( !strncmp( id, "DMVL", 4 ) )
696 return init_new_movie_lib(filename,fp);
697 else if ( !strncmp( id, "DHF", 3 ) ) {
698 fseek(fp,-1,SEEK_CUR); //old file had 3 char id
699 return init_old_movie_lib(filename,fp);
708 void close_movie(int i)
711 d_free(movie_libs[i]->movies);
712 d_free(movie_libs[i]);
721 for (i=0;i<N_MOVIE_LIBS;i++)
726 void init_movie(char *filename,int libnum,int is_robots,int required)
732 if (FindArg("-nomovies")) {
733 movie_libs[libnum] = NULL;
738 //for robots, load highres versions if highres menus set
740 high_res = MenuHiresAvailable;
742 high_res = MovieHires;
745 strchr(filename,'.')[-1] = 'h';
749 if ((movie_libs[libnum] = init_movie_lib(filename)) == NULL) {
752 strcpy(name2,CDROM_dir);
753 strcat(name2,filename);
754 movie_libs[libnum] = init_movie_lib(name2);
756 if (movie_libs[libnum] != NULL)
757 movie_libs[libnum]->flags |= MLF_ON_CD;
760 Warning("Cannot open movie file <%s>\n",filename);
763 if (!try) { // first try
764 if (strchr(filename, '.')[-1] == 'h') { // try again with lowres
765 strchr(filename, '.')[-1] = 'l';
766 Warning("Trying to open movie file <%s> instead\n", filename);
769 } else if (strchr(filename, '.')[-1] == 'l') { // try again with highres
770 strchr(filename, '.')[-1] = 'h';
771 Warning("Trying to open movie file <%s> instead\n", filename);
779 if (is_robots && movie_libs[libnum]!=NULL)
780 robot_movies = high_res?2:1;
784 //find and initialize the movie libraries
790 for (i=0;i<N_BUILTIN_MOVIE_LIBS;i++) {
792 if (!strnicmp(movielib_files[i],"robot",5))
797 init_movie(movielib_files[i],i,is_robots,1);
800 movie_libs[EXTRA_ROBOT_LIB] = NULL;
802 atexit(close_movies);
806 void init_extra_robot_movie(char *filename)
808 close_movie(EXTRA_ROBOT_LIB);
809 init_movie(filename,EXTRA_ROBOT_LIB,1,0);
813 //looks through a movie library for a movie file
814 //returns filehandle, with fileposition at movie, or -1 if can't find
815 int search_movie_lib(movielib *lib,char *filename,int must_have)
823 for (i=0;i<lib->n_movies;i++)
824 if (!stricmp(filename,lib->movies[i].name)) { //found the movie in a library
827 from_cd = (lib->flags & MLF_ON_CD);
830 songs_stop_redbook(); //ready to read from CD
832 do { //keep trying until we get the file handle
834 /* movie_handle = */ filehandle = open(lib->name, O_RDONLY);
836 if ((filehandle == -1) && (AltHogdir_initialized)) {
838 strcpy(temp, AltHogDir);
840 strcat(temp, lib->name);
841 filehandle = open(temp, O_RDONLY);
844 if (must_have && from_cd && filehandle == -1) { //didn't get file!
846 if (request_cd() == -1) //ESC from requester
847 break; //bail from here. will get error later
850 } while (must_have && from_cd && filehandle == -1);
852 if (filehandle != -1)
853 lseek(filehandle,(/* movie_start = */ lib->movies[i].offset),SEEK_SET);
862 //returns file handle
863 int open_movie_file(char *filename,int must_have)
867 for (i=0;i<N_MOVIE_LIBS;i++) {
868 if ((filehandle = search_movie_lib(movie_libs[i],filename,must_have)) != -1)
872 return -1; //couldn't find it