From b94458f561e0a78a3ef076f9f2420872c1e8553d Mon Sep 17 00:00:00 2001 From: "Azamat H. Hackimov" Date: Sat, 4 May 2024 16:21:01 +0300 Subject: [PATCH] MVE: Switch to D2X implementation (first run) Adapting movie and mve interfaces each other. Converted open() to fopen() et al. calls. movie expected image as RGB565 from old mve, but now it's RGB555, so there no need additional conversion. Fixed hicolor related conversion of width in movie's callbacks. Headers cleanup in both subsystems. Sound and frame callbacks are still unavailable. --- Descent3/cinematics.cpp | 5 +-- Descent3/cinematics.h | 2 +- lib/movie.h | 18 ++++----- libmve/CMakeLists.txt | 6 +-- libmve/decoder16.cpp | 5 +-- libmve/decoder8.cpp | 4 +- libmve/libmve.h | 9 ++++- libmve/mvelib.cpp | 16 ++------ libmve/mvelib.h | 4 +- libmve/mveplay.cpp | 50 ++++++++++++------------- movie/d3movie.cpp | 82 ++++++++++++++++++----------------------- 11 files changed, 94 insertions(+), 107 deletions(-) diff --git a/Descent3/cinematics.cpp b/Descent3/cinematics.cpp index 4ac7e36a..4a8387f9 100644 --- a/Descent3/cinematics.cpp +++ b/Descent3/cinematics.cpp @@ -28,8 +28,7 @@ #include "appdatabase.h" #include "bitmap.h" -#include -#include +#include #ifdef DEBUG static struct { @@ -144,7 +143,7 @@ tCinematic *StartMovie(const char *moviename, bool looping) { SetMovieProperties(0, 0, Max_window_w, Max_window_h, Renderer_type); - int filehandle; + FILE *filehandle; intptr_t hMovie = mve_SequenceStart(filename, &filehandle, Descent, looping); if (hMovie == 0) return NULL; diff --git a/Descent3/cinematics.h b/Descent3/cinematics.h index e9961dc5..bc9597b2 100644 --- a/Descent3/cinematics.h +++ b/Descent3/cinematics.h @@ -60,7 +60,7 @@ // Movie Cinematic struct tCinematic { intptr_t mvehandle; - int filehandle; + void *filehandle; chunked_bitmap frame_chunk; // used internally, don't access. }; diff --git a/lib/movie.h b/lib/movie.h index 53d66612..e30d716c 100644 --- a/lib/movie.h +++ b/lib/movie.h @@ -24,12 +24,12 @@ #include #define MVELIB_NOERROR 0 -#define MVELIB_FILE_ERROR -1 -#define MVELIB_NOTINIT_ERROR -2 -#define MVELIB_DDGR_ERROR -3 -#define MVELIB_INIT_ERROR -4 -#define MVELIB_PLAYBACK_ERROR -5 -#define MVELIB_PLAYBACK_ABORTED -6 +#define MVELIB_FILE_ERROR (-1) +#define MVELIB_NOTINIT_ERROR (-2) +#define MVELIB_DDGR_ERROR (-3) +#define MVELIB_INIT_ERROR (-4) +#define MVELIB_PLAYBACK_ERROR (-5) +#define MVELIB_PLAYBACK_ABORTED (-6) class oeApplication; @@ -40,16 +40,16 @@ int mve_PlayMovie(const char *mvename, oeApplication *app); // used to copy movie data to a pointer, looping will loop, fhandle will be a pointer to a file handle to be returned // handle to movie sequence is returned by function. -intptr_t mve_SequenceStart(const char *mvename, int *fhandle, oeApplication *app, bool looping = false); +intptr_t mve_SequenceStart(const char *mvename, void *fhandle, oeApplication *app, bool looping = false); // frames movies started with SequenceStart. Optionally, pass a pointer to a variable which will receive // bitmap handle containing data, nx and ny will contain adjusted x and y coordinates if needed. // passing -1 to x and y centers the frame on that axis // handle is the movie handle returned by mve_Sequence start // returned value is the handle passed into mve_SequenceFrame or new one if movie looped. -intptr_t mve_SequenceFrame(intptr_t handle, int fhandle, bool sequence, int *bm_handle); +intptr_t mve_SequenceFrame(intptr_t handle, void *fhandle, bool sequence, int *bm_handle); -bool mve_SequenceClose(intptr_t handle, int fhandle); +bool mve_SequenceClose(intptr_t handle, void *fhandle); // sets render frame boundaries. void mve_SetRenderProperties(int16_t x, int16_t y, int16_t w, int16_t h, renderer_type type, bool hicolor); diff --git a/libmve/CMakeLists.txt b/libmve/CMakeLists.txt index c8891be2..ef90c497 100644 --- a/libmve/CMakeLists.txt +++ b/libmve/CMakeLists.txt @@ -1,8 +1,8 @@ set(CPPS +# mveasm.cpp +# mvelibl.cpp +# platform.cpp lnxdsound.cpp - mveasm.cpp - mvelibl.cpp - platform.cpp # d2x implementation decoder8.cpp diff --git a/libmve/decoder16.cpp b/libmve/decoder16.cpp index 20555b3c..bae88c68 100644 --- a/libmve/decoder16.cpp +++ b/libmve/decoder16.cpp @@ -17,9 +17,8 @@ /* 16 bit decoding routines */ -#include -#include -#include +#include +#include #include "decoders.h" diff --git a/libmve/decoder8.cpp b/libmve/decoder8.cpp index 722faf4e..1b17446b 100644 --- a/libmve/decoder8.cpp +++ b/libmve/decoder8.cpp @@ -17,8 +17,8 @@ /* 8 bit decoding routines */ -#include -#include +#include +#include #include "decoders.h" diff --git a/libmve/libmve.h b/libmve/libmve.h index ea97ed88..6d87a2fd 100644 --- a/libmve/libmve.h +++ b/libmve/libmve.h @@ -18,6 +18,8 @@ #ifndef _LIBMVE_H #define _LIBMVE_H +#include "SystemInterfaces.h" + #define MVE_ERR_EOF 1 typedef struct { @@ -35,6 +37,8 @@ void MVE_rmEndMovie(void); void MVE_getVideoSpec(MVE_videoSpec *vSpec); +void MVE_sndInit(ISoundDevice *lpDS); + void MVE_sndInit(int x); typedef unsigned int (*mve_cb_Read)(void *stream, void *buffer, unsigned int count); @@ -43,7 +47,8 @@ typedef void *(*mve_cb_Alloc)(unsigned int size); typedef void (*mve_cb_Free)(void *ptr); typedef void (*mve_cb_ShowFrame)(unsigned char *buffer, unsigned int bufw, unsigned int bufh, unsigned int sx, - unsigned int sy, unsigned int w, unsigned int h, unsigned int dstx, unsigned int dsty); + unsigned int sy, unsigned int w, unsigned int h, unsigned int dstx, unsigned int dsty, + unsigned int hicolor); typedef void (*mve_cb_SetPalette)(unsigned char *p, unsigned int start, unsigned int count); @@ -52,4 +57,6 @@ void MVE_memCallbacks(mve_cb_Alloc mem_alloc, mve_cb_Free mem_free); void MVE_sfCallbacks(mve_cb_ShowFrame showframe); void MVE_palCallbacks(mve_cb_SetPalette setpalette); +void MVE_ReleaseMem(void); + #endif /* _LIBMVE_H */ diff --git a/libmve/mvelib.cpp b/libmve/mvelib.cpp index 987c962b..7e9b1a5c 100644 --- a/libmve/mvelib.cpp +++ b/libmve/mvelib.cpp @@ -15,17 +15,7 @@ * along with this program. If not, see . */ -#ifdef HAVE_CONFIG_H -#include "conf.h" -#endif - -#include // for mem* functions -#if !defined(_WIN32) && !defined(macintosh) -#include -#include -#include -#include -#endif +#include // for mem* functions #include "mvelib.h" @@ -462,7 +452,9 @@ static MVESTREAM *_mvestream_alloc(void) { */ static void _mvestream_free(MVESTREAM *movie) { /* close MVEFILE */ - if (movie->movie) + if (movie == nullptr) + return; + if (movie->movie != nullptr) mvefile_close(movie->movie); movie->movie = NULL; diff --git a/libmve/mvelib.h b/libmve/mvelib.h index fa74932e..11a29027 100644 --- a/libmve/mvelib.h +++ b/libmve/mvelib.h @@ -18,8 +18,8 @@ #ifndef INCLUDED_MVELIB_H #define INCLUDED_MVELIB_H -#include -#include +#include +#include #include "libmve.h" diff --git a/libmve/mveplay.cpp b/libmve/mveplay.cpp index 220fdcb7..fa9b2b56 100644 --- a/libmve/mveplay.cpp +++ b/libmve/mveplay.cpp @@ -15,31 +15,15 @@ * along with this program. If not, see . */ -#ifdef HAVE_CONFIG_H -#include -#endif +// TODO +//#define AUDIO -#ifndef __MSDOS__ -#define AUDIO -#endif -// #define DEBUG - -#include +#include #ifdef _WIN32 #include #else -#include -#include -#include -#ifdef macintosh -#include -#include -#else +#include #include -#include -#include -#include -#endif // macintosh #endif // _WIN32 #if defined(AUDIO) @@ -47,12 +31,11 @@ #include "SDL_mixer.h" #endif +#include "decoders.h" +#include "libmve.h" #include "mvelib.h" #include "mve_audio.h" - -#include "decoders.h" - -#include "libmve.h" +#include "SystemInterfaces.h" #define MVE_OPCODE_ENDOFSTREAM 0x00 #define MVE_OPCODE_ENDOFCHUNK 0x01 @@ -81,6 +64,8 @@ int g_spdFactorNum = 0; static int g_spdFactorDenom = 10; static int g_frameUpdated = 0; +static ISoundDevice *snd_ds = NULL; + static short get_short(unsigned char *data) { short value; value = data[0] | (data[1] << 8); @@ -226,6 +211,8 @@ end: /************************* * audio handlers *************************/ + +static int mve_audio_enabled = 0; #ifdef AUDIO #define TOTAL_AUDIO_BUFFERS 64 @@ -493,7 +480,7 @@ static int display_video_handler(unsigned char major, unsigned char minor, unsig if (g_destY == -1) // center it g_destY = (g_screenHeight - g_height) >> 1; - mve_showframe((unsigned char *)g_vBackBuf1, g_width, g_height, 0, 0, g_width, g_height, g_destX, g_destY); + mve_showframe((unsigned char *)g_vBackBuf1, g_width, g_height, 0, 0, g_width, g_height, g_destX, g_destY, g_truecolor); g_frameUpdated = 1; @@ -587,6 +574,13 @@ void MVE_sfCallbacks(mve_cb_ShowFrame showframe) { mve_showframe = showframe; } void MVE_palCallbacks(mve_cb_SetPalette setpalette) { mve_setpalette = setpalette; } +void MVE_ReleaseMem() { + MVE_rmEndMovie(); +// ioRelease(); +// sndRelease(); +// nfRelease(); +} + int MVE_rmPrepMovie(void *src, int x, int y, int track) { int i; @@ -705,6 +699,12 @@ void MVE_rmEndMovie() { void MVE_rmHoldMovie() { timer_started = 0; } +void MVE_sndInit(ISoundDevice *lpDS) { +#ifdef AUDIO + snd_ds = lpDS; +#endif +} + void MVE_sndInit(int x) { #ifdef AUDIO if (x == -1) diff --git a/movie/d3movie.cpp b/movie/d3movie.cpp index 426b30e4..a5a96775 100644 --- a/movie/d3movie.cpp +++ b/movie/d3movie.cpp @@ -16,14 +16,9 @@ * along with this program. If not, see . */ -#include - #ifdef __LINUX__ #include -#include -#include - -#define O_BINARY 0 +#include "lnxdsound.h" #endif @@ -34,11 +29,11 @@ #include "dsound.h" #endif -#include -#include +#include #include "movie.h" -#include "mvelibw.h" +//#include "mvelibw.h" +#include "libmve.h" #include "pserror.h" #include "renderer.h" #include "application.h" @@ -239,7 +234,7 @@ public: static void *CallbackAlloc(uint32_t size); static void CallbackFree(void *p); -static uint32_t CallbackFileRead(int hFile, void *pBuffer, uint32_t bufferCount); +static uint32_t CallbackFileRead(void *stream, void *pBuffer, uint32_t bufferCount); static void InitializePalette(); static void CallbackSetPalette(uint8_t *pBuffer, uint32_t start, uint32_t count); static void CallbackShowFrame(uint8_t *buf, uint32_t bufw, uint32_t bufh, uint32_t sx, @@ -314,8 +309,8 @@ int mve_PlayMovie(const char *pMovieName, oeApplication *pApp) { real_name = pMovieName; #endif // open movie file. - int hFile = open(real_name.u8string().c_str(), O_RDONLY | O_BINARY); - if (hFile == -1) { + FILE *hFile = fopen(real_name.u8string().c_str(), "rb"); + if (hFile == nullptr) { mprintf(0, "MOVIE: Unable to open %s\n", real_name.u8string().c_str()); return MVELIB_FILE_ERROR; } @@ -325,11 +320,11 @@ int mve_PlayMovie(const char *pMovieName, oeApplication *pApp) { bool highColor = (pExtension != NULL && stricmp(pExtension, ".mv8") != 0); // setup - MVE_rmFastMode(MVE_RM_NORMAL); + // MVE_rmFastMode(MVE_RM_NORMAL); MVE_sfCallbacks(CallbackShowFrame); MVE_memCallbacks(CallbackAlloc, CallbackFree); MVE_ioCallbacks(CallbackFileRead); - MVE_sfSVGA(640, 480, 480, 0, NULL, 0, 0, NULL, highColor ? 1 : 0); + // MVE_sfSVGA(640, 480, 480, 0, NULL, 0, 0, NULL, highColor ? 1 : 0); MVE_palCallbacks(CallbackSetPalette); InitializePalette(); Movie_bm_handle = -1; @@ -337,14 +332,14 @@ int mve_PlayMovie(const char *pMovieName, oeApplication *pApp) { MovieSoundDevice soundDevice; if (!mve_InitSound(pApp, soundDevice)) { mprintf(0, "Failed to initialize sound\n"); - close(hFile); + fclose(hFile); return MVELIB_INIT_ERROR; } int result = MVE_rmPrepMovie(hFile, -1, -1, 0); if (result != 0) { mprintf(0, "PrepMovie result = %d\n", result); - close(hFile); + fclose(hFile); mve_CloseSound(soundDevice); return MVELIB_INIT_ERROR; } @@ -370,7 +365,7 @@ int mve_PlayMovie(const char *pMovieName, oeApplication *pApp) { } // close our file handle - close(hFile); + fclose(hFile); // determine the return code int err = MVELIB_NOERROR; @@ -398,8 +393,8 @@ void *CallbackAlloc(uint32_t size) { return mem_malloc(size); } void CallbackFree(void *p) { mem_free(p); } -uint32_t CallbackFileRead(int hFile, void *pBuffer, uint32_t bufferCount) { - uint32_t numRead = read(hFile, pBuffer, bufferCount); +uint32_t CallbackFileRead(void *stream, void *pBuffer, uint32_t bufferCount) { + uint32_t numRead = fread(pBuffer, 1, bufferCount, (FILE *)stream); return (numRead == bufferCount) ? 1 : 0; } @@ -439,7 +434,7 @@ int NextPow2(int n) { void BlitToMovieBitmap(uint8_t *buf, uint32_t bufw, uint32_t bufh, uint32_t hicolor, bool usePow2Texture, int &texW, int &texH) { // get some sizes - int drawWidth = hicolor ? (bufw >> 1) : bufw; + int drawWidth = bufw; int drawHeight = bufh; if (usePow2Texture) { @@ -465,10 +460,8 @@ void BlitToMovieBitmap(uint8_t *buf, uint32_t bufw, uint32_t bufh, uint32_t hico for (int y = 0; y < drawHeight; ++y) { for (int x = 0; x < drawWidth; ++x) { uint16_t col16 = *wBuf++; - uint32_t b = ((col16 >> 11) & 0x1F) << 3; - uint32_t g = ((col16 >> 5) & 0x3F) << 2; - uint32_t r = ((col16 >> 0) & 0x1F) << 3; - pPixelData[x] = OPAQUE_FLAG | GR_RGB16(r, g, b); + // Convert to RGB555 + pPixelData[x] = col16 | OPAQUE_FLAG; } pPixelData += texW; @@ -492,7 +485,7 @@ void CallbackShowFrame(uint8_t *buf, uint32_t bufw, uint32_t bufh, uint32_t sx, BlitToMovieBitmap(buf, bufw, bufh, hicolor, true, texW, texH); // calculate UVs from texture - int drawWidth = hicolor ? (bufw >> 1) : bufw; + int drawWidth = bufw; int drawHeight = bufh; float u = float(drawWidth - 1) / float(texW - 1); float v = float(drawHeight - 1) / float(texH - 1); @@ -524,7 +517,7 @@ void CallbackShowFrame(uint8_t *buf, uint32_t bufw, uint32_t bufh, uint32_t sx, } #endif -intptr_t mve_SequenceStart(const char *mvename, int *fhandle, oeApplication *app, bool looping) { +intptr_t mve_SequenceStart(const char *mvename, void *fhandle, oeApplication *app, bool looping) { #ifndef NO_MOVIES // first, find that movie.. std::filesystem::path real_name; @@ -532,22 +525,22 @@ intptr_t mve_SequenceStart(const char *mvename, int *fhandle, oeApplication *app real_name = mve_FindMovieFileRealName(mvename); if (real_name.empty()) { mprintf(0, "MOVIE: No such file %s\n", mvename); - *fhandle = -1; + fhandle = nullptr; return 0; } #else real_name = mvename; #endif - int hfile = open(real_name.u8string().c_str(), O_RDONLY | O_BINARY); + FILE *hfile = fopen(real_name.u8string().c_str(), "rb"); - if (hfile == -1) { + if (hfile == nullptr) { mprintf(1, "MOVIE: Unable to open %s\n", real_name.u8string().c_str()); - *fhandle = -1; + fhandle = nullptr; return 0; } // setup - MVE_rmFastMode(MVE_RM_NORMAL); + //MVE_rmFastMode(MVE_RM_NORMAL); MVE_memCallbacks(CallbackAlloc, CallbackFree); MVE_ioCallbacks(CallbackFileRead); InitializePalette(); @@ -557,14 +550,15 @@ intptr_t mve_SequenceStart(const char *mvename, int *fhandle, oeApplication *app // let the render know we will be copying bitmaps to framebuffer (or something) rend_SetFrameBufferCopyState(true); - *fhandle = hfile; - return (intptr_t)MVE_frOpen(CallbackFileRead, hfile, NULL); + fhandle = hfile; + // TODO return (intptr_t)MVE_frOpen(CallbackFileRead, hfile, NULL); + return 0; #else return 0; #endif } -intptr_t mve_SequenceFrame(intptr_t handle, int fhandle, bool sequence, int *bm_handle) { +intptr_t mve_SequenceFrame(intptr_t handle, void *fhandle, bool sequence, int *bm_handle) { #ifndef NO_MOVIES if (bm_handle) { *bm_handle = -1; @@ -581,14 +575,14 @@ reread_frame: // get the next frame of data uint8_t *pBuffer = NULL; - err = MVE_frGet((MVE_frStream)handle, &pBuffer, &sw, &sh, &hicolor); + // TODO err = MVE_frGet((MVE_frStream)handle, &pBuffer, &sw, &sh, &hicolor); // refresh our palette { uint32_t palstart = 0; uint32_t palcount = 0; uint8_t *pal = NULL; - MVE_frPal((MVE_frStream)handle, &pal, &palstart, &palcount); + // TODO MVE_frPal((MVE_frStream)handle, &pal, &palstart, &palcount); CallbackSetPalette(pal, palstart, palcount); } @@ -605,13 +599,9 @@ reread_frame: } if (Movie_looping && err == MVE_ERR_EOF) { - MVE_frClose((MVE_frStream)handle); -#ifdef WIN32 - _lseek(fhandle, 0, SEEK_SET); -#else - lseek(fhandle, 0, SEEK_SET); -#endif - handle = (intptr_t)MVE_frOpen(CallbackFileRead, fhandle, NULL); + // TODO MVE_frClose((MVE_frStream)handle); + fseek((FILE *)fhandle, 0, SEEK_SET); + // TODO handle = (intptr_t)MVE_frOpen(CallbackFileRead, (FILE *)fhandle, NULL); sequence = true; goto reread_frame; } @@ -622,14 +612,14 @@ reread_frame: #endif } -bool mve_SequenceClose(intptr_t hMovie, int hFile) { +bool mve_SequenceClose(intptr_t hMovie, void *hFile) { #ifndef NO_MOVIES if (hMovie == -1) return false; - MVE_frClose((MVE_frStream)hMovie); + // TODO MVE_frClose((MVE_frStream)hMovie); MVE_ReleaseMem(); - close(hFile); + fclose((FILE *)hFile); // free our bitmap if (Movie_bm_handle != -1) {