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;
}
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)

34
Source/controls/touch/renderers.h

@ -2,7 +2,6 @@
#include <SDL.h>
#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<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 {
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);

7
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

5
Source/engine/dx.cpp

@ -8,13 +8,16 @@
#include <SDL.h>
#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

5
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

Loading…
Cancel
Save