Skip to content

Commit

Permalink
Add variant of newCursor which takes an array, for different DPI scales.
Browse files Browse the repository at this point in the history
Resolves #1708.
  • Loading branch information
slime73 committed Dec 25, 2024
1 parent 3d9d162 commit 7076688
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/modules/mouse/Mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Mouse : public Module
// Implements Module.
virtual ModuleType getModuleType() const { return M_MOUSE; }

virtual Cursor *newCursor(love::image::ImageData *data, int hotx, int hoty) = 0;
virtual Cursor *newCursor(const std::vector<image::ImageData *> &data, int hotx, int hoty) = 0;
virtual Cursor *getSystemCursor(Cursor::SystemCursor cursortype) = 0;

virtual void setCursor(Cursor *cursor) = 0;
Expand Down
48 changes: 37 additions & 11 deletions src/modules/mouse/sdl/Cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,49 @@ namespace mouse
namespace sdl
{

Cursor::Cursor(image::ImageData *data, int hotx, int hoty)
Cursor::Cursor(const std::vector<image::ImageData *> &imageData, int hotx, int hoty)
: cursor(nullptr)
, type(CURSORTYPE_IMAGE)
, systemType(CURSOR_MAX_ENUM)
{
int w = data->getWidth();
int h = data->getHeight();
int pitch = w * 4;
if (imageData.empty())
throw love::Exception("At least one ImageData must be provided for a custom cursor.");

SDL_Surface *surface = SDL_CreateSurfaceFrom(w, h, SDL_PIXELFORMAT_ABGR8888, data->getData(), pitch);
if (!surface)
throw love::Exception("Cannot create cursor: out of memory.");
std::vector<SDL_Surface *> surfaces;

cursor = SDL_CreateColorCursor(surface, hotx, hoty);
SDL_DestroySurface(surface);
for (image::ImageData *data : imageData)
{
int w = data->getWidth();
int h = data->getHeight();
int pitch = w * 4;

if (!cursor)
if (getLinearPixelFormat(data->getFormat()) != PIXELFORMAT_RGBA8_UNORM)
{
for (SDL_Surface *surface : surfaces)
SDL_DestroySurface(surface);
throw love::Exception("Cannot create cursor: ImageData pixel format must be rgba8.");
}

surfaces.push_back(SDL_CreateSurfaceFrom(w, h, SDL_PIXELFORMAT_ABGR8888, data->getData(), pitch));

if (surfaces.back() == nullptr)
{
for (SDL_Surface *surface : surfaces)
SDL_DestroySurface(surface);
throw love::Exception("Cannot create cursor: out of memory.");
}
}

// Add alternate representations for the OS to use in different DPI scales.
for (size_t i = 1; i < surfaces.size(); i++)
SDL_AddSurfaceAlternateImage(surfaces[0], surfaces[i]);

cursor = SDL_CreateColorCursor(surfaces[0], hotx, hoty);

for (SDL_Surface *surface : surfaces)
SDL_DestroySurface(surface);

if (cursor == nullptr)
throw love::Exception("Cannot create cursor: %s", SDL_GetError());
}

Expand All @@ -61,7 +87,7 @@ Cursor::Cursor(mouse::Cursor::SystemCursor cursortype)
else
throw love::Exception("Cannot create system cursor: invalid type.");

if (!cursor)
if (cursor == nullptr)
throw love::Exception("Cannot create system cursor: %s", SDL_GetError());
}

Expand Down
4 changes: 3 additions & 1 deletion src/modules/mouse/sdl/Cursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
// SDL
#include <SDL3/SDL_mouse.h>

#include <vector>

namespace love
{
namespace mouse
Expand All @@ -39,7 +41,7 @@ class Cursor : public love::mouse::Cursor
{
public:

Cursor(image::ImageData *imageData, int hotx, int hoty);
Cursor(const std::vector<image::ImageData *> &imageData, int hotx, int hoty);
Cursor(SystemCursor cursortype);
~Cursor();

Expand Down
2 changes: 1 addition & 1 deletion src/modules/mouse/sdl/Mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Mouse::~Mouse()
SDL_QuitSubSystem(SDL_INIT_VIDEO);
}

love::mouse::Cursor *Mouse::newCursor(love::image::ImageData *data, int hotx, int hoty)
love::mouse::Cursor *Mouse::newCursor(const std::vector<image::ImageData *> &data, int hotx, int hoty)
{
return new Cursor(data, hotx, hoty);
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/mouse/sdl/Mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Mouse : public love::mouse::Mouse
Mouse();
virtual ~Mouse();

love::mouse::Cursor *newCursor(love::image::ImageData *data, int hotx, int hoty) override;
love::mouse::Cursor *newCursor(const std::vector<image::ImageData *> &data, int hotx, int hoty) override;
love::mouse::Cursor *getSystemCursor(Cursor::SystemCursor cursortype) override;

void setCursor(love::mouse::Cursor *cursor) override;
Expand Down
48 changes: 44 additions & 4 deletions src/modules/mouse/wrap_Mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,55 @@ namespace mouse
int w_newCursor(lua_State *L)
{
Cursor *cursor = nullptr;
std::vector<love::image::ImageData *> data;

if (lua_isstring(L, 1) || luax_istype(L, 1, love::filesystem::File::type) || luax_istype(L, 1, love::filesystem::FileData::type))
luax_convobj(L, 1, "image", "newImageData");
if (lua_istable(L, 1))
{
// Do some type checking first, because memory will leak if we hit an error in the loop after this.
for (size_t i = 1; i <= luax_objlen(L, 1); i++)
{
lua_rawgeti(L, 1, i);

if (!luax_istype(L, -1, love::image::ImageData::type)
&& !(lua_isstring(L, -1) || luax_istype(L, -1, love::filesystem::File::type) || luax_istype(L, -1, love::filesystem::FileData::type)))
{
luax_checktype<love::image::ImageData>(L, -1);
}

lua_pop(L, 1);
}

for (size_t i = 1; i <= luax_objlen(L, 1); i++)
{
lua_rawgeti(L, 1, i);

if (lua_isstring(L, -1) || luax_istype(L, -1, love::filesystem::File::type) || luax_istype(L, -1, love::filesystem::FileData::type))
luax_convobj(L, -1, "image", "newImageData");

data.push_back(luax_checktype<love::image::ImageData>(L, -1));

// If a GC step happens within the loop, previous ImageData objects created within the loop may be released.
data.back()->retain();

lua_pop(L, 1);
}
}
else
{
if (lua_isstring(L, 1) || luax_istype(L, 1, love::filesystem::File::type) || luax_istype(L, 1, love::filesystem::FileData::type))
luax_convobj(L, 1, "image", "newImageData");

data.push_back(luax_checktype<love::image::ImageData>(L, 1));
data.back()->retain();
}

love::image::ImageData *data = luax_checktype<love::image::ImageData>(L, 1);
int hotx = (int) luaL_optinteger(L, 2, 0);
int hoty = (int) luaL_optinteger(L, 3, 0);

luax_catchexcept(L, [&](){ cursor = instance()->newCursor(data, hotx, hoty); });
luax_catchexcept(L,
[&](){ cursor = instance()->newCursor(data, hotx, hoty); },
[&](bool /*shoulderror*/) { for (auto d : data) d->release(); }
);

luax_pushtype(L, cursor);
cursor->release();
Expand Down

0 comments on commit 7076688

Please sign in to comment.