From b56216f8758700487d636f1c975f928eb6b69e7e Mon Sep 17 00:00:00 2001 From: Gregory Montoir Date: Mon, 23 Apr 2018 08:48:15 +0800 Subject: [PATCH] Move color conversion to textureconvert --- Makefile | 2 +- Makefile.mingw | 2 +- cutscenepsx.cpp | 26 ++++---------------------- resourcepsx.cpp | 8 +++----- texturecache.cpp | 15 ++++----------- textureconvert.cpp | 31 +++++++++++++++++++++++++++++++ textureconvert.h | 10 ++++++++++ 7 files changed, 54 insertions(+), 40 deletions(-) create mode 100644 textureconvert.cpp create mode 100644 textureconvert.h diff --git a/Makefile b/Makefile index 7896685..a7cd6b5 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ LDFLAGS += $(LTO) SRCS = cabinet.cpp camera.cpp collision.cpp cutscene.cpp cutscenepsx.cpp decoder.cpp file.cpp \ font.cpp game.cpp icons.cpp input.cpp installer.cpp inventory.cpp main.cpp menu.cpp mixer.cpp \ opcodes.cpp raycast.cpp render.cpp resource.cpp resourcepsx.cpp saveload.cpp scaler.cpp \ - screenshot.cpp sound.cpp spritecache.cpp stub.cpp texturecache.cpp \ + screenshot.cpp sound.cpp spritecache.cpp stub.cpp texturecache.cpp textureconvert.cpp \ trigo.cpp util.cpp xmiplayer.cpp OBJS = $(SRCS:.cpp=.o) diff --git a/Makefile.mingw b/Makefile.mingw index 045ebfe..26c53ce 100644 --- a/Makefile.mingw +++ b/Makefile.mingw @@ -18,7 +18,7 @@ LIBS = $(FFMPEG_LIBS) $(SDL_LIBS) -lopengl32 -lWildMidi.dll -lfluidsynth.dll SRCS = cabinet.cpp camera.cpp collision.cpp cutscene.cpp cutscenepsx.cpp decoder.cpp file.cpp \ font.cpp game.cpp icons.cpp input.cpp installer.cpp inventory.cpp main.cpp menu.cpp mixer.cpp \ opcodes.cpp raycast.cpp render.cpp resource.cpp resourcepsx.cpp saveload.cpp scaler.cpp \ - screenshot.cpp sound.cpp spritecache.cpp stub.cpp texturecache.cpp \ + screenshot.cpp sound.cpp spritecache.cpp stub.cpp texturecache.cpp textureconvert.cpp \ trigo.cpp util.cpp xmiplayer.cpp OBJS = $(SRCS:.cpp=.o) diff --git a/cutscenepsx.cpp b/cutscenepsx.cpp index b8c6845..d6f492b 100644 --- a/cutscenepsx.cpp +++ b/cutscenepsx.cpp @@ -2,6 +2,7 @@ #include "cutscenepsx.h" #include "render.h" #include "sound.h" +#include "textureconvert.h" // // FFmpeg @@ -90,28 +91,6 @@ void DpsDecoder::initMdec(int w, int h) { } } -static uint32_t yuv420_to_rgba(int Y, int U, int V) { - float R = Y + 1.402 * (V - 128); - if (R < 0) { - R = 0; - } else if (R > 255) { - R = 255; - } - float G = Y - 0.344 * (U - 128) - 0.714 * (V - 128); - if (G < 0) { - G = 0; - } else if (G > 255) { - G = 255; - } - float B = Y + 1.772 * (U - 128); - if (B < 0) { - B = 0; - } else if (B > 255) { - B = 255; - } - return 0xFF000000 | (((uint8_t)B) << 16) | (((uint8_t)G) << 8) | ((uint8_t)R); -} - void DpsDecoder::decodeMdec(const uint8_t *data, int size) { AVPacket pkt; av_new_packet(&pkt, size); @@ -258,6 +237,9 @@ bool CutscenePsx::readSector() { } bool CutscenePsx::play() { + if (!_fp) { + return false; + } bool err = false; // demux audio and video frames uint8_t *videoData = 0; diff --git a/resourcepsx.cpp b/resourcepsx.cpp index 930cea5..d8cdb1e 100644 --- a/resourcepsx.cpp +++ b/resourcepsx.cpp @@ -1,5 +1,6 @@ #include "resourcepsx.h" +#include "textureconvert.h" static const char *_levels[] = { "1", "2a", "2b", "2c", "3", "4a", "4b", "4c", "5a", "5b", "5c", "6a", "6b" @@ -33,11 +34,8 @@ void ResourcePsx::loadLevelData(int level, int resType) { for (int y = 0; y < kVrmLoadingScreenHeight; ++y) { uint8_t *dst = _vrmLoadingBitmap + (kVrmLoadingScreenHeight - 1 - y) * dstPitch; for (int x = 0; x < kVrmLoadingScreenWidth; ++x) { - const uint16_t color = fileReadUint16LE(fp); - *dst++ = ( color & 31) << 3; - *dst++ = ((color >> 5) & 31) << 3; - *dst++ = ((color >> 10) & 31) << 3; - *dst++ = 255; + const uint32_t color = bgr555_to_rgba(fileReadUint16LE(fp)); + memcpy(dst + x * 4, &color, 4); } } h -= kVrmLoadingScreenHeight; diff --git a/texturecache.cpp b/texturecache.cpp index 8f158a6..f4bcee4 100644 --- a/texturecache.cpp +++ b/texturecache.cpp @@ -11,25 +11,18 @@ #include "scaler.h" #include "texturecache.h" -static const int kDefaultTexBufSize = 320 * 200; +static const int kLutTextureBufferSize = 320 * 200; -uint16_t convert_RGBA_5551(int r, int g, int b) { +static uint16_t convert_RGBA_5551(int r, int g, int b) { return ((r >> 3) << 11) | ((g >> 3) << 6) | ((b >> 3) << 1) | 1; } -uint16_t convert_BGRA_1555(int r, int g, int b) { - return 0x8000 | ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3); -} - static const struct { int internal; int format; int type; uint16_t (*convertColor)(int, int, int); } _formats[] = { -#ifdef __amigaos4__ - { GL_RGB5_A1, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, &convert_BGRA_1555 }, -#endif #ifdef USE_GLES { GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, &convert_RGBA_5551 }, #else @@ -107,7 +100,7 @@ void TextureCache::init(const char *filter, const char *scaler) { } } if (_scalers[_scaler].factor != 1) { - _texBuf = (uint16_t *)malloc(kDefaultTexBufSize * sizeof(uint16_t)); + _texBuf = (uint16_t *)malloc(kLutTextureBufferSize * sizeof(uint16_t)); } } @@ -186,7 +179,7 @@ void TextureCache::convertTexture(const uint8_t *src, int w, int h, const uint16 dst += dstPitch; } } else { - assert(w * h <= kDefaultTexBufSize); + assert(w * h <= kLutTextureBufferSize); int offset = 0; for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { diff --git a/textureconvert.cpp b/textureconvert.cpp new file mode 100644 index 0000000..daacd6b --- /dev/null +++ b/textureconvert.cpp @@ -0,0 +1,31 @@ + +#include "textureconvert.h" + +uint32_t yuv420_to_rgba(int Y, int U, int V) { + float R = Y + 1.402 * (V - 128); + if (R < 0) { + R = 0; + } else if (R > 255) { + R = 255; + } + float G = Y - 0.344 * (U - 128) - 0.714 * (V - 128); + if (G < 0) { + G = 0; + } else if (G > 255) { + G = 255; + } + float B = Y + 1.772 * (U - 128); + if (B < 0) { + B = 0; + } else if (B > 255) { + B = 255; + } + return 0xFF000000 | (((uint8_t)B) << 16) | (((uint8_t)G) << 8) | ((uint8_t)R); +} + +uint32_t bgr555_to_rgba(uint16_t color) { + const uint8_t r = ( color & 31) << 3; + const uint8_t g = ((color >> 5) & 31) << 3; + const uint8_t b = ((color >> 10) & 31) << 3; + return 0xFF000000 | (b << 16) | (g << 8) | r; +} diff --git a/textureconvert.h b/textureconvert.h new file mode 100644 index 0000000..543b902 --- /dev/null +++ b/textureconvert.h @@ -0,0 +1,10 @@ + +#ifndef TEXTURECONVERT_H__ +#define TEXTURECONVERT_H__ + +#include "util.h" + +uint32_t yuv420_to_rgba(int Y, int U, int V); +uint32_t bgr555_to_rgba(uint16_t color); + +#endif // TEXTURECONVERT_H__