1 /* $Id: movie.c,v 1.40 2006-02-26 02:29:06 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.40 2006-02-26 02:29:06 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 decode_text_line(char *p);
103 void draw_subtitles(int frame_num);
106 // ----------------------------------------------------------------------
107 void* MPlayAlloc(unsigned size)
109 return d_malloc(size);
112 void MPlayFree(void *p)
118 //-----------------------------------------------------------------------
120 unsigned int FileRead(void *handle, void *buf, unsigned int count)
123 numread = SDL_RWread((SDL_RWops *)handle, buf, 1, count);
124 return (numread == count);
128 //-----------------------------------------------------------------------
131 //filename will actually get modified to be either low-res or high-res
132 //returns status. see values in movie.h
133 int PlayMovie(const char *filename, int must_have)
135 char name[FILENAME_LEN],*p;
138 if (FindArg("-nomovies"))
139 return MOVIE_NOT_PLAYED;
141 strcpy(name,filename);
143 if ((p=strchr(name,'.')) == NULL) //add extension, if missing
146 //check for escape already pressed & abort if so
147 while ((c=key_inkey()) != 0)
149 return MOVIE_ABORTED;
151 // Stop all digital sounds currently playing.
160 if (!FindArg("-nosound"))
165 ret = RunMovie(name,MovieHires,must_have,-1,-1);
167 if (!FindArg("-nosound"))
170 Screen_mode = -1; //force screen reset
176 void MovieShowFrame(ubyte *buf, uint bufw, uint bufh, uint sx, uint sy,
177 uint w, uint h, uint dstx, uint dsty)
179 grs_bitmap source_bm;
181 //mprintf((0,"MovieShowFrame %d,%d %d,%d %d,%d %d,%d\n",bufw,bufh,sx,sy,w,h,dstx,dsty));
183 Assert(bufw == w && bufh == h);
185 source_bm.bm_x = source_bm.bm_y = 0;
186 source_bm.bm_w = source_bm.bm_rowsize = bufw;
187 source_bm.bm_h = bufh;
188 source_bm.bm_type = BM_LINEAR;
189 source_bm.bm_flags = 0;
190 source_bm.bm_data = buf;
192 gr_bm_ubitblt(bufw,bufh,dstx,dsty,sx,sy,&source_bm,&grd_curcanv->cv_bitmap);
195 //our routine to set the pallete, called from the movie code
196 void MovieSetPalette(unsigned char *p, unsigned start, unsigned count)
201 //mprintf((0,"SetPalette p=%x, start=%d, count=%d\n",p,start,count));
203 //Color 0 should be black, and we get color 255
204 Assert(start>=1 && start+count-1<=254);
206 //Set color 0 to be black
207 gr_palette[0] = gr_palette[1] = gr_palette[2] = 0;
209 //Set color 255 to be our subtitle color
210 gr_palette[765] = gr_palette[766] = gr_palette[767] = 50;
212 //movie libs palette into our array
213 memcpy(gr_palette+start*3,p+start*3,count*3);
215 //finally set the palette in the hardware
216 //gr_palette_load(gr_palette);
218 //MVE_SetPalette(p, start, count);
224 short x, y, w, h; // The location of the menu.
225 grs_bitmap * bmp; // The background under the menu.
228 bkg movie_bg = {0,0,0,0,NULL};
231 #define BOX_BORDER (MenuHires?40:20)
234 void show_pause_message(char *msg)
239 gr_set_current_canvas(NULL);
240 gr_set_curfont( SMALL_FONT );
242 gr_get_string_size(msg,&w,&h,&aw);
244 x = (grd_curscreen->sc_w-w)/2;
245 y = (grd_curscreen->sc_h-h)/2;
249 gr_free_bitmap(movie_bg.bmp);
253 // Save the background of the display
254 movie_bg.x=x; movie_bg.y=y; movie_bg.w=w; movie_bg.h=h;
256 movie_bg.bmp = gr_create_bitmap( w+BOX_BORDER, h+BOX_BORDER );
258 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 );
262 gr_rect(x-BOX_BORDER/2,y-BOX_BORDER/2,x+w+BOX_BORDER/2-1,y+h+BOX_BORDER/2-1);
264 gr_set_fontcolor( 255, -1 );
266 gr_ustring( 0x8000, y, msg );
271 void clear_pause_message()
276 gr_bitmap(movie_bg.x-BOX_BORDER/2, movie_bg.y-BOX_BORDER/2, movie_bg.bmp);
278 gr_free_bitmap(movie_bg.bmp);
285 //returns status. see movie.h
286 int RunMovie(char *filename, int hires_flag, int must_have,int dx,int dy)
289 int result=1,aborted=0;
299 // Open Movie file. If it doesn't exist, no movie, just return.
301 filehndl = PHYSFSRWOPS_openRead(filename);
306 con_printf(CON_URGENT, "Can't open movie <%s>: %s\n", filename, PHYSFS_getLastError());
307 return MOVIE_NOT_PLAYED;
310 MVE_memCallbacks(MPlayAlloc, MPlayFree);
311 MVE_ioCallbacks(FileRead);
314 gr_set_mode(SM(640,480));
316 gr_set_mode(SM(320,200));
319 set_screen_mode(SCREEN_MENU);
320 gr_copy_palette(pal_save, gr_palette, 768);
321 memset(gr_palette, 0, 768);
322 gr_palette_load(gr_palette);
325 MVE_sfCallbacks(MovieShowFrame);
326 MVE_palCallbacks(MovieSetPalette);
328 if (MVE_rmPrepMovie((void *)filehndl, dx, dy, track)) {
330 return MOVIE_NOT_PLAYED;
333 MVE_sfCallbacks(MovieShowFrame);
334 MVE_palCallbacks(MovieSetPalette);
338 FontHires = FontHiresAvailable && hires_flag;
340 while((result = MVE_rmStepMovie()) == 0) {
342 draw_subtitles(frame_num);
344 gr_palette_load(gr_palette); // moved this here because of flashing
350 // If ESCAPE pressed, then quit movie.
351 if (key == KEY_ESC) {
352 result = aborted = 1;
356 // If PAUSE pressed, then pause movie
357 if (key == KEY_PAUSE) {
359 show_pause_message(TXT_PAUSE);
360 while (!key_inkey()) ;
361 clear_pause_message();
364 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
365 if ((key == KEY_ALTED+KEY_ENTER) ||
366 (key == KEY_ALTED+KEY_PADENTER))
367 gr_toggle_fullscreen();
373 Assert(aborted || result == MVE_ERR_EOF); ///movie should be over
377 SDL_FreeRW(filehndl); // Close Movie File
379 // Restore old graphic state
381 Screen_mode=-1; //force reset of screen mode
383 gr_copy_palette(gr_palette, pal_save, 768);
384 gr_palette_load(pal_save);
387 return (aborted?MOVIE_ABORTED:MOVIE_PLAYED_FULL);
391 int InitMovieBriefing()
395 gr_set_mode(SM(640,480));
397 gr_set_mode(SM(320,200));
399 gr_init_sub_canvas( &VR_screen_pages[0], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
400 gr_init_sub_canvas( &VR_screen_pages[1], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
407 //returns 1 if frame updated ok
412 err = MVE_rmStepMovie();
414 gr_palette_load(gr_palette);
416 if (err == MVE_ERR_EOF) //end of movie, so reset
418 SDL_RWseek(RoboFile, 0, SEEK_SET);
419 if (MVE_rmPrepMovie(RoboFile, MenuHires?280:140, MenuHires?200:80, 0))
434 void DeInitRobotMovie(void)
437 SDL_FreeRW(RoboFile); // Close Movie File
441 int InitRobotMovie(char *filename)
443 if (FindArg("-nomovies"))
446 con_printf(CON_DEBUG, "RoboFile=%s\n", filename);
448 MVE_sndInit(-1); //tell movies to play no sound for robots
450 RoboFile = PHYSFSRWOPS_openRead(filename);
454 con_printf(CON_URGENT, "Can't open movie <%s>: %s\n", filename, PHYSFS_getLastError());
455 return MOVIE_NOT_PLAYED;
458 Vid_State = VID_PLAY;
460 if (MVE_rmPrepMovie((void *)RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
470 * Subtitle system code
473 char *subtitle_raw_data;
476 //search for next field following whitespace
477 char *next_field (char *p)
479 while (*p && !isspace(*p))
485 while (*p && isspace(*p))
495 int init_subtitles(char *filename)
504 if (! FindArg("-subtitles"))
507 ifile = cfopen(filename,"rb"); //try text version
509 if (!ifile) { //no text version, try binary version
510 char filename2[FILENAME_LEN];
511 change_filename_extension(filename2, filename, ".TXB");
512 ifile = cfopen(filename2,"rb");
518 size = cfilelength(ifile);
520 MALLOC (subtitle_raw_data, char, size+1);
522 read_count = cfread(subtitle_raw_data, 1, size, ifile);
526 subtitle_raw_data[size] = 0;
528 if (read_count != size) {
529 d_free(subtitle_raw_data);
533 p = subtitle_raw_data;
535 while (p && p < subtitle_raw_data+size) {
538 endp = strchr(p,'\n');
540 if (endp[-1] == '\r')
541 endp[-1] = 0; //handle 0d0a pair
542 *endp = 0; //string termintor
549 Subtitles[Num_subtitles].first_frame = atoi(p);
550 p = next_field(p); if (!p) continue;
551 Subtitles[Num_subtitles].last_frame = atoi(p);
552 p = next_field(p); if (!p) continue;
553 Subtitles[Num_subtitles].msg = p;
555 Assert(Num_subtitles==0 || Subtitles[Num_subtitles].first_frame >= Subtitles[Num_subtitles-1].first_frame);
556 Assert(Subtitles[Num_subtitles].last_frame >= Subtitles[Num_subtitles].first_frame);
569 void close_subtitles()
571 if (subtitle_raw_data)
572 d_free(subtitle_raw_data);
573 subtitle_raw_data = NULL;
578 //draw the subtitles for this frame
579 void draw_subtitles(int frame_num)
581 static int active_subtitles[MAX_ACTIVE_SUBTITLES];
582 static int num_active_subtitles,next_subtitle,line_spacing;
586 if (frame_num == 0) {
587 num_active_subtitles = 0;
589 gr_set_curfont( GAME_FONT );
590 line_spacing = grd_curcanv->cv_font->ft_h + (grd_curcanv->cv_font->ft_h >> 2);
591 gr_set_fontcolor(255,-1);
594 //get rid of any subtitles that have expired
595 for (t=0;t<num_active_subtitles;)
596 if (frame_num > Subtitles[active_subtitles[t]].last_frame) {
598 for (t2=t;t2<num_active_subtitles-1;t2++)
599 active_subtitles[t2] = active_subtitles[t2+1];
600 num_active_subtitles--;
606 //get any subtitles new for this frame
607 while (next_subtitle < Num_subtitles && frame_num >= Subtitles[next_subtitle].first_frame) {
608 if (num_active_subtitles >= MAX_ACTIVE_SUBTITLES)
609 Error("Too many active subtitles!");
610 active_subtitles[num_active_subtitles++] = next_subtitle;
614 //find y coordinate for first line of subtitles
615 y = grd_curcanv->cv_bitmap.bm_h-((line_spacing+1)*MAX_ACTIVE_SUBTITLES+2);
617 //erase old subtitles if necessary
620 gr_rect(0,y,grd_curcanv->cv_bitmap.bm_w-1,grd_curcanv->cv_bitmap.bm_h-1);
623 //now draw the current subtitles
624 for (t=0;t<num_active_subtitles;t++)
625 if (active_subtitles[t] != -1) {
626 gr_string(0x8000,y,Subtitles[active_subtitles[t]].msg);
632 void close_movie(char *movielib, int is_robots)
635 char filename[FILENAME_LEN];
638 high_res = MenuHiresAvailable;
640 high_res = MovieHires;
642 sprintf(filename, "%s-%s.mvl", movielib, high_res?"h":"l");
644 if (!cfile_close(filename))
646 con_printf(CON_URGENT, "Can't close movielib <%s>: %s\n", filename, PHYSFS_getLastError());
647 sprintf(filename, "%s-%s.mvl", movielib, high_res?"l":"h");
649 if (!cfile_close(filename))
650 con_printf(CON_URGENT, "Can't close movielib <%s>: %s\n", filename, PHYSFS_getLastError());
658 for (i = 0 ; i < N_BUILTIN_MOVIE_LIBS ; i++)
660 if (!strnicmp(movielib_files[i], "robot", 5))
665 close_movie(movielib_files[i], is_robots);
670 //ask user to put the D2 CD in.
671 //returns -1 if ESC pressed, 0 if OK chosen
672 //CD may not have been inserted
676 ubyte save_pal[256*3];
677 grs_canvas *save_canv,*tcanv;
678 int ret,was_faded=gr_palette_faded_out;
682 save_canv = grd_curcanv;
683 tcanv = gr_create_canvas(grd_curcanv->cv_w,grd_curcanv->cv_h);
685 gr_set_current_canvas(tcanv);
686 gr_ubitmap(0,0,&save_canv->cv_bitmap);
687 gr_set_current_canvas(save_canv);
689 gr_clear_canvas(BM_XRGB(0,0,0));
691 memcpy(save_pal,gr_palette,sizeof(save_pal));
693 memcpy(gr_palette,last_palette_for_color_fonts,sizeof(gr_palette));
697 ret = nm_messagebox( "CD ERROR", 1, "Ok", "Please insert your Descent II CD");
702 ret2 = nm_messagebox( "CD ERROR", 2, "Try Again", "Leave Game", "You must insert your\nDescent II CD to Continue");
704 if (ret2 == -1 || ret2 == 0)
708 force_rb_register = 1; //disc has changed; force register new CD
712 memcpy(gr_palette,save_pal,sizeof(save_pal));
714 gr_ubitmap(0,0,&tcanv->cv_bitmap);
717 gr_palette_load(gr_palette);
719 gr_free_canvas(tcanv);
723 con_printf(CON_DEBUG, "STUB: movie: request_cd\n");
729 void init_movie(char *movielib, int is_robots, int required)
732 char filename[FILENAME_LEN];
734 //for robots, load highres versions if highres menus set
736 high_res = MenuHiresAvailable;
738 high_res = MovieHires;
740 sprintf(filename, "%s-%s.mvl", movielib, high_res?"h":"l");
742 if (!cfile_init(filename))
745 con_printf(CON_URGENT, "Can't open movielib <%s>: %s\n", filename, PHYSFS_getLastError());
747 sprintf(filename, "%s-%s.mvl", movielib, high_res?"l":"h");
749 if (!cfile_init(filename))
751 con_printf(CON_URGENT, "Can't open movielib <%s>: %s\n", filename, PHYSFS_getLastError());
756 //find and initialize the movie libraries
762 if (FindArg("-nomovies"))
765 for (i=0;i<N_BUILTIN_MOVIE_LIBS;i++) {
767 if (!strnicmp(movielib_files[i],"robot",5))
772 init_movie(movielib_files[i], is_robots, 1);
775 atexit(close_movies);
779 void close_extra_robot_movie(void)
781 if (strlen(movielib_files[EXTRA_ROBOT_LIB]))
782 if (!cfile_close(movielib_files[EXTRA_ROBOT_LIB]))
783 con_printf(CON_URGENT, "Can't close robot movielib: %s\n", PHYSFS_getLastError());
786 void init_extra_robot_movie(char *movielib)
788 if (FindArg("-nomovies"))
791 close_extra_robot_movie();
792 init_movie(movielib, 1, 0);
793 strcpy(movielib_files[EXTRA_ROBOT_LIB], movielib);
794 atexit(close_extra_robot_movie);