From b496ec643b0127f36bd5c5eec6b9a96b5adbd313 Mon Sep 17 00:00:00 2001 From: Phillip Stephens Date: Tue, 15 Mar 2016 16:44:59 -0700 Subject: [PATCH] More CRasterFont work, CParticleElectric stub --- Runtime/CFactoryMgr.cpp | 2 +- Runtime/GuiSys/CRasterFont.cpp | 67 ++++++++++++++++++++++++ Runtime/GuiSys/CRasterFont.hpp | 70 ++++++++++++++++++++------ Runtime/IOStreams.cpp | 40 +++++++++++++++ Runtime/IOStreams.hpp | 8 ++- Runtime/Particle/CParticleElectric.cpp | 4 ++ Runtime/Particle/CParticleElectric.hpp | 2 + 7 files changed, 175 insertions(+), 18 deletions(-) diff --git a/Runtime/CFactoryMgr.cpp b/Runtime/CFactoryMgr.cpp index 7ff38e143..48ac66cf0 100644 --- a/Runtime/CFactoryMgr.cpp +++ b/Runtime/CFactoryMgr.cpp @@ -28,7 +28,7 @@ CFactoryFnReturn CFactoryMgr::MakeObjectFromMemory(const SObjectTag& tag, void* } else { - athena::io::MemoryReader r(buf, size); + CMemoryInStream r(buf, size); return search->second(tag, r, paramXfer); } } diff --git a/Runtime/GuiSys/CRasterFont.cpp b/Runtime/GuiSys/CRasterFont.cpp index b8944e74f..1357ab752 100644 --- a/Runtime/GuiSys/CRasterFont.cpp +++ b/Runtime/GuiSys/CRasterFont.cpp @@ -4,6 +4,73 @@ namespace urde { CRasterFont::CRasterFont(urde::CInputStream& in, urde::IObjectStore& store) { + u32 magic = in.readUint32Big(); + if (magic != SBIG('FONT')) + return; + + u32 version = in.readUint32Big(); + x4_monoWidth = in.readUint32Big(); + x8_monoHeight = in.readUint32Big(); + + if (version >= 1) + x8c_baseline = in.readUint32Big(); + else + x8c_baseline = x8_monoHeight; + + if (version >= 2) + x90_ = in.readUint32Big(); + + bool tmp1 = in.readBool(); + bool tmp2 = in.readBool(); + + u32 tmp3 = in.readUint32Big(); + u32 tmp4 = in.readUint32Big(); + std::string name= in.readString(); + u32 txtrId = in.readUint32Big(); + x80_texture = store.GetObj({'TXTR', txtrId}); + x30_fontInfo = CFontInfo(tmp1, tmp2, tmp3, tmp4, name.c_str()); + u32 mode = in.readUint32Big(); + /* TODO: Make an enum */ + if (mode == 1) + x2c_mode = 1; + else if (mode == 0) + x2c_mode = 0; + + u32 glyphCount = in.readUint32Big(); + xc_glyphs.reserve(glyphCount); + + for (u32 i = 0 ; i < glyphCount ; ++i) + { + wchar_t chr = in.readUint16Big(); + float startU = in.readFloatBig(); + float startV = in.readFloatBig(); + float endU = in.readFloatBig(); + float endV = in.readFloatBig(); + s32 a = in.readInt32Big(); + s32 b = in.readInt32Big(); + s32 c = in.readInt32Big(); + s32 cellWidth = in.readInt32Big(); + s32 cellHeight = in.readInt32Big(); + s32 baseline = in.readInt32Big(); + s32 kernStart = in.readUint32(); + xc_glyphs[i] = std::make_pair(chr, CGlyph(a, b, c, startU, startV, endU, endV, + cellWidth, cellHeight, baseline, kernStart)); + } + + std::sort(xc_glyphs.begin(), xc_glyphs.end(), [=](auto& a, auto& b) -> bool{ + return a.first < b.first; + }); + + u32 kernCount = in.readUint32Big(); + x1c_kerning.reserve(kernCount); + + for (u32 i = 0 ; i < kernCount ; ++i) + { + wchar_t first = in.readUint16Big(); + wchar_t second = in.readUint16Big(); + s32 howMuch = in.readUint32Big(); + x1c_kerning[i] = CKernPair(first, second, howMuch); + } } std::unique_ptr FRasterFontFactory(const SObjectTag& tag, CInputStream& in, const CVParamTransfer& vparms) diff --git a/Runtime/GuiSys/CRasterFont.hpp b/Runtime/GuiSys/CRasterFont.hpp index dd30d7d0f..0f3cd9e97 100644 --- a/Runtime/GuiSys/CRasterFont.hpp +++ b/Runtime/GuiSys/CRasterFont.hpp @@ -12,10 +12,10 @@ class CTexture; class CGlyph { - wchar_t x0_char; - s16 x2_leftPadding; - s16 x4_advance; - s16 x6_rightPadding; +private: + s16 x0_leftPadding; + s16 x2_advance; + s16 x4_rightPadding; float x8_startU; float xc_startV; float x10_endU; @@ -24,10 +24,19 @@ class CGlyph s16 x1a_cellHeight; s16 x1c_baseline; s16 x1e_kernStart; +public: + CGlyph() = default; + CGlyph(s16 a, s16 b, s32 c, float startU, float startV, float endU, float endV, + s16 cellWidth, s16 cellHeight, s16 baseline, s16 kernStart) + : x0_leftPadding(a), x2_advance(b), x4_rightPadding(c), + x8_startU(startU), xc_startV(startV), x10_endU(endU), x14_endV(endV), + x18_cellWidth(cellWidth), x1a_cellHeight(cellHeight), + x1c_baseline(baseline), x1e_kernStart(kernStart) + {} - s16 GetA() const { return x2_leftPadding; } - s16 GetB() const { return x4_advance; } - s16 GetC() const { return x6_rightPadding; } + s16 GetA() const { return x0_leftPadding; } + s16 GetB() const { return x2_advance; } + s16 GetC() const { return x4_rightPadding; } float GetStartU() const { return x8_startU; } float GetStartV() const { return xc_startV; } float GetEndU() const { return x10_endU; } @@ -36,38 +45,67 @@ class CGlyph s16 GetCellHeight() const { return x1a_cellHeight; } s16 GetBaseline() const { return x1c_baseline; } s16 GetKernStart() const { return x1e_kernStart; } + }; -struct CKernPair +class CKernPair { +private: wchar_t x0_first; wchar_t x2_second; s32 x4_howMuch; +public: + CKernPair() = default; + CKernPair(wchar_t first, wchar_t second, s32 howMuch) + : x0_first(first), x2_second(second), x4_howMuch(howMuch) + {} + wchar_t GetFirst() const { return x0_first; } wchar_t GetSecond() const { return x2_second; } s32 GetHowMuch() const { return x4_howMuch; } }; -class CRasterFont +class CFontInfo { bool x0_ = false; + bool x1_ = false; + s32 x4_ = 0; + s32 x8_fontSize = 0; + char* xc_name = 0; + +public: + CFontInfo() = default; + CFontInfo(bool a, bool b, s32 c, s32 fontSize, const char* name) + : x0_(a), x1_(b), x4_(c), x8_fontSize(fontSize) + { + strcpy(xc_name, name); + } +}; + +class CRasterFont +{ s32 x4_monoWidth = 16; s32 x8_monoHeight = 16; - std::unordered_map xc_glyphs; + std::vector> xc_glyphs; std::vector x1c_kerning; - s32 x2c_mode; - s32 x30_; + s32 x28_lineMargin = 0; + s32 x2c_mode = 0; + CFontInfo x30_fontInfo; TToken x80_texture; + bool x88_ = false; s32 x8c_baseline; - s32 x90_; + s32 x90_ = 0; + char* fontName; CGlyph* InternalGetGlyph(wchar_t chr) { - if (xc_glyphs.find(chr) == xc_glyphs.end()) - return nullptr; + u32 i = 0; + for (; i < xc_glyphs.size(); ++i) + if (chr == xc_glyphs[i].first) + break; - return &xc_glyphs[chr]; + return &xc_glyphs[i].second; } public: diff --git a/Runtime/IOStreams.cpp b/Runtime/IOStreams.cpp index df0697066..c1200edde 100644 --- a/Runtime/IOStreams.cpp +++ b/Runtime/IOStreams.cpp @@ -4,6 +4,46 @@ namespace urde { +s32 DecryptionCtx::DecryptRead(CInputStream& in, s32 key) +{ + int ret = 0; + if (x20_encShift >= key) + { + int diff = x20_encShift - 0x20; + int baseVal = -1; + if (x20_encShift != 0x20) + baseVal = 1 << (x20_encShift - 1); + x20_encShift = key - x20_encShift; + ret = baseVal | (x1c_encVal >> diff); + } + else + { + int diff = x20_encShift - key; + int rem = x20_encShift - 0x20; + int baseVal1 = -1; + if (x20_encShift != 0x20) + baseVal1 = 1 << (x20_encShift - 1); + + int bit = diff & 7; + x20_encShift = 0; + int count = (diff >> 3) + ((-bit | bit) >> 31); + int baseVal2 = (baseVal1 & (x1c_encVal >> rem)) << diff; + in.readBytesToBuf(&x1c_encVal, count); + + int baseVal3 = -1; + if (diff != 0x20) + baseVal3 = 1 << (diff - 1); + + int tmpShift = x20_encShift; + x20_encShift = (count << 3); + ret = baseVal2 | (baseVal3 & (x1c_encVal >> (diff - 0x20))) << tmpShift; + x20_encShift = diff - x20_encShift; + x1c_encVal = x1c_encVal >> diff; + } + + return ret; +} + class CZipSupport { public: diff --git a/Runtime/IOStreams.hpp b/Runtime/IOStreams.hpp index 31655f6bb..cbc1586de 100644 --- a/Runtime/IOStreams.hpp +++ b/Runtime/IOStreams.hpp @@ -9,10 +9,16 @@ namespace urde { - using CInputStream = athena::io::IStreamReader; using COutputStream = athena::io::IStreamWriter; +struct DecryptionCtx +{ + s32 x1c_encVal; + s32 x20_encShift; + s32 DecryptRead(CInputStream& in, s32 key); +}; + using CMemoryInStream = athena::io::MemoryReader; class CZipInputStream : public CInputStream diff --git a/Runtime/Particle/CParticleElectric.cpp b/Runtime/Particle/CParticleElectric.cpp index ed7d684b4..85ebe7f81 100644 --- a/Runtime/Particle/CParticleElectric.cpp +++ b/Runtime/Particle/CParticleElectric.cpp @@ -10,6 +10,10 @@ namespace urde { +void CParticleElectric::RenderSwooshes() +{ +} + CParticleElectric::CParticleElectric(const TToken& token) : x1c_elecDesc(token) { diff --git a/Runtime/Particle/CParticleElectric.hpp b/Runtime/Particle/CParticleElectric.hpp index 661afe357..8fadd2329 100644 --- a/Runtime/Particle/CParticleElectric.hpp +++ b/Runtime/Particle/CParticleElectric.hpp @@ -70,6 +70,8 @@ private: }; u8 dummy = 0; }; + + void RenderSwooshes(); public: