From 2cf15cebc8e1e0fa42d7e3d485049e52133b967d Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Sat, 20 May 2023 14:26:22 +0100 Subject: [PATCH] Add optional debug logging for hwcursor To help debug hardware cursor issues --- Source/hwcursor.cpp | 31 ++++++++++++++++++++++++++----- Source/hwcursor.hpp | 18 ++++++++++++++++++ 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/Source/hwcursor.cpp b/Source/hwcursor.cpp index f484f07f7..268a34dbd 100644 --- a/Source/hwcursor.cpp +++ b/Source/hwcursor.cpp @@ -69,13 +69,16 @@ bool ShouldUseBilinearScaling() return *sgOptions.Graphics.scaleQuality != ScalingQuality::NearestPixel; } -bool SetHardwareCursor(SDL_Surface *surface, HotpointPosition hotpointPosition) +bool SetHardwareCursorFromSurface(SDL_Surface *surface, HotpointPosition hotpointPosition) { SDLCursorUniquePtr newCursor; const Size size { surface->w, surface->h }; const Size scaledSize = ScaledSize(size); if (size == scaledSize) { +#if LOG_HWCURSOR + Log("hwcursor: SetHardwareCursorFromSurface {}x{}", size.width, size.height); +#endif const Point hotpoint = GetHotpointPosition(*surface, hotpointPosition); newCursor = SDLCursorUniquePtr { SDL_CreateColorCursor(surface, hotpoint.x, hotpoint.y) }; } else { @@ -84,8 +87,16 @@ bool SetHardwareCursor(SDL_Surface *surface, HotpointPosition hotpointPosition) SDLSurfaceUniquePtr scaledSurface = SDLWrap::CreateRGBSurfaceWithFormat(0, scaledSize.width, scaledSize.height, 32, SDL_PIXELFORMAT_ARGB8888); if (ShouldUseBilinearScaling()) { +#if LOG_HWCURSOR + Log("hwcursor: SetHardwareCursorFromSurface {}x{} scaled to {}x{} using bilinear scaling", + size.width, size.height, scaledSize.width, scaledSize.height); +#endif BilinearScale32(converted.get(), scaledSurface.get()); } else { +#if LOG_HWCURSOR + Log("hwcursor: SetHardwareCursorFromSurface {}x{} scaled to {}x{} using nearest neighbour scaling", + size.width, size.height, scaledSize.width, scaledSize.height); +#endif SDL_BlitScaled(converted.get(), nullptr, scaledSurface.get(), nullptr); } const Point hotpoint = GetHotpointPosition(*scaledSurface, hotpointPosition); @@ -98,13 +109,13 @@ bool SetHardwareCursor(SDL_Surface *surface, HotpointPosition hotpointPosition) return true; } -bool SetHardwareCursor(ClxSprite sprite, HotpointPosition hotpointPosition) +bool SetHardwareCursorFromClxSprite(ClxSprite sprite, HotpointPosition hotpointPosition) { OwnedSurface surface { sprite.width(), sprite.height() }; SDL_SetSurfacePalette(surface.surface, Palette.get()); SDL_SetColorKey(surface.surface, SDL_TRUE, 0); RenderClxSprite(surface, sprite, { 0, 0 }); - return SetHardwareCursor(surface.surface, hotpointPosition); + return SetHardwareCursorFromSurface(surface.surface, hotpointPosition); } bool SetHardwareCursorFromSprite(int pcurs) @@ -132,7 +143,8 @@ bool SetHardwareCursorFromSprite(int pcurs) SDL_SetColorKey(out.surface, 1, TransparentColor); DrawSoftwareCursor(out, { outlineWidth, size.height - outlineWidth }, pcurs); - const bool result = SetHardwareCursor(out.surface, isItem ? HotpointPosition::Center : HotpointPosition::TopLeft); + const bool result = SetHardwareCursorFromSurface( + out.surface, isItem ? HotpointPosition::Center : HotpointPosition::TopLeft); return result; } #endif @@ -150,16 +162,25 @@ void SetHardwareCursor(CursorInfo cursorInfo) CurrentCursorInfo = cursorInfo; switch (cursorInfo.type()) { case CursorType::Game: +#if LOG_HWCURSOR + Log("hwcursor: SetHardwareCursor Game"); +#endif CurrentCursorInfo.SetEnabled(SetHardwareCursorFromSprite(cursorInfo.id())); break; case CursorType::UserInterface: +#if LOG_HWCURSOR + Log("hwcursor: SetHardwareCursor UserInterface"); +#endif // ArtCursor is null while loading the game on the progress screen, // called via palette fade from ShowProgress. CurrentCursorInfo.SetEnabled( ArtCursor && IsCursorSizeAllowed(Size { (*ArtCursor)[0].width(), (*ArtCursor)[0].height() }) - && SetHardwareCursor((*ArtCursor)[0], HotpointPosition::TopLeft)); + && SetHardwareCursorFromClxSprite((*ArtCursor)[0], HotpointPosition::TopLeft)); break; case CursorType::Unknown: +#if LOG_HWCURSOR + Log("hwcursor: SetHardwareCursor Unknown"); +#endif CurrentCursorInfo.SetEnabled(false); break; } diff --git a/Source/hwcursor.hpp b/Source/hwcursor.hpp index 998259044..4d82596b0 100644 --- a/Source/hwcursor.hpp +++ b/Source/hwcursor.hpp @@ -11,6 +11,12 @@ #include "options.h" +// Set this to 1 to log the hardware cursor state changes. +#define LOG_HWCURSOR 0 +#if LOG_HWCURSOR +#include "utils/log.hpp" +#endif + namespace devilution { // Whether the hardware cursor is enabled in settings. @@ -29,6 +35,12 @@ inline bool IsHardwareCursorEnabled() inline bool SetHardwareCursorVisible(bool visible) { #if SDL_VERSION_ATLEAST(2, 0, 0) +#if LOG_HWCURSOR + const bool isVisible = SDL_ShowCursor(SDL_QUERY) == SDL_ENABLE; + if (isVisible != visible) { + Log("hwcursor: SetHardwareCursorVisible {}", visible); + } +#endif return SDL_ShowCursor(visible ? SDL_ENABLE : SDL_DISABLE) == 1; #else return false; @@ -77,6 +89,9 @@ public: void SetEnabled(bool value) { +#if LOG_HWCURSOR + Log("hwcursor: SetEnabled {}", value); +#endif enabled_ = value; } @@ -117,6 +132,9 @@ void SetHardwareCursor(CursorInfo cursorInfo); inline void ReinitializeHardwareCursor() { +#if LOG_HWCURSOR + Log("hwcursor: ReinitializeHardwareCursor"); +#endif SetHardwareCursor(GetCurrentCursorInfo()); }