From 580d3cb6ee204bfee2cfaf987b9eeb76e9c4fd49 Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Sun, 7 Aug 2022 19:54:42 +0100 Subject: [PATCH] touch/renderers: Migrate away from Art --- Source/controls/touch/renderers.cpp | 57 +++++++++++++++++------------ Source/controls/touch/renderers.h | 34 ++++++++++++----- Source/diablo.cpp | 7 +++- Source/engine/dx.cpp | 5 ++- Source/engine/render/scrollrt.cpp | 5 ++- 5 files changed, 71 insertions(+), 37 deletions(-) diff --git a/Source/controls/touch/renderers.cpp b/Source/controls/touch/renderers.cpp index 1e388f929..4e7294bdc 100644 --- a/Source/controls/touch/renderers.cpp +++ b/Source/controls/touch/renderers.cpp @@ -88,16 +88,14 @@ VirtualGamepadButtonType GetStandButtonType(bool isPressed) return isPressed ? GAMEPAD_STANDDOWN : GAMEPAD_STAND; } -void LoadButtonArt(Art *buttonArt, SDL_Renderer *renderer) +void LoadButtonArt(ButtonTexture *buttonArt, SDL_Renderer *renderer) { - const int Frames = 26; + constexpr unsigned Frames = 26; buttonArt->surface.reset(LoadPNG("ui_art\\button.png")); if (buttonArt->surface == nullptr) return; - buttonArt->logical_width = buttonArt->surface->w; - buttonArt->frame_height = buttonArt->surface->h / Frames; - buttonArt->frames = Frames; + buttonArt->numFrames = Frames; if (renderer != nullptr) { buttonArt->texture.reset(SDL_CreateTextureFromSurface(renderer, buttonArt->surface.get())); @@ -105,7 +103,7 @@ void LoadButtonArt(Art *buttonArt, SDL_Renderer *renderer) } } -void LoadPotionArt(Art *potionArt, SDL_Renderer *renderer) +void LoadPotionArt(ButtonTexture *potionArt, SDL_Renderer *renderer) { item_cursor_graphic potionGraphics[] { ICURS_POTION_OF_HEALING, @@ -144,9 +142,7 @@ void LoadPotionArt(Art *potionArt, SDL_Renderer *renderer) ClxDraw(Surface(surface.get()), position, GetInvItemSprite(cursorID)); } - potionArt->logical_width = potionSize.width; - potionArt->frame_height = potionSize.height; - potionArt->frames = sizeof(potionGraphics); + potionArt->numFrames = sizeof(potionGraphics); if (renderer == nullptr) { potionArt->surface.reset(SDL_ConvertSurfaceFormat(surface.get(), SDL_PIXELFORMAT_ARGB8888, 0)); @@ -176,12 +172,25 @@ bool InteractsWithCharButton(Point point) } // namespace +Size ButtonTexture::size() const +{ + int w, h; + if (surface != nullptr) { + w = surface->w; + h = surface->h; + } else { + SDL_QueryTexture(texture.get(), /*format=*/nullptr, /*access=*/nullptr, &w, &h); + } + h /= numFrames; + return Size { w, h }; +} + void RenderVirtualGamepad(SDL_Renderer *renderer) { if (!gbRunGame) return; - RenderFunction renderFunction = [&](Art &art, SDL_Rect *src, SDL_Rect *dst) { + RenderFunction renderFunction = [renderer](const ButtonTexture &art, SDL_Rect *src, SDL_Rect *dst) { if (art.texture == nullptr) return; @@ -197,7 +206,7 @@ void RenderVirtualGamepad(SDL_Surface *surface) if (!gbRunGame) return; - RenderFunction renderFunction = [&](Art &art, SDL_Rect *src, SDL_Rect *dst) { + RenderFunction renderFunction = [surface](const ButtonTexture &art, SDL_Rect *src, SDL_Rect *dst) { if (art.surface == nullptr) return; @@ -308,14 +317,15 @@ void VirtualDirectionPadRenderer::RenderKnob(RenderFunction renderFunction) renderFunction(knobArt, nullptr, &rect); } -void VirtualPadButtonRenderer::Render(RenderFunction renderFunction, Art &buttonArt) +void VirtualPadButtonRenderer::Render(RenderFunction renderFunction, const ButtonTexture &buttonArt) { if (!virtualPadButton->isUsable()) return; VirtualGamepadButtonType buttonType = GetButtonType(); + Size size = buttonArt.size(); int frame = buttonType; - int offset = buttonArt.h() * frame; + int offset = size.height * frame; auto center = virtualPadButton->area.position; auto radius = virtualPadButton->area.radius; @@ -326,12 +336,12 @@ void VirtualPadButtonRenderer::Render(RenderFunction renderFunction, Art &button int width = diameter; int height = diameter; - SDL_Rect src = MakeSdlRect(0, offset, buttonArt.w(), buttonArt.h()); + SDL_Rect src = MakeSdlRect(0, offset, size.width, size.height); SDL_Rect dst = MakeSdlRect(x, y, width, height); renderFunction(buttonArt, &src, &dst); } -void PotionButtonRenderer::RenderPotion(RenderFunction renderFunction, Art &potionArt) +void PotionButtonRenderer::RenderPotion(RenderFunction renderFunction, const ButtonTexture &potionArt) { if (!virtualPadButton->isUsable()) return; @@ -341,7 +351,8 @@ void PotionButtonRenderer::RenderPotion(RenderFunction renderFunction, Art &poti return; int frame = *potionType; - int offset = potionArt.h() * frame; + Size size = potionArt.size(); + int offset = size.height * frame; auto center = virtualPadButton->area.position; auto radius = virtualPadButton->area.radius * 8 / 10; @@ -352,7 +363,7 @@ void PotionButtonRenderer::RenderPotion(RenderFunction renderFunction, Art &poti int width = diameter; int height = diameter; - SDL_Rect src = MakeSdlRect(0, offset, potionArt.w(), potionArt.h()); + SDL_Rect src = MakeSdlRect(0, offset, size.width, size.height); SDL_Rect dst = MakeSdlRect(x, y, width, height); renderFunction(potionArt, &src, &dst); } @@ -496,20 +507,20 @@ void VirtualGamepadRenderer::UnloadArt() { menuPanelRenderer.UnloadArt(); directionPadRenderer.UnloadArt(); - buttonArt.Unload(); - potionArt.Unload(); + buttonArt.clear(); + potionArt.clear(); } void VirtualMenuPanelRenderer::UnloadArt() { - menuArt.Unload(); - menuArtLevelUp.Unload(); + menuArt.clear(); + menuArtLevelUp.clear(); } void VirtualDirectionPadRenderer::UnloadArt() { - padArt.Unload(); - knobArt.Unload(); + padArt.clear(); + knobArt.clear(); } void InitVirtualGamepadGFX(SDL_Renderer *renderer) diff --git a/Source/controls/touch/renderers.h b/Source/controls/touch/renderers.h index 5946e660c..2e6100563 100644 --- a/Source/controls/touch/renderers.h +++ b/Source/controls/touch/renderers.h @@ -2,7 +2,6 @@ #include -#include "DiabloUI/art.h" #include "controls/plrctrls.h" #include "controls/touch/gamepad.h" #include "engine/surface.hpp" @@ -51,7 +50,22 @@ enum VirtualGamepadPotionType { GAMEPAD_SCROLL_OF_HEALING, }; -typedef std::function RenderFunction; +struct ButtonTexture { + SDLSurfaceUniquePtr surface; + SDLTextureUniquePtr texture; + unsigned numFrames = 1; + + Size size() const; + + void clear() + { + surface = nullptr; + texture = nullptr; + numFrames = 1; + } +}; + +typedef std::function RenderFunction; class VirtualMenuPanelRenderer { public: @@ -66,8 +80,8 @@ public: private: VirtualMenuPanel *virtualMenuPanel; - Art menuArt; - Art menuArtLevelUp; + ButtonTexture menuArt; + ButtonTexture menuArtLevelUp; }; class VirtualDirectionPadRenderer { @@ -83,8 +97,8 @@ public: private: VirtualDirectionPad *virtualDirectionPad; - Art padArt; - Art knobArt; + ButtonTexture padArt; + ButtonTexture knobArt; void RenderPad(RenderFunction renderFunction); void RenderKnob(RenderFunction renderFunction); @@ -97,7 +111,7 @@ public: { } - void Render(RenderFunction renderFunction, Art &buttonArt); + void Render(RenderFunction renderFunction, const ButtonTexture &buttonArt); protected: VirtualPadButton *virtualPadButton; @@ -171,7 +185,7 @@ public: { } - void RenderPotion(RenderFunction renderFunction, Art &potionArt); + void RenderPotion(RenderFunction renderFunction, const ButtonTexture &potionArt); private: belt_item_type potionType; @@ -212,8 +226,8 @@ private: PotionButtonRenderer healthButtonRenderer; PotionButtonRenderer manaButtonRenderer; - Art buttonArt; - Art potionArt; + ButtonTexture buttonArt; + ButtonTexture potionArt; }; void InitVirtualGamepadGFX(SDL_Renderer *renderer); diff --git a/Source/diablo.cpp b/Source/diablo.cpp index bdd7ed127..82f080eae 100644 --- a/Source/diablo.cpp +++ b/Source/diablo.cpp @@ -20,8 +20,6 @@ #endif #include "DiabloUI/diabloui.h" #include "controls/plrctrls.h" -#include "controls/touch/gamepad.h" -#include "controls/touch/renderers.h" #include "diablo.h" #include "discord/discord.h" #include "doom.h" @@ -82,6 +80,11 @@ #include "utils/str_cat.hpp" #include "utils/utf8.hpp" +#ifndef USE_SDL1 +#include "controls/touch/gamepad.h" +#include "controls/touch/renderers.h" +#endif + #ifdef __vita__ #include "platform/vita/touch.h" #endif diff --git a/Source/engine/dx.cpp b/Source/engine/dx.cpp index 13abf1337..a0ae42f58 100644 --- a/Source/engine/dx.cpp +++ b/Source/engine/dx.cpp @@ -8,13 +8,16 @@ #include #include "controls/plrctrls.h" -#include "controls/touch/renderers.h" #include "engine.h" #include "options.h" #include "utils/display.h" #include "utils/log.hpp" #include "utils/sdl_wrap.h" +#ifndef USE_SDL1 +#include "controls/touch/renderers.h" +#endif + #ifdef __3DS__ #include <3ds.h> #endif diff --git a/Source/engine/render/scrollrt.cpp b/Source/engine/render/scrollrt.cpp index b132652fe..365600384 100644 --- a/Source/engine/render/scrollrt.cpp +++ b/Source/engine/render/scrollrt.cpp @@ -8,7 +8,6 @@ #include "DiabloUI/ui_flags.hpp" #include "automap.h" #include "controls/plrctrls.h" -#include "controls/touch/renderers.h" #include "cursor.h" #include "dead.h" #include "doom.h" @@ -46,6 +45,10 @@ #include "utils/log.hpp" #include "utils/str_cat.hpp" +#ifndef USE_SDL1 +#include "controls/touch/renderers.h" +#endif + #ifdef _DEBUG #include "debug.h" #endif