1 /* $Id: movie.c,v 1.39 2005-07-30 01:50:17 chris 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.39 2005-07-30 01:50:17 chris Exp $";
32 # include <sys/types.h>
33 # include <sys/stat.h>
61 #include "physfsrwops.h"
63 extern int MenuHiresAvailable;
64 extern char CDROM_dir[];
74 short first_frame,last_frame;
78 #define MAX_SUBTITLES 500
79 #define MAX_ACTIVE_SUBTITLES 3
80 subtitle Subtitles[MAX_SUBTITLES];
86 char movielib_files[5][FILENAME_LEN] = {"intro","other","robots","oem"};
88 char movielib_files[4][FILENAME_LEN] = {"intro","other","robots"};
91 #define N_MOVIE_LIBS (sizeof(movielib_files) / sizeof(*movielib_files))
92 #define N_BUILTIN_MOVIE_LIBS (N_MOVIE_LIBS - 1)
93 #define EXTRA_ROBOT_LIB N_BUILTIN_MOVIE_LIBS
95 int MovieHires = 1; //default is highres
99 // Function Prototypes
100 int RunMovie(char *filename, int highres_flag, int allow_abort,int dx,int dy);
102 void change_filename_ext( char *dest, char *src, char *ext );
103 void decode_text_line(char *p);
104 void draw_subtitles(int frame_num);
107 // ----------------------------------------------------------------------
108 void* MPlayAlloc(unsigned size)
110 return d_malloc(size);
113 void MPlayFree(void *p)
119 //-----------------------------------------------------------------------
121 unsigned int FileRead(void *handle, void *buf, unsigned int count)
124 numread = SDL_RWread((SDL_RWops *)handle, buf, 1, count);
125 return (numread == count);
129 //-----------------------------------------------------------------------
132 //filename will actually get modified to be either low-res or high-res
133 //returns status. see values in movie.h
134 int PlayMovie(const char *filename, int must_have)
136 char name[FILENAME_LEN],*p;
139 if (FindArg("-nomovies"))
140 return MOVIE_NOT_PLAYED;
142 strcpy(name,filename);
144 if ((p=strchr(name,'.')) == NULL) //add extension, if missing
147 //check for escape already pressed & abort if so
148 while ((c=key_inkey()) != 0)
150 return MOVIE_ABORTED;
152 // Stop all digital sounds currently playing.
161 if (!FindArg("-nosound"))
166 ret = RunMovie(name,MovieHires,must_have,-1,-1);
168 if (!FindArg("-nosound"))
171 Screen_mode = -1; //force screen reset
177 void MovieShowFrame(ubyte *buf, uint bufw, uint bufh, uint sx, uint sy,
178 uint w, uint h, uint dstx, uint dsty)
180 grs_bitmap source_bm;
182 //mprintf((0,"MovieShowFrame %d,%d %d,%d %d,%d %d,%d\n",bufw,bufh,sx,sy,w,h,dstx,dsty));
184 Assert(bufw == w && bufh == h);
186 source_bm.bm_x = source_bm.bm_y = 0;
187 source_bm.bm_w = source_bm.bm_rowsize = bufw;
188 source_bm.bm_h = bufh;
189 source_bm.bm_type = BM_LINEAR;
190 source_bm.bm_flags = 0;
191 source_bm.bm_data = buf;
193 gr_bm_ubitblt(bufw,bufh,dstx,dsty,sx,sy,&source_bm,&grd_curcanv->cv_bitmap);
196 //our routine to set the pallete, called from the movie code
197 void MovieSetPalette(unsigned char *p, unsigned start, unsigned count)
202 //mprintf((0,"SetPalette p=%x, start=%d, count=%d\n",p,start,count));
204 //Color 0 should be black, and we get color 255
205 Assert(start>=1 && start+count-1<=254);
207 //Set color 0 to be black
208 gr_palette[0] = gr_palette[1] = gr_palette[2] = 0;
210 //Set color 255 to be our subtitle color
211 gr_palette[765] = gr_palette[766] = gr_palette[767] = 50;
213 //movie libs palette into our array
214 memcpy(gr_palette+start*3,p+start*3,count*3);
216 //finally set the palette in the hardware
217 //gr_palette_load(gr_palette);
219 //MVE_SetPalette(p, start, count);
225 short x, y, w, h; // The location of the menu.
226 grs_bitmap * bmp; // The background under the menu.
229 bkg movie_bg = {0,0,0,0,NULL};
232 #define BOX_BORDER (MenuHires?40:20)
235 void show_pause_message(char *msg)
240 gr_set_current_canvas(NULL);
241 gr_set_curfont( SMALL_FONT );
243 gr_get_string_size(msg,&w,&h,&aw);
245 x = (grd_curscreen->sc_w-w)/2;
246 y = (grd_curscreen->sc_h-h)/2;
250 gr_free_bitmap(movie_bg.bmp);
254 // Save the background of the display
255 movie_bg.x=x; movie_bg.y=y; movie_bg.w=w; movie_bg.h=h;
257 movie_bg.bmp = gr_create_bitmap( w+BOX_BORDER, h+BOX_BORDER );
259 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 );
263 gr_rect(x-BOX_BORDER/2,y-BOX_BORDER/2,x+w+BOX_BORDER/2-1,y+h+BOX_BORDER/2-1);
265 gr_set_fontcolor( 255, -1 );
267 gr_ustring( 0x8000, y, msg );
272 void clear_pause_message()
277 gr_bitmap(movie_bg.x-BOX_BORDER/2, movie_bg.y-BOX_BORDER/2, movie_bg.bmp);
279 gr_free_bitmap(movie_bg.bmp);
286 //returns status. see movie.h
287 int RunMovie(char *filename, int hires_flag, int must_have,int dx,int dy)
290 int result=1,aborted=0;
300 // Open Movie file. If it doesn't exist, no movie, just return.
302 filehndl = PHYSFSRWOPS_openRead(filename);
307 con_printf(CON_URGENT, "Can't open movie <%s>: %s\n", filename, PHYSFS_getLastError());
308 return MOVIE_NOT_PLAYED;
311 MVE_memCallbacks(MPlayAlloc, MPlayFree);
312 MVE_ioCallbacks(FileRead);
315 gr_set_mode(SM(640,480));
317 gr_set_mode(SM(320,200));
320 set_screen_mode(SCREEN_MENU);
321 gr_copy_palette(pal_save, gr_palette, 768);
322 memset(gr_palette, 0, 768);
323 gr_palette_load(gr_palette);
326 MVE_sfCallbacks(MovieShowFrame);
327 MVE_palCallbacks(MovieSetPalette);
329 if (MVE_rmPrepMovie((void *)filehndl, dx, dy, track)) {
331 return MOVIE_NOT_PLAYED;
334 MVE_sfCallbacks(MovieShowFrame);
335 MVE_palCallbacks(MovieSetPalette);
339 FontHires = FontHiresAvailable && hires_flag;
341 while((result = MVE_rmStepMovie()) == 0) {
343 draw_subtitles(frame_num);
345 gr_palette_load(gr_palette); // moved this here because of flashing
351 // If ESCAPE pressed, then quit movie.
352 if (key == KEY_ESC) {
353 result = aborted = 1;
357 // If PAUSE pressed, then pause movie
358 if (key == KEY_PAUSE) {
360 show_pause_message(TXT_PAUSE);
361 while (!key_inkey()) ;
362 clear_pause_message();
365 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
366 if ((key == KEY_ALTED+KEY_ENTER) ||
367 (key == KEY_ALTED+KEY_PADENTER))
368 gr_toggle_fullscreen();
374 Assert(aborted || result == MVE_ERR_EOF); ///movie should be over
378 SDL_FreeRW(filehndl); // Close Movie File
380 // Restore old graphic state
382 Screen_mode=-1; //force reset of screen mode
384 gr_copy_palette(gr_palette, pal_save, 768);
385 gr_palette_load(pal_save);
388 return (aborted?MOVIE_ABORTED:MOVIE_PLAYED_FULL);
392 int InitMovieBriefing()
396 gr_set_mode(SM(640,480));
398 gr_set_mode(SM(320,200));
400 gr_init_sub_canvas( &VR_screen_pages[0], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
401 gr_init_sub_canvas( &VR_screen_pages[1], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
408 //returns 1 if frame updated ok
413 err = MVE_rmStepMovie();
415 gr_palette_load(gr_palette);
417 if (err == MVE_ERR_EOF) //end of movie, so reset
419 SDL_RWseek(RoboFile, 0, SEEK_SET);
420 if (MVE_rmPrepMovie(RoboFile, MenuHires?280:140, MenuHires?200:80, 0))
435 void DeInitRobotMovie(void)
438 SDL_FreeRW(RoboFile); // Close Movie File
442 int InitRobotMovie(char *filename)
444 if (FindArg("-nomovies"))
447 con_printf(CON_DEBUG, "RoboFile=%s\n", filename);
449 MVE_sndInit(-1); //tell movies to play no sound for robots
451 RoboFile = PHYSFSRWOPS_openRead(filename);
455 con_printf(CON_URGENT, "Can't open movie <%s>: %s\n", filename, PHYSFS_getLastError());
456 return MOVIE_NOT_PLAYED;
459 Vid_State = VID_PLAY;
461 if (MVE_rmPrepMovie((void *)RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
471 * Subtitle system code
474 char *subtitle_raw_data;
477 //search for next field following whitespace
478 char *next_field (char *p)
480 while (*p && !isspace(*p))
486 while (*p && isspace(*p))
496 int init_subtitles(char *filename)
505 if (! FindArg("-subtitles"))
508 ifile = cfopen(filename,"rb"); //try text version
510 if (!ifile) { //no text version, try binary version
511 char filename2[FILENAME_LEN];
512 change_filename_ext(filename2,filename,".TXB");
513 ifile = cfopen(filename2,"rb");
519 size = cfilelength(ifile);
521 MALLOC (subtitle_raw_data, char, size+1);
523 read_count = cfread(subtitle_raw_data, 1, size, ifile);
527 subtitle_raw_data[size] = 0;
529 if (read_count != size) {
530 d_free(subtitle_raw_data);
534 p = subtitle_raw_data;
536 while (p && p < subtitle_raw_data+size) {
539 endp = strchr(p,'\n');
541 if (endp[-1] == '\r')
542 endp[-1] = 0; //handle 0d0a pair
543 *endp = 0; //string termintor
550 Subtitles[Num_subtitles].first_frame = atoi(p);
551 p = next_field(p); if (!p) continue;
552 Subtitles[Num_subtitles].last_frame = atoi(p);
553 p = next_field(p); if (!p) continue;
554 Subtitles[Num_subtitles].msg = p;
556 Assert(Num_subtitles==0 || Subtitles[Num_subtitles].first_frame >= Subtitles[Num_subtitles-1].first_frame);
557 Assert(Subtitles[Num_subtitles].last_frame >= Subtitles[Num_subtitles].first_frame);
570 void close_subtitles()
572 if (subtitle_raw_data)
573 d_free(subtitle_raw_data);
574 subtitle_raw_data = NULL;
579 //draw the subtitles for this frame
580 void draw_subtitles(int frame_num)
582 static int active_subtitles[MAX_ACTIVE_SUBTITLES];
583 static int num_active_subtitles,next_subtitle,line_spacing;
587 if (frame_num == 0) {
588 num_active_subtitles = 0;
590 gr_set_curfont( GAME_FONT );
591 line_spacing = grd_curcanv->cv_font->ft_h + (grd_curcanv->cv_font->ft_h >> 2);
592 gr_set_fontcolor(255,-1);
595 //get rid of any subtitles that have expired
596 for (t=0;t<num_active_subtitles;)
597 if (frame_num > Subtitles[active_subtitles[t]].last_frame) {
599 for (t2=t;t2<num_active_subtitles-1;t2++)
600 active_subtitles[t2] = active_subtitles[t2+1];
601 num_active_subtitles--;
607 //get any subtitles new for this frame
608 while (next_subtitle < Num_subtitles && frame_num >= Subtitles[next_subtitle].first_frame) {
609 if (num_active_subtitles >= MAX_ACTIVE_SUBTITLES)
610 Error("Too many active subtitles!");
611 active_subtitles[num_active_subtitles++] = next_subtitle;
615 //find y coordinate for first line of subtitles
616 y = grd_curcanv->cv_bitmap.bm_h-((line_spacing+1)*MAX_ACTIVE_SUBTITLES+2);
618 //erase old subtitles if necessary
621 gr_rect(0,y,grd_curcanv->cv_bitmap.bm_w-1,grd_curcanv->cv_bitmap.bm_h-1);
624 //now draw the current subtitles
625 for (t=0;t<num_active_subtitles;t++)
626 if (active_subtitles[t] != -1) {
627 gr_string(0x8000,y,Subtitles[active_subtitles[t]].msg);
633 void close_movie(char *movielib, int is_robots)
636 char filename[FILENAME_LEN];
639 high_res = MenuHiresAvailable;
641 high_res = MovieHires;
643 sprintf(filename, "%s-%s.mvl", movielib, high_res?"h":"l");
645 if (!cfile_close(filename))
647 con_printf(CON_URGENT, "Can't close movielib <%s>: %s\n", filename, PHYSFS_getLastError());
648 sprintf(filename, "%s-%s.mvl", movielib, high_res?"l":"h");
650 if (!cfile_close(filename))
651 con_printf(CON_URGENT, "Can't close movielib <%s>: %s\n", filename, PHYSFS_getLastError());
659 for (i = 0 ; i < N_BUILTIN_MOVIE_LIBS ; i++)
661 if (!strnicmp(movielib_files[i], "robot", 5))
666 close_movie(movielib_files[i], is_robots);
671 //ask user to put the D2 CD in.
672 //returns -1 if ESC pressed, 0 if OK chosen
673 //CD may not have been inserted
677 ubyte save_pal[256*3];
678 grs_canvas *save_canv,*tcanv;
679 int ret,was_faded=gr_palette_faded_out;
683 save_canv = grd_curcanv;
684 tcanv = gr_create_canvas(grd_curcanv->cv_w,grd_curcanv->cv_h);
686 gr_set_current_canvas(tcanv);
687 gr_ubitmap(0,0,&save_canv->cv_bitmap);
688 gr_set_current_canvas(save_canv);
690 gr_clear_canvas(BM_XRGB(0,0,0));
692 memcpy(save_pal,gr_palette,sizeof(save_pal));
694 memcpy(gr_palette,last_palette_for_color_fonts,sizeof(gr_palette));
698 ret = nm_messagebox( "CD ERROR", 1, "Ok", "Please insert your Descent II CD");
703 ret2 = nm_messagebox( "CD ERROR", 2, "Try Again", "Leave Game", "You must insert your\nDescent II CD to Continue");
705 if (ret2 == -1 || ret2 == 0)
709 force_rb_register = 1; //disc has changed; force register new CD
713 memcpy(gr_palette,save_pal,sizeof(save_pal));
715 gr_ubitmap(0,0,&tcanv->cv_bitmap);
718 gr_palette_load(gr_palette);
720 gr_free_canvas(tcanv);
724 con_printf(CON_DEBUG, "STUB: movie: request_cd\n");
730 void init_movie(char *movielib, int is_robots, int required)
733 char filename[FILENAME_LEN];
735 //for robots, load highres versions if highres menus set
737 high_res = MenuHiresAvailable;
739 high_res = MovieHires;
741 sprintf(filename, "%s-%s.mvl", movielib, high_res?"h":"l");
743 if (!cfile_init(filename))
746 con_printf(CON_URGENT, "Can't open movielib <%s>: %s\n", filename, PHYSFS_getLastError());
748 sprintf(filename, "%s-%s.mvl", movielib, high_res?"l":"h");
750 if (!cfile_init(filename))
752 con_printf(CON_URGENT, "Can't open movielib <%s>: %s\n", filename, PHYSFS_getLastError());
757 //find and initialize the movie libraries
763 if (FindArg("-nomovies"))
766 for (i=0;i<N_BUILTIN_MOVIE_LIBS;i++) {
768 if (!strnicmp(movielib_files[i],"robot",5))
773 init_movie(movielib_files[i], is_robots, 1);
776 atexit(close_movies);
780 void close_extra_robot_movie(void)
782 if (strlen(movielib_files[EXTRA_ROBOT_LIB]))
783 if (!cfile_close(movielib_files[EXTRA_ROBOT_LIB]))
784 con_printf(CON_URGENT, "Can't close robot movielib: %s\n", PHYSFS_getLastError());
787 void init_extra_robot_movie(char *movielib)
789 if (FindArg("-nomovies"))
792 close_extra_robot_movie();
793 init_movie(movielib, 1, 0);
794 strcpy(movielib_files[EXTRA_ROBOT_LIB], movielib);
795 atexit(close_extra_robot_movie);