2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
15 * $Source: /cvs/cvsroot/d2x/main/movie.c,v $
18 * $Date: 2002-01-18 07:26:54 $
20 * Movie stuff (converts mve's to exe files, and plays them externally (e.g. with wine)
22 * $Log: not supported by cvs2svn $
53 int RoboFile=0,MVEPaletteCalls=0;
55 // Function Prototypes
56 int RunMovie(char *filename, int highres_flag, int allow_abort,int dx,int dy);
60 short first_frame,last_frame;
65 // #define BUFFER_MOVIE
67 #define MAX_SUBTITLES 500
68 subtitle Subtitles[MAX_SUBTITLES];
71 int MovieHires = 0; //default for now is lores
73 //filename will actually get modified to be either low-res or high-res
74 //returns status. see values in movie.h
75 int PlayMovie(const char *filename, int must_have)
77 char name[FILENAME_LEN],*p;
81 if (FindArg("-nomovies"))
82 return MOVIE_NOT_PLAYED;
85 strcpy(name,filename);
87 if ((p=strchr(name,'.')) == NULL) //add extension, if missing
90 //check for escape already pressed & abort if so
91 while ((c=key_inkey()) != 0)
95 // Stop all digital sounds currently playing.
101 ret = RunMovie(name,MovieHires,must_have,-1,-1);
106 int open_movie_file(char *filename,int must_have, int *lenp);
108 //returns status. see movie.h
109 int RunMovie(char *filename, int hires_flag, int must_have,int dx,int dy)
114 // Open Movie file. If it doesn't exist, no movie, just return.
116 filehndl = open_movie_file(filename,must_have, &size);
118 if (filehndl == -1) {
123 Error("Cannot open movie file <%s>",filename);
126 return MOVIE_NOT_PLAYED;
128 return MOVIE_NOT_PLAYED;
134 gr_set_mode(SM_640x480V);
136 gr_set_mode(SM_320x200C);
148 strcpy(filename+strlen(filename)-4,".exe"); //change extension
149 if (stat(filename, &stats)) {
150 stubfile = "fstrailw.stub";
151 if (stat(stubfile, &stats)) {
152 con_printf(CON_NORMAL, "Error loading %s, aborting movie.\n", stubfile);
153 return MOVIE_NOT_PLAYED;
158 fil = fopen(stubfile, "r");
159 fread(buf, len, 1, fil);
162 fil = fopen(filename, "w");
163 fwrite(buf, len, 1, fil);
168 read(filehndl, buf, len);
169 fwrite(buf, len, 1, fil);
173 sprintf(execcmd, "wine %s", filename);
174 if(system(execcmd) == -1) {
175 con_printf(CON_NORMAL, "Error executing %s, movie aborted.\n", filename);
176 return MOVIE_NOT_PLAYED;
182 close(filehndl); // Close Movie File
184 Screen_mode=-1; //force reset of screen mode
186 return (aborted?MOVIE_ABORTED:MOVIE_PLAYED_FULL);
189 int InitMovieBriefing ()
194 //returns 1 if frame updated ok
200 void DeInitRobotMovie()
202 close(RoboFile); // Close Movie File
205 int InitRobotMovie (char *filename)
209 if (FindArg("-nomovies"))
210 return MOVIE_NOT_PLAYED;
214 mprintf ((0,"RoboFile=%s\n",filename));
216 RoboFile = open_movie_file(filename,1, &len);
218 if (RoboFile == -1) {
220 Error("Cannot open movie file <%s>",filename);
222 return MOVIE_NOT_PLAYED;
230 * Subtitle system code
233 ubyte *subtitle_raw_data;
235 //search for next field following whitespace
236 ubyte *next_field(ubyte *p)
238 while (*p && !isspace(*p))
244 while (*p && isspace(*p))
253 void change_filename_ext( char *dest, char *src, char *ext );
254 void decode_text_line(char *p);
256 int init_subtitles(char *filename)
265 if (! FindArg("-subtitles"))
268 ifile = cfopen(filename,"rb"); //try text version
270 if (!ifile) { //no text version, try binary version
271 char filename2[FILENAME_LEN];
272 change_filename_ext(filename2,filename,".TXB");
273 ifile = cfopen(filename2,"rb");
279 size = cfilelength(ifile);
281 MALLOC (subtitle_raw_data, ubyte, size+1);
283 read_count = cfread(subtitle_raw_data, 1, size, ifile);
287 subtitle_raw_data[size] = 0;
289 if (read_count != size) {
290 free(subtitle_raw_data);
294 p = subtitle_raw_data;
296 while (p && p < subtitle_raw_data+size) {
299 endp = strchr(p,'\n');
301 if (endp[-1] == '\r')
302 endp[-1] = 0; //handle 0d0a pair
303 *endp = 0; //string termintor
310 Subtitles[Num_subtitles].first_frame = atoi(p);
311 p = next_field(p); if (!p) continue;
312 Subtitles[Num_subtitles].last_frame = atoi(p);
313 p = next_field(p); if (!p) continue;
314 Subtitles[Num_subtitles].msg = p;
316 Assert(Num_subtitles==0 || Subtitles[Num_subtitles].first_frame >= Subtitles[Num_subtitles-1].first_frame);
317 Assert(Subtitles[Num_subtitles].last_frame >= Subtitles[Num_subtitles].first_frame);
330 void close_subtitles()
332 if (subtitle_raw_data)
333 free(subtitle_raw_data);
334 subtitle_raw_data = NULL;
339 char name[FILENAME_LEN];
346 char name[100]; //[FILENAME_LEN];
352 #define MAX_MOVIES_PER_LIB 50 //determines size of malloc
354 movielib *init_new_movie_lib(char *filename,FILE *fp)
360 //read movie file header
362 fread(&nfiles,4,1,fp); //get number of files
364 table = malloc(sizeof(*table) + sizeof(ml_entry)*nfiles);
366 strcpy(table->name,filename);
367 table->n_movies = nfiles;
369 offset = 4+4+nfiles*(13+4); //id + nfiles + nfiles * (filename + size)
371 for (i=0;i<nfiles;i++) {
374 n = fread( table->movies[i].name, 13, 1, fp );
376 break; //end of file (probably)
378 n = fread( &len, 4, 1, fp );
380 Error("error reading movie library <%s>",filename);
382 table->movies[i].len = INTEL_INT(len);
383 table->movies[i].offset = offset;
385 offset += table->movies[i].len;
397 movielib *init_old_movie_lib(char *filename,FILE *fp)
401 movielib *table,*table2;
406 table = malloc(sizeof(*table) + sizeof(ml_entry)*MAX_MOVIES_PER_LIB);
411 i = fread( table->movies[nfiles].name, 13, 1, fp );
413 break; //end of file (probably)
415 i = fread( &len, 4, 1, fp );
417 Error("error reading movie library <%s>",filename);
419 table->movies[nfiles].len = INTEL_INT(len);
420 table->movies[nfiles].offset = ftell( fp );
422 fseek( fp, INTEL_INT(len), SEEK_CUR ); //skip data
427 //allocate correct-sized table
428 size = sizeof(*table) + sizeof(ml_entry)*nfiles;
429 table2 = malloc(size);
430 memcpy(table2,table,size);
434 strcpy(table->name,filename);
436 table->n_movies = nfiles;
446 //find the specified movie library, and read in list of movies in it
447 movielib *init_movie_lib(char *filename)
449 //note: this based on cfile_init_hogfile()
454 fp = fopen( filename, "rb" );
458 fread( id, 4, 1, fp );
459 if ( !strncmp( id, "DMVL", 4 ) )
460 return init_new_movie_lib(filename,fp);
461 else if ( !strncmp( id, "DHF", 3 ) ) {
462 fseek(fp,-1,SEEK_CUR); //old file had 3 char id
463 return init_old_movie_lib(filename,fp);
472 char *movielib_files[] = {"intro-l.mvl","other-l.mvl","robots-l.mvl","oem-l.mvl"};
474 char *movielib_files[] = {"intro-l.mvl","other-l.mvl","robots-l.mvl"};
477 #define N_BUILTIN_MOVIE_LIBS (sizeof(movielib_files)/sizeof(*movielib_files))
478 #define N_MOVIE_LIBS (N_BUILTIN_MOVIE_LIBS+1)
479 #define EXTRA_ROBOT_LIB N_BUILTIN_MOVIE_LIBS
480 movielib *movie_libs[N_MOVIE_LIBS];
482 void close_movie(int i)
492 for (i=0;i<N_MOVIE_LIBS;i++)
498 extern char CDROM_dir[];
499 extern int MenuHiresAvailable;
501 extern ubyte last_palette_for_color_fonts[];
503 extern int force_rb_register;
505 //ask user to put the D2 CD in.
506 //returns -1 if ESC pressed, 0 if OK chosen
507 //CD may not have been inserted
510 ubyte save_pal[256*3];
511 grs_canvas *save_canv,*tcanv;
512 int ret,was_faded=gr_palette_faded_out;
516 save_canv = grd_curcanv;
517 tcanv = gr_create_canvas(grd_curcanv->cv_w,grd_curcanv->cv_h);
519 gr_set_current_canvas(tcanv);
520 gr_ubitmap(0,0,&save_canv->cv_bitmap);
521 gr_set_current_canvas(save_canv);
523 gr_clear_canvas(BM_XRGB(0,0,0));
525 memcpy(save_pal,gr_palette,sizeof(save_pal));
527 memcpy(gr_palette,last_palette_for_color_fonts,sizeof(gr_palette));
531 ret = nm_messagebox( "CD ERROR", 1, "Ok", "Please insert your Descent II CD");
536 ret2 = nm_messagebox( "CD ERROR", 2, "Try Again", "Leave Game", "You must insert your\nDescent II CD to Continue");
538 if (ret2 == -1 || ret2 == 0)
542 force_rb_register = 1; //disc has changed; force register new CD
546 memcpy(gr_palette,save_pal,sizeof(save_pal));
548 gr_ubitmap(0,0,&tcanv->cv_bitmap);
551 gr_palette_load(gr_palette);
553 gr_free_canvas(tcanv);
558 //do we have the robot movies available
559 int robot_movies=0; //0 means none, 1 means lowres, 2 means hires
561 void init_movie(char *filename,int libnum,int is_robots,int required)
566 if (FindArg("-nomovies")) {
567 movie_libs[libnum] = NULL;
572 //for robots, load highres versions if highres menus set
574 high_res = MenuHiresAvailable;
576 high_res = MovieHires;
579 strchr(filename,'.')[-1] = 'h';
585 if ((movie_libs[libnum] = init_movie_lib(filename)) == NULL) {
588 strcpy(name2,CDROM_dir);
589 strcat(name2,filename);
590 movie_libs[libnum] = init_movie_lib(name2);
592 if (movie_libs[libnum] != NULL)
593 movie_libs[libnum]->flags |= MLF_ON_CD;
596 #if defined(RELEASE) && !defined(D2_OEM) //allow no movies if not release
598 Error("Cannot open movie file <%s>",filename);
601 #if defined(D2_OEM) //if couldn't get higres, try low
602 if (is_robots == 1) { //first try, try again with lowres
603 strchr(filename,'.')[-1] = 'l';
608 else if (is_robots == 2) { //failed twice. bail with error
610 Error("Cannot open movie file <%s>",filename);
616 if (is_robots && movie_libs[libnum]!=NULL)
617 robot_movies = high_res?2:1;
620 //find and initialize the movie libraries
626 for (i=0;i<N_BUILTIN_MOVIE_LIBS;i++) {
628 if (!strnicmp(movielib_files[i],"robot",5))
633 init_movie(movielib_files[i],i,is_robots,1);
636 movie_libs[EXTRA_ROBOT_LIB] = NULL;
638 atexit(close_movies);
642 void init_extra_robot_movie(char *filename)
644 close_movie(EXTRA_ROBOT_LIB);
645 init_movie(filename,EXTRA_ROBOT_LIB,1,0);
648 int movie_handle,movie_start;
650 //looks through a movie library for a movie file
651 //returns filehandle, with fileposition at movie, or -1 if can't find
652 int search_movie_lib(movielib *lib,char *filename,int must_have, int *lenp)
660 for (i=0;i<lib->n_movies;i++)
661 if (!stricmp(filename,lib->movies[i].name)) { //found the movie in a library
664 *lenp = lib->movies[i].len;
666 from_cd = (lib->flags & MLF_ON_CD);
669 songs_stop_redbook(); //ready to read from CD
671 do { //keep trying until we get the file handle
673 movie_handle = filehandle = open(lib->name, O_RDONLY);
675 if (must_have && from_cd && filehandle == -1) { //didn't get file!
677 if (request_cd() == -1) //ESC from requester
678 break; //bail from here. will get error later
681 } while (must_have && from_cd && filehandle == -1);
683 if (filehandle != -1)
684 lseek(filehandle,(movie_start=lib->movies[i].offset),SEEK_SET);
692 //returns file handle
693 int open_movie_file(char *filename,int must_have, int *lenp)
697 for (i=0;i<N_MOVIE_LIBS;i++) {
699 if ((filehandle = search_movie_lib(movie_libs[i],filename,must_have, lenp)) != -1)
703 return -1; //couldn't find it