1 /* $Id: movie.c,v 1.19 2003-02-19 03:09:38 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.19 2003-02-19 03:09:38 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];
75 char name[FILENAME_LEN];
80 #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];
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
106 int RoboFile = 0, RoboFilePos = 0;
108 // Function Prototypes
109 int RunMovie(char *filename, int highres_flag, int allow_abort,int dx,int dy);
111 int open_movie_file(char *filename,int must_have);
112 int reset_movie_file(int handle);
114 void change_filename_ext( char *dest, char *src, char *ext );
115 void decode_text_line(char *p);
116 void draw_subtitles(int frame_num);
119 //-----------------------------------------------------------------------
122 //filename will actually get modified to be either low-res or high-res
123 //returns status. see values in movie.h
124 int PlayMovie(const char *filename, int must_have)
126 char name[FILENAME_LEN],*p;
130 if (FindArg("-nomovies"))
131 return MOVIE_NOT_PLAYED;
134 strcpy(name,filename);
136 if ((p=strchr(name,'.')) == NULL) //add extension, if missing
139 //check for escape already pressed & abort if so
140 while ((c=key_inkey()) != 0)
142 return MOVIE_ABORTED;
144 // Stop all digital sounds currently playing.
153 if (!FindArg("-nosound"))
158 ret = RunMovie(name,MovieHires,must_have,-1,-1);
160 if (!FindArg("-nosound"))
163 Screen_mode = -1; //force screen reset
171 short x, y, w, h; // The location of the menu.
172 grs_bitmap * bmp; // The background under the menu.
175 bkg movie_bg = {0,0,0,0,NULL};
178 #define BOX_BORDER (MenuHires?40:20)
181 void show_pause_message(char *msg)
186 gr_set_current_canvas(NULL);
187 gr_set_curfont( SMALL_FONT );
189 gr_get_string_size(msg,&w,&h,&aw);
191 x = (grd_curscreen->sc_w-w)/2;
192 y = (grd_curscreen->sc_h-h)/2;
196 gr_free_bitmap(movie_bg.bmp);
200 // Save the background of the display
201 movie_bg.x=x; movie_bg.y=y; movie_bg.w=w; movie_bg.h=h;
203 movie_bg.bmp = gr_create_bitmap( w+BOX_BORDER, h+BOX_BORDER );
205 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 );
209 gr_rect(x-BOX_BORDER/2,y-BOX_BORDER/2,x+w+BOX_BORDER/2-1,y+h+BOX_BORDER/2-1);
211 gr_set_fontcolor( 255, -1 );
213 gr_ustring( 0x8000, y, msg );
218 void clear_pause_message()
223 gr_bitmap(movie_bg.x-BOX_BORDER/2, movie_bg.y-BOX_BORDER/2, movie_bg.bmp);
225 gr_free_bitmap(movie_bg.bmp);
232 //returns status. see movie.h
233 int RunMovie(char *filename, int hires_flag, int must_have,int dx,int dy)
236 int result=1,aborted=0;
243 // Open Movie file. If it doesn't exist, no movie, just return.
245 filehndl = open_movie_file(filename,must_have);
247 if (filehndl == -1) {
249 Warning("movie: RunMovie: Cannot open movie <%s>\n",filename);
250 return MOVIE_NOT_PLAYED;
254 gr_set_mode(SM(640,480));
256 gr_set_mode(SM(320,200));
259 if (MVE_rmPrepMovie(filehndl, dx, dy, track)) {
261 return MOVIE_NOT_PLAYED;
266 FontHires = FontHiresAvailable && hires_flag;
268 while((result = MVE_rmStepMovie()) == 0) {
270 draw_subtitles(frame_num);
276 // If ESCAPE pressed, then quit movie.
277 if (key == KEY_ESC) {
278 result = aborted = 1;
282 // If PAUSE pressed, then pause movie
283 if (key == KEY_PAUSE) {
285 show_pause_message(TXT_PAUSE);
286 while (!key_inkey()) ;
287 clear_pause_message();
290 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
291 if ((key == KEY_CTRLED+KEY_SHIFTED+KEY_PADENTER) ||
292 (key == KEY_ALTED+KEY_CTRLED+KEY_PADENTER) ||
293 (key == KEY_ALTED+KEY_SHIFTED+KEY_PADENTER))
294 gr_toggle_fullscreen();
300 Assert(aborted || result == MVE_ERR_EOF); ///movie should be over
304 close(filehndl); // Close Movie File
306 // Restore old graphic state
308 Screen_mode=-1; //force reset of screen mode
310 return (aborted?MOVIE_ABORTED:MOVIE_PLAYED_FULL);
314 int InitMovieBriefing()
317 gr_set_mode(SM(640,480));
319 gr_set_mode(SM(320,200));
325 //returns 1 if frame updated ok
330 err = MVE_rmStepMovie();
332 if (err == MVE_ERR_EOF) //end of movie, so reset
334 reset_movie_file(RoboFile);
335 if (MVE_rmPrepMovie(RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
349 void DeInitRobotMovie(void)
352 close(RoboFile); // Close Movie File
356 int InitRobotMovie(char *filename)
358 if (FindArg("-nomovies"))
361 con_printf(DEBUG_LEVEL, "RoboFile=%s\n", filename);
363 MVE_sndInit(-1); //tell movies to play no sound for robots
365 RoboFile = open_movie_file(filename, 1);
367 if (RoboFile == -1) {
368 Warning("movie: InitRobotMovie: Cannot open movie file <%s>",filename);
369 return MOVIE_NOT_PLAYED;
372 Vid_State = VID_PLAY;
374 if (MVE_rmPrepMovie(RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
379 RoboFilePos=lseek (RoboFile,0L,SEEK_CUR);
381 con_printf(DEBUG_LEVEL, "RoboFilePos=%d!\n", RoboFilePos);
388 * Subtitle system code
391 ubyte *subtitle_raw_data;
394 //search for next field following whitespace
395 ubyte *next_field(ubyte *p)
397 while (*p && !isspace(*p))
403 while (*p && isspace(*p))
413 int init_subtitles(char *filename)
422 if (! FindArg("-subtitles"))
425 ifile = cfopen(filename,"rb"); //try text version
427 if (!ifile) { //no text version, try binary version
428 char filename2[FILENAME_LEN];
429 change_filename_ext(filename2,filename,".TXB");
430 ifile = cfopen(filename2,"rb");
436 size = cfilelength(ifile);
438 MALLOC (subtitle_raw_data, ubyte, size+1);
440 read_count = cfread(subtitle_raw_data, 1, size, ifile);
444 subtitle_raw_data[size] = 0;
446 if (read_count != size) {
447 d_free(subtitle_raw_data);
451 p = subtitle_raw_data;
453 while (p && p < subtitle_raw_data+size) {
456 endp = strchr(p,'\n');
458 if (endp[-1] == '\r')
459 endp[-1] = 0; //handle 0d0a pair
460 *endp = 0; //string termintor
467 Subtitles[Num_subtitles].first_frame = atoi(p);
468 p = next_field(p); if (!p) continue;
469 Subtitles[Num_subtitles].last_frame = atoi(p);
470 p = next_field(p); if (!p) continue;
471 Subtitles[Num_subtitles].msg = p;
473 Assert(Num_subtitles==0 || Subtitles[Num_subtitles].first_frame >= Subtitles[Num_subtitles-1].first_frame);
474 Assert(Subtitles[Num_subtitles].last_frame >= Subtitles[Num_subtitles].first_frame);
487 void close_subtitles()
489 if (subtitle_raw_data)
490 d_free(subtitle_raw_data);
491 subtitle_raw_data = NULL;
496 //draw the subtitles for this frame
497 void draw_subtitles(int frame_num)
499 static int active_subtitles[MAX_ACTIVE_SUBTITLES];
500 static int num_active_subtitles,next_subtitle,line_spacing;
504 if (frame_num == 0) {
505 num_active_subtitles = 0;
507 gr_set_curfont( GAME_FONT );
508 line_spacing = grd_curcanv->cv_font->ft_h + (grd_curcanv->cv_font->ft_h >> 2);
509 gr_set_fontcolor(255,-1);
512 //get rid of any subtitles that have expired
513 for (t=0;t<num_active_subtitles;)
514 if (frame_num > Subtitles[active_subtitles[t]].last_frame) {
516 for (t2=t;t2<num_active_subtitles-1;t2++)
517 active_subtitles[t2] = active_subtitles[t2+1];
518 num_active_subtitles--;
524 //get any subtitles new for this frame
525 while (next_subtitle < Num_subtitles && frame_num >= Subtitles[next_subtitle].first_frame) {
526 if (num_active_subtitles >= MAX_ACTIVE_SUBTITLES)
527 Error("Too many active subtitles!");
528 active_subtitles[num_active_subtitles++] = next_subtitle;
532 //find y coordinate for first line of subtitles
533 y = grd_curcanv->cv_bitmap.bm_h-((line_spacing+1)*MAX_ACTIVE_SUBTITLES+2);
535 //erase old subtitles if necessary
538 gr_rect(0,y,grd_curcanv->cv_bitmap.bm_w-1,grd_curcanv->cv_bitmap.bm_h-1);
541 //now draw the current subtitles
542 for (t=0;t<num_active_subtitles;t++)
543 if (active_subtitles[t] != -1) {
544 gr_string(0x8000,y,Subtitles[active_subtitles[t]].msg);
550 movielib *init_new_movie_lib(char *filename,FILE *fp)
556 //read movie file header
558 nfiles = file_read_int(fp); //get number of files
560 //table = d_malloc(sizeof(*table) + sizeof(ml_entry)*nfiles);
561 MALLOC(table, movielib, 1);
562 MALLOC(table->movies, ml_entry, nfiles);
564 strcpy(table->name,filename);
565 table->n_movies = nfiles;
567 offset = 4+4+nfiles*(13+4); //id + nfiles + nfiles * (filename + size)
569 for (i=0;i<nfiles;i++) {
572 n = fread( table->movies[i].name, 13, 1, fp );
574 break; //end of file (probably)
576 len = file_read_int(fp);
578 table->movies[i].len = len;
579 table->movies[i].offset = offset;
581 offset += table->movies[i].len;
594 movielib *init_old_movie_lib(char *filename,FILE *fp)
598 movielib *table,*table2;
603 table = d_malloc(sizeof(*table) + sizeof(ml_entry)*MAX_MOVIES_PER_LIB);
608 i = fread( table->movies[nfiles].name, 13, 1, fp );
610 break; //end of file (probably)
612 i = fread( &len, 4, 1, fp );
614 Error("error reading movie library <%s>",filename);
616 table->movies[nfiles].len = INTEL_INT(len);
617 table->movies[nfiles].offset = ftell( fp );
619 fseek( fp, INTEL_INT(len), SEEK_CUR ); //skip data
624 //allocate correct-sized table
625 size = sizeof(*table) + sizeof(ml_entry)*nfiles;
626 table2 = d_malloc(size);
627 memcpy(table2,table,size);
631 strcpy(table->name,filename);
633 table->n_movies = nfiles;
644 //find the specified movie library, and read in list of movies in it
645 movielib *init_movie_lib(char *filename)
647 //note: this based on cfile_init_hogfile()
652 fp = fopen( filename, "rb" );
654 if ((fp == NULL) && (AltHogdir_initialized)) {
656 strcpy(temp, AltHogDir);
658 strcat(temp, filename);
659 fp = fopen(temp, "rb");
665 fread( id, 4, 1, fp );
666 if ( !strncmp( id, "DMVL", 4 ) )
667 return init_new_movie_lib(filename,fp);
668 else if ( !strncmp( id, "DHF", 3 ) ) {
669 fseek(fp,-1,SEEK_CUR); //old file had 3 char id
670 return init_old_movie_lib(filename,fp);
679 void close_movie(int i)
682 d_free(movie_libs[i]->movies);
683 d_free(movie_libs[i]);
692 for (i=0;i<N_MOVIE_LIBS;i++)
697 //ask user to put the D2 CD in.
698 //returns -1 if ESC pressed, 0 if OK chosen
699 //CD may not have been inserted
703 ubyte save_pal[256*3];
704 grs_canvas *save_canv,*tcanv;
705 int ret,was_faded=gr_palette_faded_out;
709 save_canv = grd_curcanv;
710 tcanv = gr_create_canvas(grd_curcanv->cv_w,grd_curcanv->cv_h);
712 gr_set_current_canvas(tcanv);
713 gr_ubitmap(0,0,&save_canv->cv_bitmap);
714 gr_set_current_canvas(save_canv);
716 gr_clear_canvas(BM_XRGB(0,0,0));
718 memcpy(save_pal,gr_palette,sizeof(save_pal));
720 memcpy(gr_palette,last_palette_for_color_fonts,sizeof(gr_palette));
724 ret = nm_messagebox( "CD ERROR", 1, "Ok", "Please insert your Descent II CD");
729 ret2 = nm_messagebox( "CD ERROR", 2, "Try Again", "Leave Game", "You must insert your\nDescent II CD to Continue");
731 if (ret2 == -1 || ret2 == 0)
735 force_rb_register = 1; //disc has changed; force register new CD
739 memcpy(gr_palette,save_pal,sizeof(save_pal));
741 gr_ubitmap(0,0,&tcanv->cv_bitmap);
744 gr_palette_load(gr_palette);
746 gr_free_canvas(tcanv);
750 con_printf(DEBUG_LEVEL, "STUB: movie: request_cd\n");
756 void init_movie(char *filename,int libnum,int is_robots,int required)
762 if (FindArg("-nomovies")) {
763 movie_libs[libnum] = NULL;
768 //for robots, load highres versions if highres menus set
770 high_res = MenuHiresAvailable;
772 high_res = MovieHires;
775 strchr(filename,'.')[-1] = 'h';
779 if ((movie_libs[libnum] = init_movie_lib(filename)) == NULL) {
782 strcpy(name2,CDROM_dir);
783 strcat(name2,filename);
784 movie_libs[libnum] = init_movie_lib(name2);
786 if (movie_libs[libnum] != NULL)
787 movie_libs[libnum]->flags |= MLF_ON_CD;
790 Warning("Cannot open movie file <%s>\n",filename);
793 if (!try) { // first try
794 if (strchr(filename, '.')[-1] == 'h') { // try again with lowres
795 strchr(filename, '.')[-1] = 'l';
797 Warning("Trying to open movie file <%s> instead\n", filename);
800 } else if (strchr(filename, '.')[-1] == 'l') { // try again with highres
801 strchr(filename, '.')[-1] = 'h';
803 Warning("Trying to open movie file <%s> instead\n", filename);
811 if (is_robots && movie_libs[libnum]!=NULL)
812 robot_movies = high_res?2:1;
816 //find and initialize the movie libraries
822 for (i=0;i<N_BUILTIN_MOVIE_LIBS;i++) {
824 if (!strnicmp(movielib_files[i],"robot",5))
829 init_movie(movielib_files[i],i,is_robots,1);
832 movie_libs[EXTRA_ROBOT_LIB] = NULL;
834 atexit(close_movies);
838 void init_extra_robot_movie(char *filename)
840 close_movie(EXTRA_ROBOT_LIB);
841 init_movie(filename,EXTRA_ROBOT_LIB,1,0);
845 int movie_handle,movie_start;
847 //looks through a movie library for a movie file
848 //returns filehandle, with fileposition at movie, or -1 if can't find
849 int search_movie_lib(movielib *lib,char *filename,int must_have)
857 for (i=0;i<lib->n_movies;i++)
858 if (!stricmp(filename,lib->movies[i].name)) { //found the movie in a library
861 from_cd = (lib->flags & MLF_ON_CD);
864 songs_stop_redbook(); //ready to read from CD
866 do { //keep trying until we get the file handle
869 movie_handle = filehandle = open(lib->name, O_RDONLY | O_BINARY);
871 movie_handle = filehandle = open(lib->name, O_RDONLY);
874 if ((filehandle == -1) && (AltHogdir_initialized)) {
876 strcpy(temp, AltHogDir);
878 strcat(temp, lib->name);
880 movie_handle = filehandle = open(temp, O_RDONLY | O_BINARY);
882 movie_handle = filehandle = open(temp, O_RDONLY);
886 if (must_have && from_cd && filehandle == -1) { //didn't get file!
888 if (request_cd() == -1) //ESC from requester
889 break; //bail from here. will get error later
892 } while (must_have && from_cd && filehandle == -1);
894 if (filehandle != -1)
895 lseek(filehandle,(movie_start=lib->movies[i].offset),SEEK_SET);
904 //returns file handle
905 int open_movie_file(char *filename,int must_have)
909 for (i=0;i<N_MOVIE_LIBS;i++) {
910 if ((filehandle = search_movie_lib(movie_libs[i],filename,must_have)) != -1)
914 return -1; //couldn't find it
917 //sets the file position to the start of this already-open file
918 int reset_movie_file(int handle)
920 Assert(handle == movie_handle);
922 lseek(handle,movie_start,SEEK_SET);
924 return 0; //everything is cool