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.
27 # include <sys/types.h>
28 # include <sys/stat.h>
48 #include "physfsrwops.h"
49 #include "ignorecase.h"
55 short first_frame,last_frame;
61 #define MAX_SUBTITLES 500
62 #define MAX_ACTIVE_SUBTITLES 3
63 subtitle Subtitles[MAX_SUBTITLES];
69 char movielib_files[5][FILENAME_LEN] = {"intro","other","robots","oem"};
71 char movielib_files[4][FILENAME_LEN] = {"intro","other","robots"};
74 #define N_MOVIE_LIBS (sizeof(movielib_files) / sizeof(*movielib_files))
75 #define N_BUILTIN_MOVIE_LIBS (N_MOVIE_LIBS - 1)
76 #define EXTRA_ROBOT_LIB N_BUILTIN_MOVIE_LIBS
78 cvar_t MovieHires = { "MovieHires", "1", CVAR_ARCHIVE }; // default is highres
82 unsigned char RobBufCount = 0, PlayingBuf = 0, RobBufLimit = 0;
86 SDL_RWops *RoboFile = NULL;
87 MVE_videoSpec MVESpec;
89 // Function Prototypes
90 int RunMovie(char *filename, int highres_flag, int allow_abort,int dx,int dy);
92 void decode_text_line(char *p);
93 void draw_subtitles(int frame_num);
95 // Abstraction for movie files
96 static SDL_RWops *open_movie_file(const char *filename, int must_have);
97 #define read_movie_file(handle, buf, count) SDL_RWread((SDL_RWops *)handle, buf, 1, count);
98 #define reset_movie_file(handle) SDL_RWseek(handle, 0, SEEK_SET)
99 #define close_movie_file(handle) SDL_RWclose(handle)
102 // Callbacks for MVE lib
104 // ----------------------------------------------------------------------
105 void* MPlayAlloc(unsigned size)
107 return d_malloc(size);
110 void MPlayFree(void *p)
116 //-----------------------------------------------------------------------
118 unsigned int FileRead(void *handle, void *buf, unsigned int count)
121 numread = read_movie_file(handle, buf, count);
122 return (numread == count);
126 //-----------------------------------------------------------------------
129 //filename will actually get modified to be either low-res or high-res
130 //returns status. see values in movie.h
131 int PlayMovie(const char *filename, int must_have)
133 char name[FILENAME_LEN],*p;
136 if (FindArg("-nomovies"))
137 return MOVIE_NOT_PLAYED;
139 strcpy(name,filename);
141 if ((p=strchr(name,'.')) == NULL) //add extension, if missing
144 //check for escape already pressed & abort if so
145 while ((c = newmenu_inkey()) != 0)
147 return MOVIE_ABORTED;
149 // Stop all digital sounds currently playing.
158 if (!FindArg("-nosound"))
163 ret = RunMovie(name, MovieHires.intval, must_have, -1, -1);
165 if (!FindArg("-nosound"))
168 Screen_mode = -1; //force screen reset
174 void MovieShowFrame(ubyte *buf, uint bufw, uint bufh, uint sx, uint sy,
175 uint w, uint h, uint dstx, uint dsty)
177 grs_bitmap source_bm;
178 grs_canvas *dest_canv, *save_canv;
180 //mprintf((0,"MovieShowFrame %d,%d %d,%d %d,%d %d,%d\n",bufw,bufh,sx,sy,w,h,dstx,dsty));
182 Assert(bufw == w && bufh == h);
184 source_bm.bm_x = source_bm.bm_y = 0;
185 source_bm.bm_w = source_bm.bm_rowsize = bufw;
186 source_bm.bm_h = bufh;
187 source_bm.bm_type = BM_LINEAR;
188 source_bm.bm_flags = 0;
189 source_bm.bm_data = buf;
193 memcpy(RoboBuffer[RobBufCount++], buf, bufw * bufh);
196 if (menu_use_game_res.intval) {
197 float aspect = (float)w / (float)h;
200 h = h * GHEIGHT / MVESpec.screenHeight;
202 dstx = dstx * GWIDTH / MVESpec.screenWidth;
203 dsty = dsty * GHEIGHT / MVESpec.screenHeight;
205 w = w * GWIDTH / MVESpec.screenWidth;
207 dstx = dstx * GWIDTH / MVESpec.screenWidth;
208 dsty = GHEIGHT / 2 - h / 2;
210 dest_canv = gr_create_sub_canvas(grd_curcanv, dstx, dsty, w, h);
211 save_canv = grd_curcanv;
212 gr_set_current_canvas(dest_canv);
213 gr_bitmap_fullscr(&source_bm);
214 gr_set_current_canvas(save_canv);
215 gr_free_sub_canvas(dest_canv);
217 gr_bm_ubitblt(bufw,bufh,dstx,dsty,sx,sy,&source_bm,&grd_curcanv->cv_bitmap);
220 //our routine to set the pallete, called from the movie code
221 void MovieSetPalette(unsigned char *p, unsigned start, unsigned count)
226 //mprintf((0,"SetPalette p=%x, start=%d, count=%d\n",p,start,count));
228 //Color 0 should be black, and we get color 255
229 Assert(start>=1 && start+count-1<=254);
231 //Set color 0 to be black
232 gr_palette[0] = gr_palette[1] = gr_palette[2] = 0;
234 //Set color 255 to be our subtitle color
235 gr_palette[765] = gr_palette[766] = gr_palette[767] = 50;
237 //movie libs palette into our array
238 memcpy(gr_palette+start*3,p+start*3,count*3);
240 //finally set the palette in the hardware
241 gr_palette_load(gr_palette);
243 //MVE_SetPalette(p, start, count);
249 short x, y, w, h; // The location of the menu.
250 grs_bitmap * bmp; // The background under the menu.
253 bkg movie_bg = {0,0,0,0,NULL};
256 #define BOX_BORDER (MenuHires?40:20)
259 void show_pause_message(char *msg)
264 gr_set_current_canvas(NULL);
265 gr_set_curfont( SMALL_FONT );
267 gr_get_string_size(msg,&w,&h,&aw);
269 x = (grd_curscreen->sc_w-w)/2;
270 y = (grd_curscreen->sc_h-h)/2;
274 gr_free_bitmap(movie_bg.bmp);
278 // Save the background of the display
279 movie_bg.x=x; movie_bg.y=y; movie_bg.w=w; movie_bg.h=h;
281 movie_bg.bmp = gr_create_bitmap( w+BOX_BORDER, h+BOX_BORDER );
283 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 );
287 gr_rect(x-BOX_BORDER/2,y-BOX_BORDER/2,x+w+BOX_BORDER/2-1,y+h+BOX_BORDER/2-1);
289 gr_set_fontcolor( 255, -1 );
291 gr_ustring( 0x8000, y, msg );
296 void clear_pause_message()
301 gr_bitmap(movie_bg.x-BOX_BORDER/2, movie_bg.y-BOX_BORDER/2, movie_bg.bmp);
303 gr_free_bitmap(movie_bg.bmp);
310 //returns status. see movie.h
311 int RunMovie(char *filename, int hires_flag, int must_have,int dx,int dy)
314 int result=1,aborted=0;
321 // Open Movie file. If it doesn't exist, no movie, just return.
323 filehndl = open_movie_file(filename, must_have);
328 con_printf(CON_URGENT, "Can't open movie <%s>: %s\n", filename, PHYSFS_getLastError());
329 return MOVIE_NOT_PLAYED;
332 MVE_memCallbacks(MPlayAlloc, MPlayFree);
333 MVE_ioCallbacks(FileRead);
334 MVE_sfCallbacks(MovieShowFrame);
335 MVE_palCallbacks(MovieSetPalette);
337 vid_set_mode(MOVIE_SCREEN_MODE);
339 set_screen_mode(SCREEN_MENU);
342 if (MVE_rmPrepMovie((void *)filehndl, dx, dy, track)) {
344 return MOVIE_NOT_PLAYED;
347 MVE_getVideoSpec(&MVESpec);
351 FontHires = FontHiresAvailable && hires_flag;
353 while((result = MVE_rmStepMovie()) == 0) {
355 draw_subtitles(frame_num);
359 key = newmenu_inkey();
361 // If ESCAPE pressed, then quit movie.
362 if (key == KEY_ESC) {
363 result = aborted = 1;
367 // If PAUSE pressed, then pause movie
368 if (key == KEY_PAUSE) {
370 show_pause_message(TXT_PAUSE);
371 while (!newmenu_inkey()) ;
372 clear_pause_message();
375 #ifdef VID_SUPPORTS_FULLSCREEN_TOGGLE
376 if ((key == KEY_COMMAND+KEY_SHIFTED+KEY_F) ||
377 (key == KEY_ALTED+KEY_ENTER) ||
378 (key == KEY_ALTED+KEY_PADENTER))
379 vid_toggle_fullscreen();
385 Assert(aborted || result == MVE_ERR_EOF); ///movie should be over
389 close_movie_file(filehndl); // Close Movie File
391 // Restore old graphic state
393 Screen_mode=-1; //force reset of screen mode
395 return (aborted?MOVIE_ABORTED:MOVIE_PLAYED_FULL);
399 int InitMovieBriefing()
403 vid_set_mode(SM(640,480));
405 vid_set_mode(SM(320,200));
407 gr_init_sub_canvas( &VR_screen_pages[0], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
408 gr_init_sub_canvas( &VR_screen_pages[1], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
416 static fix RobBufTime = 0;
420 void ShowRobotBuffer()
422 // shows a frame from the robot buffer
425 Int3(); // Get Jason...how'd we get here?
428 grs_bitmap source_bm;
429 grs_canvas *dest_canv, *save_canv;
430 int rw, rh, rdx, rdy;
433 if (timer_get_approx_seconds() < (RobBufTime + fixdiv(F1_0, i2f(15))))
436 RobBufTime = timer_get_approx_seconds();
439 rw=320; rh=200; rdx=280; rdy=200;
441 rw=160; rh=100; rdx=140; rdy=80;
444 source_bm.bm_x = source_bm.bm_y = 0;
445 source_bm.bm_w = source_bm.bm_rowsize = rw;
447 source_bm.bm_type = BM_LINEAR;
448 source_bm.bm_flags = 0;
450 source_bm.bm_data = (unsigned char *)RoboBuffer[RobBufCount];
452 if (menu_use_game_res.intval) {
453 float aspect = (float)rw / (float)rh;
455 rh = rh * GHEIGHT / MVESpec.screenHeight;
457 rdx = rdx * GWIDTH / MVESpec.screenWidth;
458 rdy = rdy * GHEIGHT / MVESpec.screenHeight;
460 dest_canv = gr_create_sub_canvas(grd_curcanv, rdx, rdy, rw, rh);
461 save_canv = grd_curcanv;
462 gr_set_current_canvas(dest_canv);
463 gr_bitmap_fullscr(&source_bm);
464 gr_set_current_canvas(save_canv);
465 gr_free_sub_canvas(dest_canv);
467 gr_bm_ubitblt(rw, rh, rdx, rdy, 0, 0, &source_bm, &grd_curcanv->cv_bitmap);
470 RobBufCount %= RobBufLimit;
475 //returns 1 if frame updated ok
488 err = MVE_rmStepMovie();
490 if (err == MVE_ERR_EOF) //end of movie, so reset
494 RobBufLimit = RobBufCount;
496 //RobBufTime=timer_get_approx_seconds();
499 reset_movie_file(RoboFile);
500 if (MVE_rmPrepMovie(RoboFile, MenuHires?280:140, MenuHires?200:80, 0))
516 void FreeRoboBuffer(int n)
518 // frees the 64k frame buffers, starting with n and then working down
526 for (i = n; i >= 0; i--)
527 d_free(RoboBuffer[i]);
532 void DeInitRobotMovie(void)
543 close_movie_file(RoboFile); // Close Movie File
548 int InitRobotMovie(char *filename)
558 if (FindArg("-nomovies"))
561 con_printf(CON_DEBUG, "RoboFile=%s\n", filename);
565 for (i = 0; i < 50; i++)
568 RoboBuffer[i] = d_malloc(65000L);
570 RoboBuffer[i] = d_malloc(17000L);
572 if (RoboBuffer[i] == NULL)
574 con_printf(CON_URGENT, "ROBOERROR: Could't allocate frame %d!\n", i);
576 FreeRoboBuffer(i - 1);
582 MVE_sndInit(-1); //tell movies to play no sound for robots
584 MVE_memCallbacks(MPlayAlloc, MPlayFree);
585 MVE_ioCallbacks(FileRead);
587 RoboFile = open_movie_file(filename, 1);
592 con_printf(CON_URGENT, "Can't open movie <%s>: %s\n", filename, PHYSFS_getLastError());
593 return MOVIE_NOT_PLAYED;
596 MVE_palCallbacks(MovieSetPalette);
597 MVE_sfCallbacks(MovieShowFrame);
599 if (MVE_rmPrepMovie((void *)RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
605 MVE_getVideoSpec(&MVESpec);
608 RoboFilePos = SDL_RWseek(RoboFile, 0L, SEEK_CUR);
609 con_printf(CON_DEBUG, "RoboFilePos=%d!\n", RoboFilePos);
617 * Subtitle system code
620 char *subtitle_raw_data;
623 //search for next field following whitespace
624 char *next_field (char *p)
626 while (*p && !isspace(*p))
632 while (*p && isspace(*p))
642 int init_subtitles(char *filename)
651 if (! FindArg("-subtitles"))
654 ifile = cfopen(filename,"rb"); //try text version
656 if (!ifile) { //no text version, try binary version
657 char filename2[FILENAME_LEN];
658 change_filename_extension(filename2, filename, ".TXB");
659 ifile = cfopen(filename2,"rb");
665 size = cfilelength(ifile);
667 MALLOC (subtitle_raw_data, char, size+1);
669 read_count = cfread(subtitle_raw_data, 1, size, ifile);
673 subtitle_raw_data[size] = 0;
675 if (read_count != size) {
676 d_free(subtitle_raw_data);
680 p = subtitle_raw_data;
682 while (p && p < subtitle_raw_data+size) {
685 endp = strchr(p,'\n');
687 if (endp[-1] == '\r')
688 endp[-1] = 0; //handle 0d0a pair
689 *endp = 0; //string termintor
696 Subtitles[Num_subtitles].first_frame = atoi(p);
697 p = next_field(p); if (!p) continue;
698 Subtitles[Num_subtitles].last_frame = atoi(p);
699 p = next_field(p); if (!p) continue;
700 Subtitles[Num_subtitles].msg = p;
702 Assert(Num_subtitles==0 || Subtitles[Num_subtitles].first_frame >= Subtitles[Num_subtitles-1].first_frame);
703 Assert(Subtitles[Num_subtitles].last_frame >= Subtitles[Num_subtitles].first_frame);
716 void close_subtitles()
718 if (subtitle_raw_data)
719 d_free(subtitle_raw_data);
720 subtitle_raw_data = NULL;
725 //draw the subtitles for this frame
726 void draw_subtitles(int frame_num)
728 static int active_subtitles[MAX_ACTIVE_SUBTITLES];
729 static int num_active_subtitles,next_subtitle,line_spacing;
733 if (frame_num == 0) {
734 num_active_subtitles = 0;
736 gr_set_curfont( GAME_FONT );
737 line_spacing = grd_curcanv->cv_font->ft_h + (grd_curcanv->cv_font->ft_h >> 2);
738 gr_set_fontcolor(255,-1);
741 //get rid of any subtitles that have expired
742 for (t=0;t<num_active_subtitles;)
743 if (frame_num > Subtitles[active_subtitles[t]].last_frame) {
745 for (t2=t;t2<num_active_subtitles-1;t2++)
746 active_subtitles[t2] = active_subtitles[t2+1];
747 num_active_subtitles--;
753 //get any subtitles new for this frame
754 while (next_subtitle < Num_subtitles && frame_num >= Subtitles[next_subtitle].first_frame) {
755 if (num_active_subtitles >= MAX_ACTIVE_SUBTITLES)
756 Error("Too many active subtitles!");
757 active_subtitles[num_active_subtitles++] = next_subtitle;
761 //find y coordinate for first line of subtitles
762 y = grd_curcanv->cv_bitmap.bm_h-((line_spacing+1)*MAX_ACTIVE_SUBTITLES+2);
764 //erase old subtitles if necessary
767 gr_rect(0,y,grd_curcanv->cv_bitmap.bm_w-1,grd_curcanv->cv_bitmap.bm_h-1);
770 //now draw the current subtitles
771 for (t=0;t<num_active_subtitles;t++)
772 if (active_subtitles[t] != -1) {
773 gr_string(0x8000,y,Subtitles[active_subtitles[t]].msg);
779 //find the specified movie library, and read in list of movies in it
780 static int init_movie_lib(char *filename)
782 //note: this based on cfile_init_hogfile()
784 char pathname[PATH_MAX];
786 if (!PHYSFSX_getRealPath(filename, pathname))
789 return PHYSFS_mount(pathname, "movies", 1);
793 void close_movie(char *movielib, int is_robots)
796 char filename[FILENAME_LEN];
797 char pathname[PATH_MAX];
800 high_res = MenuHiresAvailable;
802 high_res = MovieHires.intval;
804 sprintf(filename, "%s-%s.mvl", movielib, high_res?"h":"l");
806 if (!PHYSFSX_getRealPath(filename, pathname) ||
807 !PHYSFS_removeFromSearchPath(pathname))
809 con_printf(CON_URGENT, "Can't close movielib <%s>: %s\n", filename, PHYSFS_getLastError());
810 sprintf(filename, "%s-%s.mvl", movielib, high_res?"l":"h");
812 if (!PHYSFSX_getRealPath(filename, pathname) ||
813 !PHYSFS_removeFromSearchPath(pathname))
814 con_printf(CON_URGENT, "Can't close movielib <%s>: %s\n", filename, PHYSFS_getLastError());
822 for (i = 0 ; i < N_BUILTIN_MOVIE_LIBS ; i++)
824 if (!strnicmp(movielib_files[i], "robot", 5))
829 close_movie(movielib_files[i], is_robots);
834 void init_movie(char *movielib, int is_robots, int required)
837 char filename[FILENAME_LEN];
839 //for robots, load highres versions if highres menus set
841 high_res = MenuHiresAvailable;
843 high_res = MovieHires.intval;
845 sprintf(filename, "%s-%s.mvl", movielib, high_res?"h":"l");
847 if (!init_movie_lib(filename))
850 con_printf(CON_URGENT, "Can't open movielib <%s>: %s\n", filename, PHYSFS_getLastError());
852 sprintf(filename, "%s-%s.mvl", movielib, high_res?"l":"h");
854 if (!init_movie_lib(filename))
856 con_printf(CON_URGENT, "Can't open movielib <%s>: %s\n", filename, PHYSFS_getLastError());
861 //find and initialize the movie libraries
867 if (FindArg("-nomovies"))
870 for (i=0;i<N_BUILTIN_MOVIE_LIBS;i++) {
872 if (!strnicmp(movielib_files[i],"robot",5))
877 init_movie(movielib_files[i], is_robots, 1);
880 atexit(close_movies);
884 void close_extra_robot_movie(void)
886 if (strlen(movielib_files[EXTRA_ROBOT_LIB]))
887 close_movie(movielib_files[EXTRA_ROBOT_LIB], 1);
890 void init_extra_robot_movie(char *movielib)
892 if (FindArg("-nomovies"))
895 close_extra_robot_movie();
896 init_movie(movielib, 1, 0);
897 strcpy(movielib_files[EXTRA_ROBOT_LIB], movielib);
898 atexit(close_extra_robot_movie);
902 //returns file handle
903 static SDL_RWops *open_movie_file(const char *filename, int must_have)
905 char moviepath[PATH_MAX];
907 strcpy(moviepath, "movies/");
908 strcat(moviepath, filename);
910 PHYSFSEXT_locateCorrectCase(moviepath);
912 return PHYSFSRWOPS_openRead(moviepath);