1 /* $Id: movie.c,v 1.33 2003-11-26 12:26:30 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.33 2003-11-26 12:26:30 btb Exp $";
29 #define DEBUG_LEVEL CON_NORMAL
33 #include <sys/types.h>
62 extern int MenuHiresAvailable;
63 extern char CDROM_dir[];
73 short first_frame,last_frame;
77 #define MAX_SUBTITLES 500
78 #define MAX_ACTIVE_SUBTITLES 3
79 subtitle Subtitles[MAX_SUBTITLES];
84 char name[FILENAME_LEN];
89 #define MAX_MOVIES_PER_LIB 50 //determines size of malloc
92 char name[100]; //[FILENAME_LEN];
99 char movielib_files[][FILENAME_LEN] = {"intro-l.mvl","other-l.mvl","robots-l.mvl","oem-l.mvl"};
101 char movielib_files[][FILENAME_LEN] = {"intro-l.mvl","other-l.mvl","robots-l.mvl"};
104 #define N_BUILTIN_MOVIE_LIBS (sizeof(movielib_files)/sizeof(*movielib_files))
105 #define N_MOVIE_LIBS (N_BUILTIN_MOVIE_LIBS+1)
106 #define EXTRA_ROBOT_LIB N_BUILTIN_MOVIE_LIBS
107 movielib *movie_libs[N_MOVIE_LIBS];
110 //do we have the robot movies available
111 int robot_movies = 0; //0 means none, 1 means lowres, 2 means hires
113 int MovieHires = 1; //default is highres
115 CFILE *RoboFile = NULL;
118 // Function Prototypes
119 int RunMovie(char *filename, int highres_flag, int allow_abort,int dx,int dy);
121 CFILE *open_movie_file(char *filename, int must_have);
122 int reset_movie_file(CFILE *handle);
124 void change_filename_ext( char *dest, char *src, char *ext );
125 void decode_text_line(char *p);
126 void draw_subtitles(int frame_num);
129 // ----------------------------------------------------------------------
130 void* MPlayAlloc(unsigned size)
132 return d_malloc(size);
135 void MPlayFree(void *p)
141 //-----------------------------------------------------------------------
143 unsigned int FileRead(void *handle, void *buf, unsigned int count)
146 numread = cfread(buf, 1, count, (CFILE *)handle);
147 return (numread == count);
151 //-----------------------------------------------------------------------
154 //filename will actually get modified to be either low-res or high-res
155 //returns status. see values in movie.h
156 int PlayMovie(const char *filename, int must_have)
158 char name[FILENAME_LEN],*p;
162 if (FindArg("-nomovies"))
163 return MOVIE_NOT_PLAYED;
166 strcpy(name,filename);
168 if ((p=strchr(name,'.')) == NULL) //add extension, if missing
171 //check for escape already pressed & abort if so
172 while ((c=key_inkey()) != 0)
174 return MOVIE_ABORTED;
176 // Stop all digital sounds currently playing.
185 if (!FindArg("-nosound"))
190 ret = RunMovie(name,MovieHires,must_have,-1,-1);
192 if (!FindArg("-nosound"))
195 Screen_mode = -1; //force screen reset
201 void MovieShowFrame(ubyte *buf, uint bufw, uint bufh, uint sx, uint sy,
202 uint w, uint h, uint dstx, uint dsty)
204 grs_bitmap source_bm;
206 //mprintf((0,"MovieShowFrame %d,%d %d,%d %d,%d %d,%d\n",bufw,bufh,sx,sy,w,h,dstx,dsty));
208 Assert(bufw == w && bufh == h);
210 source_bm.bm_x = source_bm.bm_y = 0;
211 source_bm.bm_w = source_bm.bm_rowsize = bufw;
212 source_bm.bm_h = bufh;
213 source_bm.bm_type = BM_LINEAR;
214 source_bm.bm_flags = 0;
215 source_bm.bm_data = buf;
217 gr_bm_ubitblt(bufw,bufh,dstx,dsty,sx,sy,&source_bm,&grd_curcanv->cv_bitmap);
220 //our routine to set the pallete, called from the movie code
221 void MovieSetPalette(unsigned char *p, unsigned start, unsigned count)
226 //mprintf((0,"SetPalette p=%x, start=%d, count=%d\n",p,start,count));
228 //Color 0 should be black, and we get color 255
229 Assert(start>=1 && start+count-1<=254);
231 //Set color 0 to be black
232 gr_palette[0] = gr_palette[1] = gr_palette[2] = 0;
234 //Set color 255 to be our subtitle color
235 gr_palette[765] = gr_palette[766] = gr_palette[767] = 50;
237 //movie libs palette into our array
238 memcpy(gr_palette+start*3,p+start*3,count*3);
240 //finally set the palette in the hardware
241 //gr_palette_load(gr_palette);
243 //MVE_SetPalette(p, start, count);
249 short x, y, w, h; // The location of the menu.
250 grs_bitmap * bmp; // The background under the menu.
253 bkg movie_bg = {0,0,0,0,NULL};
256 #define BOX_BORDER (MenuHires?40:20)
259 void show_pause_message(char *msg)
264 gr_set_current_canvas(NULL);
265 gr_set_curfont( SMALL_FONT );
267 gr_get_string_size(msg,&w,&h,&aw);
269 x = (grd_curscreen->sc_w-w)/2;
270 y = (grd_curscreen->sc_h-h)/2;
274 gr_free_bitmap(movie_bg.bmp);
278 // Save the background of the display
279 movie_bg.x=x; movie_bg.y=y; movie_bg.w=w; movie_bg.h=h;
281 movie_bg.bmp = gr_create_bitmap( w+BOX_BORDER, h+BOX_BORDER );
283 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 );
287 gr_rect(x-BOX_BORDER/2,y-BOX_BORDER/2,x+w+BOX_BORDER/2-1,y+h+BOX_BORDER/2-1);
289 gr_set_fontcolor( 255, -1 );
291 gr_ustring( 0x8000, y, msg );
296 void clear_pause_message()
301 gr_bitmap(movie_bg.x-BOX_BORDER/2, movie_bg.y-BOX_BORDER/2, movie_bg.bmp);
303 gr_free_bitmap(movie_bg.bmp);
310 //returns status. see movie.h
311 int RunMovie(char *filename, int hires_flag, int must_have,int dx,int dy)
314 int result=1,aborted=0;
324 // Open Movie file. If it doesn't exist, no movie, just return.
326 filehndl = open_movie_file(filename,must_have);
328 if (filehndl == NULL)
331 con_printf(CON_NORMAL, "movie: RunMovie: Cannot open movie <%s>\n",filename);
332 return MOVIE_NOT_PLAYED;
335 MVE_memCallbacks(MPlayAlloc, MPlayFree);
336 MVE_ioCallbacks(FileRead);
339 gr_set_mode(SM(640,480));
341 gr_set_mode(SM(320,200));
344 set_screen_mode(SCREEN_MENU);
345 gr_copy_palette(pal_save, gr_palette, 768);
346 memset(gr_palette, 0, 768);
347 gr_palette_load(gr_palette);
350 #if !defined(POLY_ACC)
351 MVE_sfCallbacks(MovieShowFrame);
352 MVE_palCallbacks(MovieSetPalette);
355 if (MVE_rmPrepMovie((void *)filehndl, dx, dy, track)) {
357 return MOVIE_NOT_PLAYED;
362 FontHires = FontHiresAvailable && hires_flag;
364 while((result = MVE_rmStepMovie()) == 0) {
366 draw_subtitles(frame_num);
368 gr_palette_load(gr_palette); // moved this here because of flashing
374 // If ESCAPE pressed, then quit movie.
375 if (key == KEY_ESC) {
376 result = aborted = 1;
380 // If PAUSE pressed, then pause movie
381 if (key == KEY_PAUSE) {
383 show_pause_message(TXT_PAUSE);
384 while (!key_inkey()) ;
385 clear_pause_message();
388 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
389 if ((key == KEY_ALTED+KEY_ENTER) ||
390 (key == KEY_ALTED+KEY_PADENTER))
391 gr_toggle_fullscreen();
397 Assert(aborted || result == MVE_ERR_EOF); ///movie should be over
401 cfclose(filehndl); // Close Movie File
403 // Restore old graphic state
405 Screen_mode=-1; //force reset of screen mode
407 gr_copy_palette(gr_palette, pal_save, 768);
408 gr_palette_load(pal_save);
411 return (aborted?MOVIE_ABORTED:MOVIE_PLAYED_FULL);
415 int InitMovieBriefing()
419 gr_set_mode(SM(640,480));
421 gr_set_mode(SM(320,200));
423 gr_init_sub_canvas( &VR_screen_pages[0], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
424 gr_init_sub_canvas( &VR_screen_pages[1], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
431 //returns 1 if frame updated ok
436 err = MVE_rmStepMovie();
438 gr_palette_load(gr_palette);
440 if (err == MVE_ERR_EOF) //end of movie, so reset
442 reset_movie_file(RoboFile);
443 if (MVE_rmPrepMovie((void *)RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
457 void DeInitRobotMovie(void)
460 cfclose(RoboFile); // Close Movie File
464 int InitRobotMovie(char *filename)
466 if (FindArg("-nomovies"))
469 con_printf(DEBUG_LEVEL, "RoboFile=%s\n", filename);
471 MVE_sndInit(-1); //tell movies to play no sound for robots
473 RoboFile = open_movie_file(filename, 1);
475 if (RoboFile == NULL)
477 Warning("movie: InitRobotMovie: Cannot open movie file <%s>",filename);
478 return MOVIE_NOT_PLAYED;
481 Vid_State = VID_PLAY;
483 if (MVE_rmPrepMovie((void *)RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
488 RoboFilePos = cfseek(RoboFile, 0L, SEEK_CUR);
490 con_printf(DEBUG_LEVEL, "RoboFilePos=%d!\n", RoboFilePos);
497 * Subtitle system code
500 ubyte *subtitle_raw_data;
503 //search for next field following whitespace
504 ubyte *next_field(ubyte *p)
506 while (*p && !isspace(*p))
512 while (*p && isspace(*p))
522 int init_subtitles(char *filename)
531 if (! FindArg("-subtitles"))
534 ifile = cfopen(filename,"rb"); //try text version
536 if (!ifile) { //no text version, try binary version
537 char filename2[FILENAME_LEN];
538 change_filename_ext(filename2,filename,".TXB");
539 ifile = cfopen(filename2,"rb");
545 size = cfilelength(ifile);
547 MALLOC (subtitle_raw_data, ubyte, size+1);
549 read_count = cfread(subtitle_raw_data, 1, size, ifile);
553 subtitle_raw_data[size] = 0;
555 if (read_count != size) {
556 d_free(subtitle_raw_data);
560 p = subtitle_raw_data;
562 while (p && p < subtitle_raw_data+size) {
565 endp = strchr(p,'\n');
567 if (endp[-1] == '\r')
568 endp[-1] = 0; //handle 0d0a pair
569 *endp = 0; //string termintor
576 Subtitles[Num_subtitles].first_frame = atoi(p);
577 p = next_field(p); if (!p) continue;
578 Subtitles[Num_subtitles].last_frame = atoi(p);
579 p = next_field(p); if (!p) continue;
580 Subtitles[Num_subtitles].msg = p;
582 Assert(Num_subtitles==0 || Subtitles[Num_subtitles].first_frame >= Subtitles[Num_subtitles-1].first_frame);
583 Assert(Subtitles[Num_subtitles].last_frame >= Subtitles[Num_subtitles].first_frame);
596 void close_subtitles()
598 if (subtitle_raw_data)
599 d_free(subtitle_raw_data);
600 subtitle_raw_data = NULL;
605 //draw the subtitles for this frame
606 void draw_subtitles(int frame_num)
608 static int active_subtitles[MAX_ACTIVE_SUBTITLES];
609 static int num_active_subtitles,next_subtitle,line_spacing;
613 if (frame_num == 0) {
614 num_active_subtitles = 0;
616 gr_set_curfont( GAME_FONT );
617 line_spacing = grd_curcanv->cv_font->ft_h + (grd_curcanv->cv_font->ft_h >> 2);
618 gr_set_fontcolor(255,-1);
621 //get rid of any subtitles that have expired
622 for (t=0;t<num_active_subtitles;)
623 if (frame_num > Subtitles[active_subtitles[t]].last_frame) {
625 for (t2=t;t2<num_active_subtitles-1;t2++)
626 active_subtitles[t2] = active_subtitles[t2+1];
627 num_active_subtitles--;
633 //get any subtitles new for this frame
634 while (next_subtitle < Num_subtitles && frame_num >= Subtitles[next_subtitle].first_frame) {
635 if (num_active_subtitles >= MAX_ACTIVE_SUBTITLES)
636 Error("Too many active subtitles!");
637 active_subtitles[num_active_subtitles++] = next_subtitle;
641 //find y coordinate for first line of subtitles
642 y = grd_curcanv->cv_bitmap.bm_h-((line_spacing+1)*MAX_ACTIVE_SUBTITLES+2);
644 //erase old subtitles if necessary
647 gr_rect(0,y,grd_curcanv->cv_bitmap.bm_w-1,grd_curcanv->cv_bitmap.bm_h-1);
650 //now draw the current subtitles
651 for (t=0;t<num_active_subtitles;t++)
652 if (active_subtitles[t] != -1) {
653 gr_string(0x8000,y,Subtitles[active_subtitles[t]].msg);
659 movielib *init_new_movie_lib(char *filename, CFILE *fp)
665 //read movie file header
667 nfiles = cfile_read_int(fp); //get number of files
669 //table = d_malloc(sizeof(*table) + sizeof(ml_entry)*nfiles);
670 MALLOC(table, movielib, 1);
671 MALLOC(table->movies, ml_entry, nfiles);
673 strcpy(table->name,filename);
674 table->n_movies = nfiles;
676 offset = 4+4+nfiles*(13+4); //id + nfiles + nfiles * (filename + size)
678 for (i=0;i<nfiles;i++) {
681 n = cfread(table->movies[i].name, 13, 1, fp);
683 break; //end of file (probably)
685 len = cfile_read_int(fp);
687 table->movies[i].len = len;
688 table->movies[i].offset = offset;
690 offset += table->movies[i].len;
703 movielib *init_old_movie_lib(char *filename, CFILE *fp)
707 movielib *table,*table2;
712 table = d_malloc(sizeof(*table) + sizeof(ml_entry)*MAX_MOVIES_PER_LIB);
717 i = cfread(table->movies[nfiles].name, 13, 1, fp);
719 break; //end of file (probably)
721 i = cfread(&len, 4, 1, fp);
723 Error("error reading movie library <%s>",filename);
725 table->movies[nfiles].len = INTEL_INT(len);
726 table->movies[nfiles].offset = cftell(fp);
728 cfseek(fp, INTEL_INT(len), SEEK_CUR); //skip data
733 //allocate correct-sized table
734 size = sizeof(*table) + sizeof(ml_entry)*nfiles;
735 table2 = d_malloc(size);
736 memcpy(table2,table,size);
740 strcpy(table->name,filename);
742 table->n_movies = nfiles;
753 //find the specified movie library, and read in list of movies in it
754 movielib *init_movie_lib(char *filename)
756 //note: this based on cfile_init_hogfile()
761 fp = cfopen(filename, "rb");
766 cfread(id, 4, 1, fp);
767 if ( !strncmp( id, "DMVL", 4 ) )
768 return init_new_movie_lib(filename,fp);
769 else if ( !strncmp( id, "DHF", 3 ) ) {
770 cfseek(fp,-1,SEEK_CUR); //old file had 3 char id
771 return init_old_movie_lib(filename,fp);
780 void close_movie(int i)
783 d_free(movie_libs[i]->movies);
784 d_free(movie_libs[i]);
793 for (i=0;i<N_MOVIE_LIBS;i++)
798 //ask user to put the D2 CD in.
799 //returns -1 if ESC pressed, 0 if OK chosen
800 //CD may not have been inserted
804 ubyte save_pal[256*3];
805 grs_canvas *save_canv,*tcanv;
806 int ret,was_faded=gr_palette_faded_out;
810 save_canv = grd_curcanv;
811 tcanv = gr_create_canvas(grd_curcanv->cv_w,grd_curcanv->cv_h);
813 gr_set_current_canvas(tcanv);
814 gr_ubitmap(0,0,&save_canv->cv_bitmap);
815 gr_set_current_canvas(save_canv);
817 gr_clear_canvas(BM_XRGB(0,0,0));
819 memcpy(save_pal,gr_palette,sizeof(save_pal));
821 memcpy(gr_palette,last_palette_for_color_fonts,sizeof(gr_palette));
825 ret = nm_messagebox( "CD ERROR", 1, "Ok", "Please insert your Descent II CD");
830 ret2 = nm_messagebox( "CD ERROR", 2, "Try Again", "Leave Game", "You must insert your\nDescent II CD to Continue");
832 if (ret2 == -1 || ret2 == 0)
836 force_rb_register = 1; //disc has changed; force register new CD
840 memcpy(gr_palette,save_pal,sizeof(save_pal));
842 gr_ubitmap(0,0,&tcanv->cv_bitmap);
845 gr_palette_load(gr_palette);
847 gr_free_canvas(tcanv);
851 con_printf(DEBUG_LEVEL, "STUB: movie: request_cd\n");
857 void init_movie(char *filename, int libnum, int is_robots, int required)
860 char *res = strchr(filename, '.') - 1; // 'h' == high resolution, 'l' == low
863 if (FindArg("-nomovies")) {
864 movie_libs[libnum] = NULL;
869 //for robots, load highres versions if highres menus set
871 high_res = MenuHiresAvailable;
873 high_res = MovieHires;
878 for (try = 0; (movie_libs[libnum] = init_movie_lib(filename)) == NULL; try++) {
881 strcpy(name2,CDROM_dir);
882 strcat(name2,filename);
883 movie_libs[libnum] = init_movie_lib(name2);
885 if (movie_libs[libnum] != NULL) {
886 movie_libs[libnum]->flags |= MLF_ON_CD;
887 break; // we found our movie on the CD
889 if (try == 0) { // first try
890 if (*res == 'h') { // try low res instead
893 } else if (*res == 'l') { // try high
898 Warning("Cannot open movie file <%s>",filename);
904 Warning("Cannot open any movie file <%s>", filename);
911 if (is_robots && movie_libs[libnum]!=NULL)
912 robot_movies = high_res?2:1;
916 //find and initialize the movie libraries
922 for (i=0;i<N_BUILTIN_MOVIE_LIBS;i++) {
924 if (!strnicmp(movielib_files[i],"robot",5))
929 init_movie(movielib_files[i],i,is_robots,1);
932 movie_libs[EXTRA_ROBOT_LIB] = NULL;
934 atexit(close_movies);
938 void init_extra_robot_movie(char *filename)
940 close_movie(EXTRA_ROBOT_LIB);
941 init_movie(filename,EXTRA_ROBOT_LIB,1,0);
948 //looks through a movie library for a movie file
949 //returns filehandle, with fileposition at movie, or -1 if can't find
950 CFILE *search_movie_lib(movielib *lib, char *filename, int must_have)
958 for (i=0;i<lib->n_movies;i++)
959 if (!stricmp(filename,lib->movies[i].name)) { //found the movie in a library
962 from_cd = (lib->flags & MLF_ON_CD);
965 songs_stop_redbook(); //ready to read from CD
967 do { //keep trying until we get the file handle
969 movie_handle = filehandle = cfopen(lib->name, "rb");
971 if (must_have && from_cd && filehandle == NULL)
974 if (request_cd() == -1) //ESC from requester
975 break; //bail from here. will get error later
978 } while (must_have && from_cd && filehandle == NULL);
981 cfseek(filehandle, (movie_start = lib->movies[i].offset), SEEK_SET);
990 //returns file handle
991 CFILE *open_movie_file(char *filename, int must_have)
996 for (i=0;i<N_MOVIE_LIBS;i++) {
997 if ((filehandle = search_movie_lib(movie_libs[i], filename, must_have)) != NULL)
1001 return NULL; //couldn't find it
1004 //sets the file position to the start of this already-open file
1005 int reset_movie_file(CFILE *handle)
1007 Assert(handle == movie_handle);
1009 cfseek(handle, movie_start, SEEK_SET);
1011 return 0; //everything is cool