1 /* $Id: movie.c,v 1.16 2003-02-14 03:45:31 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.16 2003-02-14 03:45:31 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();
173 if (!FindArg("-nosound"))
177 Screen_mode = -1; //force screen reset
184 short x, y, w, h; // The location of the menu.
185 grs_bitmap * bmp; // The background under the menu.
188 bkg movie_bg = {0,0,0,0,NULL};
191 #define BOX_BORDER (MenuHires?40:20)
193 void show_pause_message(char *msg)
198 gr_set_current_canvas(NULL);
199 gr_set_curfont( SMALL_FONT );
201 gr_get_string_size(msg,&w,&h,&aw);
203 x = (grd_curscreen->sc_w-w)/2;
204 y = (grd_curscreen->sc_h-h)/2;
208 gr_free_bitmap(movie_bg.bmp);
212 // Save the background of the display
213 movie_bg.x=x; movie_bg.y=y; movie_bg.w=w; movie_bg.h=h;
215 movie_bg.bmp = gr_create_bitmap( w+BOX_BORDER, h+BOX_BORDER );
217 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 );
221 gr_rect(x-BOX_BORDER/2,y-BOX_BORDER/2,x+w+BOX_BORDER/2-1,y+h+BOX_BORDER/2-1);
223 gr_set_fontcolor( 255, -1 );
225 gr_ustring( 0x8000, y, msg );
230 void clear_pause_message()
235 gr_bitmap(movie_bg.x-BOX_BORDER/2, movie_bg.y-BOX_BORDER/2, movie_bg.bmp);
237 gr_free_bitmap(movie_bg.bmp);
243 //returns status. see movie.h
244 int RunMovie(char *filename, int hires_flag, int must_have,int dx,int dy)
247 int result=1,aborted=0;
250 grs_bitmap *mve_bitmap;
256 // Open Movie file. If it doesn't exist, no movie, just return.
258 filehndl = open_movie_file(filename,must_have);
260 if (filehndl == -1) {
263 Warning("movie: RunMovie: Cannot open movie <%s>\n",filename);
265 return MOVIE_NOT_PLAYED;
269 gr_set_mode(SM(640,480));
275 gr_set_mode(SM(320,200));
284 FontHires = FontHiresAvailable && hires_flag;
286 mve = mve_open_filehandle(filehndl);
288 mve_bitmap = gr_create_bitmap(w, h); // w, h must match the mve exactly!
290 mveplay_initializeMovie(mve, mve_bitmap);
294 result = mveplay_stepMovie(mve);
296 gr_bitmap(x, y, mve_bitmap);
298 draw_subtitles(frame_num);
304 // If ESCAPE pressed, then quit movie.
309 // If PAUSE pressed, then pause movie
310 show_pause_message(TXT_PAUSE);
311 while (!key_inkey()) ;
312 mveplay_restartTimer(mve);
313 clear_pause_message();
315 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
316 case KEY_CTRLED+KEY_SHIFTED+KEY_PADENTER:
317 case KEY_ALTED+KEY_CTRLED+KEY_PADENTER:
318 case KEY_ALTED+KEY_SHIFTED+KEY_PADENTER:
319 gr_toggle_fullscreen();
327 Assert(aborted || !result); ///movie should be over
329 mveplay_shutdownMovie(mve);
331 gr_free_bitmap(mve_bitmap);
335 close(filehndl); // Close Movie File
337 // Restore old graphic state
339 Screen_mode=-1; //force reset of screen mode
341 return (aborted?MOVIE_ABORTED:MOVIE_PLAYED_FULL);
345 int InitMovieBriefing()
348 gr_set_mode(SM(640,480));
350 gr_set_mode(SM(320,200));
356 //returns 1 if frame updated ok
361 ret = mveplay_stepMovie(Robo_mve);
362 gr_bitmap(MenuHires?280:140, MenuHires?200:80, Robo_bitmap);
365 mveplay_shutdownMovie(Robo_mve);
367 lseek(RoboFile, Robo_filepos, SEEK_SET);
368 Robo_mve = mve_open_filehandle(RoboFile);
369 mveplay_initializeMovie(Robo_mve, Robo_bitmap);
376 void DeInitRobotMovie(void)
378 mveplay_shutdownMovie(Robo_mve);
381 gr_free_bitmap(Robo_bitmap);
387 int InitRobotMovie(char *filename)
389 RoboFile = open_movie_file(filename, 1);
391 if (RoboFile == -1) {
392 Warning("movie: InitRobotMovie: Cannot open movie file <%s>",filename);
393 return MOVIE_NOT_PLAYED;
396 Robo_filepos = lseek(RoboFile, 0, SEEK_CUR);
398 Robo_mve = mve_open_filehandle(RoboFile);
400 Robo_bitmap = gr_create_bitmap(MenuHires?320:160, MenuHires?200:88);
402 mveplay_initializeMovie(Robo_mve, Robo_bitmap);
409 * Subtitle system code
412 ubyte *subtitle_raw_data;
415 //search for next field following whitespace
416 ubyte *next_field(ubyte *p)
418 while (*p && !isspace(*p))
424 while (*p && isspace(*p))
434 int init_subtitles(char *filename)
443 if (! FindArg("-subtitles"))
446 ifile = cfopen(filename,"rb"); //try text version
448 if (!ifile) { //no text version, try binary version
449 char filename2[FILENAME_LEN];
450 change_filename_ext(filename2,filename,".TXB");
451 ifile = cfopen(filename2,"rb");
457 size = cfilelength(ifile);
459 MALLOC (subtitle_raw_data, ubyte, size+1);
461 read_count = cfread(subtitle_raw_data, 1, size, ifile);
465 subtitle_raw_data[size] = 0;
467 if (read_count != size) {
468 d_free(subtitle_raw_data);
472 p = subtitle_raw_data;
474 while (p && p < subtitle_raw_data+size) {
477 endp = strchr(p,'\n');
479 if (endp[-1] == '\r')
480 endp[-1] = 0; //handle 0d0a pair
481 *endp = 0; //string termintor
488 Subtitles[Num_subtitles].first_frame = atoi(p);
489 p = next_field(p); if (!p) continue;
490 Subtitles[Num_subtitles].last_frame = atoi(p);
491 p = next_field(p); if (!p) continue;
492 Subtitles[Num_subtitles].msg = p;
494 Assert(Num_subtitles==0 || Subtitles[Num_subtitles].first_frame >= Subtitles[Num_subtitles-1].first_frame);
495 Assert(Subtitles[Num_subtitles].last_frame >= Subtitles[Num_subtitles].first_frame);
508 void close_subtitles()
510 if (subtitle_raw_data)
511 d_free(subtitle_raw_data);
512 subtitle_raw_data = NULL;
517 //draw the subtitles for this frame
518 void draw_subtitles(int frame_num)
520 static int active_subtitles[MAX_ACTIVE_SUBTITLES];
521 static int num_active_subtitles,next_subtitle,line_spacing;
525 if (frame_num == 0) {
526 num_active_subtitles = 0;
528 gr_set_curfont( GAME_FONT );
529 line_spacing = grd_curcanv->cv_font->ft_h + (grd_curcanv->cv_font->ft_h >> 2);
530 gr_set_fontcolor(255,-1);
533 //get rid of any subtitles that have expired
534 for (t=0;t<num_active_subtitles;)
535 if (frame_num > Subtitles[active_subtitles[t]].last_frame) {
537 for (t2=t;t2<num_active_subtitles-1;t2++)
538 active_subtitles[t2] = active_subtitles[t2+1];
539 num_active_subtitles--;
545 //get any subtitles new for this frame
546 while (next_subtitle < Num_subtitles && frame_num >= Subtitles[next_subtitle].first_frame) {
547 if (num_active_subtitles >= MAX_ACTIVE_SUBTITLES)
548 Error("Too many active subtitles!");
549 active_subtitles[num_active_subtitles++] = next_subtitle;
553 //find y coordinate for first line of subtitles
554 y = grd_curcanv->cv_bitmap.bm_h-((line_spacing+1)*MAX_ACTIVE_SUBTITLES+2);
556 //erase old subtitles if necessary
559 gr_rect(0,y,grd_curcanv->cv_bitmap.bm_w-1,grd_curcanv->cv_bitmap.bm_h-1);
562 //now draw the current subtitles
563 for (t=0;t<num_active_subtitles;t++)
564 if (active_subtitles[t] != -1) {
565 gr_string(0x8000,y,Subtitles[active_subtitles[t]].msg);
575 con_printf(DEBUG_LEVEL, "STUB: movie: request_cd\n");
580 movielib *init_new_movie_lib(char *filename,FILE *fp)
586 //read movie file header
588 nfiles = file_read_int(fp); //get number of files
590 //table = d_malloc(sizeof(*table) + sizeof(ml_entry)*nfiles);
591 MALLOC(table, movielib, 1);
592 MALLOC(table->movies, ml_entry, nfiles);
594 strcpy(table->name,filename);
595 table->n_movies = nfiles;
597 offset = 4+4+nfiles*(13+4); //id + nfiles + nfiles * (filename + size)
599 for (i=0;i<nfiles;i++) {
602 n = fread( table->movies[i].name, 13, 1, fp );
604 break; //end of file (probably)
606 len = file_read_int(fp);
608 table->movies[i].len = len;
609 table->movies[i].offset = offset;
611 offset += table->movies[i].len;
624 movielib *init_old_movie_lib(char *filename,FILE *fp)
628 movielib *table,*table2;
633 table = d_malloc(sizeof(*table) + sizeof(ml_entry)*MAX_MOVIES_PER_LIB);
638 i = fread( table->movies[nfiles].name, 13, 1, fp );
640 break; //end of file (probably)
642 i = fread( &len, 4, 1, fp );
644 Error("error reading movie library <%s>",filename);
646 table->movies[nfiles].len = INTEL_INT(len);
647 table->movies[nfiles].offset = ftell( fp );
649 fseek( fp, INTEL_INT(len), SEEK_CUR ); //skip data
654 //allocate correct-sized table
655 size = sizeof(*table) + sizeof(ml_entry)*nfiles;
656 table2 = d_malloc(size);
657 memcpy(table2,table,size);
661 strcpy(table->name,filename);
663 table->n_movies = nfiles;
674 //find the specified movie library, and read in list of movies in it
675 movielib *init_movie_lib(char *filename)
677 //note: this based on cfile_init_hogfile()
682 fp = fopen( filename, "rb" );
684 if ((fp == NULL) && (AltHogdir_initialized)) {
686 strcpy(temp, AltHogDir);
688 strcat(temp, filename);
689 fp = fopen(temp, "rb");
695 fread( id, 4, 1, fp );
696 if ( !strncmp( id, "DMVL", 4 ) )
697 return init_new_movie_lib(filename,fp);
698 else if ( !strncmp( id, "DHF", 3 ) ) {
699 fseek(fp,-1,SEEK_CUR); //old file had 3 char id
700 return init_old_movie_lib(filename,fp);
709 void close_movie(int i)
712 d_free(movie_libs[i]->movies);
713 d_free(movie_libs[i]);
722 for (i=0;i<N_MOVIE_LIBS;i++)
727 void init_movie(char *filename,int libnum,int is_robots,int required)
733 if (FindArg("-nomovies")) {
734 movie_libs[libnum] = NULL;
739 //for robots, load highres versions if highres menus set
741 high_res = MenuHiresAvailable;
743 high_res = MovieHires;
746 strchr(filename,'.')[-1] = 'h';
750 if ((movie_libs[libnum] = init_movie_lib(filename)) == NULL) {
753 strcpy(name2,CDROM_dir);
754 strcat(name2,filename);
755 movie_libs[libnum] = init_movie_lib(name2);
757 if (movie_libs[libnum] != NULL)
758 movie_libs[libnum]->flags |= MLF_ON_CD;
761 Warning("Cannot open movie file <%s>\n",filename);
764 if (!try) { // first try
765 if (strchr(filename, '.')[-1] == 'h') { // try again with lowres
766 strchr(filename, '.')[-1] = 'l';
768 Warning("Trying to open movie file <%s> instead\n", filename);
771 } else if (strchr(filename, '.')[-1] == 'l') { // try again with highres
772 strchr(filename, '.')[-1] = 'h';
774 Warning("Trying to open movie file <%s> instead\n", filename);
782 if (is_robots && movie_libs[libnum]!=NULL)
783 robot_movies = high_res?2:1;
787 //find and initialize the movie libraries
793 for (i=0;i<N_BUILTIN_MOVIE_LIBS;i++) {
795 if (!strnicmp(movielib_files[i],"robot",5))
800 init_movie(movielib_files[i],i,is_robots,1);
803 movie_libs[EXTRA_ROBOT_LIB] = NULL;
805 atexit(close_movies);
809 void init_extra_robot_movie(char *filename)
811 close_movie(EXTRA_ROBOT_LIB);
812 init_movie(filename,EXTRA_ROBOT_LIB,1,0);
816 //looks through a movie library for a movie file
817 //returns filehandle, with fileposition at movie, or -1 if can't find
818 int search_movie_lib(movielib *lib,char *filename,int must_have)
826 for (i=0;i<lib->n_movies;i++)
827 if (!stricmp(filename,lib->movies[i].name)) { //found the movie in a library
830 from_cd = (lib->flags & MLF_ON_CD);
833 songs_stop_redbook(); //ready to read from CD
835 do { //keep trying until we get the file handle
838 /* movie_handle = */ filehandle = open(lib->name, O_RDONLY | O_BINARY);
840 /* movie_handle = */ filehandle = open(lib->name, O_RDONLY);
843 if ((filehandle == -1) && (AltHogdir_initialized)) {
845 strcpy(temp, AltHogDir);
847 strcat(temp, lib->name);
849 filehandle = open(temp, O_RDONLY | O_BINARY);
851 filehandle = open(temp, O_RDONLY);
855 if (must_have && from_cd && filehandle == -1) { //didn't get file!
857 if (request_cd() == -1) //ESC from requester
858 break; //bail from here. will get error later
861 } while (must_have && from_cd && filehandle == -1);
863 if (filehandle != -1)
864 lseek(filehandle,(/* movie_start = */ lib->movies[i].offset),SEEK_SET);
873 //returns file handle
874 int open_movie_file(char *filename,int must_have)
878 for (i=0;i<N_MOVIE_LIBS;i++) {
879 if ((filehandle = search_movie_lib(movie_libs[i],filename,must_have)) != -1)
883 return -1; //couldn't find it