1 /* $Id: movie.c,v 1.32 2003-11-07 06:30:06 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.
26 static char rcsid[] = "$Id: movie.c,v 1.32 2003-11-07 06:30:06 btb Exp $";
29 #define DEBUG_LEVEL CON_NORMAL
32 #include <sys/types.h>
58 extern int MenuHiresAvailable;
59 extern char CDROM_dir[];
69 short first_frame,last_frame;
73 #define MAX_SUBTITLES 500
74 #define MAX_ACTIVE_SUBTITLES 3
75 subtitle Subtitles[MAX_SUBTITLES];
80 char name[FILENAME_LEN];
85 #define MAX_MOVIES_PER_LIB 50 //determines size of malloc
88 char name[100]; //[FILENAME_LEN];
95 char movielib_files[][FILENAME_LEN] = {"intro-l.mvl","other-l.mvl","robots-l.mvl","oem-l.mvl"};
97 char movielib_files[][FILENAME_LEN] = {"intro-l.mvl","other-l.mvl","robots-l.mvl"};
100 #define N_BUILTIN_MOVIE_LIBS (sizeof(movielib_files)/sizeof(*movielib_files))
101 #define N_MOVIE_LIBS (N_BUILTIN_MOVIE_LIBS+1)
102 #define EXTRA_ROBOT_LIB N_BUILTIN_MOVIE_LIBS
103 movielib *movie_libs[N_MOVIE_LIBS];
106 //do we have the robot movies available
107 int robot_movies = 0; //0 means none, 1 means lowres, 2 means hires
109 int MovieHires = 1; //default is highres
111 CFILE *RoboFile = NULL;
114 // Function Prototypes
115 int RunMovie(char *filename, int highres_flag, int allow_abort,int dx,int dy);
117 CFILE *open_movie_file(char *filename, int must_have);
118 int reset_movie_file(CFILE *handle);
120 void change_filename_ext( char *dest, char *src, char *ext );
121 void decode_text_line(char *p);
122 void draw_subtitles(int frame_num);
125 // ----------------------------------------------------------------------
126 void* MPlayAlloc(unsigned size)
128 return d_malloc(size);
131 void MPlayFree(void *p)
137 //-----------------------------------------------------------------------
139 unsigned int FileRead(void *handle, void *buf, unsigned int count)
142 numread = cfread(buf, 1, count, (CFILE *)handle);
143 return (numread == count);
147 //-----------------------------------------------------------------------
150 //filename will actually get modified to be either low-res or high-res
151 //returns status. see values in movie.h
152 int PlayMovie(const char *filename, int must_have)
154 char name[FILENAME_LEN],*p;
158 if (FindArg("-nomovies"))
159 return MOVIE_NOT_PLAYED;
162 strcpy(name,filename);
164 if ((p=strchr(name,'.')) == NULL) //add extension, if missing
167 //check for escape already pressed & abort if so
168 while ((c=key_inkey()) != 0)
170 return MOVIE_ABORTED;
172 // Stop all digital sounds currently playing.
181 if (!FindArg("-nosound"))
186 ret = RunMovie(name,MovieHires,must_have,-1,-1);
188 if (!FindArg("-nosound"))
191 Screen_mode = -1; //force screen reset
197 void MovieShowFrame(ubyte *buf, uint bufw, uint bufh, uint sx, uint sy,
198 uint w, uint h, uint dstx, uint dsty)
200 grs_bitmap source_bm;
202 //mprintf((0,"MovieShowFrame %d,%d %d,%d %d,%d %d,%d\n",bufw,bufh,sx,sy,w,h,dstx,dsty));
204 Assert(bufw == w && bufh == h);
206 source_bm.bm_x = source_bm.bm_y = 0;
207 source_bm.bm_w = source_bm.bm_rowsize = bufw;
208 source_bm.bm_h = bufh;
209 source_bm.bm_type = BM_LINEAR;
210 source_bm.bm_flags = 0;
211 source_bm.bm_data = buf;
213 gr_bm_ubitblt(bufw,bufh,dstx,dsty,sx,sy,&source_bm,&grd_curcanv->cv_bitmap);
216 //our routine to set the pallete, called from the movie code
217 void MovieSetPalette(unsigned char *p, unsigned start, unsigned count)
222 //mprintf((0,"SetPalette p=%x, start=%d, count=%d\n",p,start,count));
224 //Color 0 should be black, and we get color 255
225 Assert(start>=1 && start+count-1<=254);
227 //Set color 0 to be black
228 gr_palette[0] = gr_palette[1] = gr_palette[2] = 0;
230 //Set color 255 to be our subtitle color
231 gr_palette[765] = gr_palette[766] = gr_palette[767] = 50;
233 //movie libs palette into our array
234 memcpy(gr_palette+start*3,p+start*3,count*3);
236 //finally set the palette in the hardware
237 //gr_palette_load(gr_palette);
239 //MVE_SetPalette(p, start, count);
245 short x, y, w, h; // The location of the menu.
246 grs_bitmap * bmp; // The background under the menu.
249 bkg movie_bg = {0,0,0,0,NULL};
252 #define BOX_BORDER (MenuHires?40:20)
255 void show_pause_message(char *msg)
260 gr_set_current_canvas(NULL);
261 gr_set_curfont( SMALL_FONT );
263 gr_get_string_size(msg,&w,&h,&aw);
265 x = (grd_curscreen->sc_w-w)/2;
266 y = (grd_curscreen->sc_h-h)/2;
270 gr_free_bitmap(movie_bg.bmp);
274 // Save the background of the display
275 movie_bg.x=x; movie_bg.y=y; movie_bg.w=w; movie_bg.h=h;
277 movie_bg.bmp = gr_create_bitmap( w+BOX_BORDER, h+BOX_BORDER );
279 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 );
283 gr_rect(x-BOX_BORDER/2,y-BOX_BORDER/2,x+w+BOX_BORDER/2-1,y+h+BOX_BORDER/2-1);
285 gr_set_fontcolor( 255, -1 );
287 gr_ustring( 0x8000, y, msg );
292 void clear_pause_message()
297 gr_bitmap(movie_bg.x-BOX_BORDER/2, movie_bg.y-BOX_BORDER/2, movie_bg.bmp);
299 gr_free_bitmap(movie_bg.bmp);
306 //returns status. see movie.h
307 int RunMovie(char *filename, int hires_flag, int must_have,int dx,int dy)
310 int result=1,aborted=0;
320 // Open Movie file. If it doesn't exist, no movie, just return.
322 filehndl = open_movie_file(filename,must_have);
324 if (filehndl == NULL)
327 con_printf(CON_NORMAL, "movie: RunMovie: Cannot open movie <%s>\n",filename);
328 return MOVIE_NOT_PLAYED;
331 MVE_memCallbacks(MPlayAlloc, MPlayFree);
332 MVE_ioCallbacks(FileRead);
335 gr_set_mode(SM(640,480));
337 gr_set_mode(SM(320,200));
340 set_screen_mode(SCREEN_MENU);
341 gr_copy_palette(pal_save, gr_palette, 768);
342 memset(gr_palette, 0, 768);
343 gr_palette_load(gr_palette);
346 #if !defined(POLY_ACC)
347 MVE_sfCallbacks(MovieShowFrame);
348 MVE_palCallbacks(MovieSetPalette);
351 if (MVE_rmPrepMovie((void *)filehndl, dx, dy, track)) {
353 return MOVIE_NOT_PLAYED;
358 FontHires = FontHiresAvailable && hires_flag;
360 while((result = MVE_rmStepMovie()) == 0) {
362 draw_subtitles(frame_num);
364 gr_palette_load(gr_palette); // moved this here because of flashing
370 // If ESCAPE pressed, then quit movie.
371 if (key == KEY_ESC) {
372 result = aborted = 1;
376 // If PAUSE pressed, then pause movie
377 if (key == KEY_PAUSE) {
379 show_pause_message(TXT_PAUSE);
380 while (!key_inkey()) ;
381 clear_pause_message();
384 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
385 if ((key == KEY_ALTED+KEY_ENTER) ||
386 (key == KEY_ALTED+KEY_PADENTER))
387 gr_toggle_fullscreen();
393 Assert(aborted || result == MVE_ERR_EOF); ///movie should be over
397 cfclose(filehndl); // Close Movie File
399 // Restore old graphic state
401 Screen_mode=-1; //force reset of screen mode
403 gr_copy_palette(gr_palette, pal_save, 768);
404 gr_palette_load(pal_save);
407 return (aborted?MOVIE_ABORTED:MOVIE_PLAYED_FULL);
411 int InitMovieBriefing()
415 gr_set_mode(SM(640,480));
417 gr_set_mode(SM(320,200));
419 gr_init_sub_canvas( &VR_screen_pages[0], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
420 gr_init_sub_canvas( &VR_screen_pages[1], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
427 //returns 1 if frame updated ok
432 err = MVE_rmStepMovie();
434 gr_palette_load(gr_palette);
436 if (err == MVE_ERR_EOF) //end of movie, so reset
438 reset_movie_file(RoboFile);
439 if (MVE_rmPrepMovie((void *)RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
453 void DeInitRobotMovie(void)
456 cfclose(RoboFile); // Close Movie File
460 int InitRobotMovie(char *filename)
462 if (FindArg("-nomovies"))
465 con_printf(DEBUG_LEVEL, "RoboFile=%s\n", filename);
467 MVE_sndInit(-1); //tell movies to play no sound for robots
469 RoboFile = open_movie_file(filename, 1);
471 if (RoboFile == NULL)
473 Warning("movie: InitRobotMovie: Cannot open movie file <%s>",filename);
474 return MOVIE_NOT_PLAYED;
477 Vid_State = VID_PLAY;
479 if (MVE_rmPrepMovie((void *)RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
484 RoboFilePos = cfseek(RoboFile, 0L, SEEK_CUR);
486 con_printf(DEBUG_LEVEL, "RoboFilePos=%d!\n", RoboFilePos);
493 * Subtitle system code
496 ubyte *subtitle_raw_data;
499 //search for next field following whitespace
500 ubyte *next_field(ubyte *p)
502 while (*p && !isspace(*p))
508 while (*p && isspace(*p))
518 int init_subtitles(char *filename)
527 if (! FindArg("-subtitles"))
530 ifile = cfopen(filename,"rb"); //try text version
532 if (!ifile) { //no text version, try binary version
533 char filename2[FILENAME_LEN];
534 change_filename_ext(filename2,filename,".TXB");
535 ifile = cfopen(filename2,"rb");
541 size = cfilelength(ifile);
543 MALLOC (subtitle_raw_data, ubyte, size+1);
545 read_count = cfread(subtitle_raw_data, 1, size, ifile);
549 subtitle_raw_data[size] = 0;
551 if (read_count != size) {
552 d_free(subtitle_raw_data);
556 p = subtitle_raw_data;
558 while (p && p < subtitle_raw_data+size) {
561 endp = strchr(p,'\n');
563 if (endp[-1] == '\r')
564 endp[-1] = 0; //handle 0d0a pair
565 *endp = 0; //string termintor
572 Subtitles[Num_subtitles].first_frame = atoi(p);
573 p = next_field(p); if (!p) continue;
574 Subtitles[Num_subtitles].last_frame = atoi(p);
575 p = next_field(p); if (!p) continue;
576 Subtitles[Num_subtitles].msg = p;
578 Assert(Num_subtitles==0 || Subtitles[Num_subtitles].first_frame >= Subtitles[Num_subtitles-1].first_frame);
579 Assert(Subtitles[Num_subtitles].last_frame >= Subtitles[Num_subtitles].first_frame);
592 void close_subtitles()
594 if (subtitle_raw_data)
595 d_free(subtitle_raw_data);
596 subtitle_raw_data = NULL;
601 //draw the subtitles for this frame
602 void draw_subtitles(int frame_num)
604 static int active_subtitles[MAX_ACTIVE_SUBTITLES];
605 static int num_active_subtitles,next_subtitle,line_spacing;
609 if (frame_num == 0) {
610 num_active_subtitles = 0;
612 gr_set_curfont( GAME_FONT );
613 line_spacing = grd_curcanv->cv_font->ft_h + (grd_curcanv->cv_font->ft_h >> 2);
614 gr_set_fontcolor(255,-1);
617 //get rid of any subtitles that have expired
618 for (t=0;t<num_active_subtitles;)
619 if (frame_num > Subtitles[active_subtitles[t]].last_frame) {
621 for (t2=t;t2<num_active_subtitles-1;t2++)
622 active_subtitles[t2] = active_subtitles[t2+1];
623 num_active_subtitles--;
629 //get any subtitles new for this frame
630 while (next_subtitle < Num_subtitles && frame_num >= Subtitles[next_subtitle].first_frame) {
631 if (num_active_subtitles >= MAX_ACTIVE_SUBTITLES)
632 Error("Too many active subtitles!");
633 active_subtitles[num_active_subtitles++] = next_subtitle;
637 //find y coordinate for first line of subtitles
638 y = grd_curcanv->cv_bitmap.bm_h-((line_spacing+1)*MAX_ACTIVE_SUBTITLES+2);
640 //erase old subtitles if necessary
643 gr_rect(0,y,grd_curcanv->cv_bitmap.bm_w-1,grd_curcanv->cv_bitmap.bm_h-1);
646 //now draw the current subtitles
647 for (t=0;t<num_active_subtitles;t++)
648 if (active_subtitles[t] != -1) {
649 gr_string(0x8000,y,Subtitles[active_subtitles[t]].msg);
655 movielib *init_new_movie_lib(char *filename, CFILE *fp)
661 //read movie file header
663 nfiles = cfile_read_int(fp); //get number of files
665 //table = d_malloc(sizeof(*table) + sizeof(ml_entry)*nfiles);
666 MALLOC(table, movielib, 1);
667 MALLOC(table->movies, ml_entry, nfiles);
669 strcpy(table->name,filename);
670 table->n_movies = nfiles;
672 offset = 4+4+nfiles*(13+4); //id + nfiles + nfiles * (filename + size)
674 for (i=0;i<nfiles;i++) {
677 n = cfread(table->movies[i].name, 13, 1, fp);
679 break; //end of file (probably)
681 len = cfile_read_int(fp);
683 table->movies[i].len = len;
684 table->movies[i].offset = offset;
686 offset += table->movies[i].len;
699 movielib *init_old_movie_lib(char *filename, CFILE *fp)
703 movielib *table,*table2;
708 table = d_malloc(sizeof(*table) + sizeof(ml_entry)*MAX_MOVIES_PER_LIB);
713 i = cfread(table->movies[nfiles].name, 13, 1, fp);
715 break; //end of file (probably)
717 i = cfread(&len, 4, 1, fp);
719 Error("error reading movie library <%s>",filename);
721 table->movies[nfiles].len = INTEL_INT(len);
722 table->movies[nfiles].offset = cftell(fp);
724 cfseek(fp, INTEL_INT(len), SEEK_CUR); //skip data
729 //allocate correct-sized table
730 size = sizeof(*table) + sizeof(ml_entry)*nfiles;
731 table2 = d_malloc(size);
732 memcpy(table2,table,size);
736 strcpy(table->name,filename);
738 table->n_movies = nfiles;
749 //find the specified movie library, and read in list of movies in it
750 movielib *init_movie_lib(char *filename)
752 //note: this based on cfile_init_hogfile()
757 fp = cfopen(filename, "rb");
762 cfread(id, 4, 1, fp);
763 if ( !strncmp( id, "DMVL", 4 ) )
764 return init_new_movie_lib(filename,fp);
765 else if ( !strncmp( id, "DHF", 3 ) ) {
766 cfseek(fp,-1,SEEK_CUR); //old file had 3 char id
767 return init_old_movie_lib(filename,fp);
776 void close_movie(int i)
779 d_free(movie_libs[i]->movies);
780 d_free(movie_libs[i]);
789 for (i=0;i<N_MOVIE_LIBS;i++)
794 //ask user to put the D2 CD in.
795 //returns -1 if ESC pressed, 0 if OK chosen
796 //CD may not have been inserted
800 ubyte save_pal[256*3];
801 grs_canvas *save_canv,*tcanv;
802 int ret,was_faded=gr_palette_faded_out;
806 save_canv = grd_curcanv;
807 tcanv = gr_create_canvas(grd_curcanv->cv_w,grd_curcanv->cv_h);
809 gr_set_current_canvas(tcanv);
810 gr_ubitmap(0,0,&save_canv->cv_bitmap);
811 gr_set_current_canvas(save_canv);
813 gr_clear_canvas(BM_XRGB(0,0,0));
815 memcpy(save_pal,gr_palette,sizeof(save_pal));
817 memcpy(gr_palette,last_palette_for_color_fonts,sizeof(gr_palette));
821 ret = nm_messagebox( "CD ERROR", 1, "Ok", "Please insert your Descent II CD");
826 ret2 = nm_messagebox( "CD ERROR", 2, "Try Again", "Leave Game", "You must insert your\nDescent II CD to Continue");
828 if (ret2 == -1 || ret2 == 0)
832 force_rb_register = 1; //disc has changed; force register new CD
836 memcpy(gr_palette,save_pal,sizeof(save_pal));
838 gr_ubitmap(0,0,&tcanv->cv_bitmap);
841 gr_palette_load(gr_palette);
843 gr_free_canvas(tcanv);
847 con_printf(DEBUG_LEVEL, "STUB: movie: request_cd\n");
853 void init_movie(char *filename, int libnum, int is_robots, int required)
856 char *res = strchr(filename, '.') - 1; // 'h' == high resolution, 'l' == low
859 if (FindArg("-nomovies")) {
860 movie_libs[libnum] = NULL;
865 //for robots, load highres versions if highres menus set
867 high_res = MenuHiresAvailable;
869 high_res = MovieHires;
874 for (try = 0; (movie_libs[libnum] = init_movie_lib(filename)) == NULL; try++) {
877 strcpy(name2,CDROM_dir);
878 strcat(name2,filename);
879 movie_libs[libnum] = init_movie_lib(name2);
881 if (movie_libs[libnum] != NULL) {
882 movie_libs[libnum]->flags |= MLF_ON_CD;
883 break; // we found our movie on the CD
885 if (try == 0) { // first try
886 if (*res == 'h') { // try low res instead
889 } else if (*res == 'l') { // try high
894 Warning("Cannot open movie file <%s>",filename);
900 Warning("Cannot open any movie file <%s>", filename);
907 if (is_robots && movie_libs[libnum]!=NULL)
908 robot_movies = high_res?2:1;
912 //find and initialize the movie libraries
918 for (i=0;i<N_BUILTIN_MOVIE_LIBS;i++) {
920 if (!strnicmp(movielib_files[i],"robot",5))
925 init_movie(movielib_files[i],i,is_robots,1);
928 movie_libs[EXTRA_ROBOT_LIB] = NULL;
930 atexit(close_movies);
934 void init_extra_robot_movie(char *filename)
936 close_movie(EXTRA_ROBOT_LIB);
937 init_movie(filename,EXTRA_ROBOT_LIB,1,0);
944 //looks through a movie library for a movie file
945 //returns filehandle, with fileposition at movie, or -1 if can't find
946 CFILE *search_movie_lib(movielib *lib, char *filename, int must_have)
954 for (i=0;i<lib->n_movies;i++)
955 if (!stricmp(filename,lib->movies[i].name)) { //found the movie in a library
958 from_cd = (lib->flags & MLF_ON_CD);
961 songs_stop_redbook(); //ready to read from CD
963 do { //keep trying until we get the file handle
965 movie_handle = filehandle = cfopen(lib->name, "rb");
967 if (must_have && from_cd && filehandle == NULL)
970 if (request_cd() == -1) //ESC from requester
971 break; //bail from here. will get error later
974 } while (must_have && from_cd && filehandle == NULL);
977 cfseek(filehandle, (movie_start = lib->movies[i].offset), SEEK_SET);
986 //returns file handle
987 CFILE *open_movie_file(char *filename, int must_have)
992 for (i=0;i<N_MOVIE_LIBS;i++) {
993 if ((filehandle = search_movie_lib(movie_libs[i], filename, must_have)) != NULL)
997 return NULL; //couldn't find it
1000 //sets the file position to the start of this already-open file
1001 int reset_movie_file(CFILE *handle)
1003 Assert(handle == movie_handle);
1005 cfseek(handle, movie_start, SEEK_SET);
1007 return 0; //everything is cool