| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include <cstring> |
| 20 | |
| 21 | #include "application.h" |
| 22 | #include "args.h" |
| 23 | #include "bitmap.h" |
| 24 | #include "d3movie.h" |
| 25 | #include "ddio.h" |
| 26 | #include "ddvid.h" |
| 27 | #include "game.h" |
| 28 | #include "gamefont.h" |
| 29 | #include "grtext.h" |
| 30 | #include "mem.h" |
| 31 | #include "mvelib.h" |
| 32 | #include "pserror.h" |
| 33 | #include "renderer.h" |
| 34 | |
| 35 | namespace { |
| 36 | MovieFrameCallback_fp Movie_callback = nullptr; |
| 37 | uint16_t CurrentPalette[256]; |
| 38 | int Movie_bm_handle = -1; |
| 39 | uint32_t Movie_current_framenum = 0; |
| 40 | bool Movie_looping = false; |
| 41 | } // namespace |
| 42 | |
| 43 | static void *CallbackAlloc(uint32_t size); |
| 44 | static void CallbackFree(void *p); |
| 45 | static uint32_t CallbackFileRead(void *stream, void *pBuffer, uint32_t bufferCount); |
| 46 | static void InitializePalette(); |
| 47 | static void CallbackSetPalette(const uint8_t *pBuffer, uint32_t start, uint32_t count); |
| 48 | static void CallbackShowFrame(uint8_t *buf, uint32_t bufw, uint32_t bufh, uint32_t sx, |
| 49 | uint32_t sy, uint32_t w, uint32_t h, uint32_t dstx, uint32_t dsty, |
| 50 | uint32_t hicolor); |
| 51 | |
| 52 | #ifndef NO_MOVIES |
| 53 | static bool mve_InitSound(); |
| 54 | static void mve_CloseSound(); |
| 55 | #endif |
| 56 | |
| 57 | // sets the directory where movies are stored |
| 58 | int mve_Init(const char *dir, const char *sndcard) { |
| 59 | #ifndef NO_MOVIES |
| 60 | return MVELIB_NOERROR; |
| 61 | #else |
| 62 | return MVELIB_INIT_ERROR; |
| 63 | #endif |
| 64 | } |
| 65 | |
| 66 | // callback called per frame of playback of movies. |
| 67 | void mve_SetCallback(MovieFrameCallback_fp callBack) { |
| 68 | #ifndef NO_MOVIES |
| 69 | Movie_callback = callBack; |
| 70 | #endif |
| 71 | } |
| 72 | |
| 73 | // used to tell movie library how to render movies. |
| 74 | void mve_SetRenderProperties(int16_t x, int16_t y, int16_t w, int16_t h, renderer_type type, bool hicolor) {} |
| 75 | |
| 76 | #if defined(POSIX) |
| 77 | // locates the case-sensitive movie file name |
| 78 | std::filesystem::path mve_FindMovieFileRealName(const std::filesystem::path &movie) { |
| 79 | // split into directory and file... |
| 80 | std::filesystem::path t_file = movie.filename(); |
| 81 | std::filesystem::path t_dir = movie.parent_path(); |
| 82 | std::filesystem::path t_out; |
| 83 | |
| 84 | // found a directory? |
| 85 | if (!t_dir.empty()) { |
| 86 | // map the bits (or fail) |
| 87 | t_out = cf_FindRealFileNameCaseInsensitive(t_file, t_dir); |
| 88 | if (t_out.empty()) |
| 89 | return t_out; |
| 90 | // re-assemble |
| 91 | return (t_dir / t_out); |
| 92 | } else { |
| 93 | // just a file, map that |
| 94 | t_out = cf_FindRealFileNameCaseInsensitive(t_file); |
| 95 | if (t_out.empty()) |
| 96 | return t_out; |
| 97 | // re-assemble |
| 98 | return t_out; |
| 99 | } |
| 100 | } |
| 101 | #endif |
| 102 | |
| 103 | // plays a movie using the current screen. |
| 104 | int mve_PlayMovie(const std::filesystem::path &pMovieName, oeApplication *pApp) { |
| 105 | #ifndef NO_MOVIES |
| 106 | // first, find that movie.. |
| 107 | std::filesystem::path real_name; |
| 108 | #if defined(POSIX) |
| 109 | real_name = mve_FindMovieFileRealName(pMovieName); |
| 110 | if (real_name.empty()) { |
| 111 | mprintf(0, "MOVIE: No such file %s\n", pMovieName.u8string().c_str()); |
| 112 | return MVELIB_FILE_ERROR; |
| 113 | } |
| 114 | #else |
| 115 | real_name = pMovieName; |
| 116 | #endif |
| 117 | // open movie file. |
| 118 | FILE *hFile = fopen(real_name.u8string().c_str(), "rb"); |
| 119 | if (hFile == nullptr) { |
| 120 | mprintf(0, "MOVIE: Unable to open %s\n", real_name.u8string().c_str()); |
| 121 | return MVELIB_FILE_ERROR; |
| 122 | } |
| 123 | |
| 124 | // setup |
| 125 | MVE_sfCallbacks(CallbackShowFrame); |
| 126 | MVE_memCallbacks(CallbackAlloc, CallbackFree); |
| 127 | MVE_ioCallbacks(CallbackFileRead); |
| 128 | MVE_palCallbacks(CallbackSetPalette); |
| 129 | InitializePalette(); |
| 130 | Movie_bm_handle = -1; |
| 131 | |
| 132 | if (!mve_InitSound()) { |
| 133 | mprintf(0, "Failed to initialize sound\n"); |
| 134 | fclose(hFile); |
| 135 | return MVELIB_INIT_ERROR; |
| 136 | } |
| 137 | |
| 138 | MVESTREAM *mve = MVE_rmPrepMovie(hFile, -1, -1, 0); |
| 139 | if (mve == nullptr) { |
| 140 | mprintf(0, "Failed to prepMovie %s\n", pMovieName.u8string().c_str()); |
| 141 | fclose(hFile); |
| 142 | mve_CloseSound(); |
| 143 | return MVELIB_INIT_ERROR; |
| 144 | } |
| 145 | |
| 146 | bool aborted = false; |
| 147 | Movie_current_framenum = 0; |
| 148 | int result; |
| 149 | while ((result = MVE_rmStepMovie(mve)) == 0) { |
| 150 | // let the OS do its thing |
| 151 | pApp->defer(); |
| 152 | |
| 153 | // check for bail |
| 154 | int key = ddio_KeyInKey(); |
| 155 | if (key == KEY_ESC) { |
| 156 | aborted = true; |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // free our bitmap |
| 162 | if (Movie_bm_handle != -1) { |
| 163 | bm_FreeBitmap(Movie_bm_handle); |
| 164 | Movie_bm_handle = -1; |
| 165 | } |
| 166 | |
| 167 | // close our file handle |
| 168 | fclose(hFile); |
| 169 | |
| 170 | // determine the return code |
| 171 | int err = MVELIB_NOERROR; |
| 172 | if (aborted) { |
| 173 | err = MVELIB_PLAYBACK_ABORTED; |
| 174 | } else if (result != MVE_ERR_EOF) { |
| 175 | err = MVELIB_PLAYBACK_ERROR; |
| 176 | } |
| 177 | |
| 178 | // cleanup and shutdown |
| 179 | MVE_rmEndMovie(mve); |
| 180 | |
| 181 | // reset sound |
| 182 | mve_CloseSound(); |
| 183 | |
| 184 | // return out |
| 185 | return err; |
| 186 | #else |
| 187 | return MVELIB_INIT_ERROR; |
| 188 | #endif |
| 189 | } |
| 190 | |
| 191 | void *CallbackAlloc(uint32_t size) { return mem_malloc(size); } |
| 192 | |
| 193 | void CallbackFree(void *p) { mem_free(p); } |
| 194 | |
| 195 | uint32_t CallbackFileRead(void *stream, void *pBuffer, uint32_t bufferCount) { |
| 196 | uint32_t numRead = fread(pBuffer, 1, bufferCount, (FILE *)stream); |
| 197 | return (numRead == bufferCount) ? 1 : 0; |
| 198 | } |
| 199 | |
| 200 | void InitializePalette() { |
| 201 | for (unsigned short & i : CurrentPalette) { |
| 202 | i = OPAQUE_FLAG | GR_RGB16(0, 0, 0); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | void CallbackSetPalette(const uint8_t *pBuffer, uint32_t start, uint32_t count) { |
| 207 | #ifndef NO_MOVIES |
| 208 | pBuffer += start * 3; |
| 209 | |
| 210 | for (uint32_t i = 0; i < count; ++i) { |
| 211 | uint32_t r = pBuffer[0] << 2; |
| 212 | uint32_t g = pBuffer[1] << 2; |
| 213 | uint32_t b = pBuffer[2] << 2; |
| 214 | pBuffer += 3; |
| 215 | |
| 216 | CurrentPalette[start + i] = OPAQUE_FLAG | GR_RGB16(r, g, b); |
| 217 | } |
| 218 | #endif |
| 219 | } |
| 220 | |
| 221 | int NextPow2(int n) { |
| 222 | n--; |
| 223 | n |= n >> 1; |
| 224 | n |= n >> 2; |
| 225 | n |= n >> 4; |
| 226 | n |= n >> 8; |
| 227 | n |= n >> 16; |
| 228 | n++; |
| 229 | return n; |
| 230 | } |
| 231 | |
| 232 | #ifndef NO_MOVIES |
| 233 | void BlitToMovieBitmap(uint8_t *buf, uint32_t bufw, uint32_t bufh, uint32_t hicolor, |
| 234 | bool usePow2Texture, int &texW, int &texH) { |
| 235 | // get some sizes |
| 236 | int drawWidth = bufw; |
| 237 | int drawHeight = bufh; |
| 238 | |
| 239 | if (usePow2Texture) { |
| 240 | int wPow2 = NextPow2(drawWidth); |
| 241 | int hPow2 = NextPow2(drawHeight); |
| 242 | int texSize = (wPow2 > hPow2) ? wPow2 : hPow2; |
| 243 | texW = texSize; |
| 244 | texH = texSize; |
| 245 | } else { |
| 246 | texW = drawWidth; |
| 247 | texH = drawHeight; |
| 248 | } |
| 249 | |
| 250 | if (Movie_bm_handle == -1) { |
| 251 | // Allocate our bitmap |
| 252 | Movie_bm_handle = bm_AllocBitmap(texW, texH, 0); |
| 253 | } |
| 254 | |
| 255 | uint16_t *pPixelData = (uint16_t *)bm_data(Movie_bm_handle, 0); |
| 256 | GameBitmaps[Movie_bm_handle].flags |= BF_CHANGED; |
| 257 | if (hicolor) { |
| 258 | uint16_t *wBuf = (uint16_t *)buf; |
| 259 | for (int y = 0; y < drawHeight; ++y) { |
| 260 | for (int x = 0; x < drawWidth; ++x) { |
| 261 | uint16_t col16 = *wBuf++; |
| 262 | // Convert to RGB555 |
| 263 | pPixelData[x] = col16 | OPAQUE_FLAG; |
| 264 | } |
| 265 | |
| 266 | pPixelData += texW; |
| 267 | } |
| 268 | } else { |
| 269 | for (int y = 0; y < drawHeight; ++y) { |
| 270 | for (int x = 0; x < drawWidth; ++x) { |
| 271 | uint8_t palIndex = *buf++; |
| 272 | pPixelData[x] = CurrentPalette[palIndex]; |
| 273 | } |
| 274 | |
| 275 | pPixelData += texW; |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | void CallbackShowFrame(uint8_t *buf, uint32_t bufw, uint32_t bufh, uint32_t sx, uint32_t sy, |
| 281 | uint32_t w, uint32_t h, uint32_t dstx, uint32_t dsty, uint32_t hicolor) { |
| 282 | // prepare our bitmap |
| 283 | int texW, texH; |
| 284 | BlitToMovieBitmap(buf, bufw, bufh, hicolor, true, texW, texH); |
| 285 | |
| 286 | // calculate UVs from texture |
| 287 | unsigned int drawWidth = bufw; |
| 288 | unsigned int drawHeight = bufh; |
| 289 | float u = float(drawWidth - 1) / float(texW - 1); |
| 290 | float v = float(drawHeight - 1) / float(texH - 1); |
| 291 | |
| 292 | StartFrame(0, 0, 640, 480, false); |
| 293 | |
| 294 | rend_ClearScreen(GR_BLACK); |
| 295 | rend_SetAlphaType(AT_CONSTANT); |
| 296 | rend_SetAlphaValue(255); |
| 297 | rend_SetLighting(LS_NONE); |
| 298 | rend_SetColorModel(CM_MONO); |
| 299 | rend_SetOverlayType(OT_NONE); |
| 300 | rend_SetWrapType(WT_CLAMP); |
| 301 | rend_SetFiltering(0); |
| 302 | rend_SetZBufferState(0); |
| 303 | rend_DrawScaledBitmap(dstx, dsty, dstx + drawWidth, dsty + drawHeight, Movie_bm_handle, 0.0f, 0.0f, u, v); |
| 304 | rend_SetFiltering(1); |
| 305 | rend_SetZBufferState(1); |
| 306 | |
| 307 | // call our callback |
| 308 | if (Movie_callback != nullptr) { |
| 309 | Movie_callback(dstx, dsty, Movie_current_framenum); |
| 310 | } |
| 311 | ++Movie_current_framenum; |
| 312 | |
| 313 | EndFrame(); |
| 314 | |
| 315 | rend_Flip(); |
| 316 | } |
| 317 | |
| 318 | // This callback is same as CallbackShowFrame() but don't flip renderer at end, so there no flickering |
| 319 | void CallbackShowFrameNoFlip(unsigned char *buf, unsigned int bufw, unsigned int bufh, unsigned int sx, unsigned int sy, |
| 320 | unsigned int w, unsigned int h, unsigned int dstx, unsigned int dsty, unsigned int hicolor) { |
| 321 | // prepare our bitmap |
| 322 | int texW, texH; |
| 323 | BlitToMovieBitmap(buf, bufw, bufh, hicolor, true, texW, texH); |
| 324 | |
| 325 | // calculate UVs from texture |
| 326 | unsigned int drawWidth = bufw; |
| 327 | unsigned int drawHeight = bufh; |
| 328 | float u = float(drawWidth - 1) / float(texW - 1); |
| 329 | float v = float(drawHeight - 1) / float(texH - 1); |
| 330 | |
| 331 | StartFrame(0, 0, 640, 480, false); |
| 332 | |
| 333 | rend_ClearScreen(GR_BLACK); |
| 334 | rend_SetAlphaType(AT_CONSTANT); |
| 335 | rend_SetAlphaValue(255); |
| 336 | rend_SetLighting(LS_NONE); |
| 337 | rend_SetColorModel(CM_MONO); |
| 338 | rend_SetOverlayType(OT_NONE); |
| 339 | rend_SetWrapType(WT_CLAMP); |
| 340 | rend_SetFiltering(0); |
| 341 | rend_SetZBufferState(0); |
| 342 | rend_DrawScaledBitmap(dstx, dsty, dstx + drawWidth, dsty + drawHeight, Movie_bm_handle, 0.0f, 0.0f, u, v); |
| 343 | rend_SetFiltering(1); |
| 344 | rend_SetZBufferState(1); |
| 345 | |
| 346 | // call our callback |
| 347 | if (Movie_callback != nullptr) { |
| 348 | Movie_callback(dstx, dsty, Movie_current_framenum); |
| 349 | } |
| 350 | ++Movie_current_framenum; |
| 351 | |
| 352 | EndFrame(); |
| 353 | } |
| 354 | #endif |
| 355 | |
| 356 | intptr_t mve_SequenceStart(const char *mvename, void *fhandle, oeApplication *app, bool looping) { |
| 357 | #ifndef NO_MOVIES |
| 358 | // first, find that movie.. |
| 359 | std::filesystem::path real_name; |
| 360 | #if defined(POSIX) |
| 361 | real_name = mve_FindMovieFileRealName(mvename); |
| 362 | if (real_name.empty()) { |
| 363 | mprintf(0, "MOVIE: No such file %s\n", mvename); |
| 364 | fhandle = nullptr; |
| 365 | return 0; |
| 366 | } |
| 367 | #else |
| 368 | real_name = mvename; |
| 369 | #endif |
| 370 | fhandle = fopen(real_name.u8string().c_str(), "rb"); |
| 371 | |
| 372 | if (fhandle == nullptr) { |
| 373 | mprintf(1, "MOVIE: Unable to open %s\n", real_name.u8string().c_str()); |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | // setup |
| 378 | MVE_memCallbacks(CallbackAlloc, CallbackFree); |
| 379 | MVE_ioCallbacks(CallbackFileRead); |
| 380 | MVE_sfCallbacks(CallbackShowFrameNoFlip); |
| 381 | MVE_palCallbacks(CallbackSetPalette); |
| 382 | InitializePalette(); |
| 383 | Movie_bm_handle = -1; |
| 384 | Movie_looping = looping; |
| 385 | |
| 386 | // let the render know we will be copying bitmaps to framebuffer (or something) |
| 387 | rend_SetFrameBufferCopyState(true); |
| 388 | |
| 389 | MVESTREAM *mve = MVE_rmPrepMovie(fhandle, -1, -1, 0); |
| 390 | if (mve == nullptr) { |
| 391 | mprintf(0, "Failed to PrepMovie %s\n", mvename); |
| 392 | fclose((FILE *)fhandle); |
| 393 | return MVELIB_INIT_ERROR; |
| 394 | } |
| 395 | |
| 396 | return (intptr_t)mve; |
| 397 | #else |
| 398 | return nullptr; |
| 399 | #endif |
| 400 | } |
| 401 | |
| 402 | intptr_t mve_SequenceFrame(intptr_t handle, void *fhandle, bool sequence, int *bm_handle) { |
| 403 | #ifndef NO_MOVIES |
| 404 | if (bm_handle) { |
| 405 | *bm_handle = -1; |
| 406 | } |
| 407 | |
| 408 | if (handle == -1) { |
| 409 | return -1; |
| 410 | } |
| 411 | |
| 412 | static unsigned sw = 0, sh = 0, hicolor = 0; |
| 413 | int err = 0; |
| 414 | |
| 415 | reread_frame: |
| 416 | |
| 417 | // get the next frame of data |
| 418 | uint8_t *pBuffer = nullptr; |
| 419 | err = MVE_rmStepMovie((MVESTREAM *)handle); |
| 420 | |
| 421 | if (err == 0) { |
| 422 | // blit to bitmap |
| 423 | int texW, texH; |
| 424 | BlitToMovieBitmap(pBuffer, sw, sh, hicolor, false, texW, texH); |
| 425 | |
| 426 | if (bm_handle) { |
| 427 | *bm_handle = Movie_bm_handle; |
| 428 | } |
| 429 | |
| 430 | return handle; |
| 431 | } |
| 432 | |
| 433 | if (Movie_looping && err == MVE_ERR_EOF) { |
| 434 | mve_reset((MVESTREAM *)handle); |
| 435 | sequence = true; |
| 436 | goto reread_frame; |
| 437 | } |
| 438 | |
| 439 | return -1; |
| 440 | #else |
| 441 | return -1; |
| 442 | #endif |
| 443 | } |
| 444 | |
| 445 | bool mve_SequenceClose(intptr_t hMovie, void *hFile) { |
| 446 | #ifndef NO_MOVIES |
| 447 | if (hMovie == -1) |
| 448 | return false; |
| 449 | |
| 450 | MVE_rmEndMovie((MVESTREAM *)hMovie); |
| 451 | |
| 452 | // free our bitmap |
| 453 | if (Movie_bm_handle != -1) { |
| 454 | bm_FreeBitmap(Movie_bm_handle); |
| 455 | Movie_bm_handle = -1; |
| 456 | } |
| 457 | |
| 458 | // We're no longer needing this |
| 459 | rend_SetFrameBufferCopyState(false); |
| 460 | |
| 461 | return true; |
| 462 | #else |
| 463 | return false; |
| 464 | #endif |
| 465 | } |
| 466 | |
| 467 | void mve_Puts(int16_t x, int16_t y, ddgr_color col, const char *txt) { |
| 468 | grtext_SetFont(BRIEFING_FONT); |
| 469 | grtext_SetColor(col); |
| 470 | grtext_SetAlpha(255); |
| 471 | grtext_SetFlags(GRTEXTFLAG_SHADOW); |
| 472 | grtext_CenteredPrintf(0, y, txt); |
| 473 | grtext_Flush(); |
| 474 | } |
| 475 | |
| 476 | void mve_ClearRect(int16_t x1, int16_t y1, int16_t x2, int16_t y2) { |
| 477 | // Note: I can not figure out how to clear, and then write over it with text. It always covers my text! |
| 478 | // rend_FillRect( GR_BLACK, x1, y1, x2, y2 ); |
| 479 | } |
| 480 | |
| 481 | #ifndef NO_MOVIES |
| 482 | bool mve_InitSound() { |
| 483 | MVE_sndInit(FindArg("-nosound") == 0); |
| 484 | |
| 485 | return true; |
| 486 | } |
| 487 | |
| 488 | void mve_CloseSound() { |
| 489 | // TODO: close the driver out |
| 490 | } |
| 491 | |
| 492 | #endif |