1 /* $Id: movie.c,v 1.21 2003-02-24 12:03:16 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.21 2003-02-24 12:03:16 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];
76 char name[FILENAME_LEN];
81 #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];
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
107 int RoboFile = 0, RoboFilePos = 0;
109 // Function Prototypes
110 int RunMovie(char *filename, int highres_flag, int allow_abort,int dx,int dy);
112 int open_movie_file(char *filename,int must_have);
113 int reset_movie_file(int handle);
115 void change_filename_ext( char *dest, char *src, char *ext );
116 void decode_text_line(char *p);
117 void draw_subtitles(int frame_num);
120 //-----------------------------------------------------------------------
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;
131 if (FindArg("-nomovies"))
132 return MOVIE_NOT_PLAYED;
135 strcpy(name,filename);
137 if ((p=strchr(name,'.')) == NULL) //add extension, if missing
140 //check for escape already pressed & abort if so
141 while ((c=key_inkey()) != 0)
143 return MOVIE_ABORTED;
145 // Stop all digital sounds currently playing.
154 if (!FindArg("-nosound"))
159 ret = RunMovie(name,MovieHires,must_have,-1,-1);
161 if (!FindArg("-nosound"))
164 Screen_mode = -1; //force screen reset
172 short x, y, w, h; // The location of the menu.
173 grs_bitmap * bmp; // The background under the menu.
176 bkg movie_bg = {0,0,0,0,NULL};
179 #define BOX_BORDER (MenuHires?40:20)
182 void show_pause_message(char *msg)
187 gr_set_current_canvas(NULL);
188 gr_set_curfont( SMALL_FONT );
190 gr_get_string_size(msg,&w,&h,&aw);
192 x = (grd_curscreen->sc_w-w)/2;
193 y = (grd_curscreen->sc_h-h)/2;
197 gr_free_bitmap(movie_bg.bmp);
201 // Save the background of the display
202 movie_bg.x=x; movie_bg.y=y; movie_bg.w=w; movie_bg.h=h;
204 movie_bg.bmp = gr_create_bitmap( w+BOX_BORDER, h+BOX_BORDER );
206 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 );
210 gr_rect(x-BOX_BORDER/2,y-BOX_BORDER/2,x+w+BOX_BORDER/2-1,y+h+BOX_BORDER/2-1);
212 gr_set_fontcolor( 255, -1 );
214 gr_ustring( 0x8000, y, msg );
219 void clear_pause_message()
224 gr_bitmap(movie_bg.x-BOX_BORDER/2, movie_bg.y-BOX_BORDER/2, movie_bg.bmp);
226 gr_free_bitmap(movie_bg.bmp);
233 //returns status. see movie.h
234 int RunMovie(char *filename, int hires_flag, int must_have,int dx,int dy)
237 int result=1,aborted=0;
244 // Open Movie file. If it doesn't exist, no movie, just return.
246 filehndl = open_movie_file(filename,must_have);
248 if (filehndl == -1) {
250 Warning("movie: RunMovie: Cannot open movie <%s>\n",filename);
251 return MOVIE_NOT_PLAYED;
255 gr_set_mode(SM(640,480));
257 gr_set_mode(SM(320,200));
260 if (MVE_rmPrepMovie(filehndl, dx, dy, track)) {
262 return MOVIE_NOT_PLAYED;
267 FontHires = FontHiresAvailable && hires_flag;
269 while((result = MVE_rmStepMovie()) == 0) {
271 draw_subtitles(frame_num);
277 // If ESCAPE pressed, then quit movie.
278 if (key == KEY_ESC) {
279 result = aborted = 1;
283 // If PAUSE pressed, then pause movie
284 if (key == KEY_PAUSE) {
286 show_pause_message(TXT_PAUSE);
287 while (!key_inkey()) ;
288 clear_pause_message();
291 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
292 if ((key == KEY_CTRLED+KEY_SHIFTED+KEY_PADENTER) ||
293 (key == KEY_ALTED+KEY_CTRLED+KEY_PADENTER) ||
294 (key == KEY_ALTED+KEY_SHIFTED+KEY_PADENTER))
295 gr_toggle_fullscreen();
301 Assert(aborted || result == MVE_ERR_EOF); ///movie should be over
305 close(filehndl); // Close Movie File
307 // Restore old graphic state
309 Screen_mode=-1; //force reset of screen mode
311 return (aborted?MOVIE_ABORTED:MOVIE_PLAYED_FULL);
315 int InitMovieBriefing()
318 gr_set_mode(SM(640,480));
320 gr_set_mode(SM(320,200));
322 gr_init_sub_canvas( &VR_screen_pages[0], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
323 gr_init_sub_canvas( &VR_screen_pages[1], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
329 //returns 1 if frame updated ok
334 err = MVE_rmStepMovie();
336 if (err == MVE_ERR_EOF) //end of movie, so reset
338 reset_movie_file(RoboFile);
339 if (MVE_rmPrepMovie(RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
353 void DeInitRobotMovie(void)
356 close(RoboFile); // Close Movie File
360 int InitRobotMovie(char *filename)
362 if (FindArg("-nomovies"))
365 con_printf(DEBUG_LEVEL, "RoboFile=%s\n", filename);
367 MVE_sndInit(-1); //tell movies to play no sound for robots
369 RoboFile = open_movie_file(filename, 1);
371 if (RoboFile == -1) {
372 Warning("movie: InitRobotMovie: Cannot open movie file <%s>",filename);
373 return MOVIE_NOT_PLAYED;
376 Vid_State = VID_PLAY;
378 if (MVE_rmPrepMovie(RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
383 RoboFilePos=lseek (RoboFile,0L,SEEK_CUR);
385 con_printf(DEBUG_LEVEL, "RoboFilePos=%d!\n", RoboFilePos);
392 * Subtitle system code
395 ubyte *subtitle_raw_data;
398 //search for next field following whitespace
399 ubyte *next_field(ubyte *p)
401 while (*p && !isspace(*p))
407 while (*p && isspace(*p))
417 int init_subtitles(char *filename)
426 if (! FindArg("-subtitles"))
429 ifile = cfopen(filename,"rb"); //try text version
431 if (!ifile) { //no text version, try binary version
432 char filename2[FILENAME_LEN];
433 change_filename_ext(filename2,filename,".TXB");
434 ifile = cfopen(filename2,"rb");
440 size = cfilelength(ifile);
442 MALLOC (subtitle_raw_data, ubyte, size+1);
444 read_count = cfread(subtitle_raw_data, 1, size, ifile);
448 subtitle_raw_data[size] = 0;
450 if (read_count != size) {
451 d_free(subtitle_raw_data);
455 p = subtitle_raw_data;
457 while (p && p < subtitle_raw_data+size) {
460 endp = strchr(p,'\n');
462 if (endp[-1] == '\r')
463 endp[-1] = 0; //handle 0d0a pair
464 *endp = 0; //string termintor
471 Subtitles[Num_subtitles].first_frame = atoi(p);
472 p = next_field(p); if (!p) continue;
473 Subtitles[Num_subtitles].last_frame = atoi(p);
474 p = next_field(p); if (!p) continue;
475 Subtitles[Num_subtitles].msg = p;
477 Assert(Num_subtitles==0 || Subtitles[Num_subtitles].first_frame >= Subtitles[Num_subtitles-1].first_frame);
478 Assert(Subtitles[Num_subtitles].last_frame >= Subtitles[Num_subtitles].first_frame);
491 void close_subtitles()
493 if (subtitle_raw_data)
494 d_free(subtitle_raw_data);
495 subtitle_raw_data = NULL;
500 //draw the subtitles for this frame
501 void draw_subtitles(int frame_num)
503 static int active_subtitles[MAX_ACTIVE_SUBTITLES];
504 static int num_active_subtitles,next_subtitle,line_spacing;
508 if (frame_num == 0) {
509 num_active_subtitles = 0;
511 gr_set_curfont( GAME_FONT );
512 line_spacing = grd_curcanv->cv_font->ft_h + (grd_curcanv->cv_font->ft_h >> 2);
513 gr_set_fontcolor(255,-1);
516 //get rid of any subtitles that have expired
517 for (t=0;t<num_active_subtitles;)
518 if (frame_num > Subtitles[active_subtitles[t]].last_frame) {
520 for (t2=t;t2<num_active_subtitles-1;t2++)
521 active_subtitles[t2] = active_subtitles[t2+1];
522 num_active_subtitles--;
528 //get any subtitles new for this frame
529 while (next_subtitle < Num_subtitles && frame_num >= Subtitles[next_subtitle].first_frame) {
530 if (num_active_subtitles >= MAX_ACTIVE_SUBTITLES)
531 Error("Too many active subtitles!");
532 active_subtitles[num_active_subtitles++] = next_subtitle;
536 //find y coordinate for first line of subtitles
537 y = grd_curcanv->cv_bitmap.bm_h-((line_spacing+1)*MAX_ACTIVE_SUBTITLES+2);
539 //erase old subtitles if necessary
542 gr_rect(0,y,grd_curcanv->cv_bitmap.bm_w-1,grd_curcanv->cv_bitmap.bm_h-1);
545 //now draw the current subtitles
546 for (t=0;t<num_active_subtitles;t++)
547 if (active_subtitles[t] != -1) {
548 gr_string(0x8000,y,Subtitles[active_subtitles[t]].msg);
554 movielib *init_new_movie_lib(char *filename,FILE *fp)
560 //read movie file header
562 nfiles = file_read_int(fp); //get number of files
564 //table = d_malloc(sizeof(*table) + sizeof(ml_entry)*nfiles);
565 MALLOC(table, movielib, 1);
566 MALLOC(table->movies, ml_entry, nfiles);
568 strcpy(table->name,filename);
569 table->n_movies = nfiles;
571 offset = 4+4+nfiles*(13+4); //id + nfiles + nfiles * (filename + size)
573 for (i=0;i<nfiles;i++) {
576 n = fread( table->movies[i].name, 13, 1, fp );
578 break; //end of file (probably)
580 len = file_read_int(fp);
582 table->movies[i].len = len;
583 table->movies[i].offset = offset;
585 offset += table->movies[i].len;
598 movielib *init_old_movie_lib(char *filename,FILE *fp)
602 movielib *table,*table2;
607 table = d_malloc(sizeof(*table) + sizeof(ml_entry)*MAX_MOVIES_PER_LIB);
612 i = fread( table->movies[nfiles].name, 13, 1, fp );
614 break; //end of file (probably)
616 i = fread( &len, 4, 1, fp );
618 Error("error reading movie library <%s>",filename);
620 table->movies[nfiles].len = INTEL_INT(len);
621 table->movies[nfiles].offset = ftell( fp );
623 fseek( fp, INTEL_INT(len), SEEK_CUR ); //skip data
628 //allocate correct-sized table
629 size = sizeof(*table) + sizeof(ml_entry)*nfiles;
630 table2 = d_malloc(size);
631 memcpy(table2,table,size);
635 strcpy(table->name,filename);
637 table->n_movies = nfiles;
648 //find the specified movie library, and read in list of movies in it
649 movielib *init_movie_lib(char *filename)
651 //note: this based on cfile_init_hogfile()
656 fp = fopen( filename, "rb" );
658 if ((fp == NULL) && (AltHogdir_initialized)) {
660 strcpy(temp, AltHogDir);
662 strcat(temp, filename);
663 fp = fopen(temp, "rb");
669 fread( id, 4, 1, fp );
670 if ( !strncmp( id, "DMVL", 4 ) )
671 return init_new_movie_lib(filename,fp);
672 else if ( !strncmp( id, "DHF", 3 ) ) {
673 fseek(fp,-1,SEEK_CUR); //old file had 3 char id
674 return init_old_movie_lib(filename,fp);
683 void close_movie(int i)
686 d_free(movie_libs[i]->movies);
687 d_free(movie_libs[i]);
696 for (i=0;i<N_MOVIE_LIBS;i++)
701 //ask user to put the D2 CD in.
702 //returns -1 if ESC pressed, 0 if OK chosen
703 //CD may not have been inserted
707 ubyte save_pal[256*3];
708 grs_canvas *save_canv,*tcanv;
709 int ret,was_faded=gr_palette_faded_out;
713 save_canv = grd_curcanv;
714 tcanv = gr_create_canvas(grd_curcanv->cv_w,grd_curcanv->cv_h);
716 gr_set_current_canvas(tcanv);
717 gr_ubitmap(0,0,&save_canv->cv_bitmap);
718 gr_set_current_canvas(save_canv);
720 gr_clear_canvas(BM_XRGB(0,0,0));
722 memcpy(save_pal,gr_palette,sizeof(save_pal));
724 memcpy(gr_palette,last_palette_for_color_fonts,sizeof(gr_palette));
728 ret = nm_messagebox( "CD ERROR", 1, "Ok", "Please insert your Descent II CD");
733 ret2 = nm_messagebox( "CD ERROR", 2, "Try Again", "Leave Game", "You must insert your\nDescent II CD to Continue");
735 if (ret2 == -1 || ret2 == 0)
739 force_rb_register = 1; //disc has changed; force register new CD
743 memcpy(gr_palette,save_pal,sizeof(save_pal));
745 gr_ubitmap(0,0,&tcanv->cv_bitmap);
748 gr_palette_load(gr_palette);
750 gr_free_canvas(tcanv);
754 con_printf(DEBUG_LEVEL, "STUB: movie: request_cd\n");
760 void init_movie(char *filename,int libnum,int is_robots,int required)
766 if (FindArg("-nomovies")) {
767 movie_libs[libnum] = NULL;
772 //for robots, load highres versions if highres menus set
774 high_res = MenuHiresAvailable;
776 high_res = MovieHires;
779 strchr(filename,'.')[-1] = 'h';
783 if ((movie_libs[libnum] = init_movie_lib(filename)) == NULL) {
786 strcpy(name2,CDROM_dir);
787 strcat(name2,filename);
788 movie_libs[libnum] = init_movie_lib(name2);
790 if (movie_libs[libnum] != NULL)
791 movie_libs[libnum]->flags |= MLF_ON_CD;
794 Warning("Cannot open movie file <%s>\n",filename);
797 if (!try) { // first try
798 if (strchr(filename, '.')[-1] == 'h') { // try again with lowres
799 strchr(filename, '.')[-1] = 'l';
801 Warning("Trying to open movie file <%s> instead\n", filename);
804 } else if (strchr(filename, '.')[-1] == 'l') { // try again with highres
805 strchr(filename, '.')[-1] = 'h';
807 Warning("Trying to open movie file <%s> instead\n", filename);
815 if (is_robots && movie_libs[libnum]!=NULL)
816 robot_movies = high_res?2:1;
820 //find and initialize the movie libraries
826 for (i=0;i<N_BUILTIN_MOVIE_LIBS;i++) {
828 if (!strnicmp(movielib_files[i],"robot",5))
833 init_movie(movielib_files[i],i,is_robots,1);
836 movie_libs[EXTRA_ROBOT_LIB] = NULL;
838 atexit(close_movies);
842 void init_extra_robot_movie(char *filename)
844 close_movie(EXTRA_ROBOT_LIB);
845 init_movie(filename,EXTRA_ROBOT_LIB,1,0);
849 int movie_handle,movie_start;
851 //looks through a movie library for a movie file
852 //returns filehandle, with fileposition at movie, or -1 if can't find
853 int search_movie_lib(movielib *lib,char *filename,int must_have)
861 for (i=0;i<lib->n_movies;i++)
862 if (!stricmp(filename,lib->movies[i].name)) { //found the movie in a library
865 from_cd = (lib->flags & MLF_ON_CD);
868 songs_stop_redbook(); //ready to read from CD
870 do { //keep trying until we get the file handle
873 movie_handle = filehandle = open(lib->name, O_RDONLY | O_BINARY);
875 movie_handle = filehandle = open(lib->name, O_RDONLY);
878 if ((filehandle == -1) && (AltHogdir_initialized)) {
880 strcpy(temp, AltHogDir);
882 strcat(temp, lib->name);
884 movie_handle = filehandle = open(temp, O_RDONLY | O_BINARY);
886 movie_handle = filehandle = open(temp, O_RDONLY);
890 if (must_have && from_cd && filehandle == -1) { //didn't get file!
892 if (request_cd() == -1) //ESC from requester
893 break; //bail from here. will get error later
896 } while (must_have && from_cd && filehandle == -1);
898 if (filehandle != -1)
899 lseek(filehandle,(movie_start=lib->movies[i].offset),SEEK_SET);
908 //returns file handle
909 int open_movie_file(char *filename,int must_have)
913 for (i=0;i<N_MOVIE_LIBS;i++) {
914 if ((filehandle = search_movie_lib(movie_libs[i],filename,must_have)) != -1)
918 return -1; //couldn't find it
921 //sets the file position to the start of this already-open file
922 int reset_movie_file(int handle)
924 Assert(handle == movie_handle);
926 lseek(handle,movie_start,SEEK_SET);
928 return 0; //everything is cool