Browse Source

touch/renderers: Migrate away from Art

pull/5221/head
Gleb Mazovetskiy 4 years ago
parent
commit
580d3cb6ee
  1. 57
      Source/controls/touch/renderers.cpp
  2. 34
      Source/controls/touch/renderers.h
  3. 7
      Source/diablo.cpp
  4. 5
      Source/engine/dx.cpp
  5. 5
      Source/engine/render/scrollrt.cpp

57
Source/controls/touch/renderers.cpp

@ -88,16 +88,14 @@ VirtualGamepadButtonType GetStandButtonType(bool isPressed)
return isPressed ? GAMEPAD_STANDDOWN : GAMEPAD_STAND; 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")); buttonArt->surface.reset(LoadPNG("ui_art\\button.png"));
if (buttonArt->surface == nullptr) if (buttonArt->surface == nullptr)
return; return;
buttonArt->logical_width = buttonArt->surface->w; buttonArt->numFrames = Frames;
buttonArt->frame_height = buttonArt->surface->h / Frames;
buttonArt->frames = Frames;
if (renderer != nullptr) { if (renderer != nullptr) {
buttonArt->texture.reset(SDL_CreateTextureFromSurface(renderer, buttonArt->surface.get())); 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[] { item_cursor_graphic potionGraphics[] {
ICURS_POTION_OF_HEALING, ICURS_POTION_OF_HEALING,
@ -144,9 +142,7 @@ void LoadPotionArt(Art *potionArt, SDL_Renderer *renderer)
ClxDraw(Surface(surface.get()), position, GetInvItemSprite(cursorID)); ClxDraw(Surface(surface.get()), position, GetInvItemSprite(cursorID));
} }
potionArt->logical_width = potionSize.width; potionArt->numFrames = sizeof(potionGraphics);
potionArt->frame_height = potionSize.height;
potionArt->frames = sizeof(potionGraphics);
if (renderer == nullptr) { if (renderer == nullptr) {
potionArt->surface.reset(SDL_ConvertSurfaceFormat(surface.get(), SDL_PIXELFORMAT_ARGB8888, 0)); potionArt->surface.reset(SDL_ConvertSurfaceFormat(surface.get(), SDL_PIXELFORMAT_ARGB8888, 0));
@ -176,12 +172,25 @@ bool InteractsWithCharButton(Point point)
} // namespace } // 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) void RenderVirtualGamepad(SDL_Renderer *renderer)
{ {
if (!gbRunGame) if (!gbRunGame)
return; 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) if (art.texture == nullptr)
return; return;
@ -197,7 +206,7 @@ void RenderVirtualGamepad(SDL_Surface *surface)
if (!gbRunGame) if (!gbRunGame)
return; 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) if (art.surface == nullptr)
return; return;
@ -308,14 +317,15 @@ void VirtualDirectionPadRenderer::RenderKnob(RenderFunction renderFunction)
renderFunction(knobArt, nullptr, &rect); renderFunction(knobArt, nullptr, &rect);
} }
void VirtualPadButtonRenderer::Render(RenderFunction renderFunction, Art &buttonArt) void VirtualPadButtonRenderer::Render(RenderFunction renderFunction, const ButtonTexture &buttonArt)
{ {
if (!virtualPadButton->isUsable()) if (!virtualPadButton->isUsable())
return; return;
VirtualGamepadButtonType buttonType = GetButtonType(); VirtualGamepadButtonType buttonType = GetButtonType();
Size size = buttonArt.size();
int frame = buttonType; int frame = buttonType;
int offset = buttonArt.h() * frame; int offset = size.height * frame;
auto center = virtualPadButton->area.position; auto center = virtualPadButton->area.position;
auto radius = virtualPadButton->area.radius; auto radius = virtualPadButton->area.radius;
@ -326,12 +336,12 @@ void VirtualPadButtonRenderer::Render(RenderFunction renderFunction, Art &button
int width = diameter; int width = diameter;
int height = 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); SDL_Rect dst = MakeSdlRect(x, y, width, height);
renderFunction(buttonArt, &src, &dst); renderFunction(buttonArt, &src, &dst);
} }
void PotionButtonRenderer::RenderPotion(RenderFunction renderFunction, Art &potionArt) void PotionButtonRenderer::RenderPotion(RenderFunction renderFunction, const ButtonTexture &potionArt)
{ {
if (!virtualPadButton->isUsable()) if (!virtualPadButton->isUsable())
return; return;
@ -341,7 +351,8 @@ void PotionButtonRenderer::RenderPotion(RenderFunction renderFunction, Art &poti
return; return;
int frame = *potionType; int frame = *potionType;
int offset = potionArt.h() * frame; Size size = potionArt.size();
int offset = size.height * frame;
auto center = virtualPadButton->area.position; auto center = virtualPadButton->area.position;
auto radius = virtualPadButton->area.radius * 8 / 10; auto radius = virtualPadButton->area.radius * 8 / 10;
@ -352,7 +363,7 @@ void PotionButtonRenderer::RenderPotion(RenderFunction renderFunction, Art &poti
int width = diameter; int width = diameter;
int height = 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); SDL_Rect dst = MakeSdlRect(x, y, width, height);
renderFunction(potionArt, &src, &dst); renderFunction(potionArt, &src, &dst);
} }
@ -496,20 +507,20 @@ void VirtualGamepadRenderer::UnloadArt()
{ {
menuPanelRenderer.UnloadArt(); menuPanelRenderer.UnloadArt();
directionPadRenderer.UnloadArt(); directionPadRenderer.UnloadArt();
buttonArt.Unload(); buttonArt.clear();
potionArt.Unload(); potionArt.clear();
} }
void VirtualMenuPanelRenderer::UnloadArt() void VirtualMenuPanelRenderer::UnloadArt()
{ {
menuArt.Unload(); menuArt.clear();
menuArtLevelUp.Unload(); menuArtLevelUp.clear();
} }
void VirtualDirectionPadRenderer::UnloadArt() void VirtualDirectionPadRenderer::UnloadArt()
{ {
padArt.Unload(); padArt.clear();
knobArt.Unload(); knobArt.clear();
} }
void InitVirtualGamepadGFX(SDL_Renderer *renderer) void InitVirtualGamepadGFX(SDL_Renderer *renderer)

34
Source/controls/touch/renderers.h

@ -2,7 +2,6 @@
#include <SDL.h> #include <SDL.h>
#include "DiabloUI/art.h"
#include "controls/plrctrls.h" #include "controls/plrctrls.h"
#include "controls/touch/gamepad.h" #include "controls/touch/gamepad.h"
#include "engine/surface.hpp" #include "engine/surface.hpp"
@ -51,7 +50,22 @@ enum VirtualGamepadPotionType {
GAMEPAD_SCROLL_OF_HEALING, GAMEPAD_SCROLL_OF_HEALING,
}; };
typedef std::function<void(Art &art, SDL_Rect *src, SDL_Rect *dst)> RenderFunction; struct ButtonTexture {
SDLSurfaceUniquePtr surface;
SDLTextureUniquePtr texture;
unsigned numFrames = 1;
Size size() const;
void clear()
{
surface = nullptr;
texture = nullptr;
numFrames = 1;
}
};
typedef std::function<void(const ButtonTexture &art, SDL_Rect *src, SDL_Rect *dst)> RenderFunction;
class VirtualMenuPanelRenderer { class VirtualMenuPanelRenderer {
public: public:
@ -66,8 +80,8 @@ public:
private: private:
VirtualMenuPanel *virtualMenuPanel; VirtualMenuPanel *virtualMenuPanel;
Art menuArt; ButtonTexture menuArt;
Art menuArtLevelUp; ButtonTexture menuArtLevelUp;
}; };
class VirtualDirectionPadRenderer { class VirtualDirectionPadRenderer {
@ -83,8 +97,8 @@ public:
private: private:
VirtualDirectionPad *virtualDirectionPad; VirtualDirectionPad *virtualDirectionPad;
Art padArt; ButtonTexture padArt;
Art knobArt; ButtonTexture knobArt;
void RenderPad(RenderFunction renderFunction); void RenderPad(RenderFunction renderFunction);
void RenderKnob(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: protected:
VirtualPadButton *virtualPadButton; VirtualPadButton *virtualPadButton;
@ -171,7 +185,7 @@ public:
{ {
} }
void RenderPotion(RenderFunction renderFunction, Art &potionArt); void RenderPotion(RenderFunction renderFunction, const ButtonTexture &potionArt);
private: private:
belt_item_type potionType; belt_item_type potionType;
@ -212,8 +226,8 @@ private:
PotionButtonRenderer healthButtonRenderer; PotionButtonRenderer healthButtonRenderer;
PotionButtonRenderer manaButtonRenderer; PotionButtonRenderer manaButtonRenderer;
Art buttonArt; ButtonTexture buttonArt;
Art potionArt; ButtonTexture potionArt;
}; };
void InitVirtualGamepadGFX(SDL_Renderer *renderer); void InitVirtualGamepadGFX(SDL_Renderer *renderer);

7
Source/diablo.cpp

@ -20,8 +20,6 @@
#endif #endif
#include "DiabloUI/diabloui.h" #include "DiabloUI/diabloui.h"
#include "controls/plrctrls.h" #include "controls/plrctrls.h"
#include "controls/touch/gamepad.h"
#include "controls/touch/renderers.h"
#include "diablo.h" #include "diablo.h"
#include "discord/discord.h" #include "discord/discord.h"
#include "doom.h" #include "doom.h"
@ -82,6 +80,11 @@
#include "utils/str_cat.hpp" #include "utils/str_cat.hpp"
#include "utils/utf8.hpp" #include "utils/utf8.hpp"
#ifndef USE_SDL1
#include "controls/touch/gamepad.h"
#include "controls/touch/renderers.h"
#endif
#ifdef __vita__ #ifdef __vita__
#include "platform/vita/touch.h" #include "platform/vita/touch.h"
#endif #endif

5
Source/engine/dx.cpp

@ -8,13 +8,16 @@
#include <SDL.h> #include <SDL.h>
#include "controls/plrctrls.h" #include "controls/plrctrls.h"
#include "controls/touch/renderers.h"
#include "engine.h" #include "engine.h"
#include "options.h" #include "options.h"
#include "utils/display.h" #include "utils/display.h"
#include "utils/log.hpp" #include "utils/log.hpp"
#include "utils/sdl_wrap.h" #include "utils/sdl_wrap.h"
#ifndef USE_SDL1
#include "controls/touch/renderers.h"
#endif
#ifdef __3DS__ #ifdef __3DS__
#include <3ds.h> #include <3ds.h>
#endif #endif

5
Source/engine/render/scrollrt.cpp

@ -8,7 +8,6 @@
#include "DiabloUI/ui_flags.hpp" #include "DiabloUI/ui_flags.hpp"
#include "automap.h" #include "automap.h"
#include "controls/plrctrls.h" #include "controls/plrctrls.h"
#include "controls/touch/renderers.h"
#include "cursor.h" #include "cursor.h"
#include "dead.h" #include "dead.h"
#include "doom.h" #include "doom.h"
@ -46,6 +45,10 @@
#include "utils/log.hpp" #include "utils/log.hpp"
#include "utils/str_cat.hpp" #include "utils/str_cat.hpp"
#ifndef USE_SDL1
#include "controls/touch/renderers.h"
#endif
#ifdef _DEBUG #ifdef _DEBUG
#include "debug.h" #include "debug.h"
#endif #endif

Loading…
Cancel
Save