| 1 |
/* |
| 2 |
* Portions of this file are copyright Rebirth contributors and licensed as |
| 3 |
* described in COPYING.txt. |
| 4 |
* Portions of this file are copyright Parallax Software and licensed |
| 5 |
* according to the Parallax license below. |
| 6 |
* See COPYING.txt for license details. |
| 7 |
|
| 8 |
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX |
| 9 |
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO |
| 10 |
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A |
| 11 |
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS |
| 12 |
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS |
| 13 |
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE |
| 14 |
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE |
| 15 |
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS |
| 16 |
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. |
| 17 |
COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 21 |
* |
| 22 |
* Movie Playing Stuff |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <string.h> |
| 27 |
#ifndef macintosh |
| 28 |
# include <sys/types.h> |
| 29 |
# include <sys/stat.h> |
| 30 |
# include <fcntl.h> |
| 31 |
# ifndef _MSC_VER |
| 32 |
# include <unistd.h> |
| 33 |
# endif |
| 34 |
#endif // ! macintosh |
| 35 |
#include <ctype.h> |
| 36 |
|
| 37 |
#include "movie.h" |
| 38 |
#include "window.h" |
| 39 |
#include "console.h" |
| 40 |
#include "config.h" |
| 41 |
#include "physfsx.h" |
| 42 |
#include "key.h" |
| 43 |
#include "mouse.h" |
| 44 |
#include "digi.h" |
| 45 |
#include "songs.h" |
| 46 |
#include "inferno.h" |
| 47 |
#include "palette.h" |
| 48 |
#include "strutil.h" |
| 49 |
#include "dxxerror.h" |
| 50 |
#include "u_mem.h" |
| 51 |
#include "gr.h" |
| 52 |
#include "gamefont.h" |
| 53 |
#include "menu.h" |
| 54 |
#include "libmve.h" |
| 55 |
#include "text.h" |
| 56 |
#include "screens.h" |
| 57 |
#include "timer.h" |
| 58 |
#include "physfsrwops.h" |
| 59 |
#if DXX_USE_OGL |
| 60 |
#include "ogl_init.h" |
| 61 |
#endif |
| 62 |
#include "args.h" |
| 63 |
|
| 64 |
#include "compiler-range_for.h" |
| 65 |
#include "partial_range.h" |
| 66 |
#include "d_zip.h" |
| 67 |
|
| 68 |
namespace d2x { |
| 69 |
|
| 70 |
namespace { |
| 71 |
|
| 72 |
// Subtitle data |
| 73 |
struct subtitle { |
| 74 |
typename std::conditional<sizeof(char *) == sizeof(uint32_t), uint16_t, uint32_t>::type first_frame, last_frame; |
| 75 |
const char *msg; |
| 76 |
}; |
| 77 |
|
| 78 |
struct d_loaded_subtitle_state |
| 79 |
{ |
| 80 |
unsigned Num_subtitles{}; |
| 81 |
std::unique_ptr<char[]> subtitle_raw_data; |
| 82 |
std::array<subtitle, 500> Subtitles |
| 83 |
#ifndef NDEBUG |
| 84 |
= {} |
| 85 |
#endif |
| 86 |
; |
| 87 |
}; |
| 88 |
|
| 89 |
static int init_subtitles(d_loaded_subtitle_state &SubtitleState, std::span<const char> filename); |
| 90 |
|
| 91 |
// Movielib data |
| 92 |
|
| 93 |
constexpr std::array<std::array<char, 8>, 3> movielib_files{{ |
| 94 |
{"intro"}, {"other"}, {"robots"} |
| 95 |
}}; |
| 96 |
|
| 97 |
// Function Prototypes |
| 98 |
static movie_play_status RunMovie(const char *filename, std::span<const char> subtitles, int highres_flag, play_movie_warn_missing, MVE_play_sounds audio_enabled, int dx, int dy); |
| 99 |
|
| 100 |
static void draw_subtitles(const d_loaded_subtitle_state &, int frame_num); |
| 101 |
|
| 102 |
//----------------------------------------------------------------------- |
| 103 |
|
| 104 |
struct movie_pause_window : window |
| 105 |
{ |
| 106 |
movie_pause_window(grs_canvas &src) : |
| 107 |
window(src, 0, 0, src.cv_bitmap.bm_w, src.cv_bitmap.bm_h) |
| 108 |
{ |
| 109 |
} |
| 110 |
virtual window_event_result event_handler(const d_event &) override; |
| 111 |
}; |
| 112 |
|
| 113 |
static constexpr bool valid_palette_color_range(const unsigned start, const unsigned count) |
| 114 |
{ |
| 115 |
if (start == 0) |
| 116 |
return false; |
| 117 |
if (start > 254) |
| 118 |
return false; |
| 119 |
if (count > 254) |
| 120 |
return false; |
| 121 |
if (start + count > 255) |
| 122 |
return false; |
| 123 |
return true; |
| 124 |
} |
| 125 |
|
| 126 |
} |
| 127 |
|
| 128 |
unsigned MovieFileRead(SDL_RWops *const handle, const std::span<uint8_t> buf) |
| 129 |
{ |
| 130 |
const auto count{buf.size()}; |
| 131 |
const auto numread{SDL_RWread(handle, buf.data(), 1, count)}; |
| 132 |
return (numread == count); |
| 133 |
} |
| 134 |
|
| 135 |
//----------------------------------------------------------------------- |
| 136 |
|
| 137 |
//filename will actually get modified to be either low-res or high-res |
| 138 |
//returns status. see values in movie.h |
| 139 |
movie_play_status PlayMovie(const std::span<const char> subtitles, const char *const name, const play_movie_warn_missing must_have) |
| 140 |
{ |
| 141 |
if (GameArg.SysNoMovies) |
| 142 |
return movie_play_status::skipped; |
| 143 |
|
| 144 |
// Stop all digital sounds currently playing. |
| 145 |
digi_stop_digi_sounds(); |
| 146 |
|
| 147 |
// Stop all songs |
| 148 |
songs_stop_all(); |
| 149 |
|
| 150 |
// MD2211: if using SDL_Mixer, we never reinit the sound system |
| 151 |
if (CGameArg.SndDisableSdlMixer) |
| 152 |
digi_close(); |
| 153 |
|
| 154 |
const auto ret{RunMovie(name, subtitles, !GameArg.GfxSkipHiresMovie, must_have, !CGameArg.SndNoSound ? MVE_play_sounds::enabled : MVE_play_sounds::silent, -1, -1)}; |
| 155 |
|
| 156 |
// MD2211: if using SDL_Mixer, we never reinit the sound system |
| 157 |
if (!CGameArg.SndNoSound |
| 158 |
&& CGameArg.SndDisableSdlMixer |
| 159 |
) |
| 160 |
digi_init(); |
| 161 |
|
| 162 |
Screen_mode = -1; //force screen reset |
| 163 |
return ret; |
| 164 |
} |
| 165 |
|
| 166 |
void MovieShowFrame(const uint8_t *buf, int dstx, int dsty, int bufw, int bufh, int sw, int sh) |
| 167 |
{ |
| 168 |
grs_bitmap source_bm; |
| 169 |
static palette_array_t old_pal; |
| 170 |
float scale{1.0f}; |
| 171 |
|
| 172 |
if (old_pal != gr_palette) |
| 173 |
{ |
| 174 |
old_pal = gr_palette; |
| 175 |
return; |
| 176 |
} |
| 177 |
old_pal = gr_palette; |
| 178 |
|
| 179 |
source_bm.bm_x = source_bm.bm_y = 0; |
| 180 |
source_bm.bm_w = source_bm.bm_rowsize = bufw; |
| 181 |
source_bm.bm_h = bufh; |
| 182 |
source_bm.set_type(bm_mode::linear); |
| 183 |
source_bm.clear_flags(); |
| 184 |
source_bm.bm_data = buf; |
| 185 |
|
| 186 |
if (dstx == -1 && dsty == -1) // Fullscreen movie so set scale to fit the actual screen size |
| 187 |
{ |
| 188 |
if ((static_cast<float>(SWIDTH)/SHEIGHT) < (static_cast<float>(sw)/bufh)) |
| 189 |
scale = (static_cast<float>(SWIDTH)/sw); |
| 190 |
else |
| 191 |
scale = (static_cast<float>(SHEIGHT)/bufh); |
| 192 |
} |
| 193 |
else // Other (robot) movie so set scale to min. screen dimension |
| 194 |
{ |
| 195 |
if ((static_cast<float>(SWIDTH)/bufw) < (static_cast<float>(SHEIGHT)/bufh)) |
| 196 |
scale = (static_cast<float>(SWIDTH)/sw); |
| 197 |
else |
| 198 |
scale = (static_cast<float>(SHEIGHT)/sh); |
| 199 |
} |
| 200 |
|
| 201 |
if (dstx == -1) // center it |
| 202 |
dstx = (SWIDTH/2)-((bufw*scale)/2); |
| 203 |
if (dsty == -1) // center it |
| 204 |
dsty = (SHEIGHT/2)-((bufh*scale)/2); |
| 205 |
|
| 206 |
#if DXX_USE_OGL |
| 207 |
glDisable (GL_BLEND); |
| 208 |
|
| 209 |
ogl_ubitblt_i( |
| 210 |
bufw*scale, bufh*scale, |
| 211 |
dstx, dsty, |
| 212 |
bufw, bufh, 0, 0, source_bm, grd_curcanv->cv_bitmap, (GameCfg.MovieTexFilt) ? opengl_texture_filter::trilinear : opengl_texture_filter::classic); |
| 213 |
|
| 214 |
glEnable (GL_BLEND); |
| 215 |
#else |
| 216 |
gr_bm_ubitbltm(*grd_curcanv, bufw, bufh, dstx, dsty, 0, 0, source_bm); |
| 217 |
#endif |
| 218 |
} |
| 219 |
|
| 220 |
//our routine to set the pallete, called from the movie code |
| 221 |
void MovieSetPalette(const unsigned char *p, unsigned start, unsigned count) |
| 222 |
{ |
| 223 |
if (count == 0) |
| 224 |
return; |
| 225 |
static_assert(valid_palette_color_range(1, 254)); |
| 226 |
static_assert(!valid_palette_color_range(1, 255)); |
| 227 |
static_assert(!valid_palette_color_range(1, 256)); |
| 228 |
static_assert(!valid_palette_color_range(0, 254)); |
| 229 |
static_assert(!valid_palette_color_range(2, 254)); |
| 230 |
if (!valid_palette_color_range(start, count)) |
| 231 |
{ |
| 232 |
/* Color 0 is reserved by the movie subsystem to be black. |
| 233 |
* Color 255 is reserved to be the subtitle color. |
| 234 |
* Colors above 255 are not supported. |
| 235 |
*/ |
| 236 |
con_printf(CON_URGENT, "Rebirth: error: movie palette tried to write color range [%u, %u]", start, count); |
| 237 |
return; |
| 238 |
} |
| 239 |
|
| 240 |
//Set color 0 to be black |
| 241 |
gr_palette[0] = {}; |
| 242 |
|
| 243 |
//Set color 255 to be our subtitle color |
| 244 |
gr_palette[255] = rgb_t{ |
| 245 |
.r = 50, |
| 246 |
.g = 50, |
| 247 |
.b = 50, |
| 248 |
}; |
| 249 |
|
| 250 |
//movie libs palette into our array |
| 251 |
memcpy(&gr_palette[start],p+start*3,count*3); |
| 252 |
} |
| 253 |
|
| 254 |
namespace { |
| 255 |
|
| 256 |
struct movie : window, mixin_trackable_window |
| 257 |
{ |
| 258 |
MVE_StepStatus result{MVE_StepStatus::EndOfFile}; |
| 259 |
uint8_t paused{}; |
| 260 |
int frame_num{}; |
| 261 |
const MVESTREAM_ptr_t pMovie; |
| 262 |
d_loaded_subtitle_state SubtitleState; |
| 263 |
movie(grs_canvas &src, MVESTREAM_ptr_t mvestream) : |
| 264 |
window(src, 0, 0, src.cv_bitmap.bm_w, src.cv_bitmap.bm_h), |
| 265 |
pMovie(std::move(mvestream)) |
| 266 |
{ |
| 267 |
} |
| 268 |
virtual window_event_result event_handler(const d_event &) override; |
| 269 |
}; |
| 270 |
|
| 271 |
window_event_result movie_pause_window::event_handler(const d_event &event) |
| 272 |
{ |
| 273 |
switch (event.type) |
| 274 |
{ |
| 275 |
case event_type::mouse_button_down: |
| 276 |
if (event_mouse_get_button(event) != mbtn::left) |
| 277 |
return window_event_result::ignored; |
| 278 |
[[fallthrough]]; |
| 279 |
case event_type::key_command: |
| 280 |
if (const auto result{call_default_handler(event)}; result == window_event_result::ignored) |
| 281 |
return window_event_result::close; |
| 282 |
else |
| 283 |
return result; |
| 284 |
case event_type::idle: |
| 285 |
timer_delay(F1_0 / 4); |
| 286 |
break; |
| 287 |
|
| 288 |
case event_type::window_draw: |
| 289 |
{ |
| 290 |
const char *msg{TXT_PAUSE}; |
| 291 |
|
| 292 |
gr_set_default_canvas(); |
| 293 |
auto &canvas{*grd_curcanv}; |
| 294 |
const auto &game_font{*GAME_FONT}; |
| 295 |
const auto h{gr_get_string_size(game_font, msg).height}; |
| 296 |
|
| 297 |
const auto y{(grd_curscreen->get_screen_height() - h) / 2}; |
| 298 |
|
| 299 |
gr_set_fontcolor(canvas, 255, -1); |
| 300 |
|
| 301 |
gr_ustring(canvas, game_font, 0x8000, y, msg); |
| 302 |
break; |
| 303 |
} |
| 304 |
|
| 305 |
default: |
| 306 |
break; |
| 307 |
} |
| 308 |
return window_event_result::ignored; |
| 309 |
} |
| 310 |
|
| 311 |
window_event_result movie::event_handler(const d_event &event) |
| 312 |
{ |
| 313 |
switch (event.type) |
| 314 |
{ |
| 315 |
case event_type::window_activated: |
| 316 |
paused = 0; |
| 317 |
break; |
| 318 |
|
| 319 |
case event_type::window_deactivated: |
| 320 |
paused = 1; |
| 321 |
MVE_rmHoldMovie(); |
| 322 |
break; |
| 323 |
|
| 324 |
case event_type::key_command: |
| 325 |
{ |
| 326 |
const auto key{event_key_get(event)}; |
| 327 |
|
| 328 |
// If ESCAPE pressed, then quit movie. |
| 329 |
if (key == KEY_ESC) { |
| 330 |
return window_event_result::close; |
| 331 |
} |
| 332 |
|
| 333 |
// If PAUSE pressed, then pause movie |
| 334 |
if ((key == KEY_PAUSE) || (key == KEY_COMMAND + KEY_P)) |
| 335 |
{ |
| 336 |
if (auto pause_window{window_create<movie_pause_window>(w_canv)}) |
| 337 |
{ |
| 338 |
(void)pause_window; |
| 339 |
MVE_rmHoldMovie(); |
| 340 |
} |
| 341 |
return window_event_result::handled; |
| 342 |
} |
| 343 |
break; |
| 344 |
} |
| 345 |
|
| 346 |
case event_type::window_draw: |
| 347 |
{ |
| 348 |
const auto f{frame_num}; |
| 349 |
if (!paused) |
| 350 |
{ |
| 351 |
result = MVE_rmStepMovie(*pMovie); |
| 352 |
if (result == MVE_StepStatus::EndOfFile) |
| 353 |
return window_event_result::close; |
| 354 |
if (result != MVE_StepStatus::Continue) |
| 355 |
{ |
| 356 |
return window_event_result::handled; |
| 357 |
} |
| 358 |
++frame_num; |
| 359 |
} |
| 360 |
|
| 361 |
draw_subtitles(SubtitleState, f); |
| 362 |
} |
| 363 |
|
| 364 |
gr_palette_load(gr_palette); |
| 365 |
|
| 366 |
break; |
| 367 |
|
| 368 |
case event_type::window_close: |
| 369 |
break; |
| 370 |
default: |
| 371 |
break; |
| 372 |
} |
| 373 |
return window_event_result::ignored; |
| 374 |
} |
| 375 |
|
| 376 |
//returns status. see movie.h |
| 377 |
movie_play_status RunMovie(const char *const filename, const std::span<const char> subtitles, const int hires_flag, const play_movie_warn_missing warn_missing, const MVE_play_sounds audio_enabled, const int dx, const int dy) |
| 378 |
{ |
| 379 |
#if DXX_USE_OGL |
| 380 |
palette_array_t pal_save; |
| 381 |
#endif |
| 382 |
|
| 383 |
// Open Movie file. If it doesn't exist, no movie, just return. |
| 384 |
|
| 385 |
auto &&[filehndl, physfserr]{PHYSFSRWOPS_openRead(filename)}; |
| 386 |
if (!filehndl) |
| 387 |
{ |
| 388 |
con_printf(warn_missing == play_movie_warn_missing::verbose ? CON_VERBOSE : CON_URGENT, "Failed to open movie <%s>: %s", filename, PHYSFS_getErrorByCode(physfserr)); |
| 389 |
return movie_play_status::skipped; |
| 390 |
} |
| 391 |
MVESTREAM_ptr_t mvestream; |
| 392 |
if (!(mvestream = MVE_rmPrepMovie(audio_enabled, dx, dy, std::move(filehndl)))) |
| 393 |
{ |
| 394 |
return movie_play_status::skipped; |
| 395 |
} |
| 396 |
const auto reshow{hide_menus()}; |
| 397 |
auto wind{window_create<movie>(grd_curscreen->sc_canvas, std::move(mvestream))}; |
| 398 |
init_subtitles(wind->SubtitleState, subtitles); |
| 399 |
|
| 400 |
#if DXX_USE_OGL |
| 401 |
set_screen_mode(SCREEN_MOVIE); |
| 402 |
gr_copy_palette(pal_save, gr_palette); |
| 403 |
gr_palette_load(gr_palette); |
| 404 |
(void)hires_flag; |
| 405 |
#else |
| 406 |
gr_set_mode(hires_flag ? screen_mode{640, 480} : screen_mode{320, 200}); |
| 407 |
#endif |
| 408 |
|
| 409 |
for (const auto exists{wind->track()}; *exists;) |
| 410 |
event_process(); |
| 411 |
wind = nullptr; |
| 412 |
|
| 413 |
if (reshow) |
| 414 |
show_menus(); |
| 415 |
|
| 416 |
// Restore old graphic state |
| 417 |
|
| 418 |
Screen_mode=-1; //force reset of screen mode |
| 419 |
#if DXX_USE_OGL |
| 420 |
gr_copy_palette(gr_palette, pal_save); |
| 421 |
gr_palette_load(pal_save); |
| 422 |
#endif |
| 423 |
|
| 424 |
return movie_play_status::started; |
| 425 |
} |
| 426 |
|
| 427 |
} |
| 428 |
|
| 429 |
//returns 1 if frame updated ok |
| 430 |
int RotateRobot(MVESTREAM *const pMovie) |
| 431 |
{ |
| 432 |
auto err{MVE_rmStepMovie(*pMovie)}; |
| 433 |
gr_palette_load(gr_palette); |
| 434 |
|
| 435 |
if (err == MVE_StepStatus::EndOfFile) //end of movie, so reset |
| 436 |
{ |
| 437 |
SDL_RWseek(pMovie->movie->stream.get(), 0, SEEK_SET); |
| 438 |
mve_reset(pMovie); |
| 439 |
err = MVE_rmStepMovie(*pMovie); |
| 440 |
} |
| 441 |
if (err != MVE_StepStatus::Continue) |
| 442 |
{ |
| 443 |
Int3(); |
| 444 |
return 0; |
| 445 |
} |
| 446 |
|
| 447 |
return 1; |
| 448 |
} |
| 449 |
|
| 450 |
void DeInitRobotMovie(MVESTREAM_ptr_t &pMovie) |
| 451 |
{ |
| 452 |
pMovie.reset(); |
| 453 |
} |
| 454 |
|
| 455 |
const MVESTREAM *InitRobotMovie(const char *filename, MVESTREAM_ptr_t &pMovie) |
| 456 |
{ |
| 457 |
if (GameArg.SysNoMovies) |
| 458 |
return nullptr; |
| 459 |
|
| 460 |
con_printf(CON_DEBUG, "RoboFile=%s", filename); |
| 461 |
|
| 462 |
auto &&[RoboFile, physfserr]{PHYSFSRWOPS_openRead(filename)}; |
| 463 |
if (!RoboFile) |
| 464 |
{ |
| 465 |
con_printf(CON_URGENT, "Failed to open movie <%s>: %s", filename, PHYSFS_getErrorByCode(physfserr)); |
| 466 |
return nullptr; |
| 467 |
} |
| 468 |
//tell movies to play no sound for robots |
| 469 |
if (!(pMovie = MVE_rmPrepMovie(MVE_play_sounds::silent, SWIDTH / 2.3, SHEIGHT / 2.3, std::move(RoboFile)))) |
| 470 |
{ |
| 471 |
Int3(); |
| 472 |
return nullptr; |
| 473 |
} |
| 474 |
return pMovie.get(); |
| 475 |
} |
| 476 |
|
| 477 |
namespace { |
| 478 |
|
| 479 |
/* |
| 480 |
* Subtitle system code |
| 481 |
*/ |
| 482 |
|
| 483 |
//search for next field following whitespace |
| 484 |
static char *next_field (char *p) |
| 485 |
{ |
| 486 |
while (*p && !isspace(*p)) |
| 487 |
p++; |
| 488 |
|
| 489 |
if (!*p) |
| 490 |
return NULL; |
| 491 |
|
| 492 |
while (*p && isspace(*p)) |
| 493 |
p++; |
| 494 |
|
| 495 |
if (!*p) |
| 496 |
return NULL; |
| 497 |
|
| 498 |
return p; |
| 499 |
} |
| 500 |
|
| 501 |
static int init_subtitles(d_loaded_subtitle_state &SubtitleState, const std::span<const char> filename) |
| 502 |
{ |
| 503 |
if (filename.empty()) |
| 504 |
return 0; |
| 505 |
char *p; |
| 506 |
int have_binary{}; |
| 507 |
|
| 508 |
SubtitleState.Num_subtitles = 0; |
| 509 |
|
| 510 |
if (!GameCfg.MovieSubtitles) |
| 511 |
{ |
| 512 |
con_puts(CON_VERBOSE, "Rebirth: movie subtitles are disabled"); |
| 513 |
return 0; |
| 514 |
} |
| 515 |
|
| 516 |
auto &&[ifile, physfserr]{PHYSFSX_openReadBuffered(filename.data())}; //try text version |
| 517 |
|
| 518 |
if (!ifile) { //no text version, try binary version |
| 519 |
std::array<char, FILENAME_LEN> filename2; |
| 520 |
if (!change_filename_extension(filename2, filename.data(), "txb")) |
| 521 |
{ |
| 522 |
con_printf(CON_NORMAL, "Rebirth: skipping subtitles because cannot open \"%s\" (\"%s\")", filename.data(), PHYSFS_getErrorByCode(physfserr)); |
| 523 |
return 0; |
| 524 |
} |
| 525 |
auto &&[ifile2, physfserr2]{PHYSFSX_openReadBuffered_updateCase(filename2.data())}; |
| 526 |
if (!ifile2) |
| 527 |
{ |
| 528 |
con_printf(CON_VERBOSE, "Rebirth: skipping subtitles because cannot open \"%s\" or \"%s\" (\"%s\", \"%s\")", filename.data(), filename2.data(), PHYSFS_getErrorByCode(physfserr), PHYSFS_getErrorByCode(physfserr2)); |
| 529 |
return 0; |
| 530 |
} |
| 531 |
ifile = std::move(ifile2); |
| 532 |
have_binary = 1; |
| 533 |
con_printf(CON_VERBOSE, "Rebirth: found encoded subtitles in \"%s\"", filename2.data()); |
| 534 |
} |
| 535 |
else |
| 536 |
con_printf(CON_VERBOSE, "Rebirth: found text subtitles in \"%s\"", filename.data()); |
| 537 |
|
| 538 |
const auto size{PHYSFS_fileLength(ifile)}; |
| 539 |
const auto subtitle_raw_data{(SubtitleState.subtitle_raw_data = std::make_unique<char[]>(size + 1)).get()}; |
| 540 |
const auto read_count{PHYSFSX_readBytes(ifile, subtitle_raw_data, size)}; |
| 541 |
ifile.reset(); |
| 542 |
|
| 543 |
if (read_count != size) { |
| 544 |
con_puts(CON_VERBOSE, "Rebirth: skipping subtitles because cannot read full subtitle file"); |
| 545 |
return 0; |
| 546 |
} |
| 547 |
|
| 548 |
subtitle_raw_data[size] = 0; |
| 549 |
p = subtitle_raw_data; |
| 550 |
|
| 551 |
while (p && p < subtitle_raw_data+size) { |
| 552 |
char *const endp{strchr(p,'\n')}; |
| 553 |
if (endp) { |
| 554 |
if (endp[-1] == '\r') |
| 555 |
endp[-1] = 0; //handle 0d0a pair |
| 556 |
*endp = 0; //string termintor |
| 557 |
} |
| 558 |
|
| 559 |
if (have_binary) |
| 560 |
decode_text_line(p); |
| 561 |
|
| 562 |
if (*p != ';') { |
| 563 |
const auto Num_subtitles{SubtitleState.Num_subtitles}; |
| 564 |
auto &Subtitles{SubtitleState.Subtitles}; |
| 565 |
auto &s{Subtitles[SubtitleState.Num_subtitles++]}; |
| 566 |
s.first_frame = atoi(p); |
| 567 |
p = next_field(p); if (!p) continue; |
| 568 |
s.last_frame = atoi(p); |
| 569 |
p = next_field(p); if (!p) continue; |
| 570 |
s.msg = p; |
| 571 |
|
| 572 |
if (Num_subtitles) |
| 573 |
{ |
| 574 |
assert(s.first_frame >= Subtitles[Num_subtitles - 1].first_frame); |
| 575 |
} |
| 576 |
assert(s.last_frame >= s.first_frame); |
| 577 |
} |
| 578 |
|
| 579 |
p = endp+1; |
| 580 |
} |
| 581 |
return 1; |
| 582 |
} |
| 583 |
|
| 584 |
//draw the subtitles for this frame |
| 585 |
static void draw_subtitles(const d_loaded_subtitle_state &SubtitleState, const int frame_num) |
| 586 |
{ |
| 587 |
static constexpr std::size_t MAX_ACTIVE_SUBTITLES{3}; |
| 588 |
static std::array<uint16_t, MAX_ACTIVE_SUBTITLES> active_subtitles; |
| 589 |
static int next_subtitle; |
| 590 |
static unsigned num_active_subtitles; |
| 591 |
int y; |
| 592 |
uint8_t must_erase{}; |
| 593 |
|
| 594 |
if (frame_num == 0) { |
| 595 |
num_active_subtitles = 0; |
| 596 |
next_subtitle = 0; |
| 597 |
gr_set_curfont(*grd_curcanv, *GAME_FONT); |
| 598 |
gr_set_fontcolor(*grd_curcanv, 255, -1); |
| 599 |
} |
| 600 |
|
| 601 |
//get rid of any subtitles that have expired |
| 602 |
auto &Subtitles{SubtitleState.Subtitles}; |
| 603 |
for (int t{}; t < num_active_subtitles;) |
| 604 |
if (frame_num > Subtitles[active_subtitles[t]].last_frame) { |
| 605 |
int t2; |
| 606 |
for (t2=t;t2<num_active_subtitles-1;t2++) |
| 607 |
active_subtitles[t2] = active_subtitles[t2+1]; |
| 608 |
num_active_subtitles--; |
| 609 |
must_erase = 1; |
| 610 |
} |
| 611 |
else |
| 612 |
t++; |
| 613 |
|
| 614 |
//get any subtitles new for this frame |
| 615 |
while (next_subtitle < SubtitleState.Num_subtitles && frame_num >= Subtitles[next_subtitle].first_frame) { |
| 616 |
if (num_active_subtitles >= MAX_ACTIVE_SUBTITLES) |
| 617 |
Error("Too many active subtitles!"); |
| 618 |
active_subtitles[num_active_subtitles++] = next_subtitle; |
| 619 |
next_subtitle++; |
| 620 |
} |
| 621 |
|
| 622 |
//find y coordinate for first line of subtitles |
| 623 |
const auto &&line_spacing{LINE_SPACING(*grd_curcanv->cv_font, *GAME_FONT)}; |
| 624 |
y = grd_curcanv->cv_bitmap.bm_h - (line_spacing * (MAX_ACTIVE_SUBTITLES + 2)); |
| 625 |
|
| 626 |
//erase old subtitles if necessary |
| 627 |
if (must_erase) { |
| 628 |
gr_rect(*grd_curcanv, 0,y,grd_curcanv->cv_bitmap.bm_w-1,grd_curcanv->cv_bitmap.bm_h-1, color_palette_index{0}); |
| 629 |
} |
| 630 |
|
| 631 |
//now draw the current subtitles |
| 632 |
range_for (const auto &t, partial_range(active_subtitles, num_active_subtitles)) |
| 633 |
{ |
| 634 |
gr_string(*grd_curcanv, *grd_curcanv->cv_font, 0x8000, y, Subtitles[t].msg); |
| 635 |
y += line_spacing; |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
[[nodiscard]] |
| 640 |
static PHYSFS_ErrorCode init_movie(const char *movielib, char resolution, int required, LoadedMovie &movie) |
| 641 |
{ |
| 642 |
std::array<char, FILENAME_LEN + 2> filename; |
| 643 |
if (static_cast<std::size_t>(snprintf(filename.data(), filename.size(), "%.8s-%c.mvl", movielib, resolution)) > filename.size()) |
| 644 |
return PHYSFS_ERR_BAD_FILENAME; |
| 645 |
const auto r{PHYSFSX_addRelToSearchPath(filename.data(), movie.pathname, physfs_search_path::prepend)}; |
| 646 |
if (r != PHYSFS_ERR_OK) |
| 647 |
{ |
| 648 |
movie.pathname[0] = 0; |
| 649 |
con_printf(required ? CON_URGENT : CON_VERBOSE, "Failed to open movielib <%s>: %s", filename.data(), PHYSFS_getErrorByCode(r)); |
| 650 |
} |
| 651 |
return r; |
| 652 |
} |
| 653 |
|
| 654 |
static std::pair<PHYSFS_ErrorCode, movie_resolution> init_movie(const char *movielib, int required, LoadedMovie &movie) |
| 655 |
{ |
| 656 |
if (!GameArg.GfxSkipHiresMovie) |
| 657 |
{ |
| 658 |
if (const auto r{init_movie(movielib, 'h', required, movie)}; r == PHYSFS_ERR_OK) |
| 659 |
return {r, movie_resolution::high}; |
| 660 |
} |
| 661 |
return {init_movie(movielib, 'l', required, movie), movie_resolution::low}; |
| 662 |
} |
| 663 |
|
| 664 |
} |
| 665 |
|
| 666 |
//find and initialize the movie libraries |
| 667 |
std::unique_ptr<BuiltinMovies> init_movies() |
| 668 |
{ |
| 669 |
if (GameArg.SysNoMovies) |
| 670 |
return nullptr; |
| 671 |
return std::make_unique<BuiltinMovies>(); |
| 672 |
} |
| 673 |
|
| 674 |
BuiltinMovies::BuiltinMovies() |
| 675 |
{ |
| 676 |
for (auto &&[f, m] : zip(movielib_files, movies)) |
| 677 |
init_movie(f.data(), 1, m); |
| 678 |
} |
| 679 |
|
| 680 |
LoadedMovie::~LoadedMovie() |
| 681 |
{ |
| 682 |
const auto movielib{pathname.data()}; |
| 683 |
if (!*movielib) |
| 684 |
return; |
| 685 |
if (!PHYSFS_unmount(movielib)) |
| 686 |
con_printf(CON_URGENT, "Cannot close movielib <%s>: %s", movielib, PHYSFS_getLastError()); |
| 687 |
else |
| 688 |
con_printf(CON_VERBOSE, "Unloaded movielib <%s>", movielib); |
| 689 |
} |
| 690 |
|
| 691 |
std::unique_ptr<LoadedMovieWithResolution> init_extra_robot_movie(const char *movielib) |
| 692 |
{ |
| 693 |
std::unique_ptr<LoadedMovieWithResolution> r; |
| 694 |
if (GameArg.SysNoMovies) |
| 695 |
{ |
| 696 |
[[unlikely]]; |
| 697 |
} |
| 698 |
else if (const auto &&[code, resolution]{init_movie(movielib, 0, *(r = std::make_unique_for_overwrite<LoadedMovieWithResolution>()))}; code != PHYSFS_ERR_OK) |
| 699 |
{ |
| 700 |
[[unlikely]]; |
| 701 |
r.reset(); |
| 702 |
} |
| 703 |
else |
| 704 |
r->resolution = resolution; |
| 705 |
return r; |
| 706 |
} |
| 707 |
|
| 708 |
} |