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>
56 #include "physfsrwops.h"
58 extern int MenuHiresAvailable;
59 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];
76 char movielib_files[5][FILENAME_LEN] = {"intro","other","robots","oem"};
78 char movielib_files[4][FILENAME_LEN] = {"intro","other","robots"};
81 #define N_MOVIE_LIBS (sizeof(movielib_files) / sizeof(*movielib_files))
82 #define N_BUILTIN_MOVIE_LIBS (N_MOVIE_LIBS - 1)
83 #define EXTRA_ROBOT_LIB N_BUILTIN_MOVIE_LIBS
85 int MovieHires = 1; //default is highres
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);
96 // ----------------------------------------------------------------------
97 void* MPlayAlloc(unsigned size)
99 return d_malloc(size);
102 void MPlayFree(void *p)
108 //-----------------------------------------------------------------------
110 unsigned int FileRead(void *handle, void *buf, unsigned int count)
113 numread = SDL_RWread((SDL_RWops *)handle, buf, 1, count);
114 return (numread == count);
118 //-----------------------------------------------------------------------
121 //filename will actually get modified to be either low-res or high-res
122 //returns status. see values in movie.h
123 int PlayMovie(const char *filename, int must_have)
125 char name[FILENAME_LEN],*p;
128 if (FindArg("-nomovies"))
129 return MOVIE_NOT_PLAYED;
131 strcpy(name,filename);
133 if ((p=strchr(name,'.')) == NULL) //add extension, if missing
136 //check for escape already pressed & abort if so
137 while ((c=key_inkey()) != 0)
139 return MOVIE_ABORTED;
141 // Stop all digital sounds currently playing.
150 if (!FindArg("-nosound"))
155 ret = RunMovie(name,MovieHires,must_have,-1,-1);
157 if (!FindArg("-nosound"))
160 Screen_mode = -1; //force screen reset
166 void MovieShowFrame(ubyte *buf, uint bufw, uint bufh, uint sx, uint sy,
167 uint w, uint h, uint dstx, uint dsty)
169 grs_bitmap source_bm;
171 //mprintf((0,"MovieShowFrame %d,%d %d,%d %d,%d %d,%d\n",bufw,bufh,sx,sy,w,h,dstx,dsty));
173 Assert(bufw == w && bufh == h);
175 source_bm.bm_x = source_bm.bm_y = 0;
176 source_bm.bm_w = source_bm.bm_rowsize = bufw;
177 source_bm.bm_h = bufh;
178 source_bm.bm_type = BM_LINEAR;
179 source_bm.bm_flags = 0;
180 source_bm.bm_data = buf;
182 gr_bm_ubitblt(bufw,bufh,dstx,dsty,sx,sy,&source_bm,&grd_curcanv->cv_bitmap);
185 //our routine to set the pallete, called from the movie code
186 void MovieSetPalette(unsigned char *p, unsigned start, unsigned count)
191 //mprintf((0,"SetPalette p=%x, start=%d, count=%d\n",p,start,count));
193 //Color 0 should be black, and we get color 255
194 Assert(start>=1 && start+count-1<=254);
196 //Set color 0 to be black
197 gr_palette[0] = gr_palette[1] = gr_palette[2] = 0;
199 //Set color 255 to be our subtitle color
200 gr_palette[765] = gr_palette[766] = gr_palette[767] = 50;
202 //movie libs palette into our array
203 memcpy(gr_palette+start*3,p+start*3,count*3);
205 //finally set the palette in the hardware
206 gr_palette_load(gr_palette);
208 //MVE_SetPalette(p, start, count);
214 short x, y, w, h; // The location of the menu.
215 grs_bitmap * bmp; // The background under the menu.
218 bkg movie_bg = {0,0,0,0,NULL};
221 #define BOX_BORDER (MenuHires?40:20)
224 void show_pause_message(char *msg)
229 gr_set_current_canvas(NULL);
230 gr_set_curfont( SMALL_FONT );
232 gr_get_string_size(msg,&w,&h,&aw);
234 x = (grd_curscreen->sc_w-w)/2;
235 y = (grd_curscreen->sc_h-h)/2;
239 gr_free_bitmap(movie_bg.bmp);
243 // Save the background of the display
244 movie_bg.x=x; movie_bg.y=y; movie_bg.w=w; movie_bg.h=h;
246 movie_bg.bmp = gr_create_bitmap( w+BOX_BORDER, h+BOX_BORDER );
248 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 );
252 gr_rect(x-BOX_BORDER/2,y-BOX_BORDER/2,x+w+BOX_BORDER/2-1,y+h+BOX_BORDER/2-1);
254 gr_set_fontcolor( 255, -1 );
256 gr_ustring( 0x8000, y, msg );
261 void clear_pause_message()
266 gr_bitmap(movie_bg.x-BOX_BORDER/2, movie_bg.y-BOX_BORDER/2, movie_bg.bmp);
268 gr_free_bitmap(movie_bg.bmp);
275 //returns status. see movie.h
276 int RunMovie(char *filename, int hires_flag, int must_have,int dx,int dy)
279 int result=1,aborted=0;
286 // Open Movie file. If it doesn't exist, no movie, just return.
288 filehndl = PHYSFSRWOPS_openRead(filename);
293 con_printf(CON_URGENT, "Can't open movie <%s>: %s\n", filename, PHYSFS_getLastError());
294 return MOVIE_NOT_PLAYED;
297 MVE_memCallbacks(MPlayAlloc, MPlayFree);
298 MVE_ioCallbacks(FileRead);
299 MVE_sfCallbacks(MovieShowFrame);
300 MVE_palCallbacks(MovieSetPalette);
303 gr_set_mode(SM(640,480));
305 gr_set_mode(SM(320,200));
308 set_screen_mode(SCREEN_MENU);
311 if (MVE_rmPrepMovie((void *)filehndl, dx, dy, track)) {
313 return MOVIE_NOT_PLAYED;
318 FontHires = FontHiresAvailable && hires_flag;
320 while((result = MVE_rmStepMovie()) == 0) {
322 draw_subtitles(frame_num);
328 // If ESCAPE pressed, then quit movie.
329 if (key == KEY_ESC) {
330 result = aborted = 1;
334 // If PAUSE pressed, then pause movie
335 if (key == KEY_PAUSE) {
337 show_pause_message(TXT_PAUSE);
338 while (!key_inkey()) ;
339 clear_pause_message();
342 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
343 if ((key == KEY_ALTED+KEY_ENTER) ||
344 (key == KEY_ALTED+KEY_PADENTER))
345 gr_toggle_fullscreen();
351 Assert(aborted || result == MVE_ERR_EOF); ///movie should be over
355 SDL_FreeRW(filehndl); // Close Movie File
357 // Restore old graphic state
359 Screen_mode=-1; //force reset of screen mode
361 return (aborted?MOVIE_ABORTED:MOVIE_PLAYED_FULL);
365 int InitMovieBriefing()
369 gr_set_mode(SM(640,480));
371 gr_set_mode(SM(320,200));
373 gr_init_sub_canvas( &VR_screen_pages[0], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
374 gr_init_sub_canvas( &VR_screen_pages[1], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
381 //returns 1 if frame updated ok
386 err = MVE_rmStepMovie();
388 if (err == MVE_ERR_EOF) //end of movie, so reset
390 SDL_RWseek(RoboFile, 0, SEEK_SET);
391 if (MVE_rmPrepMovie(RoboFile, MenuHires?280:140, MenuHires?200:80, 0))
406 void DeInitRobotMovie(void)
409 SDL_FreeRW(RoboFile); // Close Movie File
413 int InitRobotMovie(char *filename)
415 if (FindArg("-nomovies"))
418 con_printf(CON_DEBUG, "RoboFile=%s\n", filename);
420 MVE_sndInit(-1); //tell movies to play no sound for robots
422 RoboFile = PHYSFSRWOPS_openRead(filename);
426 con_printf(CON_URGENT, "Can't open movie <%s>: %s\n", filename, PHYSFS_getLastError());
427 return MOVIE_NOT_PLAYED;
430 if (MVE_rmPrepMovie((void *)RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
440 * Subtitle system code
443 char *subtitle_raw_data;
446 //search for next field following whitespace
447 char *next_field (char *p)
449 while (*p && !isspace(*p))
455 while (*p && isspace(*p))
465 int init_subtitles(char *filename)
474 if (! FindArg("-subtitles"))
477 ifile = cfopen(filename,"rb"); //try text version
479 if (!ifile) { //no text version, try binary version
480 char filename2[FILENAME_LEN];
481 change_filename_extension(filename2, filename, ".TXB");
482 ifile = cfopen(filename2,"rb");
488 size = cfilelength(ifile);
490 MALLOC (subtitle_raw_data, char, size+1);
492 read_count = cfread(subtitle_raw_data, 1, size, ifile);
496 subtitle_raw_data[size] = 0;
498 if (read_count != size) {
499 d_free(subtitle_raw_data);
503 p = subtitle_raw_data;
505 while (p && p < subtitle_raw_data+size) {
508 endp = strchr(p,'\n');
510 if (endp[-1] == '\r')
511 endp[-1] = 0; //handle 0d0a pair
512 *endp = 0; //string termintor
519 Subtitles[Num_subtitles].first_frame = atoi(p);
520 p = next_field(p); if (!p) continue;
521 Subtitles[Num_subtitles].last_frame = atoi(p);
522 p = next_field(p); if (!p) continue;
523 Subtitles[Num_subtitles].msg = p;
525 Assert(Num_subtitles==0 || Subtitles[Num_subtitles].first_frame >= Subtitles[Num_subtitles-1].first_frame);
526 Assert(Subtitles[Num_subtitles].last_frame >= Subtitles[Num_subtitles].first_frame);
539 void close_subtitles()
541 if (subtitle_raw_data)
542 d_free(subtitle_raw_data);
543 subtitle_raw_data = NULL;
548 //draw the subtitles for this frame
549 void draw_subtitles(int frame_num)
551 static int active_subtitles[MAX_ACTIVE_SUBTITLES];
552 static int num_active_subtitles,next_subtitle,line_spacing;
556 if (frame_num == 0) {
557 num_active_subtitles = 0;
559 gr_set_curfont( GAME_FONT );
560 line_spacing = grd_curcanv->cv_font->ft_h + (grd_curcanv->cv_font->ft_h >> 2);
561 gr_set_fontcolor(255,-1);
564 //get rid of any subtitles that have expired
565 for (t=0;t<num_active_subtitles;)
566 if (frame_num > Subtitles[active_subtitles[t]].last_frame) {
568 for (t2=t;t2<num_active_subtitles-1;t2++)
569 active_subtitles[t2] = active_subtitles[t2+1];
570 num_active_subtitles--;
576 //get any subtitles new for this frame
577 while (next_subtitle < Num_subtitles && frame_num >= Subtitles[next_subtitle].first_frame) {
578 if (num_active_subtitles >= MAX_ACTIVE_SUBTITLES)
579 Error("Too many active subtitles!");
580 active_subtitles[num_active_subtitles++] = next_subtitle;
584 //find y coordinate for first line of subtitles
585 y = grd_curcanv->cv_bitmap.bm_h-((line_spacing+1)*MAX_ACTIVE_SUBTITLES+2);
587 //erase old subtitles if necessary
590 gr_rect(0,y,grd_curcanv->cv_bitmap.bm_w-1,grd_curcanv->cv_bitmap.bm_h-1);
593 //now draw the current subtitles
594 for (t=0;t<num_active_subtitles;t++)
595 if (active_subtitles[t] != -1) {
596 gr_string(0x8000,y,Subtitles[active_subtitles[t]].msg);
602 void close_movie(char *movielib, int is_robots)
605 char filename[FILENAME_LEN];
608 high_res = MenuHiresAvailable;
610 high_res = MovieHires;
612 sprintf(filename, "%s-%s.mvl", movielib, high_res?"h":"l");
614 if (!cfile_close(filename))
616 con_printf(CON_URGENT, "Can't close movielib <%s>: %s\n", filename, PHYSFS_getLastError());
617 sprintf(filename, "%s-%s.mvl", movielib, high_res?"l":"h");
619 if (!cfile_close(filename))
620 con_printf(CON_URGENT, "Can't close movielib <%s>: %s\n", filename, PHYSFS_getLastError());
628 for (i = 0 ; i < N_BUILTIN_MOVIE_LIBS ; i++)
630 if (!strnicmp(movielib_files[i], "robot", 5))
635 close_movie(movielib_files[i], is_robots);
640 //ask user to put the D2 CD in.
641 //returns -1 if ESC pressed, 0 if OK chosen
642 //CD may not have been inserted
646 ubyte save_pal[256*3];
647 grs_canvas *save_canv,*tcanv;
648 int ret,was_faded=gr_palette_faded_out;
652 save_canv = grd_curcanv;
653 tcanv = gr_create_canvas(grd_curcanv->cv_w,grd_curcanv->cv_h);
655 gr_set_current_canvas(tcanv);
656 gr_ubitmap(0,0,&save_canv->cv_bitmap);
657 gr_set_current_canvas(save_canv);
659 gr_clear_canvas(BM_XRGB(0,0,0));
661 memcpy(save_pal,gr_palette,sizeof(save_pal));
663 memcpy(gr_palette,last_palette_for_color_fonts,sizeof(gr_palette));
667 ret = nm_messagebox( "CD ERROR", 1, "Ok", "Please insert your Descent II CD");
672 ret2 = nm_messagebox( "CD ERROR", 2, "Try Again", "Leave Game", "You must insert your\nDescent II CD to Continue");
674 if (ret2 == -1 || ret2 == 0)
678 force_rb_register = 1; //disc has changed; force register new CD
682 memcpy(gr_palette,save_pal,sizeof(save_pal));
684 gr_ubitmap(0,0,&tcanv->cv_bitmap);
687 gr_palette_load(gr_palette);
689 gr_free_canvas(tcanv);
693 con_printf(CON_DEBUG, "STUB: movie: request_cd\n");
699 void init_movie(char *movielib, int is_robots, int required)
702 char filename[FILENAME_LEN];
704 //for robots, load highres versions if highres menus set
706 high_res = MenuHiresAvailable;
708 high_res = MovieHires;
710 sprintf(filename, "%s-%s.mvl", movielib, high_res?"h":"l");
712 if (!cfile_init(filename))
715 con_printf(CON_URGENT, "Can't open movielib <%s>: %s\n", filename, PHYSFS_getLastError());
717 sprintf(filename, "%s-%s.mvl", movielib, high_res?"l":"h");
719 if (!cfile_init(filename))
721 con_printf(CON_URGENT, "Can't open movielib <%s>: %s\n", filename, PHYSFS_getLastError());
726 //find and initialize the movie libraries
732 if (FindArg("-nomovies"))
735 for (i=0;i<N_BUILTIN_MOVIE_LIBS;i++) {
737 if (!strnicmp(movielib_files[i],"robot",5))
742 init_movie(movielib_files[i], is_robots, 1);
745 atexit(close_movies);
749 void close_extra_robot_movie(void)
751 if (strlen(movielib_files[EXTRA_ROBOT_LIB]))
752 if (!cfile_close(movielib_files[EXTRA_ROBOT_LIB]))
753 con_printf(CON_URGENT, "Can't close robot movielib: %s\n", PHYSFS_getLastError());
756 void init_extra_robot_movie(char *movielib)
758 if (FindArg("-nomovies"))
761 close_extra_robot_movie();
762 init_movie(movielib, 1, 0);
763 strcpy(movielib_files[EXTRA_ROBOT_LIB], movielib);
764 atexit(close_extra_robot_movie);