From 932e6ed82fcd4afeecd627a4e2922b7ef59a23e0 Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Sun, 1 Nov 2026 18:34:36 -0600 Subject: [PATCH] feat(texture): wrap modes and tiling texture:SetTexture(file, wrapModeHorizontal, wrapModeVertical), the XML hWrapMode / vWrapMode attributes, texture:SetHorizTile / GetHorizTile / SetVertTile / GetVertTile and the XML horizTile / vertTile attributes. Wrap. 1.12 fixes a texture's wrap when its file loads. CSimpleTexture::SetTexture (0x00770200) builds the flags word with CGxTexFlags (0x0058A980) from one wrap argument for both axes, and TextureCreate (0x00449D90) caches the handle under the name and that word, so a repeating copy of a file is its own shared texture. The device reads one bit per axis (3 for U, 4 for V), repeat or clamp, so the mirror and border modes clamp. A texture with a mode or a tile flag loads through a co-hook on that SetTexture: the same body, with a repeat per axis. The engine's same-file early-out (0x0044AC70) ignores flags, so the co-hook also compares the held image's wrap (its device texture's flags at +0x3C, or a loading BLP handle's at +0x150), and for the same file it swaps in the other copy and keeps the pixel shader. Changing a mode or a tile flag reloads the held file the same way. The Lua method is registered again so it stores the wrap arguments before the engine handler (0x0079BB40) runs: true or "REPEAT" repeats, anything else clamps. Tiling. A tiled axis repeats, and each non-zero coordinate on it becomes the texture's length in script units over the image's size in texels. The script units are the engine's own coordinate push: DivideByScreenScaleX (0x0041AE40) of aspect (0x0041AD70) x 1024 x length, over the effective scale. The texels are the device texture's width and height (+0x24 / +0x28): the file's size, halved only past the GPU limit and untouched by BaseMip. The layer batch keeps a pointer to the eight coordinates (0x00772FD0), so the count is written whenever an input changes: after each corner store, after both native SetTexCoord forms (0x00770410, 0x007704C0), on a tile change, and after CSimpleTexture::Emit (0x007706E0), whose device lookup loads the image. A tile change counts against the held image before the repeating copy of the same file replaces it, since the copy has no size until it is drawn. A texture showing an atlas does not tile (the atlases are parts of their files), and neither does one with SetTexCoordModifiesRect on. SetAtlas clears modes and tiling. A tiled status bar texture takes StatusBar::Fill's anchored layout instead of 1.12's crop, which would size it by the count, and its fill sets no coordinates. An XML that moves a tiled axis off 0..1 logs "Invalid TexCoords value (horizTile is on)" at error level (new Xml::Error) and falls back to the whole image. Also corrects the AtlasData.h note on the atlas tile flags: 4 is horizontal and 2 is vertical. --- docs/API.md | 71 ++++++++++ src/Offsets.h | 69 ++++++++- src/statusbar/Fill.cpp | 34 ++++- src/texture/Atlas.cpp | 8 ++ src/texture/Atlas.h | 4 + src/texture/AtlasData.h | 10 +- src/texture/Construct.h | 2 +- src/texture/CornerStore.h | 4 +- src/texture/Load.cpp | 15 +- src/texture/Load.h | 12 +- src/texture/Tile.cpp | 262 ++++++++++++++++++++++++++++++++++ src/texture/Wrap.cpp | 288 ++++++++++++++++++++++++++++++++++++++ src/texture/Wrap.h | 41 ++++++ src/xml/FrameLoad.cpp | 17 ++- src/xml/FrameLoad.h | 3 + 15 files changed, 807 insertions(+), 33 deletions(-) create mode 100644 src/texture/Tile.cpp create mode 100644 src/texture/Wrap.cpp create mode 100644 src/texture/Wrap.h diff --git a/docs/API.md b/docs/API.md index 061407f9..3fc4b866 100644 --- a/docs/API.md +++ b/docs/API.md @@ -359,6 +359,8 @@ build instructions. - [`texture:GetAtlas()`](#texturegetatlas) - [`texture:ResetTexCoord()`](#textureresettexcoord) - [`texture:SetSpriteSheetCell(cell, numRows, numColumns)`](#texturesetspritesheetcellcell-numrows-numcolumns) + - [`texture:SetTexture(file [, wrapModeHorizontal, wrapModeVertical])`](#texturesettexturefile--wrapmodehorizontal-wrapmodevertical) + - [`texture:SetHorizTile(tiling)` / `texture:SetVertTile(tiling)`](#texturesethoriztiletiling--texturesetverttiletiling) - [`texture:SetDesaturation(amount)`](#texturesetdesaturationamount) - [`texture:GetDesaturation()`](#texturegetdesaturation) - [`IsDesaturateSupported()`](#isdesaturatesupported) @@ -8571,6 +8573,7 @@ After `SetAtlas`, texture coordinates address the art and not the file it sits in, so `0` to `1` covers the whole of the art. `SetTexCoord(0, 0.5, 0, 0.5)` shows the top-left quarter of the art, and `GetTexCoord` reads back what you asked for. `SetTexture` points the texture at a file again and clears the atlas. +`SetAtlas` turns tiling off and sets both wrap modes to `"CLAMP"`. When the name is not known, the texture keeps whatever it was showing. Call `_classicapi_DumpAtlasMisses()` to list the names that were asked for and not @@ -8622,6 +8625,74 @@ t:SetTexture("Interface\\TargetingFrame\\UI-RaidTargetingIcons") t:SetSpriteSheetCell(8, 4, 4) ``` +### `texture:SetTexture(file [, wrapModeHorizontal, wrapModeVertical])` + +Points the texture at an image file. The two optional arguments set the wrap +mode of the horizontal and the vertical axis. The wrap mode decides what the +texture shows where its coordinates go past `0` or `1`: + +- `"REPEAT"` or `true` repeats the image. +- `"CLAMP"`, `false` or `nil` stretches the pixels at the edge of the image. + This is the default. + +`"CLAMPTOBLACKADDITIVE"`, `"CLAMPTOBLACK"`, `"CLAMPTOWHITE"` and `"MIRROR"` +work the same as `"CLAMP"`. + +Each call with a file sets both modes again, so `SetTexture(file)` sets them +back to `"CLAMP"`. A color fill, `SetTexture(r, g, b)`, keeps the modes. An axis +with tiling on always repeats (see +[`texture:SetHorizTile`](#texturesethoriztiletiling--texturesetverttiletiling)). + +```lua +bg:SetTexture("Interface\\AddOns\\MyAddon\\stripes", "REPEAT", "REPEAT") +bg:SetTexCoord(0, 4, 0, 2) -- four copies across, two copies down +``` + +A `` element takes the same modes in its `hWrapMode` and `vWrapMode` +attributes: + +```xml + +``` + +### `texture:SetHorizTile(tiling)` / `texture:SetVertTile(tiling)` + +Turns tiling on or off for one axis. A tiled axis repeats the image at its own +size: one pixel of the image covers one unit of the texture's width or height. For example, +an image 32 pixels wide on a texture 100 units wide shows the image a little +more than three times across. The count changes when the texture changes size. +`GetHorizTile()` and `GetVertTile()` return `true` or `false`. + +Tiling sets each coordinate on that axis that is not `0` to the repeat count, +so `GetTexCoord` returns the count. The count needs the size of the image, so +for a new image it is set when the texture is first drawn. Tiling stays on when +`SetTexture` sets a new file. A texture that shows an atlas does not tile, and +neither does a texture with `SetTexCoordModifiesRect` on. + +A tiled status bar texture keeps the image at its own size as the bar fills. +The bar shows more or fewer copies and does not squeeze the image. + +```lua +local edge = frame:CreateTexture(nil, "BORDER") +edge:SetTexture("Interface\\AddOns\\MyAddon\\border-top") +edge:SetPoint("TOPLEFT") +edge:SetPoint("TOPRIGHT") +edge:SetHeight(16) +edge:SetHorizTile(true) +``` + +A `` element takes `horizTile` and `vertTile` attributes. A +`` child must keep a tiled axis at `0` and `1`, for example +`left="0" right="1"` with `horizTile`. Other values are an error in the XML log, +and the texture then shows the whole image. The other axis can take any values, +so one strip of a file can tile: + +```xml + + + +``` + ### `texture:SetDesaturation(amount)` Removes some or all of the color from a texture. `amount` is a number from `0` diff --git a/src/Offsets.h b/src/Offsets.h index cc7da514..ad5e3a06 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -862,6 +862,20 @@ enum Offsets { // texcoords (no v-flip — the engine region path is top-left origin). FUN_SIMPLETEXTURE_SET_TEXCOORD = 0x00770410, OFF_SIMPLETEXTURE_HTEXTURE = 0xCC, // owned HTEXTURE ref (diagnostic) + // The eight coordinates, UL, LL, UR, LR as (x, y) pairs — what GetTexCoord + // pushes (0x0079BDF0) and both SetTexCoord forms write. The layer batch + // keeps a pointer to them (FUN_00772FD0 0x00773020) and reads them on + // every draw. + OFF_SIMPLETEXTURE_TEXCOORDS = 0x104, + // CSimpleTexture::Emit — `void __thiscall(texture, batch)`, `ret 4`, the + // layer-batch rebuild's per-texture call (4.3.4's CSimpleTexture::Draw): + // with an image it runs the quad append FUN_00772FD0, whose device-texture + // lookup loads the texture, so after it the device texture exists. + FUN_SIMPLETEXTURE_EMIT = 0x007706E0, + // CScriptRegion::NotifyLayerChanged — `void __thiscall(region)`: marks the + // region's layer for a batch rebuild. SetTexture (0x007702E2) and SetImage + // (0x00770300) call it after storing a new image. + FUN_REGION_NOTIFY_LAYER_CHANGED = 0x0077FDF0, // The four drawn-quad corner POSITIONS: 4 vertices of {x, y, z} floats (0xC // stride), order BL, TL, BR, TR (from FUN_REGION_STORE_CORNERS's writes). The // region draw enqueue FUN_00772fd0 (0x00772fd0) hands region+0xD4 straight to @@ -879,10 +893,10 @@ enum Offsets { // its screen rect with NO anchors (anchoring 100+ textures/frame corrupts the // UI-manager pending-layout list — the FUN_00765650 crash). Hooked by // Texture::CornerStore, whose listeners re-apply rotation - // (Texture::Transform) right after the engine restores the axis-aligned - // corners (layout resolve / SetTexCoord). Writes only the corners: - // nothing marks the layer changed, since the layer batch reads them - // through a pointer on every draw. + // (Texture::Transform) and the repeat count (Texture::Tile) right after + // the engine restores the axis-aligned corners (layout resolve / + // SetTexCoord). Writes only the corners: nothing marks the layer changed, + // since the layer batch reads them through a pointer on every draw. FUN_REGION_STORE_CORNERS = 0x007705B0, // CSimpleTexture::OnFrameSizeChanged — `__thiscall(texture+OFF_REGION_ANCHOR, // const float *oldRect)`, the layout callback: lays out dependents, then @@ -12011,6 +12025,7 @@ enum Offsets { // names them: 0 info, 1 warning, 2 error, 3 fatal. VTBL_XML_STATUS_ADD = 0x0C, XML_STATUS_WARNING = 1, + XML_STATUS_ERROR = 2, // `float __stdcall SStrToFloat(const char *)` (RET 4, x87 result) — the // frame loader's `alpha` conversion (0x00769A10). FUN_SSTR_TO_FLOAT = 0x0064AAA0, @@ -12501,8 +12516,54 @@ enum Offsets { GXTEX_FORMAT_ARGB8888 = 2, GXTEX_CMD_LATCH = 1, PTR_TEXLOAD_DESC_VTBL = 0x007FFA10, + // CGxTexFlags::CGxTexFlags — `void *__thiscall(flags, filter, wrapU, + // wrapV, 0, 0, 0, anisotropy, 0)`. CSimpleTexture::SetTexture builds its + // word with (filter, wrap, wrap, 0, 0, 0, 1, 0) (0x00770241..0x00770270), + // one wrap argument for both axes. The bits: 0..2 the filter, 3 repeat in + // U, 4 repeat in V — the OpenGL backend's ApplyTextureParams (0x0059F170) + // maps `& 8` / `& 0x10` to GL_REPEAT or a clamp, one bit per axis, so the + // device has no mirror or border mode (benilla-re gx/textures, verified). FUN_GX_TEXFLAGS_INIT = 0x0058A980, + GXTEX_FLAG_FILTER_MASK = 0x7, + GXTEX_FLAG_WRAP_U = 0x8, + GXTEX_FLAG_WRAP_V = 0x10, + // GetTextureDeviceObject — `CGxTex *__fastcall(HTEXTURE, int load, void + // *callback)`: [hTex + OFF_HTEXTURE_DEVICE], or with `load` set the + // texture is first waited for (0x0044AD03), so 0 with `load` clear means + // not loaded yet. FUN_TEXTURE_GET_RENDERABLE = 0x0044ACF0, + // The HTEXTURE object (CTexture, 0x15C bytes, benilla-re gx/textures): + // its file name as loaded (the path, extension kept), a source word whose + // bit 1 the file loaders TextureCreateFromBlp (0x0044A310), + // TextureCreateFromTga (0x0044A140) and TextureCreateFromFile (0x00449610) + // set for a load with the file flag CSimpleTexture::SetTexture always + // passes (a solid colour or a pixel source never has it), the device + // texture (0 until loaded) and, for a BLP only, the texture flags word it + // was created with (TextureCreateFromBlp stores it before queuing the + // read; the build ORs in 0x40 for mips). A TGA hands its word only to its + // device texture (OFF_GXTEX_FLAGS). + OFF_HTEXTURE_NAME = 0x0C, + OFF_HTEXTURE_SOURCE = 0x110, + HTEXTURE_SOURCE_FILE = 0x2, + OFF_HTEXTURE_DEVICE = 0x140, + OFF_HTEXTURE_TEX_FLAGS = 0x150, + // TextureNameMatch — `int __fastcall(HTEXTURE, const char *name)`: 1 when + // the texture was made from a file of that name (extension ignored, any + // case), else 0. Flags play no part, so SetTexture's same-file early-out + // (0x0077021E) keeps a held image whatever wrap a new call asks for. + FUN_TEXTURE_NAME_MATCH = 0x0044AC70, + // The device texture record (CGxTex): its width and height, stored by the + // record constructor 0x00591600 from its arguments. They are the BLP's + // size after TextureLimitSize (0x00448B60), which halves only past the + // GPU's maximum texture size; the BaseMip skip ([device + 0x31C]) is + // applied at upload and never written here. 4.3.4's CRenderBatch:: + // QueueTexture reads the same `gxTex->m_width` for its tile count. + OFF_GXTEX_WIDTH = 0x24, + OFF_GXTEX_HEIGHT = 0x28, + // Its texture flags word (FUN_GX_TEXFLAGS_INIT bits), stored by 0x00591600 + // and read on every bind by ApplyTextureParams (0x0059F170): the wrap the + // device actually samples with. + OFF_GXTEX_FLAGS = 0x3C, // SysMsgAdd — `int __fastcall(CStatus *status, uint32_t level)`: hands a // status's error text to the system-message printer 0x0044CB20, which in // this build formats it and drops it (nothing reaches the screen; seen diff --git a/src/statusbar/Fill.cpp b/src/statusbar/Fill.cpp index 832a4128..9b2faf1d 100644 --- a/src/statusbar/Fill.cpp +++ b/src/statusbar/Fill.cpp @@ -30,6 +30,10 @@ // off pins the texture to the bar again and gives it back its crop flag, as // InstallBarTexture and SetBarTexture leave it. // +// A tiled bar texture (Texture::Tile) takes the same layout, since the crop +// would size it by its repeat count, and keeps its coordinates, as 4.3.4's +// OnLayerUpdate sets none for a texture with a tile flag on. +// // The retail fill styles (Enum.StatusBarFillStyle) fit the same layout: // Reverse is the reversed fill; Center insets both ends by half the empty // part and shows the middle of the image; StandardNoRangeFill draws a bar @@ -48,6 +52,7 @@ #include "Offsets.h" #include "baselib/Ascii.h" #include "frame/Destruction.h" +#include "texture/Wrap.h" #include "xml/FrameLoad.h" #include "xml/TypeLoad.h" @@ -148,7 +153,8 @@ void SetFullTexCoord(void *texture, bool rotates) { // 4.3.4's OnLayerUpdate body. [s0, s1] is the filled part of the bar along the // fill axis, from its start edge; [t0, t1] the part of the image drawn there. -void LayOut(void *bar, State &state) { +// A tiled texture keeps its coordinates, which its repeat count owns. +void LayOut(void *bar, State &state, bool tiled) { void *texture = BarTexture(bar); float width, height; BarSize(bar, width, height); @@ -168,22 +174,22 @@ void LayOut(void *bar, State &state) { float left = 0.0f, right = 0.0f, top = 0.0f, bottom = 0.0f; if (IsVertical(bar)) { - if (state.rotatesTexture) { + if (!tiled && state.rotatesTexture) { const float corners[8] = {t1, 0.0f, t0, 0.0f, t1, 1.0f, t0, 1.0f}; // UL, LL, UR, LR reinterpret_cast(Offsets::FUN_SIMPLETEXTURE_SET_TEXCOORD_CORNERS)(texture, corners); - } else { + } else if (!tiled) { const float rect[4] = {1.0f - t1, 0.0f, 1.0f - t0, 1.0f}; reinterpret_cast(Offsets::FUN_SIMPLETEXTURE_SET_TEXCOORD)(texture, rect); } bottom = s0 * height; top = -((1.0f - s1) * height); } else { - if (state.rotatesTexture) { + if (!tiled && state.rotatesTexture) { const float corners[8] = {0.0f, 1.0f - t0, 1.0f, 1.0f - t0, 0.0f, 1.0f - t1, 1.0f, 1.0f - t1}; reinterpret_cast(Offsets::FUN_SIMPLETEXTURE_SET_TEXCOORD_CORNERS)(texture, corners); - } else { + } else if (!tiled) { const float rect[4] = {0.0f, t0, 1.0f, t1}; reinterpret_cast(Offsets::FUN_SIMPLETEXTURE_SET_TEXCOORD)(texture, rect); } @@ -213,8 +219,22 @@ void Restore(void *bar) { } void __fastcall Update_h(void *bar, void *edx, float elapsed) { - const auto it = g_state.find(bar); + void *texture = BarTexture(bar); + const bool tiled = texture != nullptr && Texture::Wrap::Tiles(texture); + auto it = g_state.find(bar); if (it == g_state.end()) { + if (!tiled) { + g_origUpdate(bar, edx, elapsed); + return; + } + // 1.12's crop would size a tiled texture by its repeat count. + it = g_state.emplace(bar, State{}).first; + Game::Ref(texture, Offsets::OFF_REGION_TEXCOORD_MODIFIES_RECT) = 0; + it->second.owned = true; + Flags(bar) |= Offsets::VALUE_FLAG_CHANGED; + } else if (!it->second.Active() && !tiled) { + Restore(bar); + g_state.erase(it); g_origUpdate(bar, edx, elapsed); return; } @@ -233,7 +253,7 @@ void __fastcall Update_h(void *bar, void *edx, float elapsed) { } if (!changed) return; - LayOut(bar, state); + LayOut(bar, state, tiled); Flags(bar) &= ~Offsets::VALUE_FLAG_CHANGED; } diff --git a/src/texture/Atlas.cpp b/src/texture/Atlas.cpp index 20e228a7..299cb5fe 100644 --- a/src/texture/Atlas.cpp +++ b/src/texture/Atlas.cpp @@ -29,6 +29,7 @@ #include "Game.h" #include "Offsets.h" #include "debug/Log.h" +#include "texture/Wrap.h" #include "xml/FrameLoad.h" #include "xml/TypeLoad.h" @@ -321,6 +322,11 @@ int __fastcall Script_SetAtlas(void *L) { return 0; } + // Retail's SetAtlas re-applies the atlas's own wrap mode, which clears + // tiling the texture had. An atlas never tiles here (the built-in ones are + // parts of their files), so that leaves it clamped. + Texture::Wrap::Clear(region); + // Reshaping the stack for each delegated handler leaves `info` untouched — it // points into the registry, which none of these calls can reach. Game::Lua::SetTop(L, 1); // (self) @@ -570,6 +576,8 @@ const Xml::TypeLoad::AutoSubscribe _xmlSub{Xml::TypeLoad::TEXTURE, &OnTextureNod } // namespace +bool Shows(void *region) { return LiveEntry(region) != nullptr; } + const Info *Find(const char *name) { if (name == nullptr || *name == '\0') return nullptr; diff --git a/src/texture/Atlas.h b/src/texture/Atlas.h index a157e99e..271470a3 100644 --- a/src/texture/Atlas.h +++ b/src/texture/Atlas.h @@ -53,6 +53,10 @@ struct Info { // The returned pointer stays valid for the life of the process. const Info *Find(const char *name); +// True while `region` shows the atlas SetAtlas last gave it: its image has not +// been replaced since. +bool Shows(void *region); + // Records an unresolved atlas name for `_classicapi_DumpAtlasMisses()`. This is // how the built-in table grows: load a real addon, read what it actually asked // for, bind those names instead of guessing at thousands. diff --git a/src/texture/AtlasData.h b/src/texture/AtlasData.h index a3872581..7f74b4d1 100644 --- a/src/texture/AtlasData.h +++ b/src/texture/AtlasData.h @@ -71,10 +71,12 @@ // - 73 names repeat case-insensitively; keep the higher ID, the same rule // `ui/ColorData.h` documents for duplicate color tags. // -// `tilesHorizontally` / `tilesVertically` come from `CommittedFlags` bits 0 and 1. -// They are stored as resolved booleans rather than the raw flags word because the -// other set bits (4, 8, 16 appear in the data) have no known meaning and must not -// leak into the public contract. +// `tilesHorizontally` / `tilesVertically` come from `CommittedFlags` 4 and 2: +// 190 of the 194 members named `_...` (Blizzard's prefix for art that tiles +// across) carry 4, and 56 of the 60 named `!...` (tiles down) carry 2; 1 is +// set only on the nameless rows. They are stored as resolved booleans rather +// than the raw flags word because the other set bits (8, 16 appear in the data) +// have no known meaning and must not leak into the public contract. namespace Texture::AtlasData { diff --git a/src/texture/Construct.h b/src/texture/Construct.h index 6d076aca..690f9568 100644 --- a/src/texture/Construct.h +++ b/src/texture/Construct.h @@ -20,7 +20,7 @@ // address. // // MinHook allows ONE hook per address and more than one feature keeps state -// keyed by a texture region (Texture::Mask), so this module owns +// keyed by a texture region (Texture::Mask, Texture::Wrap), so this module owns // the hook and fans out. Declare a file-scope // static const Texture::Construct::AutoSubscribe _sub{&OnTextureConstructed}; namespace Texture::Construct { diff --git a/src/texture/CornerStore.h b/src/texture/CornerStore.h index afaa35c0..85ef146e 100644 --- a/src/texture/CornerStore.h +++ b/src/texture/CornerStore.h @@ -20,8 +20,8 @@ // Listeners run after the corners are written. // // MinHook allows ONE hook per address and more than one feature follows the -// quad (Texture::Transform's rotation), so this module owns the hook and fans -// out. Declare a file-scope +// quad (Texture::Transform's rotation, Texture::Tile's repeat count), so this +// module owns the hook and fans out. Declare a file-scope // static const Texture::CornerStore::AutoSubscribe _sub{&OnCornersStored}; namespace Texture::CornerStore { diff --git a/src/texture/Load.cpp b/src/texture/Load.cpp index eec9eb19..7b4478d3 100644 --- a/src/texture/Load.cpp +++ b/src/texture/Load.cpp @@ -55,14 +55,17 @@ void __fastcall LatchPixels(uint32_t command, uint32_t width, uint32_t /*height* } // namespace -uint32_t DefaultFlags() { +uint32_t Flags(uint32_t filter, bool wrapU, bool wrapV) { uint32_t flags = 0; - const uint32_t blend = Game::Read(Offsets::VAR_TEXTURE_BLEND_DEFAULT); - reinterpret_cast(Offsets::FUN_GX_TEXFLAGS_INIT)(&flags, blend, 0, 0, 0, 0, 0, - 1, 0); + reinterpret_cast(Offsets::FUN_GX_TEXFLAGS_INIT)(&flags, filter, wrapU ? 1 : 0, + wrapV ? 1 : 0, 0, 0, 0, 1, 0); return flags; } +uint32_t DefaultFlags() { + return Flags(Game::Read(Offsets::VAR_TEXTURE_BLEND_DEFAULT), false, false); +} + void ReleaseHandle(void *handle) { if (handle != nullptr) reinterpret_cast(Offsets::FUN_HANDLE_RELEASE)(handle); @@ -71,7 +74,7 @@ void ReleaseHandle(void *handle) { // Same failure test and cleanup as SetTexture: a status level of 2 or more // closes the green fallback and passes the status to the (silent) // system-message printer; the status is destroyed either way. -void *LoadByPath(const char *path) { +void *LoadByPath(const char *path, uint32_t flags) { TexLoadDesc desc; desc.vtbl = reinterpret_cast(Offsets::PTR_TEXLOAD_DESC_VTBL); desc.field4 = 8; @@ -79,7 +82,7 @@ void *LoadByPath(const char *path) { desc.fieldC = reinterpret_cast(&desc.self8) | 1u; desc.field10 = 0; void *handle = reinterpret_cast(reinterpret_cast( - Offsets::FUN_TEXTURE_LOAD_BY_PATH)(path, &desc, DefaultFlags(), 0, 1)); + Offsets::FUN_TEXTURE_LOAD_BY_PATH)(path, &desc, flags, 0, 1)); if (desc.field10 >= 2) { ReleaseHandle(handle); handle = nullptr; diff --git a/src/texture/Load.h b/src/texture/Load.h index 0638fd85..9fd77a26 100644 --- a/src/texture/Load.h +++ b/src/texture/Load.h @@ -19,14 +19,18 @@ namespace Texture { -// The loaded handle, or nullptr when the file does not load. Linear filter, -// clamped, the flags SetTexture's own call builds. The caller owns one -// reference and drops it with ReleaseHandle. -void *LoadByPath(const char *path); +// The texture flags word SetTexture's own call builds, with a filter and a +// repeat per axis (FUN_GX_TEXFLAGS_INIT). +uint32_t Flags(uint32_t filter, bool wrapU, bool wrapV); // SetTexture's texture flags: the default filter, clamped, one mip level. uint32_t DefaultFlags(); +// The loaded handle, or nullptr when the file does not load, made with +// `flags` (SetTexture's own by default). The caller owns one reference and +// drops it with ReleaseHandle. +void *LoadByPath(const char *path, uint32_t flags = DefaultFlags()); + // Drops one reference on a handle (FUN_HANDLE_RELEASE). Null is a no-op. void ReleaseHandle(void *handle); diff --git a/src/texture/Tile.cpp b/src/texture/Tile.cpp new file mode 100644 index 00000000..ce45544d --- /dev/null +++ b/src/texture/Tile.cpp @@ -0,0 +1,262 @@ +// This file is part of ClassicAPI. +// +// ClassicAPI is free software: you can redistribute it and/or modify it under the terms +// of the GNU General Public License as published by the Free Software Foundation, either +// version 3 of the License, or (at your option) any later version. +// +// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +// PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// ClassicAPI. If not, see . + +// `texture:SetHorizTile` / `GetHorizTile` / `SetVertTile` / `GetVertTile` and +// the XML `horizTile` / `vertTile` attributes: the image repeated at its own +// size across the texture, as 4.3.4 tiles it. +// +// 4.3.4 keeps a tile flag per axis. After a size change its layer batch build +// (CRenderBatch::QueueTexture) sets every non-zero coordinate on a tiled axis +// to the texture's length over the image's: the length in script units, +// DDCToNDCWidth(aspect * 1024 * length) / layoutScale, over the device +// texture's width or height in texels. One texel is one unit of GetWidth. +// +// 1.12's layer batch keeps a pointer to the eight coordinates and reads them +// on every draw (FUN_00772FD0), so the count is written straight into them +// whenever an input changes: after each corner store (every layout, through +// Texture::CornerStore), after each coordinate write (both SetTexCoord +// forms), on a tile change, and after the batch build's per-texture emit +// (FUN_SIMPLETEXTURE_EMIT), where the image is first loaded and its size +// known. The script units are the engine's own push of a script coordinate, +// DivideByScreenScaleX(aspect * 1024 * length), over the effective scale. +// Counting after a coordinate write goes past 4.3.4, which waits for the next +// size change: NineSlice calls SetTexCoord after SetHorizTile. +// +// A tiled axis also repeats (Texture::Wrap). Retail fixes the tiling SetAtlas +// clears with SetVertTile(true) alone (Blizzard_LegacyChallengeButton.lua), so +// the flag carries the wrap, and 4.3.4's CSimpleStatusBar::SetBarTexture +// passes the tile flags as the wrap. +// +// Not counted: a texture showing an atlas (an atlas here is part of its file, +// which a repeat cannot repeat alone) and one with SetTexCoordModifiesRect on +// (its coordinates size its rect). A StatusBar's tiled bar texture takes the +// 4.3.4 layout, which moves the texture instead of cropping its coordinates +// (StatusBar::Fill). +// +// XML: the attributes apply when present and non-empty, as in 4.3.4's loader. +// A `left` / `right` other than 0 / 1 on a horizontally tiled +// texture, or `top` / `bottom` on a vertically tiled one, is 4.3.4's error +// "Invalid TexCoords value (horizTile is on)". 4.3.4 then ignores the element; +// here the engine has applied it already, so the coordinates go back to the +// whole image. + +#include "Game.h" +#include "Offsets.h" +#include "texture/Atlas.h" +#include "texture/CornerStore.h" +#include "texture/Wrap.h" +#include "xml/FrameLoad.h" +#include "xml/TypeLoad.h" + +#include + +namespace Texture::Tile { + +namespace { + +using GetRect_t = int(__fastcall *)(void *anchor, void *edx, float *outRect); +using ScreenAspect_t = float(__cdecl *)(); +using DivideByScreenScaleX_t = float(__stdcall *)(float value); +using SetTexCoord_t = void(__fastcall *)(void *texture, void *edx, const float *values); +using Emit_t = void(__fastcall *)(void *texture, void *edx, void *batch); + +// UL, LL, UR, LR: the whole image. +constexpr float kWholeImage[8] = {0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f}; + +// A length in rect units as script units. +float ScriptUnits(float length) { + const auto aspect = reinterpret_cast(Offsets::FUN_COORD_GET_SCALED_SCREEN_ASPECT); + const auto divide = reinterpret_cast(Offsets::FUN_COORD_DIVIDE_BY_SCREEN_SCALE_X); + return divide(aspect() * Game::Read(Offsets::FLOAT_SCRIPT_COORD_SCALE) * length); +} + +// 4.3.4's rule on one axis (0 x, 1 y) of the four corner pairs. +void Count(float *coords, int axis, float length, uint32_t texels) { + if (texels == 0 || !(length > 0.0f)) + return; + const float count = length / static_cast(texels); + for (int i = 0; i < 4; ++i) + if (coords[i * 2 + axis] != 0.0f) + coords[i * 2 + axis] = count; +} + +void Apply(void *texture) { + bool h, v; + if (!Texture::Wrap::Tiles(texture, h, v)) + return; + if (Game::Read(texture, Offsets::OFF_REGION_TEXCOORD_MODIFIES_RECT) != 0 || Texture::Atlas::Shows(texture)) + return; + void *image = Game::Read(texture, Offsets::OFF_SIMPLETEXTURE_HTEXTURE); + const void *device = image != nullptr ? Game::Read(image, Offsets::OFF_HTEXTURE_DEVICE) : nullptr; + if (device == nullptr) + return; // the emit of the next batch build counts it + float rect[4]; // bottom, left, top, right + if (reinterpret_cast(Offsets::FUN_REGION_GET_RECT)( + Game::Ptr(texture, Offsets::OFF_REGION_ANCHOR), nullptr, rect) == 0) + return; + const float scale = Game::Read(texture, Offsets::OFF_REGION_EFFECTIVE_SCALE); + if (!(scale > 0.0f)) + return; + float *coords = Game::Ptr(texture, Offsets::OFF_SIMPLETEXTURE_TEXCOORDS); + if (h) + Count(coords, 0, ScriptUnits(rect[3] - rect[1]) / scale, + Game::Read(device, Offsets::OFF_GXTEX_WIDTH)); + if (v) + Count(coords, 1, ScriptUnits(rect[2] - rect[0]) / scale, + Game::Read(device, Offsets::OFF_GXTEX_HEIGHT)); +} + +// --- what moves the count ----------------------------------------------------- + +void OnCornersStored(void *region) { Apply(region); } + +const Texture::CornerStore::AutoSubscribe _cornerSub{&OnCornersStored}; + +SetTexCoord_t g_origSetRect = nullptr; +SetTexCoord_t g_origSetCorners = nullptr; +Emit_t g_origEmit = nullptr; + +void __fastcall SetTexCoordRect_h(void *texture, void *edx, const float *rect) { + g_origSetRect(texture, edx, rect); + Apply(texture); +} + +void __fastcall SetTexCoordCorners_h(void *texture, void *edx, const float *corners) { + g_origSetCorners(texture, edx, corners); + Apply(texture); +} + +void __fastcall Emit_h(void *texture, void *edx, void *batch) { + g_origEmit(texture, edx, batch); + Apply(texture); +} + +const Game::HookAutoRegister _setRectHook{Offsets::FUN_SIMPLETEXTURE_SET_TEXCOORD, + reinterpret_cast(&SetTexCoordRect_h), + reinterpret_cast(&g_origSetRect)}; +const Game::HookAutoRegister _setCornersHook{Offsets::FUN_SIMPLETEXTURE_SET_TEXCOORD_CORNERS, + reinterpret_cast(&SetTexCoordCorners_h), + reinterpret_cast(&g_origSetCorners)}; +const Game::HookAutoRegister _emitHook{Offsets::FUN_SIMPLETEXTURE_EMIT, reinterpret_cast(&Emit_h), + reinterpret_cast(&g_origEmit)}; + +// --- Lua ----------------------------------------------------------------------- + +// Counted against the held image before Settle swaps in the repeating copy of +// the same file, which has no size until it is drawn. +void SetAxis(void *texture, bool horizontal, bool tiling) { + bool h, v; + Texture::Wrap::Tiles(texture, h, v); + (horizontal ? h : v) = tiling; + Texture::Wrap::SetTiles(texture, h, v); + Apply(texture); + Texture::Wrap::Settle(texture); +} + +int __fastcall Script_SetHorizTile(void *L) { + if (void *texture = Game::Lua::ResolveTexture(L)) + SetAxis(texture, true, Game::Lua::ToFlag(L, 2)); + return 0; +} + +int __fastcall Script_SetVertTile(void *L) { + if (void *texture = Game::Lua::ResolveTexture(L)) + SetAxis(texture, false, Game::Lua::ToFlag(L, 2)); + return 0; +} + +int __fastcall Script_GetHorizTile(void *L) { + void *texture = Game::Lua::ResolveTexture(L); + if (texture == nullptr) + return 0; + bool h, v; + Texture::Wrap::Tiles(texture, h, v); + Game::Lua::PushBool(L, h); + return 1; +} + +int __fastcall Script_GetVertTile(void *L) { + void *texture = Game::Lua::ResolveTexture(L); + if (texture == nullptr) + return 0; + bool h, v; + Texture::Wrap::Tiles(texture, h, v); + Game::Lua::PushBool(L, v); + return 1; +} + +const Game::Lua::FrameMethodEntry g_methods[] = { + {"SetHorizTile", &Script_SetHorizTile}, + {"GetHorizTile", &Script_GetHorizTile}, + {"SetVertTile", &Script_SetVertTile}, + {"GetVertTile", &Script_GetVertTile}, +}; + +void RegisterLuaFunctions() { + Game::Lua::RegisterFrameMethods(reinterpret_cast(Offsets::VAR_TEXTURE_METHOD_REGISTRY), g_methods, + static_cast(sizeof(g_methods) / sizeof(g_methods[0]))); +} + +const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions}; + +// --- XML ----------------------------------------------------------------------- + +// The tile attribute a node's breaks, as 4.3.4's loader tests it, +// or null. +const char *Conflict(const void *node, bool h, bool v) { + for (const void *child = Xml::FirstChild(node); child != nullptr; child = Xml::NextSibling(child)) { + if (!Xml::TagIs(child, "TexCoords")) + continue; + float value = 0.0f; + if (h && ((Xml::FloatAttr(child, "left", value) && value != 0.0f) || + (Xml::FloatAttr(child, "right", value) && value != 1.0f))) + return "horizTile"; + if (v && ((Xml::FloatAttr(child, "top", value) && value != 0.0f) || + (Xml::FloatAttr(child, "bottom", value) && value != 1.0f))) + return "vertTile"; + } + return nullptr; +} + +void OnTextureNode(void *object, const void *node, void *status) { + const char *h = Xml::Attr(node, "horizTile"); + const char *v = Xml::Attr(node, "vertTile"); + const bool hasH = h != nullptr && *h != '\0'; + const bool hasV = v != nullptr && *v != '\0'; + bool tileH, tileV; + Texture::Wrap::Tiles(object, tileH, tileV); + if (hasH) + tileH = Game::StringToFlag(h); + if (hasV) + tileV = Game::StringToFlag(v); + if (hasH || hasV) + Texture::Wrap::SetTiles(object, tileH, tileV); + if (tileH || tileV) { + if (const char *flag = Conflict(node, tileH, tileV)) { + const char *name = Game::Read(object, Offsets::OFF_SCRIPT_REGION_NAME); + Xml::Error(status, "Texture %s: Invalid TexCoords value (%s is on)", + name != nullptr ? name : "", flag); + reinterpret_cast(Offsets::FUN_SIMPLETEXTURE_SET_TEXCOORD_CORNERS)(object, nullptr, + kWholeImage); + } + Apply(object); + } + if (hasH || hasV) + Texture::Wrap::Settle(object); +} + +const Xml::TypeLoad::AutoSubscribe _xmlSub{Xml::TypeLoad::TEXTURE, &OnTextureNode}; + +} // namespace + +} // namespace Texture::Tile diff --git a/src/texture/Wrap.cpp b/src/texture/Wrap.cpp new file mode 100644 index 00000000..ec24daeb --- /dev/null +++ b/src/texture/Wrap.cpp @@ -0,0 +1,288 @@ +// This file is part of ClassicAPI. +// +// ClassicAPI is free software: you can redistribute it and/or modify it under the terms +// of the GNU General Public License as published by the Free Software Foundation, either +// version 3 of the License, or (at your option) any later version. +// +// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +// PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// ClassicAPI. If not, see . + +// `texture:SetTexture(file, wrapModeHorizontal, wrapModeVertical)`, the XML +// `hWrapMode` / `vWrapMode` attributes, and the repeat a tiled axis needs +// (Texture::Tile). +// +// 1.12 fixes a texture's wrap when its file loads. CSimpleTexture::SetTexture +// (FUN_SIMPLETEXTURE_SET_TEXTURE) builds the texture flags word with ONE wrap +// argument for both axes, and TextureCreate caches the handle under the name +// and that word, so a repeating copy of a file is its own shared texture. The +// device samples with the word's two wrap bits, repeat or clamp per axis +// (FUN_GX_TEXFLAGS_INIT). Its callers pass 0, except the XML texture loader, +// which passes 1 when a value lies outside 0..1. +// +// A texture with a mode or a tile flag loads through a co-hook on that +// SetTexture: its own body, with a repeat per axis built into the flags word - +// the engine's wrap argument, the mode, or a tile flag. The engine's same-file +// early-out (FUN_TEXTURE_NAME_MATCH) ignores flags, so the co-hook also +// compares the held image's wrap, and for the same file with another wrap it +// swaps in the other copy and keeps the pixel shader the early-out would have +// kept. A change of mode or tiling reloads the held file the same way. +// +// The wrap arguments follow retail: true or "REPEAT" repeats, and anything +// else clamps, nil included, so SetTexture(file) clamps again. The device has +// no mirror or border mode, so "MIRROR", "CLAMPTOBLACK", "CLAMPTOBLACKADDITIVE" +// and "CLAMPTOWHITE" clamp. Tile flags survive SetTexture, as 4.3.4's do; +// SetAtlas clears modes and tiling (Clear), and while a texture shows an atlas +// its image keeps the atlas's clamp: a reload would replace the image the +// atlas binding is tied to. + +#include "texture/Wrap.h" + +#include "Game.h" +#include "Offsets.h" +#include "texture/Atlas.h" +#include "texture/Construct.h" +#include "texture/Load.h" +#include "xml/FrameLoad.h" +#include "xml/TypeLoad.h" + +#include +#include +#include + +namespace Texture::Wrap { + +namespace { + +struct State { + bool baseH = false, baseV = false; // the engine's own wrap argument for the image + bool modeH = false, modeV = false; + bool tileH = false, tileV = false; + + bool Any() const { return modeH || modeV || tileH || tileV; } + bool RepeatH() const { return baseH || modeH || tileH; } + bool RepeatV() const { return baseV || modeV || tileV; } +}; + +std::unordered_map g_state; + +constexpr uint32_t kWrapBits = Offsets::GXTEX_FLAG_WRAP_U | Offsets::GXTEX_FLAG_WRAP_V; + +using SetTexture_t = uint32_t(__fastcall *)(void *texture, void *edx, const char *name, uint32_t wrap, + uint32_t filter, uint32_t shader); +using NameMatch_t = int(__fastcall *)(void *hTexture, const char *name); +using GetUiPixelShader_t = void *(__fastcall *)(uint32_t index); +using NotifyLayerChanged_t = void(__fastcall *)(void *region); + +SetTexture_t g_origSetTexture = nullptr; + +void *HeldImage(const void *texture) { return Game::Read(texture, Offsets::OFF_SIMPLETEXTURE_HTEXTURE); } + +// The flags word the held image samples with: its device texture's, or for a +// BLP still loading, the word the handle was made with. +uint32_t HeldFlags(void *image) { + if (const void *device = Game::Read(image, Offsets::OFF_HTEXTURE_DEVICE)) + return Game::Read(device, Offsets::OFF_GXTEX_FLAGS); + return Game::Read(image, Offsets::OFF_HTEXTURE_TEX_FLAGS); +} + +// CSimpleTexture::SetTexture with a repeat per axis, and a same-file +// early-out that also compares the wrap. An empty name is the engine's clear. +uint32_t Load(void *texture, const char *name, bool repeatH, bool repeatV, uint32_t filter, uint32_t shader) { + if (name == nullptr || *name == '\0') + return g_origSetTexture(texture, nullptr, name, 0, filter, shader); + void *held = HeldImage(texture); + const uint32_t flags = Texture::Flags(filter, repeatH, repeatV); + const bool sameFile = + held != nullptr && reinterpret_cast(Offsets::FUN_TEXTURE_NAME_MATCH)(held, name) != 0; + if (sameFile && (HeldFlags(held) & kWrapBits) == (flags & kWrapBits)) + return 1; + void *image = Texture::LoadByPath(name, flags); + if (image == nullptr) + return 0; + if (!sameFile) + Game::Ref(texture, Offsets::OFF_SIMPLETEXTURE_SHADER) = + reinterpret_cast(Offsets::FUN_UI_GET_PIXEL_SHADER)(shader); + Texture::ReleaseHandle(held); + Game::Ref(texture, Offsets::OFF_SIMPLETEXTURE_HTEXTURE) = image; + reinterpret_cast(Offsets::FUN_REGION_NOTIFY_LAYER_CHANGED)(texture); + return 1; +} + +uint32_t __fastcall SetTexture_h(void *texture, void *edx, const char *name, uint32_t wrap, uint32_t filter, + uint32_t shader) { + const auto it = g_state.find(texture); + if (it == g_state.end()) + return g_origSetTexture(texture, edx, name, wrap, filter, shader); + State next = it->second; + next.baseH = next.baseV = wrap != 0; + const uint32_t loaded = Load(texture, name, next.RepeatH(), next.RepeatV(), filter, shader); + if (loaded != 0) + it->second = next; + return loaded; +} + +const Game::HookAutoRegister _setTextureHook{Offsets::FUN_SIMPLETEXTURE_SET_TEXTURE, + reinterpret_cast(&SetTexture_h), + reinterpret_cast(&g_origSetTexture)}; + +// The held file again with the state's wrap. A solid colour or a pixel source +// has no file to load, and an atlas keeps its image (see the header). +void Reconcile(void *texture, const State &state) { + void *held = HeldImage(texture); + if (held == nullptr || + (Game::Read(held, Offsets::OFF_HTEXTURE_SOURCE) & Offsets::HTEXTURE_SOURCE_FILE) == 0 || + Texture::Atlas::Shows(texture)) + return; + char name[0x104]; + std::memcpy(name, Game::Ptr(held, Offsets::OFF_HTEXTURE_NAME), sizeof(name)); + name[sizeof(name) - 1] = '\0'; + Load(texture, name, state.RepeatH(), state.RepeatV(), HeldFlags(held) & Offsets::GXTEX_FLAG_FILTER_MASK, 0); +} + +// The texture's state, made on first use with the held image's wrap as the +// engine's own: without state, the engine loaded it. +State &Touch(void *texture) { + const auto it = g_state.find(texture); + if (it != g_state.end()) + return it->second; + State state; + if (void *held = HeldImage(texture)) { + const uint32_t flags = HeldFlags(held); + state.baseH = (flags & Offsets::GXTEX_FLAG_WRAP_U) != 0; + state.baseV = (flags & Offsets::GXTEX_FLAG_WRAP_V) != 0; + } + return g_state.emplace(texture, state).first->second; +} + +// Retail's wrap mode argument: true or "REPEAT" repeats; nil, false and the +// other modes clamp. +bool RepeatsArg(void *L, int index) { + if (Game::Lua::Type(L, index) == Game::Lua::TYPE_STRING) + return Game::StrCmpI(Game::Lua::ToString(L, index), "REPEAT") == 0; + return Game::Lua::ToBoolean(L, index) != 0; +} + +// texture:SetTexture(file[, wrapModeHorizontal, wrapModeVertical]): the engine +// method, with the modes stored first so its load takes them. The colour form +// keeps the modes. +int __fastcall Script_SetTexture(void *L) { + void *texture = Game::Lua::IsNumber(L, 2) ? nullptr : Game::Lua::ResolveTexture(L, 1, false); + const bool repeatH = texture != nullptr && RepeatsArg(L, 3); + const bool repeatV = texture != nullptr && RepeatsArg(L, 4); + if (texture != nullptr && (repeatH || repeatV || g_state.count(texture) != 0)) { + State &state = Touch(texture); + state.modeH = repeatH; + state.modeV = repeatV; + } + const int results = reinterpret_cast(Offsets::FUN_SCRIPT_TEXTURE_SET_TEXTURE)(L); + if (texture != nullptr) + Settle(texture); // a file that did not load leaves the old image to match + return results; +} + +// The value of a wrap mode attribute, warning on a name retail does not have. +bool RepeatsAttr(const char *value, const void *node, void *status) { + static const char *const kClamps[] = {"CLAMP", "CLAMPTOBLACK", "CLAMPTOBLACKADDITIVE", "CLAMPTOWHITE", "MIRROR"}; + if (Game::StrCmpI(value, "REPEAT") == 0) + return true; + for (const char *mode : kClamps) + if (Game::StrCmpI(value, mode) == 0) + return false; + Xml::Warn(status, "Unknown wrap mode %s in element %s", value, Xml::Tag(node)); + return false; +} + +// Retail's `hWrapMode` / `vWrapMode`, applied when present and non-empty. +void OnTextureNode(void *object, const void *node, void *status) { + const char *h = Xml::Attr(node, "hWrapMode"); + const char *v = Xml::Attr(node, "vWrapMode"); + const bool hasH = h != nullptr && *h != '\0'; + const bool hasV = v != nullptr && *v != '\0'; + if (!hasH && !hasV) + return; + State &state = Touch(object); + if (hasH) + state.modeH = RepeatsAttr(h, node, status); + if (hasV) + state.modeV = RepeatsAttr(v, node, status); + Settle(object); +} + +const Xml::TypeLoad::AutoSubscribe _xmlSub{Xml::TypeLoad::TEXTURE, &OnTextureNode}; + +void OnTextureConstructed(void *region) { g_state.erase(region); } + +const Texture::Construct::AutoSubscribe _constructSub{&OnTextureConstructed}; + +void PrepareForReload() { g_state.clear(); } + +const Game::ReloadAutoRegister _reloadReg{&PrepareForReload}; + +const Game::Lua::FrameMethodEntry g_methods[] = { + {"SetTexture", &Script_SetTexture}, +}; + +void RegisterLuaFunctions() { + Game::Lua::RegisterFrameMethods(reinterpret_cast(Offsets::VAR_TEXTURE_METHOD_REGISTRY), g_methods, + static_cast(sizeof(g_methods) / sizeof(g_methods[0]))); +} + +const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions}; + +} // namespace + +// The image back in step with the state, and the state dropped once it holds +// nothing beyond the engine's own wrap. +void Settle(void *texture) { + const auto it = g_state.find(texture); + if (it == g_state.end()) + return; + const State state = it->second; + Reconcile(texture, state); + if (!state.Any()) + g_state.erase(texture); +} + +void SetModes(void *texture, bool repeatH, bool repeatV) { + State &state = Touch(texture); + state.modeH = repeatH; + state.modeV = repeatV; + Settle(texture); +} + +void SetTiles(void *texture, bool h, bool v) { + State &state = Touch(texture); + state.tileH = h; + state.tileV = v; +} + +bool Tiles(const void *texture, bool &h, bool &v) { + h = v = false; + if (g_state.empty()) + return false; + const auto it = g_state.find(texture); + if (it == g_state.end()) + return false; + h = it->second.tileH; + v = it->second.tileV; + return h || v; +} + +bool Tiles(const void *texture) { + bool h, v; + return Tiles(texture, h, v); +} + +void Clear(void *texture) { + if (g_state.find(texture) == g_state.end()) + return; + State &state = g_state[texture]; + state.modeH = state.modeV = state.tileH = state.tileV = false; + Settle(texture); +} + +} // namespace Texture::Wrap diff --git a/src/texture/Wrap.h b/src/texture/Wrap.h new file mode 100644 index 00000000..a586d709 --- /dev/null +++ b/src/texture/Wrap.h @@ -0,0 +1,41 @@ +// This file is part of ClassicAPI. +// +// ClassicAPI is free software: you can redistribute it and/or modify it under the terms +// of the GNU General Public License as published by the Free Software Foundation, either +// version 3 of the License, or (at your option) any later version. +// +// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +// PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// ClassicAPI. If not, see . + +#pragma once + +// A texture's wrap mode per axis, and its tile flags, which repeat an axis +// too. See Wrap.cpp. +namespace Texture::Wrap { + +// Repeat or clamp per axis: SetTexture's wrap arguments and the XML +// `hWrapMode` / `vWrapMode` attributes. +void SetModes(void *texture, bool repeatH, bool repeatV); + +// The tile flags, recorded without touching the image. Texture::Tile owns what +// they do to the coordinates: it counts against the held image, then Settle +// swaps in a copy of that file with the new repeat, which loads only when next +// drawn. +void SetTiles(void *texture, bool h, bool v); + +// Reloads the held image with the repeat the modes and tile flags ask for. +void Settle(void *texture); + +// The tile flags; true when either is on. +bool Tiles(const void *texture, bool &h, bool &v); +bool Tiles(const void *texture); + +// No modes and no tile flags, the image back on the engine's own wrap, as +// SetAtlas leaves a texture. +void Clear(void *texture); + +} // namespace Texture::Wrap diff --git a/src/xml/FrameLoad.cpp b/src/xml/FrameLoad.cpp index 951be557..e8e75ff3 100644 --- a/src/xml/FrameLoad.cpp +++ b/src/xml/FrameLoad.cpp @@ -30,6 +30,13 @@ using Hash_t = uint32_t(__stdcall *)(const char *s); using StatusAdd_t = void(__cdecl *)(void *status, int level, const char *fmt, ...); using ToFloat_t = float(__stdcall *)(const char *s); +void Report(void *status, int level, const char *fmt, const char *a, const char *b, const char *c) { + if (status == nullptr) + return; + const void *vtable = Game::Read(status, 0); + Game::Read(vtable, Offsets::VTBL_XML_STATUS_ADD)(status, level, fmt, a, b, c); +} + } // namespace const void *FirstChild(const void *node) { @@ -95,11 +102,11 @@ bool FloatAttr(const void *node, const char *name, float &out) { } void Warn(void *status, const char *fmt, const char *a, const char *b, const char *c) { - if (status == nullptr) - return; - const void *vtable = Game::Read(status, 0); - Game::Read(vtable, Offsets::VTBL_XML_STATUS_ADD)( - status, Offsets::XML_STATUS_WARNING, fmt, a, b, c); + Report(status, Offsets::XML_STATUS_WARNING, fmt, a, b, c); +} + +void Error(void *status, const char *fmt, const char *a, const char *b, const char *c) { + Report(status, Offsets::XML_STATUS_ERROR, fmt, a, b, c); } bool PushWrapper(void *L, void *object) { diff --git a/src/xml/FrameLoad.h b/src/xml/FrameLoad.h index af71f3c8..0382ba09 100644 --- a/src/xml/FrameLoad.h +++ b/src/xml/FrameLoad.h @@ -58,6 +58,9 @@ void LoadNode(void *object, const void *node, void *status); // the same warning the loaders log ("Couldn't find inherited node: %s"). void Warn(void *status, const char *fmt, const char *a, const char *b = nullptr, const char *c = nullptr); +// The same at STATUS_ERROR, the level 4.3.4's loaders give a value they reject. +void Error(void *status, const char *fmt, const char *a, const char *b = nullptr, + const char *c = nullptr); // Pushes `object`'s Lua wrapper and returns true, registering an unnamed, // never-exposed object the way the engine's own lazy path does. Pushes