1 /* $Id: movie.c,v 1.29 2003-06-22 23:04:54 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.29 2003-06-22 23:04:54 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];
74 char name[FILENAME_LEN];
79 #define MAX_MOVIES_PER_LIB 50 //determines size of malloc
82 char name[100]; //[FILENAME_LEN];
89 char movielib_files[][FILENAME_LEN] = {"intro-l.mvl","other-l.mvl","robots-l.mvl","oem-l.mvl"};
91 char movielib_files[][FILENAME_LEN] = {"intro-l.mvl","other-l.mvl","robots-l.mvl"};
94 #define N_BUILTIN_MOVIE_LIBS (sizeof(movielib_files)/sizeof(*movielib_files))
95 #define N_MOVIE_LIBS (N_BUILTIN_MOVIE_LIBS+1)
96 #define EXTRA_ROBOT_LIB N_BUILTIN_MOVIE_LIBS
97 movielib *movie_libs[N_MOVIE_LIBS];
100 //do we have the robot movies available
101 int robot_movies = 0; //0 means none, 1 means lowres, 2 means hires
103 int MovieHires = 1; //default is highres
105 CFILE *RoboFile = NULL;
108 // Function Prototypes
109 int RunMovie(char *filename, int highres_flag, int allow_abort,int dx,int dy);
111 CFILE *open_movie_file(char *filename, int must_have);
112 int reset_movie_file(CFILE *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 // ----------------------------------------------------------------------
120 void* MPlayAlloc(unsigned size)
122 return d_malloc(size);
125 void MPlayFree(void *p)
131 //-----------------------------------------------------------------------
133 unsigned int FileRead(void *handle, void *buf, unsigned int count)
136 numread = cfread(buf, 1, count, (CFILE *)handle);
137 return (numread == count);
141 //-----------------------------------------------------------------------
144 //filename will actually get modified to be either low-res or high-res
145 //returns status. see values in movie.h
146 int PlayMovie(const char *filename, int must_have)
148 char name[FILENAME_LEN],*p;
152 if (FindArg("-nomovies"))
153 return MOVIE_NOT_PLAYED;
156 strcpy(name,filename);
158 if ((p=strchr(name,'.')) == NULL) //add extension, if missing
161 //check for escape already pressed & abort if so
162 while ((c=key_inkey()) != 0)
164 return MOVIE_ABORTED;
166 // Stop all digital sounds currently playing.
175 if (!FindArg("-nosound"))
180 ret = RunMovie(name,MovieHires,must_have,-1,-1);
182 if (!FindArg("-nosound"))
185 Screen_mode = -1; //force screen reset
191 void MovieShowFrame(ubyte *buf, uint bufw, uint bufh, uint sx, uint sy,
192 uint w, uint h, uint dstx, uint dsty)
194 grs_bitmap source_bm;
196 //mprintf((0,"MovieShowFrame %d,%d %d,%d %d,%d %d,%d\n",bufw,bufh,sx,sy,w,h,dstx,dsty));
198 Assert(bufw == w && bufh == h);
200 source_bm.bm_x = source_bm.bm_y = 0;
201 source_bm.bm_w = source_bm.bm_rowsize = bufw;
202 source_bm.bm_h = bufh;
203 source_bm.bm_type = BM_LINEAR;
204 source_bm.bm_flags = 0;
205 source_bm.bm_data = buf;
207 gr_bm_ubitblt(bufw,bufh,dstx,dsty,sx,sy,&source_bm,&grd_curcanv->cv_bitmap);
210 //our routine to set the pallete, called from the movie code
211 void MovieSetPalette(unsigned char *p, unsigned start, unsigned count)
216 //mprintf((0,"SetPalette p=%x, start=%d, count=%d\n",p,start,count));
218 //Color 0 should be black, and we get color 255
219 Assert(start>=1 && start+count-1<=254);
221 //Set color 0 to be black
222 gr_palette[0] = gr_palette[1] = gr_palette[2] = 0;
224 //Set color 255 to be our subtitle color
225 gr_palette[765] = gr_palette[766] = gr_palette[767] = 50;
227 //movie libs palette into our array
228 memcpy(gr_palette+start*3,p+start*3,count*3);
230 //finally set the palette in the hardware
231 //gr_palette_load(gr_palette);
233 //MVE_SetPalette(p, start, count);
239 short x, y, w, h; // The location of the menu.
240 grs_bitmap * bmp; // The background under the menu.
243 bkg movie_bg = {0,0,0,0,NULL};
246 #define BOX_BORDER (MenuHires?40:20)
249 void show_pause_message(char *msg)
254 gr_set_current_canvas(NULL);
255 gr_set_curfont( SMALL_FONT );
257 gr_get_string_size(msg,&w,&h,&aw);
259 x = (grd_curscreen->sc_w-w)/2;
260 y = (grd_curscreen->sc_h-h)/2;
264 gr_free_bitmap(movie_bg.bmp);
268 // Save the background of the display
269 movie_bg.x=x; movie_bg.y=y; movie_bg.w=w; movie_bg.h=h;
271 movie_bg.bmp = gr_create_bitmap( w+BOX_BORDER, h+BOX_BORDER );
273 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 );
277 gr_rect(x-BOX_BORDER/2,y-BOX_BORDER/2,x+w+BOX_BORDER/2-1,y+h+BOX_BORDER/2-1);
279 gr_set_fontcolor( 255, -1 );
281 gr_ustring( 0x8000, y, msg );
286 void clear_pause_message()
291 gr_bitmap(movie_bg.x-BOX_BORDER/2, movie_bg.y-BOX_BORDER/2, movie_bg.bmp);
293 gr_free_bitmap(movie_bg.bmp);
300 //returns status. see movie.h
301 int RunMovie(char *filename, int hires_flag, int must_have,int dx,int dy)
304 int result=1,aborted=0;
314 // Open Movie file. If it doesn't exist, no movie, just return.
316 filehndl = open_movie_file(filename,must_have);
318 if (filehndl == NULL)
321 con_printf(CON_NORMAL, "movie: RunMovie: Cannot open movie <%s>\n",filename);
322 return MOVIE_NOT_PLAYED;
325 MVE_memCallbacks(MPlayAlloc, MPlayFree);
326 MVE_ioCallbacks(FileRead);
329 gr_set_mode(SM(640,480));
331 gr_set_mode(SM(320,200));
334 set_screen_mode(SCREEN_MENU);
335 gr_copy_palette(pal_save, gr_palette, 768);
336 memset(gr_palette, 0, 768);
337 gr_palette_load(gr_palette);
340 #if !defined(POLY_ACC)
341 MVE_sfCallbacks(MovieShowFrame);
342 MVE_palCallbacks(MovieSetPalette);
345 if (MVE_rmPrepMovie((void *)filehndl, dx, dy, track)) {
347 return MOVIE_NOT_PLAYED;
352 FontHires = FontHiresAvailable && hires_flag;
354 while((result = MVE_rmStepMovie()) == 0) {
356 draw_subtitles(frame_num);
358 gr_palette_load(gr_palette); // moved this here because of flashing
364 // If ESCAPE pressed, then quit movie.
365 if (key == KEY_ESC) {
366 result = aborted = 1;
370 // If PAUSE pressed, then pause movie
371 if (key == KEY_PAUSE) {
373 show_pause_message(TXT_PAUSE);
374 while (!key_inkey()) ;
375 clear_pause_message();
378 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
379 if ((key == KEY_CTRLED+KEY_SHIFTED+KEY_PADENTER) ||
380 (key == KEY_ALTED+KEY_CTRLED+KEY_PADENTER) ||
381 (key == KEY_ALTED+KEY_SHIFTED+KEY_PADENTER))
382 gr_toggle_fullscreen();
388 Assert(aborted || result == MVE_ERR_EOF); ///movie should be over
392 cfclose(filehndl); // Close Movie File
394 // Restore old graphic state
396 Screen_mode=-1; //force reset of screen mode
398 gr_copy_palette(gr_palette, pal_save, 768);
399 gr_palette_load(pal_save);
402 return (aborted?MOVIE_ABORTED:MOVIE_PLAYED_FULL);
406 int InitMovieBriefing()
410 gr_set_mode(SM(640,480));
412 gr_set_mode(SM(320,200));
414 gr_init_sub_canvas( &VR_screen_pages[0], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
415 gr_init_sub_canvas( &VR_screen_pages[1], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
422 //returns 1 if frame updated ok
427 err = MVE_rmStepMovie();
429 gr_palette_load(gr_palette);
431 if (err == MVE_ERR_EOF) //end of movie, so reset
433 reset_movie_file(RoboFile);
434 if (MVE_rmPrepMovie((void *)RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
448 void DeInitRobotMovie(void)
451 cfclose(RoboFile); // Close Movie File
455 int InitRobotMovie(char *filename)
457 if (FindArg("-nomovies"))
460 con_printf(DEBUG_LEVEL, "RoboFile=%s\n", filename);
462 MVE_sndInit(-1); //tell movies to play no sound for robots
464 RoboFile = open_movie_file(filename, 1);
466 if (RoboFile == NULL)
468 Warning("movie: InitRobotMovie: Cannot open movie file <%s>",filename);
469 return MOVIE_NOT_PLAYED;
472 Vid_State = VID_PLAY;
474 if (MVE_rmPrepMovie((void *)RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
479 RoboFilePos = cfseek(RoboFile, 0L, SEEK_CUR);
481 con_printf(DEBUG_LEVEL, "RoboFilePos=%d!\n", RoboFilePos);
488 * Subtitle system code
491 ubyte *subtitle_raw_data;
494 //search for next field following whitespace
495 ubyte *next_field(ubyte *p)
497 while (*p && !isspace(*p))
503 while (*p && isspace(*p))
513 int init_subtitles(char *filename)
522 if (! FindArg("-subtitles"))
525 ifile = cfopen(filename,"rb"); //try text version
527 if (!ifile) { //no text version, try binary version
528 char filename2[FILENAME_LEN];
529 change_filename_ext(filename2,filename,".TXB");
530 ifile = cfopen(filename2,"rb");
536 size = cfilelength(ifile);
538 MALLOC (subtitle_raw_data, ubyte, size+1);
540 read_count = cfread(subtitle_raw_data, 1, size, ifile);
544 subtitle_raw_data[size] = 0;
546 if (read_count != size) {
547 d_free(subtitle_raw_data);
551 p = subtitle_raw_data;
553 while (p && p < subtitle_raw_data+size) {
556 endp = strchr(p,'\n');
558 if (endp[-1] == '\r')
559 endp[-1] = 0; //handle 0d0a pair
560 *endp = 0; //string termintor
567 Subtitles[Num_subtitles].first_frame = atoi(p);
568 p = next_field(p); if (!p) continue;
569 Subtitles[Num_subtitles].last_frame = atoi(p);
570 p = next_field(p); if (!p) continue;
571 Subtitles[Num_subtitles].msg = p;
573 Assert(Num_subtitles==0 || Subtitles[Num_subtitles].first_frame >= Subtitles[Num_subtitles-1].first_frame);
574 Assert(Subtitles[Num_subtitles].last_frame >= Subtitles[Num_subtitles].first_frame);
587 void close_subtitles()
589 if (subtitle_raw_data)
590 d_free(subtitle_raw_data);
591 subtitle_raw_data = NULL;
596 //draw the subtitles for this frame
597 void draw_subtitles(int frame_num)
599 static int active_subtitles[MAX_ACTIVE_SUBTITLES];
600 static int num_active_subtitles,next_subtitle,line_spacing;
604 if (frame_num == 0) {
605 num_active_subtitles = 0;
607 gr_set_curfont( GAME_FONT );
608 line_spacing = grd_curcanv->cv_font->ft_h + (grd_curcanv->cv_font->ft_h >> 2);
609 gr_set_fontcolor(255,-1);
612 //get rid of any subtitles that have expired
613 for (t=0;t<num_active_subtitles;)
614 if (frame_num > Subtitles[active_subtitles[t]].last_frame) {
616 for (t2=t;t2<num_active_subtitles-1;t2++)
617 active_subtitles[t2] = active_subtitles[t2+1];
618 num_active_subtitles--;
624 //get any subtitles new for this frame
625 while (next_subtitle < Num_subtitles && frame_num >= Subtitles[next_subtitle].first_frame) {
626 if (num_active_subtitles >= MAX_ACTIVE_SUBTITLES)
627 Error("Too many active subtitles!");
628 active_subtitles[num_active_subtitles++] = next_subtitle;
632 //find y coordinate for first line of subtitles
633 y = grd_curcanv->cv_bitmap.bm_h-((line_spacing+1)*MAX_ACTIVE_SUBTITLES+2);
635 //erase old subtitles if necessary
638 gr_rect(0,y,grd_curcanv->cv_bitmap.bm_w-1,grd_curcanv->cv_bitmap.bm_h-1);
641 //now draw the current subtitles
642 for (t=0;t<num_active_subtitles;t++)
643 if (active_subtitles[t] != -1) {
644 gr_string(0x8000,y,Subtitles[active_subtitles[t]].msg);
650 movielib *init_new_movie_lib(char *filename, CFILE *fp)
656 //read movie file header
658 nfiles = cfile_read_int(fp); //get number of files
660 //table = d_malloc(sizeof(*table) + sizeof(ml_entry)*nfiles);
661 MALLOC(table, movielib, 1);
662 MALLOC(table->movies, ml_entry, nfiles);
664 strcpy(table->name,filename);
665 table->n_movies = nfiles;
667 offset = 4+4+nfiles*(13+4); //id + nfiles + nfiles * (filename + size)
669 for (i=0;i<nfiles;i++) {
672 n = cfread(table->movies[i].name, 13, 1, fp);
674 break; //end of file (probably)
676 len = cfile_read_int(fp);
678 table->movies[i].len = len;
679 table->movies[i].offset = offset;
681 offset += table->movies[i].len;
694 movielib *init_old_movie_lib(char *filename, CFILE *fp)
698 movielib *table,*table2;
703 table = d_malloc(sizeof(*table) + sizeof(ml_entry)*MAX_MOVIES_PER_LIB);
708 i = cfread(table->movies[nfiles].name, 13, 1, fp);
710 break; //end of file (probably)
712 i = cfread(&len, 4, 1, fp);
714 Error("error reading movie library <%s>",filename);
716 table->movies[nfiles].len = INTEL_INT(len);
717 table->movies[nfiles].offset = cftell(fp);
719 cfseek(fp, INTEL_INT(len), SEEK_CUR); //skip data
724 //allocate correct-sized table
725 size = sizeof(*table) + sizeof(ml_entry)*nfiles;
726 table2 = d_malloc(size);
727 memcpy(table2,table,size);
731 strcpy(table->name,filename);
733 table->n_movies = nfiles;
744 //find the specified movie library, and read in list of movies in it
745 movielib *init_movie_lib(char *filename)
747 //note: this based on cfile_init_hogfile()
752 fp = cfopen(filename, "rb");
757 cfread(id, 4, 1, fp);
758 if ( !strncmp( id, "DMVL", 4 ) )
759 return init_new_movie_lib(filename,fp);
760 else if ( !strncmp( id, "DHF", 3 ) ) {
761 cfseek(fp,-1,SEEK_CUR); //old file had 3 char id
762 return init_old_movie_lib(filename,fp);
771 void close_movie(int i)
774 d_free(movie_libs[i]->movies);
775 d_free(movie_libs[i]);
784 for (i=0;i<N_MOVIE_LIBS;i++)
789 //ask user to put the D2 CD in.
790 //returns -1 if ESC pressed, 0 if OK chosen
791 //CD may not have been inserted
795 ubyte save_pal[256*3];
796 grs_canvas *save_canv,*tcanv;
797 int ret,was_faded=gr_palette_faded_out;
801 save_canv = grd_curcanv;
802 tcanv = gr_create_canvas(grd_curcanv->cv_w,grd_curcanv->cv_h);
804 gr_set_current_canvas(tcanv);
805 gr_ubitmap(0,0,&save_canv->cv_bitmap);
806 gr_set_current_canvas(save_canv);
808 gr_clear_canvas(BM_XRGB(0,0,0));
810 memcpy(save_pal,gr_palette,sizeof(save_pal));
812 memcpy(gr_palette,last_palette_for_color_fonts,sizeof(gr_palette));
816 ret = nm_messagebox( "CD ERROR", 1, "Ok", "Please insert your Descent II CD");
821 ret2 = nm_messagebox( "CD ERROR", 2, "Try Again", "Leave Game", "You must insert your\nDescent II CD to Continue");
823 if (ret2 == -1 || ret2 == 0)
827 force_rb_register = 1; //disc has changed; force register new CD
831 memcpy(gr_palette,save_pal,sizeof(save_pal));
833 gr_ubitmap(0,0,&tcanv->cv_bitmap);
836 gr_palette_load(gr_palette);
838 gr_free_canvas(tcanv);
842 con_printf(DEBUG_LEVEL, "STUB: movie: request_cd\n");
848 void init_movie(char *filename,int libnum,int is_robots,int required)
854 if (FindArg("-nomovies")) {
855 movie_libs[libnum] = NULL;
860 //for robots, load highres versions if highres menus set
862 high_res = MenuHiresAvailable;
864 high_res = MovieHires;
867 strchr(filename,'.')[-1] = 'h';
871 if ((movie_libs[libnum] = init_movie_lib(filename)) == NULL) {
874 strcpy(name2,CDROM_dir);
875 strcat(name2,filename);
876 movie_libs[libnum] = init_movie_lib(name2);
878 if (movie_libs[libnum] != NULL)
879 movie_libs[libnum]->flags |= MLF_ON_CD;
882 Warning("Cannot open movie file <%s>\n",filename);
885 if (!try) { // first try
886 if (strchr(filename, '.')[-1] == 'h') { // try again with lowres
887 strchr(filename, '.')[-1] = 'l';
889 Warning("Trying to open movie file <%s> instead\n", filename);
892 } else if (strchr(filename, '.')[-1] == 'l') { // try again with highres
893 strchr(filename, '.')[-1] = 'h';
895 Warning("Trying to open movie file <%s> instead\n", filename);
903 if (is_robots && movie_libs[libnum]!=NULL)
904 robot_movies = high_res?2:1;
908 //find and initialize the movie libraries
914 for (i=0;i<N_BUILTIN_MOVIE_LIBS;i++) {
916 if (!strnicmp(movielib_files[i],"robot",5))
921 init_movie(movielib_files[i],i,is_robots,1);
924 movie_libs[EXTRA_ROBOT_LIB] = NULL;
926 atexit(close_movies);
930 void init_extra_robot_movie(char *filename)
932 close_movie(EXTRA_ROBOT_LIB);
933 init_movie(filename,EXTRA_ROBOT_LIB,1,0);
940 //looks through a movie library for a movie file
941 //returns filehandle, with fileposition at movie, or -1 if can't find
942 CFILE *search_movie_lib(movielib *lib, char *filename, int must_have)
950 for (i=0;i<lib->n_movies;i++)
951 if (!stricmp(filename,lib->movies[i].name)) { //found the movie in a library
954 from_cd = (lib->flags & MLF_ON_CD);
957 songs_stop_redbook(); //ready to read from CD
959 do { //keep trying until we get the file handle
961 movie_handle = filehandle = cfopen(lib->name, "rb");
963 if (must_have && from_cd && filehandle == NULL)
966 if (request_cd() == -1) //ESC from requester
967 break; //bail from here. will get error later
970 } while (must_have && from_cd && filehandle == NULL);
973 cfseek(filehandle, (movie_start = lib->movies[i].offset), SEEK_SET);
982 //returns file handle
983 CFILE *open_movie_file(char *filename, int must_have)
988 for (i=0;i<N_MOVIE_LIBS;i++) {
989 if ((filehandle = search_movie_lib(movie_libs[i], filename, must_have)) != NULL)
993 return NULL; //couldn't find it
996 //sets the file position to the start of this already-open file
997 int reset_movie_file(CFILE *handle)
999 Assert(handle == movie_handle);
1001 cfseek(handle, movie_start, SEEK_SET);
1003 return 0; //everything is cool