1 /* $Id: movie.c,v 1.26 2003-06-10 04:46: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.26 2003-06-10 04:46: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];
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 = 1; //default is highres
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 // ----------------------------------------------------------------------
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 = read((int)handle, buf, count);
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 == -1) {
320 Warning("movie: RunMovie: Cannot open movie <%s>\n",filename);
321 return MOVIE_NOT_PLAYED;
324 MVE_memCallbacks(MPlayAlloc, MPlayFree);
325 MVE_ioCallbacks(FileRead);
328 gr_set_mode(SM(640,480));
330 gr_set_mode(SM(320,200));
333 set_screen_mode(SCREEN_MENU);
334 gr_copy_palette(pal_save, gr_palette, 768);
335 memset(gr_palette, 0, 768);
336 gr_palette_load(gr_palette);
339 #if !defined(POLY_ACC)
340 MVE_sfCallbacks(MovieShowFrame);
341 MVE_palCallbacks(MovieSetPalette);
344 if (MVE_rmPrepMovie((void *)filehndl, dx, dy, track)) {
346 return MOVIE_NOT_PLAYED;
351 FontHires = FontHiresAvailable && hires_flag;
353 while((result = MVE_rmStepMovie()) == 0) {
355 draw_subtitles(frame_num);
357 gr_palette_load(gr_palette); // moved this here because of flashing
363 // If ESCAPE pressed, then quit movie.
364 if (key == KEY_ESC) {
365 result = aborted = 1;
369 // If PAUSE pressed, then pause movie
370 if (key == KEY_PAUSE) {
372 show_pause_message(TXT_PAUSE);
373 while (!key_inkey()) ;
374 clear_pause_message();
377 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
378 if ((key == KEY_CTRLED+KEY_SHIFTED+KEY_PADENTER) ||
379 (key == KEY_ALTED+KEY_CTRLED+KEY_PADENTER) ||
380 (key == KEY_ALTED+KEY_SHIFTED+KEY_PADENTER))
381 gr_toggle_fullscreen();
387 Assert(aborted || result == MVE_ERR_EOF); ///movie should be over
391 close(filehndl); // Close Movie File
393 // Restore old graphic state
395 Screen_mode=-1; //force reset of screen mode
397 gr_copy_palette(gr_palette, pal_save, 768);
398 gr_palette_load(pal_save);
401 return (aborted?MOVIE_ABORTED:MOVIE_PLAYED_FULL);
405 int InitMovieBriefing()
409 gr_set_mode(SM(640,480));
411 gr_set_mode(SM(320,200));
413 gr_init_sub_canvas( &VR_screen_pages[0], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
414 gr_init_sub_canvas( &VR_screen_pages[1], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
421 //returns 1 if frame updated ok
426 err = MVE_rmStepMovie();
428 gr_palette_load(gr_palette);
430 if (err == MVE_ERR_EOF) //end of movie, so reset
432 reset_movie_file(RoboFile);
433 if (MVE_rmPrepMovie((void *)RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
447 void DeInitRobotMovie(void)
450 close(RoboFile); // Close Movie File
454 int InitRobotMovie(char *filename)
456 if (FindArg("-nomovies"))
459 con_printf(DEBUG_LEVEL, "RoboFile=%s\n", filename);
461 MVE_sndInit(-1); //tell movies to play no sound for robots
463 RoboFile = open_movie_file(filename, 1);
465 if (RoboFile == -1) {
466 Warning("movie: InitRobotMovie: Cannot open movie file <%s>",filename);
467 return MOVIE_NOT_PLAYED;
470 Vid_State = VID_PLAY;
472 if (MVE_rmPrepMovie((void *)RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
477 RoboFilePos=lseek (RoboFile,0L,SEEK_CUR);
479 con_printf(DEBUG_LEVEL, "RoboFilePos=%d!\n", RoboFilePos);
486 * Subtitle system code
489 ubyte *subtitle_raw_data;
492 //search for next field following whitespace
493 ubyte *next_field(ubyte *p)
495 while (*p && !isspace(*p))
501 while (*p && isspace(*p))
511 int init_subtitles(char *filename)
520 if (! FindArg("-subtitles"))
523 ifile = cfopen(filename,"rb"); //try text version
525 if (!ifile) { //no text version, try binary version
526 char filename2[FILENAME_LEN];
527 change_filename_ext(filename2,filename,".TXB");
528 ifile = cfopen(filename2,"rb");
534 size = cfilelength(ifile);
536 MALLOC (subtitle_raw_data, ubyte, size+1);
538 read_count = cfread(subtitle_raw_data, 1, size, ifile);
542 subtitle_raw_data[size] = 0;
544 if (read_count != size) {
545 d_free(subtitle_raw_data);
549 p = subtitle_raw_data;
551 while (p && p < subtitle_raw_data+size) {
554 endp = strchr(p,'\n');
556 if (endp[-1] == '\r')
557 endp[-1] = 0; //handle 0d0a pair
558 *endp = 0; //string termintor
565 Subtitles[Num_subtitles].first_frame = atoi(p);
566 p = next_field(p); if (!p) continue;
567 Subtitles[Num_subtitles].last_frame = atoi(p);
568 p = next_field(p); if (!p) continue;
569 Subtitles[Num_subtitles].msg = p;
571 Assert(Num_subtitles==0 || Subtitles[Num_subtitles].first_frame >= Subtitles[Num_subtitles-1].first_frame);
572 Assert(Subtitles[Num_subtitles].last_frame >= Subtitles[Num_subtitles].first_frame);
585 void close_subtitles()
587 if (subtitle_raw_data)
588 d_free(subtitle_raw_data);
589 subtitle_raw_data = NULL;
594 //draw the subtitles for this frame
595 void draw_subtitles(int frame_num)
597 static int active_subtitles[MAX_ACTIVE_SUBTITLES];
598 static int num_active_subtitles,next_subtitle,line_spacing;
602 if (frame_num == 0) {
603 num_active_subtitles = 0;
605 gr_set_curfont( GAME_FONT );
606 line_spacing = grd_curcanv->cv_font->ft_h + (grd_curcanv->cv_font->ft_h >> 2);
607 gr_set_fontcolor(255,-1);
610 //get rid of any subtitles that have expired
611 for (t=0;t<num_active_subtitles;)
612 if (frame_num > Subtitles[active_subtitles[t]].last_frame) {
614 for (t2=t;t2<num_active_subtitles-1;t2++)
615 active_subtitles[t2] = active_subtitles[t2+1];
616 num_active_subtitles--;
622 //get any subtitles new for this frame
623 while (next_subtitle < Num_subtitles && frame_num >= Subtitles[next_subtitle].first_frame) {
624 if (num_active_subtitles >= MAX_ACTIVE_SUBTITLES)
625 Error("Too many active subtitles!");
626 active_subtitles[num_active_subtitles++] = next_subtitle;
630 //find y coordinate for first line of subtitles
631 y = grd_curcanv->cv_bitmap.bm_h-((line_spacing+1)*MAX_ACTIVE_SUBTITLES+2);
633 //erase old subtitles if necessary
636 gr_rect(0,y,grd_curcanv->cv_bitmap.bm_w-1,grd_curcanv->cv_bitmap.bm_h-1);
639 //now draw the current subtitles
640 for (t=0;t<num_active_subtitles;t++)
641 if (active_subtitles[t] != -1) {
642 gr_string(0x8000,y,Subtitles[active_subtitles[t]].msg);
648 movielib *init_new_movie_lib(char *filename,FILE *fp)
654 //read movie file header
656 nfiles = file_read_int(fp); //get number of files
658 //table = d_malloc(sizeof(*table) + sizeof(ml_entry)*nfiles);
659 MALLOC(table, movielib, 1);
660 MALLOC(table->movies, ml_entry, nfiles);
662 strcpy(table->name,filename);
663 table->n_movies = nfiles;
665 offset = 4+4+nfiles*(13+4); //id + nfiles + nfiles * (filename + size)
667 for (i=0;i<nfiles;i++) {
670 n = fread( table->movies[i].name, 13, 1, fp );
672 break; //end of file (probably)
674 len = file_read_int(fp);
676 table->movies[i].len = len;
677 table->movies[i].offset = offset;
679 offset += table->movies[i].len;
692 movielib *init_old_movie_lib(char *filename,FILE *fp)
696 movielib *table,*table2;
701 table = d_malloc(sizeof(*table) + sizeof(ml_entry)*MAX_MOVIES_PER_LIB);
706 i = fread( table->movies[nfiles].name, 13, 1, fp );
708 break; //end of file (probably)
710 i = fread( &len, 4, 1, fp );
712 Error("error reading movie library <%s>",filename);
714 table->movies[nfiles].len = INTEL_INT(len);
715 table->movies[nfiles].offset = ftell( fp );
717 fseek( fp, INTEL_INT(len), SEEK_CUR ); //skip data
722 //allocate correct-sized table
723 size = sizeof(*table) + sizeof(ml_entry)*nfiles;
724 table2 = d_malloc(size);
725 memcpy(table2,table,size);
729 strcpy(table->name,filename);
731 table->n_movies = nfiles;
742 //find the specified movie library, and read in list of movies in it
743 movielib *init_movie_lib(char *filename)
745 //note: this based on cfile_init_hogfile()
750 fp = fopen( filename, "rb" );
752 if ((fp == NULL) && (AltHogdir_initialized)) {
754 strcpy(temp, AltHogDir);
756 strcat(temp, filename);
757 fp = fopen(temp, "rb");
763 fread( id, 4, 1, fp );
764 if ( !strncmp( id, "DMVL", 4 ) )
765 return init_new_movie_lib(filename,fp);
766 else if ( !strncmp( id, "DHF", 3 ) ) {
767 fseek(fp,-1,SEEK_CUR); //old file had 3 char id
768 return init_old_movie_lib(filename,fp);
777 void close_movie(int i)
780 d_free(movie_libs[i]->movies);
781 d_free(movie_libs[i]);
790 for (i=0;i<N_MOVIE_LIBS;i++)
795 //ask user to put the D2 CD in.
796 //returns -1 if ESC pressed, 0 if OK chosen
797 //CD may not have been inserted
801 ubyte save_pal[256*3];
802 grs_canvas *save_canv,*tcanv;
803 int ret,was_faded=gr_palette_faded_out;
807 save_canv = grd_curcanv;
808 tcanv = gr_create_canvas(grd_curcanv->cv_w,grd_curcanv->cv_h);
810 gr_set_current_canvas(tcanv);
811 gr_ubitmap(0,0,&save_canv->cv_bitmap);
812 gr_set_current_canvas(save_canv);
814 gr_clear_canvas(BM_XRGB(0,0,0));
816 memcpy(save_pal,gr_palette,sizeof(save_pal));
818 memcpy(gr_palette,last_palette_for_color_fonts,sizeof(gr_palette));
822 ret = nm_messagebox( "CD ERROR", 1, "Ok", "Please insert your Descent II CD");
827 ret2 = nm_messagebox( "CD ERROR", 2, "Try Again", "Leave Game", "You must insert your\nDescent II CD to Continue");
829 if (ret2 == -1 || ret2 == 0)
833 force_rb_register = 1; //disc has changed; force register new CD
837 memcpy(gr_palette,save_pal,sizeof(save_pal));
839 gr_ubitmap(0,0,&tcanv->cv_bitmap);
842 gr_palette_load(gr_palette);
844 gr_free_canvas(tcanv);
848 con_printf(DEBUG_LEVEL, "STUB: movie: request_cd\n");
854 void init_movie(char *filename,int libnum,int is_robots,int required)
860 if (FindArg("-nomovies")) {
861 movie_libs[libnum] = NULL;
866 //for robots, load highres versions if highres menus set
868 high_res = MenuHiresAvailable;
870 high_res = MovieHires;
873 strchr(filename,'.')[-1] = 'h';
877 if ((movie_libs[libnum] = init_movie_lib(filename)) == NULL) {
880 strcpy(name2,CDROM_dir);
881 strcat(name2,filename);
882 movie_libs[libnum] = init_movie_lib(name2);
884 if (movie_libs[libnum] != NULL)
885 movie_libs[libnum]->flags |= MLF_ON_CD;
888 Warning("Cannot open movie file <%s>\n",filename);
891 if (!try) { // first try
892 if (strchr(filename, '.')[-1] == 'h') { // try again with lowres
893 strchr(filename, '.')[-1] = 'l';
895 Warning("Trying to open movie file <%s> instead\n", filename);
898 } else if (strchr(filename, '.')[-1] == 'l') { // try again with highres
899 strchr(filename, '.')[-1] = 'h';
901 Warning("Trying to open movie file <%s> instead\n", filename);
909 if (is_robots && movie_libs[libnum]!=NULL)
910 robot_movies = high_res?2:1;
914 //find and initialize the movie libraries
920 for (i=0;i<N_BUILTIN_MOVIE_LIBS;i++) {
922 if (!strnicmp(movielib_files[i],"robot",5))
927 init_movie(movielib_files[i],i,is_robots,1);
930 movie_libs[EXTRA_ROBOT_LIB] = NULL;
932 atexit(close_movies);
936 void init_extra_robot_movie(char *filename)
938 close_movie(EXTRA_ROBOT_LIB);
939 init_movie(filename,EXTRA_ROBOT_LIB,1,0);
943 int movie_handle,movie_start;
945 //looks through a movie library for a movie file
946 //returns filehandle, with fileposition at movie, or -1 if can't find
947 int search_movie_lib(movielib *lib,char *filename,int must_have)
955 for (i=0;i<lib->n_movies;i++)
956 if (!stricmp(filename,lib->movies[i].name)) { //found the movie in a library
959 from_cd = (lib->flags & MLF_ON_CD);
962 songs_stop_redbook(); //ready to read from CD
964 do { //keep trying until we get the file handle
967 movie_handle = filehandle = open(lib->name, O_RDONLY | O_BINARY);
969 movie_handle = filehandle = open(lib->name, O_RDONLY);
972 if ((filehandle == -1) && (AltHogdir_initialized)) {
974 strcpy(temp, AltHogDir);
976 strcat(temp, lib->name);
978 movie_handle = filehandle = open(temp, O_RDONLY | O_BINARY);
980 movie_handle = filehandle = open(temp, O_RDONLY);
984 if (must_have && from_cd && filehandle == -1) { //didn't get file!
986 if (request_cd() == -1) //ESC from requester
987 break; //bail from here. will get error later
990 } while (must_have && from_cd && filehandle == -1);
992 if (filehandle != -1)
993 lseek(filehandle,(movie_start=lib->movies[i].offset),SEEK_SET);
1002 //returns file handle
1003 int open_movie_file(char *filename,int must_have)
1007 for (i=0;i<N_MOVIE_LIBS;i++) {
1008 if ((filehandle = search_movie_lib(movie_libs[i],filename,must_have)) != -1)
1012 return -1; //couldn't find it
1015 //sets the file position to the start of this already-open file
1016 int reset_movie_file(int handle)
1018 Assert(handle == movie_handle);
1020 lseek(handle,movie_start,SEEK_SET);
1022 return 0; //everything is cool