forked from diasurgical/devilutionX
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhwcursor.cpp
140 lines (118 loc) · 3.97 KB
/
hwcursor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "hwcursor.hpp"
#include <cstdint>
#include <tuple>
#include <SDL_mouse.h>
#include <SDL_render.h>
#include <SDL_surface.h>
#include "DiabloUI/diabloui.h"
#include "appfat.h"
#include "cursor.h"
#include "engine.h"
#include "utils/display.h"
#include "utils/sdl_bilinear_scale.hpp"
#include "utils/sdl_wrap.h"
namespace devilution {
namespace {
CursorInfo CurrentCursorInfo;
SDLCursorUniquePtr CurrentCursor;
enum class HotpointPosition {
TopLeft,
Center,
};
Size ScaledSize(Size size)
{
if (renderer != nullptr) {
float scaleX;
float scaleY;
SDL_RenderGetScale(renderer, &scaleX, &scaleY);
size.width = static_cast<int>(size.width * scaleX);
size.height = static_cast<int>(size.height * scaleY);
}
return size;
}
Point GetHotpointPosition(const SDL_Surface &surface, HotpointPosition position)
{
switch (position) {
case HotpointPosition::TopLeft:
return { 0, 0 };
case HotpointPosition::Center:
return { surface.w / 2, surface.h / 2 };
}
app_fatal("Unhandled enum value");
}
bool ShouldUseBilinearScaling()
{
return sgOptions.Graphics.szScaleQuality[0] != '0';
}
bool SetHardwareCursor(SDL_Surface *surface, HotpointPosition hotpointPosition)
{
SDLCursorUniquePtr newCursor;
const Size size { surface->w, surface->h };
const Size scaledSize = ScaledSize(size);
if (size == scaledSize) {
const Point hotpoint = GetHotpointPosition(*surface, hotpointPosition);
newCursor = SDLCursorUniquePtr { SDL_CreateColorCursor(surface, hotpoint.x, hotpoint.y) };
} else {
// SDL does not support BlitScaled from 8-bit to RGBA.
SDLSurfaceUniquePtr converted { SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ARGB8888, 0) };
SDLSurfaceUniquePtr scaledSurface = SDLWrap::CreateRGBSurfaceWithFormat(0, scaledSize.width, scaledSize.height, 32, SDL_PIXELFORMAT_ARGB8888);
if (ShouldUseBilinearScaling()) {
BilinearScale32(converted.get(), scaledSurface.get());
} else {
SDL_BlitScaled(converted.get(), nullptr, scaledSurface.get(), nullptr);
}
const Point hotpoint = GetHotpointPosition(*scaledSurface, hotpointPosition);
newCursor = SDLCursorUniquePtr { SDL_CreateColorCursor(scaledSurface.get(), hotpoint.x, hotpoint.y) };
}
if (newCursor == nullptr)
return false;
SDL_SetCursor(newCursor.get());
CurrentCursor = std::move(newCursor);
return true;
}
bool SetHardwareCursorFromSprite(int pcurs)
{
const bool isItem = IsItemSprite(pcurs);
const int outlineWidth = isItem ? 1 : 0;
auto size = GetInvItemSize(pcurs);
size.width += 2 * outlineWidth;
size.height += 2 * outlineWidth;
OwnedSurface out { size };
SDL_SetSurfacePalette(out.surface, Palette.get());
// Transparent color must not be used in the sprite itself.
// Colors 1-127 are outside of the UI palette so are safe to use.
constexpr std::uint8_t TransparentColor = 1;
SDL_FillRect(out.surface, nullptr, TransparentColor);
SDL_SetColorKey(out.surface, 1, TransparentColor);
CelDrawCursor(out, { outlineWidth, size.height - outlineWidth }, pcurs);
const bool result = SetHardwareCursor(out.surface, isItem ? HotpointPosition::Center : HotpointPosition::TopLeft);
return result;
}
} // namespace
CursorInfo GetCurrentCursorInfo()
{
return CurrentCursorInfo;
}
void SetHardwareCursor(CursorInfo cursorInfo)
{
CurrentCursorInfo = cursorInfo;
switch (cursorInfo.type()) {
case CursorType::Game:
CurrentCursorInfo.SetEnabled(SetHardwareCursorFromSprite(cursorInfo.id()));
break;
case CursorType::UserInterface:
// ArtCursor is null while loading the game on the progress screen,
// called via palette fade from ShowProgress.
if (ArtCursor.surface != nullptr) {
CurrentCursorInfo.SetEnabled(
SetHardwareCursor(ArtCursor.surface.get(), HotpointPosition::TopLeft));
}
break;
case CursorType::Unknown:
CurrentCursorInfo.SetEnabled(false);
break;
}
if (!CurrentCursorInfo.Enabled())
SetHardwareCursorVisible(false);
}
} // namespace devilution