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.2 2002-07-22 02:20:05 btb Exp $";
23 #define DEBUG_LEVEL CON_NORMAL
26 #include <sys/types.h>
51 extern int MenuHiresAvailable;
52 extern char CDROM_dir[];
62 short first_frame,last_frame;
66 #define MAX_SUBTITLES 500
67 #define MAX_ACTIVE_SUBTITLES 3
68 subtitle Subtitles[MAX_SUBTITLES];
73 char name[FILENAME_LEN];
78 #define MAX_MOVIES_PER_LIB 50 //determines size of malloc
82 char name[100]; //[FILENAME_LEN];
89 char *movielib_files[] = {"intro-l.mvl","other-l.mvl","robots-l.mvl","oem-l.mvl"};
91 char *movielib_files[] = {"intro-l.mvl","other-l.mvl","robots-l.mvl"};
94 #define N_BUILTIN_MOVIE_LIBS (sizeof(movielib_files)/sizeof(*movielib_files))
95 #define N_MOVIE_LIBS (N_BUILTIN_MOVIE_LIBS+1)
96 #define EXTRA_ROBOT_LIB N_BUILTIN_MOVIE_LIBS
97 movielib *movie_libs[N_MOVIE_LIBS];
99 int MVEPaletteCalls = 0;
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
107 // Function Prototypes
108 int RunMovie(char *filename, int highres_flag, int allow_abort,int dx,int dy);
110 int open_movie_file(char *filename,int must_have);
112 void change_filename_ext( char *dest, char *src, char *ext );
113 void decode_text_line(char *p);
114 void draw_subtitles(int frame_num);
117 //filename will actually get modified to be either low-res or high-res
118 //returns status. see values in movie.h
119 int PlayMovie(const char *filename, int must_have)
121 char name[FILENAME_LEN],*p;
123 int save_sample_rate;
125 con_printf(DEBUG_LEVEL, "movie: PlayMovie: %s %d\n", filename, must_have);
128 if (FindArg("-nomovies"))
129 return MOVIE_NOT_PLAYED;
132 strcpy(name,filename);
134 if ((p=strchr(name,'.')) == NULL) //add extension, if missing
137 //check for escape already pressed & abort if so
138 while ((c=key_inkey()) != 0)
140 return MOVIE_ABORTED;
142 // Stop all digital sounds currently playing.
148 save_sample_rate = digi_sample_rate;
149 digi_sample_rate = SAMPLE_RATE_22K; //always 22K for movies
150 digi_reset(); digi_reset();
152 ret = RunMovie(name,MovieHires,must_have,-1,-1);
154 //gr_palette_clear(); //clear out palette in case movie aborted
156 digi_sample_rate = save_sample_rate; //restore rate for game
157 digi_reset(); digi_reset();
159 //Screen_mode = -1; //force screen reset
164 void initializeMovie(MVESTREAM *mve);
165 void shutdownMovie(MVESTREAM *mve);
167 //returns status. see movie.h
168 int RunMovie(char *filename, int hires_flag, int must_have,int dx,int dy)
171 int result=1,aborted=0;
175 con_printf(DEBUG_LEVEL, "movie: RunMovie: %s, %d, %d, %d\n", filename, hires_flag, must_have, dx, dy);
177 // Open Movie file. If it doesn't exist, no movie, just return.
179 filehndl = open_movie_file(filename,must_have);
181 if (filehndl == -1) {
185 Error("movie: RunMovie: Cannot open movie file <%s>",filename);
187 return MOVIE_NOT_PLAYED;
189 return MOVIE_NOT_PLAYED;
195 gr_set_mode(SM(640,480));
197 gr_set_mode(SM(320,200));
202 FontHires = hires_flag;
204 mve = mve_open(filehndl);
207 fprintf(stderr, "can't open MVE file '%s'\n", filename);
211 initializeMovie(mve);
213 while((result = mve_play_next_chunk(mve))) {
216 draw_subtitles(frame_num);
220 // If ESCAPE pressed, then quit movie.
221 if (key == KEY_ESC) {
222 result = aborted = 1;
226 // If PAUSE pressed, then pause movie
227 if (key == KEY_PAUSE) {
229 //show_pause_message(TXT_PAUSE);
230 while (!key_inkey()) ;
231 //clear_pause_message();
237 Assert(aborted || !result); ///movie should be over
242 close(filehndl); // Close Movie File
244 // Restore old graphic state
246 //Screen_mode=-1; //force reset of screen mode
248 return (aborted?MOVIE_ABORTED:MOVIE_PLAYED_FULL);
252 int InitMovieBriefing()
254 con_printf(DEBUG_LEVEL, "movie: InitMovieBriefing\n");
258 gr_set_mode(SM(640,480));
260 gr_set_mode(SM(320,200));
268 con_printf(DEBUG_LEVEL, "STUB: movie: RotateRobot\n");
272 void DeInitRobotMovie(void)
274 con_printf(DEBUG_LEVEL, "STUB: movie: DeInitRobotMovie\n");
278 int InitRobotMovie(char *a)
280 con_printf(DEBUG_LEVEL, "STUB: movie: InitRobotMovie: %s\n", a);
287 * Subtitle system code
290 ubyte *subtitle_raw_data;
293 //search for next field following whitespace
294 ubyte *next_field(ubyte *p)
296 con_printf(DEBUG_LEVEL, "movie: next_field: %c\n", *p);
298 while (*p && !isspace(*p))
304 while (*p && isspace(*p))
314 int init_subtitles(char *filename)
321 con_printf(DEBUG_LEVEL, "movie: init_subtitles: %s\n", filename);
325 if (! FindArg("-subtitles"))
328 ifile = cfopen(filename,"rb"); //try text version
330 if (!ifile) { //no text version, try binary version
331 char filename2[FILENAME_LEN];
332 change_filename_ext(filename2,filename,".TXB");
333 ifile = cfopen(filename2,"rb");
339 size = cfilelength(ifile);
341 MALLOC (subtitle_raw_data, ubyte, size+1);
343 read_count = cfread(subtitle_raw_data, 1, size, ifile);
347 subtitle_raw_data[size] = 0;
349 if (read_count != size) {
350 d_free(subtitle_raw_data);
354 p = subtitle_raw_data;
356 while (p && p < subtitle_raw_data+size) {
359 endp = strchr(p,'\n');
361 if (endp[-1] == '\r')
362 endp[-1] = 0; //handle 0d0a pair
363 *endp = 0; //string termintor
370 Subtitles[Num_subtitles].first_frame = atoi(p);
371 p = next_field(p); if (!p) continue;
372 Subtitles[Num_subtitles].last_frame = atoi(p);
373 p = next_field(p); if (!p) continue;
374 Subtitles[Num_subtitles].msg = p;
376 Assert(Num_subtitles==0 || Subtitles[Num_subtitles].first_frame >= Subtitles[Num_subtitles-1].first_frame);
377 Assert(Subtitles[Num_subtitles].last_frame >= Subtitles[Num_subtitles].first_frame);
390 void close_subtitles()
392 con_printf(DEBUG_LEVEL, "movie: close_subtitles\n");
394 if (subtitle_raw_data)
395 d_free(subtitle_raw_data);
396 subtitle_raw_data = NULL;
401 //draw the subtitles for this frame
402 void draw_subtitles(int frame_num)
404 static int active_subtitles[MAX_ACTIVE_SUBTITLES];
405 static int num_active_subtitles,next_subtitle,line_spacing;
409 con_printf(DEBUG_LEVEL, "movie: draw_subtitles: %d\n", frame_num);
411 if (frame_num == 0) {
412 num_active_subtitles = 0;
414 gr_set_curfont( GAME_FONT );
415 line_spacing = grd_curcanv->cv_font->ft_h + (grd_curcanv->cv_font->ft_h >> 2);
416 gr_set_fontcolor(255,-1);
419 //get rid of any subtitles that have expired
420 for (t=0;t<num_active_subtitles;)
421 if (frame_num > Subtitles[active_subtitles[t]].last_frame) {
423 for (t2=t;t2<num_active_subtitles-1;t2++)
424 active_subtitles[t2] = active_subtitles[t2+1];
425 num_active_subtitles--;
431 //get any subtitles new for this frame
432 while (next_subtitle < Num_subtitles && frame_num >= Subtitles[next_subtitle].first_frame) {
433 if (num_active_subtitles >= MAX_ACTIVE_SUBTITLES)
434 Error("Too many active subtitles!");
435 active_subtitles[num_active_subtitles++] = next_subtitle;
439 //find y coordinate for first line of subtitles
440 y = grd_curcanv->cv_bitmap.bm_h-((line_spacing+1)*MAX_ACTIVE_SUBTITLES+2);
442 //erase old subtitles if necessary
445 gr_rect(0,y,grd_curcanv->cv_bitmap.bm_w-1,grd_curcanv->cv_bitmap.bm_h-1);
448 //now draw the current subtitles
449 for (t=0;t<num_active_subtitles;t++)
450 if (active_subtitles[t] != -1) {
451 con_printf(DEBUG_LEVEL, "%s\n", Subtitles[active_subtitles[t]].msg);
452 gr_string(0x8000,y,Subtitles[active_subtitles[t]].msg);
460 con_printf(DEBUG_LEVEL, "STUB: movie: request_cd\n");
465 movielib *init_new_movie_lib(char *filename,FILE *fp)
471 con_printf(DEBUG_LEVEL, "movie: init_new_movie_lib: %s\n", filename);
473 //read movie file header
475 fread(&nfiles,4,1,fp); //get number of files
477 //table = d_malloc(sizeof(*table) + sizeof(ml_entry)*nfiles);
478 MALLOC(table, movielib, 1);
479 MALLOC(table->movies, ml_entry, nfiles);
481 strcpy(table->name,filename);
482 table->n_movies = nfiles;
484 offset = 4+4+nfiles*(13+4); //id + nfiles + nfiles * (filename + size)
486 for (i=0;i<nfiles;i++) {
489 n = fread( table->movies[i].name, 13, 1, fp );
491 break; //end of file (probably)
493 n = fread( &len, 4, 1, fp );
495 Error("error reading movie library <%s>",filename);
497 table->movies[i].len = INTEL_INT(len);
498 table->movies[i].offset = offset;
500 offset += table->movies[i].len;
513 movielib *init_old_movie_lib(char *filename,FILE *fp)
517 movielib *table,*table2;
519 con_printf(DEBUG_LEVEL, "movie: init_old_movie_lib: %s\n", filename);
524 table = d_malloc(sizeof(*table) + sizeof(ml_entry)*MAX_MOVIES_PER_LIB);
529 i = fread( table->movies[nfiles].name, 13, 1, fp );
531 break; //end of file (probably)
533 i = fread( &len, 4, 1, fp );
535 Error("error reading movie library <%s>",filename);
537 table->movies[nfiles].len = INTEL_INT(len);
538 table->movies[nfiles].offset = ftell( fp );
540 fseek( fp, INTEL_INT(len), SEEK_CUR ); //skip data
545 //allocate correct-sized table
546 size = sizeof(*table) + sizeof(ml_entry)*nfiles;
547 table2 = d_malloc(size);
548 memcpy(table2,table,size);
552 strcpy(table->name,filename);
554 table->n_movies = nfiles;
565 //find the specified movie library, and read in list of movies in it
566 movielib *init_movie_lib(char *filename)
568 //note: this based on cfile_init_hogfile()
573 con_printf(DEBUG_LEVEL, "movie: init_movie_lib: %s\n", filename);
575 fp = fopen( filename, "rb" );
579 fread( id, 4, 1, fp );
580 if ( !strncmp( id, "DMVL", 4 ) )
581 return init_new_movie_lib(filename,fp);
582 else if ( !strncmp( id, "DHF", 3 ) ) {
583 fseek(fp,-1,SEEK_CUR); //old file had 3 char id
584 return init_old_movie_lib(filename,fp);
593 void close_movie(int i)
595 con_printf(DEBUG_LEVEL, "movie: close_movie\n");
598 d_free(movie_libs[i]->movies);
599 d_free(movie_libs[i]);
608 con_printf(DEBUG_LEVEL, "movie: close_movies\n");
610 for (i=0;i<N_MOVIE_LIBS;i++)
615 void init_movie(char *filename,int libnum,int is_robots,int required)
619 con_printf(DEBUG_LEVEL, "movie: init_movie: %s, %d, %d, %d\n", filename, libnum, is_robots, required);
622 if (FindArg("-nomovies")) {
623 movie_libs[libnum] = NULL;
628 //for robots, load highres versions if highres menus set
630 high_res = MenuHiresAvailable;
632 high_res = MovieHires;
635 strchr(filename,'.')[-1] = 'h';
641 if ((movie_libs[libnum] = init_movie_lib(filename)) == NULL) {
644 strcpy(name2,CDROM_dir);
645 strcat(name2,filename);
646 movie_libs[libnum] = init_movie_lib(name2);
648 if (movie_libs[libnum] != NULL)
649 movie_libs[libnum]->flags |= MLF_ON_CD;
652 #if defined(RELEASE) && !defined(D2_OEM) //allow no movies if not release
654 Error("Cannot open movie file <%s>",filename);
657 #if defined(D2_OEM) //if couldn't get higres, try low
658 if (is_robots == 1) { //first try, try again with lowres
659 strchr(filename,'.')[-1] = 'l';
664 else if (is_robots == 2) { //failed twice. bail with error
666 Error("Cannot open movie file <%s>",filename);
672 if (is_robots && movie_libs[libnum]!=NULL)
673 robot_movies = high_res?2:1;
677 //find and initialize the movie libraries
683 con_printf(DEBUG_LEVEL, "movie: init_movies\n");
685 for (i=0;i<N_BUILTIN_MOVIE_LIBS;i++) {
687 if (!strnicmp(movielib_files[i],"robot",5))
692 init_movie(movielib_files[i],i,is_robots,1);
695 movie_libs[EXTRA_ROBOT_LIB] = NULL;
697 atexit(close_movies);
701 void init_extra_robot_movie(char *f)
703 con_printf(DEBUG_LEVEL, "STUB: movie: init_extra_robot_movie: %s\n", f);
707 //looks through a movie library for a movie file
708 //returns filehandle, with fileposition at movie, or -1 if can't find
709 int search_movie_lib(movielib *lib,char *filename,int must_have)
714 con_printf(DEBUG_LEVEL, "movie: search_movie_lib: %s, %s, %d\n", lib->name, filename, must_have);
719 for (i=0;i<lib->n_movies;i++)
720 if (!stricmp(filename,lib->movies[i].name)) { //found the movie in a library
723 from_cd = (lib->flags & MLF_ON_CD);
726 songs_stop_redbook(); //ready to read from CD
728 do { //keep trying until we get the file handle
730 /* movie_handle = */ filehandle = open(lib->name, O_RDONLY);
732 if (must_have && from_cd && filehandle == -1) { //didn't get file!
734 if (request_cd() == -1) //ESC from requester
735 break; //bail from here. will get error later
738 } while (must_have && from_cd && filehandle == -1);
740 if (filehandle != -1)
741 lseek(filehandle,(/* movie_start = */ lib->movies[i].offset),SEEK_SET);
750 //returns file handle
751 int open_movie_file(char *filename,int must_have)
755 con_printf(DEBUG_LEVEL, "movie: open_movie_file: %s %d\n", filename, must_have);
757 for (i=0;i<N_MOVIE_LIBS;i++) {
758 if ((filehandle = search_movie_lib(movie_libs[i],filename,must_have)) != -1)
762 return -1; //couldn't find it