1 /* $Id: movie.c,v 1.10 2002-08-27 04:14:18 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.10 2002-08-27 04:14:18 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
111 // Function Prototypes
112 int RunMovie(char *filename, int highres_flag, int allow_abort,int dx,int dy);
114 int open_movie_file(char *filename,int must_have);
116 void change_filename_ext( char *dest, char *src, char *ext );
117 void decode_text_line(char *p);
118 void draw_subtitles(int frame_num);
121 //filename will actually get modified to be either low-res or high-res
122 //returns status. see values in movie.h
123 int PlayMovie(const char *filename, int must_have)
125 char name[FILENAME_LEN],*p;
128 int save_sample_rate;
132 //if (FindArg("-nomovies"))
133 if (!FindArg("-movies"))
134 return MOVIE_NOT_PLAYED;
137 strcpy(name,filename);
139 if ((p=strchr(name,'.')) == NULL) //add extension, if missing
142 //check for escape already pressed & abort if so
143 while ((c=key_inkey()) != 0)
145 return MOVIE_ABORTED;
147 // Stop all digital sounds currently playing.
154 save_sample_rate = digi_sample_rate;
155 digi_sample_rate = SAMPLE_RATE_22K; //always 22K for movies
156 digi_reset(); digi_reset();
161 ret = RunMovie(name,MovieHires,must_have,-1,-1);
164 gr_palette_clear(); //clear out palette in case movie aborted
168 digi_sample_rate = save_sample_rate; //restore rate for game
169 digi_reset(); digi_reset();
174 Screen_mode = -1; //force screen reset
179 void initializeMovie(MVESTREAM *mve);
180 void shutdownMovie(MVESTREAM *mve);
182 //returns status. see movie.h
183 int RunMovie(char *filename, int hires_flag, int must_have,int dx,int dy)
186 int result=1,aborted=0;
191 con_printf(DEBUG_LEVEL, "movie: RunMovie: %s %d %d %d %d\n", filename, hires_flag, must_have, dx, dy);
193 // Open Movie file. If it doesn't exist, no movie, just return.
195 filehndl = open_movie_file(filename,must_have);
197 if (filehndl == -1) {
200 Warning("movie: RunMovie: Cannot open movie <%s>\n",filename);
202 return MOVIE_NOT_PLAYED;
207 gr_set_mode(SM(640,480));
209 gr_set_mode(SM(320,200));
214 FontHires = hires_flag;
216 mve = mve_open(filehndl);
219 fprintf(stderr, "can't open MVE file '%s'\n", filename);
224 initializeMovie(mve);
226 while((result = mve_play_next_chunk(mve))) {
229 draw_subtitles(frame_num);
233 // If ESCAPE pressed, then quit movie.
234 if (key == KEY_ESC) {
240 // If PAUSE pressed, then pause movie
241 if (key == KEY_PAUSE) {
243 //show_pause_message(TXT_PAUSE);
244 while (!key_inkey()) ;
245 //clear_pause_message();
251 Assert(aborted || !result); ///movie should be over
258 close(filehndl); // Close Movie File
260 // Restore old graphic state
262 Screen_mode=-1; //force reset of screen mode
264 return (aborted?MOVIE_ABORTED:MOVIE_PLAYED_FULL);
268 int InitMovieBriefing()
272 gr_set_mode(SM(640,480));
274 gr_set_mode(SM(320,200));
280 //returns 1 if frame updated ok
283 con_printf(DEBUG_LEVEL, "movie: RotateRobot\n");
285 return mve_play_next_chunk(Robo_mve);
289 void DeInitRobotMovie(void)
291 con_printf(DEBUG_LEVEL, "movie: DeInitRobotMovie\n");
293 shutdownMovie(Robo_mve);
299 int InitRobotMovie(char *filename)
301 con_printf(DEBUG_LEVEL, "movie: InitRobotMovie: %s\n", filename);
303 RoboFile = open_movie_file(filename,1);
305 if (RoboFile == -1) {
306 Warning("movie: InitRobotMovie: Cannot open movie file <%s>",filename);
307 return MOVIE_NOT_PLAYED;
310 Robo_mve = mve_open(RoboFile);
311 if (Robo_mve == NULL)
313 fprintf(stderr, "can't open MVE file '%s'\n", filename);
317 initializeMovie(Robo_mve);
324 * Subtitle system code
327 ubyte *subtitle_raw_data;
330 //search for next field following whitespace
331 ubyte *next_field(ubyte *p)
333 while (*p && !isspace(*p))
339 while (*p && isspace(*p))
349 int init_subtitles(char *filename)
358 if (! FindArg("-subtitles"))
361 ifile = cfopen(filename,"rb"); //try text version
363 if (!ifile) { //no text version, try binary version
364 char filename2[FILENAME_LEN];
365 change_filename_ext(filename2,filename,".TXB");
366 ifile = cfopen(filename2,"rb");
372 size = cfilelength(ifile);
374 MALLOC (subtitle_raw_data, ubyte, size+1);
376 read_count = cfread(subtitle_raw_data, 1, size, ifile);
380 subtitle_raw_data[size] = 0;
382 if (read_count != size) {
383 d_free(subtitle_raw_data);
387 p = subtitle_raw_data;
389 while (p && p < subtitle_raw_data+size) {
392 endp = strchr(p,'\n');
394 if (endp[-1] == '\r')
395 endp[-1] = 0; //handle 0d0a pair
396 *endp = 0; //string termintor
403 Subtitles[Num_subtitles].first_frame = atoi(p);
404 p = next_field(p); if (!p) continue;
405 Subtitles[Num_subtitles].last_frame = atoi(p);
406 p = next_field(p); if (!p) continue;
407 Subtitles[Num_subtitles].msg = p;
409 Assert(Num_subtitles==0 || Subtitles[Num_subtitles].first_frame >= Subtitles[Num_subtitles-1].first_frame);
410 Assert(Subtitles[Num_subtitles].last_frame >= Subtitles[Num_subtitles].first_frame);
423 void close_subtitles()
425 if (subtitle_raw_data)
426 d_free(subtitle_raw_data);
427 subtitle_raw_data = NULL;
432 //draw the subtitles for this frame
433 void draw_subtitles(int frame_num)
435 static int active_subtitles[MAX_ACTIVE_SUBTITLES];
436 static int num_active_subtitles,next_subtitle,line_spacing;
440 if (frame_num == 0) {
441 num_active_subtitles = 0;
443 gr_set_curfont( GAME_FONT );
444 line_spacing = grd_curcanv->cv_font->ft_h + (grd_curcanv->cv_font->ft_h >> 2);
445 gr_set_fontcolor(255,-1);
448 //get rid of any subtitles that have expired
449 for (t=0;t<num_active_subtitles;)
450 if (frame_num > Subtitles[active_subtitles[t]].last_frame) {
452 for (t2=t;t2<num_active_subtitles-1;t2++)
453 active_subtitles[t2] = active_subtitles[t2+1];
454 num_active_subtitles--;
460 //get any subtitles new for this frame
461 while (next_subtitle < Num_subtitles && frame_num >= Subtitles[next_subtitle].first_frame) {
462 if (num_active_subtitles >= MAX_ACTIVE_SUBTITLES)
463 Error("Too many active subtitles!");
464 active_subtitles[num_active_subtitles++] = next_subtitle;
468 //find y coordinate for first line of subtitles
469 y = grd_curcanv->cv_bitmap.bm_h-((line_spacing+1)*MAX_ACTIVE_SUBTITLES+2);
471 //erase old subtitles if necessary
474 gr_rect(0,y,grd_curcanv->cv_bitmap.bm_w-1,grd_curcanv->cv_bitmap.bm_h-1);
477 //now draw the current subtitles
478 for (t=0;t<num_active_subtitles;t++)
479 if (active_subtitles[t] != -1) {
480 gr_string(0x8000,y,Subtitles[active_subtitles[t]].msg);
488 con_printf(DEBUG_LEVEL, "STUB: movie: request_cd\n");
493 movielib *init_new_movie_lib(char *filename,FILE *fp)
499 //read movie file header
501 nfiles = file_read_int(fp); //get number of files
503 //table = d_malloc(sizeof(*table) + sizeof(ml_entry)*nfiles);
504 MALLOC(table, movielib, 1);
505 MALLOC(table->movies, ml_entry, nfiles);
507 strcpy(table->name,filename);
508 table->n_movies = nfiles;
510 offset = 4+4+nfiles*(13+4); //id + nfiles + nfiles * (filename + size)
512 for (i=0;i<nfiles;i++) {
515 n = fread( table->movies[i].name, 13, 1, fp );
517 break; //end of file (probably)
519 len = file_read_int(fp);
521 table->movies[i].len = len;
522 table->movies[i].offset = offset;
524 offset += table->movies[i].len;
537 movielib *init_old_movie_lib(char *filename,FILE *fp)
541 movielib *table,*table2;
546 table = d_malloc(sizeof(*table) + sizeof(ml_entry)*MAX_MOVIES_PER_LIB);
551 i = fread( table->movies[nfiles].name, 13, 1, fp );
553 break; //end of file (probably)
555 i = fread( &len, 4, 1, fp );
557 Error("error reading movie library <%s>",filename);
559 table->movies[nfiles].len = INTEL_INT(len);
560 table->movies[nfiles].offset = ftell( fp );
562 fseek( fp, INTEL_INT(len), SEEK_CUR ); //skip data
567 //allocate correct-sized table
568 size = sizeof(*table) + sizeof(ml_entry)*nfiles;
569 table2 = d_malloc(size);
570 memcpy(table2,table,size);
574 strcpy(table->name,filename);
576 table->n_movies = nfiles;
587 //find the specified movie library, and read in list of movies in it
588 movielib *init_movie_lib(char *filename)
590 //note: this based on cfile_init_hogfile()
595 fp = fopen( filename, "rb" );
597 if ((fp == NULL) && (AltHogdir_initialized)) {
599 strcpy(temp, AltHogDir);
601 strcat(temp, filename);
602 fp = fopen(temp, "rb");
608 fread( id, 4, 1, fp );
609 if ( !strncmp( id, "DMVL", 4 ) )
610 return init_new_movie_lib(filename,fp);
611 else if ( !strncmp( id, "DHF", 3 ) ) {
612 fseek(fp,-1,SEEK_CUR); //old file had 3 char id
613 return init_old_movie_lib(filename,fp);
622 void close_movie(int i)
625 d_free(movie_libs[i]->movies);
626 d_free(movie_libs[i]);
635 for (i=0;i<N_MOVIE_LIBS;i++)
640 void init_movie(char *filename,int libnum,int is_robots,int required)
646 //if (FindArg("-nomovies")) {
647 if (!FindArg("-movies")) {
648 movie_libs[libnum] = NULL;
653 //for robots, load highres versions if highres menus set
655 high_res = MenuHiresAvailable;
657 high_res = MovieHires;
660 strchr(filename,'.')[-1] = 'h';
664 if ((movie_libs[libnum] = init_movie_lib(filename)) == NULL) {
667 strcpy(name2,CDROM_dir);
668 strcat(name2,filename);
669 movie_libs[libnum] = init_movie_lib(name2);
671 if (movie_libs[libnum] != NULL)
672 movie_libs[libnum]->flags |= MLF_ON_CD;
675 Warning("Cannot open movie file <%s>\n",filename);
678 if (!try) { // first try
679 if (strchr(filename, '.')[-1] == 'h') { // try again with lowres
680 strchr(filename, '.')[-1] = 'l';
681 Warning("Trying to open movie file <%s> instead\n", filename);
684 } else if (strchr(filename, '.')[-1] == 'l') { // try again with highres
685 strchr(filename, '.')[-1] = 'h';
686 Warning("Trying to open movie file <%s> instead\n", filename);
694 if (is_robots && movie_libs[libnum]!=NULL)
695 robot_movies = high_res?2:1;
699 //find and initialize the movie libraries
705 for (i=0;i<N_BUILTIN_MOVIE_LIBS;i++) {
707 if (!strnicmp(movielib_files[i],"robot",5))
712 init_movie(movielib_files[i],i,is_robots,1);
715 movie_libs[EXTRA_ROBOT_LIB] = NULL;
717 atexit(close_movies);
721 void init_extra_robot_movie(char *filename)
723 con_printf(DEBUG_LEVEL, "movie: init_extra_robot_movie: %s\n", filename);
725 close_movie(EXTRA_ROBOT_LIB);
726 init_movie(filename,EXTRA_ROBOT_LIB,1,0);
730 //looks through a movie library for a movie file
731 //returns filehandle, with fileposition at movie, or -1 if can't find
732 int search_movie_lib(movielib *lib,char *filename,int must_have)
740 for (i=0;i<lib->n_movies;i++)
741 if (!stricmp(filename,lib->movies[i].name)) { //found the movie in a library
744 from_cd = (lib->flags & MLF_ON_CD);
747 songs_stop_redbook(); //ready to read from CD
749 do { //keep trying until we get the file handle
751 /* movie_handle = */ filehandle = open(lib->name, O_RDONLY);
753 if ((filehandle == -1) && (AltHogdir_initialized)) {
755 strcpy(temp, AltHogDir);
757 strcat(temp, lib->name);
758 filehandle = open(temp, O_RDONLY);
761 if (must_have && from_cd && filehandle == -1) { //didn't get file!
763 if (request_cd() == -1) //ESC from requester
764 break; //bail from here. will get error later
767 } while (must_have && from_cd && filehandle == -1);
769 if (filehandle != -1)
770 lseek(filehandle,(/* movie_start = */ lib->movies[i].offset),SEEK_SET);
779 //returns file handle
780 int open_movie_file(char *filename,int must_have)
784 for (i=0;i<N_MOVIE_LIBS;i++) {
785 if ((filehandle = search_movie_lib(movie_libs[i],filename,must_have)) != -1)
789 return -1; //couldn't find it