1 /* $Id: movie.c,v 1.11 2002-08-31 12:14:01 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.11 2002-08-31 12:14:01 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];
74 char name[FILENAME_LEN];
79 #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];
100 int MVEPaletteCalls = 0;
102 //do we have the robot movies available
103 int robot_movies = 0; //0 means none, 1 means lowres, 2 means hires
105 int MovieHires = 0; //default for now is lores
108 char Robo_filename[FILENAME_LEN];
110 grs_bitmap *Robo_bitmap;
113 // Function Prototypes
114 int RunMovie(char *filename, int highres_flag, int allow_abort,int dx,int dy);
116 int open_movie_file(char *filename,int must_have);
118 void change_filename_ext( char *dest, char *src, char *ext );
119 void decode_text_line(char *p);
120 void draw_subtitles(int frame_num);
123 //filename will actually get modified to be either low-res or high-res
124 //returns status. see values in movie.h
125 int PlayMovie(const char *filename, int must_have)
127 char name[FILENAME_LEN],*p;
130 int save_sample_rate;
134 if (FindArg("-nomovies"))
135 return MOVIE_NOT_PLAYED;
138 strcpy(name,filename);
140 if ((p=strchr(name,'.')) == NULL) //add extension, if missing
143 //check for escape already pressed & abort if so
144 while ((c=key_inkey()) != 0)
146 return MOVIE_ABORTED;
148 // Stop all digital sounds currently playing.
155 save_sample_rate = digi_sample_rate;
156 digi_sample_rate = SAMPLE_RATE_22K; //always 22K for movies
157 digi_reset(); digi_reset();
162 ret = RunMovie(name,MovieHires,must_have,-1,-1);
165 gr_palette_clear(); //clear out palette in case movie aborted
169 digi_sample_rate = save_sample_rate; //restore rate for game
170 digi_reset(); digi_reset();
175 Screen_mode = -1; //force screen reset
180 void initializeMovie(MVESTREAM *mve, grs_bitmap *mve_bitmap);
181 void shutdownMovie(MVESTREAM *mve);
183 //returns status. see movie.h
184 int RunMovie(char *filename, int hires_flag, int must_have,int dx,int dy)
187 int result=1,aborted=0;
190 grs_bitmap *mve_bitmap;
193 con_printf(DEBUG_LEVEL, "movie: RunMovie: %s %d %d %d %d\n", filename, hires_flag, must_have, dx, dy);
195 // Open Movie file. If it doesn't exist, no movie, just return.
197 filehndl = open_movie_file(filename,must_have);
199 if (filehndl == -1) {
202 Warning("movie: RunMovie: Cannot open movie <%s>\n",filename);
204 return MOVIE_NOT_PLAYED;
208 gr_set_mode(SM(640,480));
210 gr_set_mode(SM(320,200));
214 FontHires = FontHiresAvailable && hires_flag;
216 mve = mve_open(filehndl);
219 fprintf(stderr, "can't open MVE file '%s'\n", filename);
223 mve_bitmap = gr_create_bitmap(hires_flag?592:320, hires_flag?312:200);
225 initializeMovie(mve, mve_bitmap);
227 while((result = mve_play_next_chunk(mve))) {
230 gr_bitmap(hires_flag?24:12, hires_flag?84:42, mve_bitmap);
232 draw_subtitles(frame_num);
238 // If ESCAPE pressed, then quit movie.
239 if (key == KEY_ESC) {
245 // If PAUSE pressed, then pause movie
246 if (key == KEY_PAUSE) {
248 //show_pause_message(TXT_PAUSE);
249 while (!key_inkey()) ;
250 //clear_pause_message();
256 Assert(aborted || !result); ///movie should be over
260 gr_free_bitmap(mve_bitmap);
264 close(filehndl); // Close Movie File
266 // Restore old graphic state
268 Screen_mode=-1; //force reset of screen mode
270 return (aborted?MOVIE_ABORTED:MOVIE_PLAYED_FULL);
274 int InitMovieBriefing()
277 gr_set_mode(SM(640,480));
279 gr_set_mode(SM(320,200));
285 //returns 1 if frame updated ok
290 ret = mve_play_next_chunk(Robo_mve);
291 gr_bitmap(MenuHires?280:200, MenuHires?140:80, Robo_bitmap);
295 InitRobotMovie(Robo_filename);
302 void DeInitRobotMovie(void)
304 con_printf(DEBUG_LEVEL, "movie: DeInitRobotMovie\n");
306 shutdownMovie(Robo_mve);
308 gr_free_bitmap(Robo_bitmap);
314 int InitRobotMovie(char *filename)
316 con_printf(DEBUG_LEVEL, "movie: InitRobotMovie: %s\n", filename);
318 RoboFile = open_movie_file(filename, 1);
320 if (RoboFile == -1) {
321 Warning("movie: InitRobotMovie: Cannot open movie file <%s>",filename);
322 return MOVIE_NOT_PLAYED;
325 strcpy(Robo_filename, filename);
327 Robo_mve = mve_open(RoboFile);
328 if (Robo_mve == NULL)
330 fprintf(stderr, "can't open MVE file '%s'\n", filename);
334 Robo_bitmap = gr_create_bitmap(MenuHires?320:160, MenuHires?200:100);
336 initializeMovie(Robo_mve, Robo_bitmap);
343 * Subtitle system code
346 ubyte *subtitle_raw_data;
349 //search for next field following whitespace
350 ubyte *next_field(ubyte *p)
352 while (*p && !isspace(*p))
358 while (*p && isspace(*p))
368 int init_subtitles(char *filename)
377 if (! FindArg("-subtitles"))
380 ifile = cfopen(filename,"rb"); //try text version
382 if (!ifile) { //no text version, try binary version
383 char filename2[FILENAME_LEN];
384 change_filename_ext(filename2,filename,".TXB");
385 ifile = cfopen(filename2,"rb");
391 size = cfilelength(ifile);
393 MALLOC (subtitle_raw_data, ubyte, size+1);
395 read_count = cfread(subtitle_raw_data, 1, size, ifile);
399 subtitle_raw_data[size] = 0;
401 if (read_count != size) {
402 d_free(subtitle_raw_data);
406 p = subtitle_raw_data;
408 while (p && p < subtitle_raw_data+size) {
411 endp = strchr(p,'\n');
413 if (endp[-1] == '\r')
414 endp[-1] = 0; //handle 0d0a pair
415 *endp = 0; //string termintor
422 Subtitles[Num_subtitles].first_frame = atoi(p);
423 p = next_field(p); if (!p) continue;
424 Subtitles[Num_subtitles].last_frame = atoi(p);
425 p = next_field(p); if (!p) continue;
426 Subtitles[Num_subtitles].msg = p;
428 Assert(Num_subtitles==0 || Subtitles[Num_subtitles].first_frame >= Subtitles[Num_subtitles-1].first_frame);
429 Assert(Subtitles[Num_subtitles].last_frame >= Subtitles[Num_subtitles].first_frame);
442 void close_subtitles()
444 if (subtitle_raw_data)
445 d_free(subtitle_raw_data);
446 subtitle_raw_data = NULL;
451 //draw the subtitles for this frame
452 void draw_subtitles(int frame_num)
454 static int active_subtitles[MAX_ACTIVE_SUBTITLES];
455 static int num_active_subtitles,next_subtitle,line_spacing;
459 if (frame_num == 0) {
460 num_active_subtitles = 0;
462 gr_set_curfont( GAME_FONT );
463 line_spacing = grd_curcanv->cv_font->ft_h + (grd_curcanv->cv_font->ft_h >> 2);
464 gr_set_fontcolor(255,-1);
467 //get rid of any subtitles that have expired
468 for (t=0;t<num_active_subtitles;)
469 if (frame_num > Subtitles[active_subtitles[t]].last_frame) {
471 for (t2=t;t2<num_active_subtitles-1;t2++)
472 active_subtitles[t2] = active_subtitles[t2+1];
473 num_active_subtitles--;
479 //get any subtitles new for this frame
480 while (next_subtitle < Num_subtitles && frame_num >= Subtitles[next_subtitle].first_frame) {
481 if (num_active_subtitles >= MAX_ACTIVE_SUBTITLES)
482 Error("Too many active subtitles!");
483 active_subtitles[num_active_subtitles++] = next_subtitle;
487 //find y coordinate for first line of subtitles
488 y = grd_curcanv->cv_bitmap.bm_h-((line_spacing+1)*MAX_ACTIVE_SUBTITLES+2);
490 //erase old subtitles if necessary
493 gr_rect(0,y,grd_curcanv->cv_bitmap.bm_w-1,grd_curcanv->cv_bitmap.bm_h-1);
496 //now draw the current subtitles
497 for (t=0;t<num_active_subtitles;t++)
498 if (active_subtitles[t] != -1) {
499 gr_string(0x8000,y,Subtitles[active_subtitles[t]].msg);
507 con_printf(DEBUG_LEVEL, "STUB: movie: request_cd\n");
512 movielib *init_new_movie_lib(char *filename,FILE *fp)
518 //read movie file header
520 nfiles = file_read_int(fp); //get number of files
522 //table = d_malloc(sizeof(*table) + sizeof(ml_entry)*nfiles);
523 MALLOC(table, movielib, 1);
524 MALLOC(table->movies, ml_entry, nfiles);
526 strcpy(table->name,filename);
527 table->n_movies = nfiles;
529 offset = 4+4+nfiles*(13+4); //id + nfiles + nfiles * (filename + size)
531 for (i=0;i<nfiles;i++) {
534 n = fread( table->movies[i].name, 13, 1, fp );
536 break; //end of file (probably)
538 len = file_read_int(fp);
540 table->movies[i].len = len;
541 table->movies[i].offset = offset;
543 offset += table->movies[i].len;
556 movielib *init_old_movie_lib(char *filename,FILE *fp)
560 movielib *table,*table2;
565 table = d_malloc(sizeof(*table) + sizeof(ml_entry)*MAX_MOVIES_PER_LIB);
570 i = fread( table->movies[nfiles].name, 13, 1, fp );
572 break; //end of file (probably)
574 i = fread( &len, 4, 1, fp );
576 Error("error reading movie library <%s>",filename);
578 table->movies[nfiles].len = INTEL_INT(len);
579 table->movies[nfiles].offset = ftell( fp );
581 fseek( fp, INTEL_INT(len), SEEK_CUR ); //skip data
586 //allocate correct-sized table
587 size = sizeof(*table) + sizeof(ml_entry)*nfiles;
588 table2 = d_malloc(size);
589 memcpy(table2,table,size);
593 strcpy(table->name,filename);
595 table->n_movies = nfiles;
606 //find the specified movie library, and read in list of movies in it
607 movielib *init_movie_lib(char *filename)
609 //note: this based on cfile_init_hogfile()
614 fp = fopen( filename, "rb" );
616 if ((fp == NULL) && (AltHogdir_initialized)) {
618 strcpy(temp, AltHogDir);
620 strcat(temp, filename);
621 fp = fopen(temp, "rb");
627 fread( id, 4, 1, fp );
628 if ( !strncmp( id, "DMVL", 4 ) )
629 return init_new_movie_lib(filename,fp);
630 else if ( !strncmp( id, "DHF", 3 ) ) {
631 fseek(fp,-1,SEEK_CUR); //old file had 3 char id
632 return init_old_movie_lib(filename,fp);
641 void close_movie(int i)
644 d_free(movie_libs[i]->movies);
645 d_free(movie_libs[i]);
654 for (i=0;i<N_MOVIE_LIBS;i++)
659 void init_movie(char *filename,int libnum,int is_robots,int required)
665 if (FindArg("-nomovies")) {
666 movie_libs[libnum] = NULL;
671 //for robots, load highres versions if highres menus set
673 high_res = MenuHiresAvailable;
675 high_res = MovieHires;
678 strchr(filename,'.')[-1] = 'h';
682 if ((movie_libs[libnum] = init_movie_lib(filename)) == NULL) {
685 strcpy(name2,CDROM_dir);
686 strcat(name2,filename);
687 movie_libs[libnum] = init_movie_lib(name2);
689 if (movie_libs[libnum] != NULL)
690 movie_libs[libnum]->flags |= MLF_ON_CD;
693 Warning("Cannot open movie file <%s>\n",filename);
696 if (!try) { // first try
697 if (strchr(filename, '.')[-1] == 'h') { // try again with lowres
698 strchr(filename, '.')[-1] = 'l';
699 Warning("Trying to open movie file <%s> instead\n", filename);
702 } else if (strchr(filename, '.')[-1] == 'l') { // try again with highres
703 strchr(filename, '.')[-1] = 'h';
704 Warning("Trying to open movie file <%s> instead\n", filename);
712 if (is_robots && movie_libs[libnum]!=NULL)
713 robot_movies = high_res?2:1;
717 //find and initialize the movie libraries
723 for (i=0;i<N_BUILTIN_MOVIE_LIBS;i++) {
725 if (!strnicmp(movielib_files[i],"robot",5))
730 init_movie(movielib_files[i],i,is_robots,1);
733 movie_libs[EXTRA_ROBOT_LIB] = NULL;
735 atexit(close_movies);
739 void init_extra_robot_movie(char *filename)
741 con_printf(DEBUG_LEVEL, "movie: init_extra_robot_movie: %s\n", filename);
743 close_movie(EXTRA_ROBOT_LIB);
744 init_movie(filename,EXTRA_ROBOT_LIB,1,0);
748 //looks through a movie library for a movie file
749 //returns filehandle, with fileposition at movie, or -1 if can't find
750 int search_movie_lib(movielib *lib,char *filename,int must_have)
758 for (i=0;i<lib->n_movies;i++)
759 if (!stricmp(filename,lib->movies[i].name)) { //found the movie in a library
762 from_cd = (lib->flags & MLF_ON_CD);
765 songs_stop_redbook(); //ready to read from CD
767 do { //keep trying until we get the file handle
769 /* movie_handle = */ filehandle = open(lib->name, O_RDONLY);
771 if ((filehandle == -1) && (AltHogdir_initialized)) {
773 strcpy(temp, AltHogDir);
775 strcat(temp, lib->name);
776 filehandle = open(temp, O_RDONLY);
779 if (must_have && from_cd && filehandle == -1) { //didn't get file!
781 if (request_cd() == -1) //ESC from requester
782 break; //bail from here. will get error later
785 } while (must_have && from_cd && filehandle == -1);
787 if (filehandle != -1)
788 lseek(filehandle,(/* movie_start = */ lib->movies[i].offset),SEEK_SET);
797 //returns file handle
798 int open_movie_file(char *filename,int must_have)
802 for (i=0;i<N_MOVIE_LIBS;i++) {
803 if ((filehandle = search_movie_lib(movie_libs[i],filename,must_have)) != -1)
807 return -1; //couldn't find it