From 5553c7fe6d5690eca1f502d9d957039041fdbd35 Mon Sep 17 00:00:00 2001 From: Sebastian Hartte Date: Wed, 20 Apr 2016 00:42:27 +0200 Subject: [PATCH] Fixes #215: Save the texture of the mouse cursor and restore it when the device has to be reset. --- Infrastructure/include/graphics/device.h | 8 ++++++++ Infrastructure/src/graphics/device.cpp | 23 +++++++++++++++++++++++ TemplePlus/tig/tig_mouse.cpp | 12 +----------- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/Infrastructure/include/graphics/device.h b/Infrastructure/include/graphics/device.h index 9dac076c0..704b8d8bc 100644 --- a/Infrastructure/include/graphics/device.h +++ b/Infrastructure/include/graphics/device.h @@ -133,6 +133,11 @@ namespace gfx { void SetDepthStencilState(const DepthStencilState &state); void SetSamplerState(int samplerIdx, const SamplerState &state); + /* + Changes the currently used cursor to the given surface. + */ + void SetCursor(int hotspotX, int hotspotY, const gfx::TextureRef &texture); + /* Take a screenshot with the given size. The image will be stretched to the given size. @@ -193,6 +198,9 @@ namespace gfx { CComPtr mSceneSurface; CComPtr mSceneDepthSurface; + gfx::TextureRef mCursor; + XMINT2 mCursorHotspot; + std::list mResourcesListeners; bool mResourcesCreated = false; diff --git a/Infrastructure/src/graphics/device.cpp b/Infrastructure/src/graphics/device.cpp index 4766ab58b..c1d1470d5 100644 --- a/Infrastructure/src/graphics/device.cpp +++ b/Infrastructure/src/graphics/device.cpp @@ -515,6 +515,11 @@ namespace gfx { // Retrieve new backbuffer surface AfterDeviceResetOrCreated(); + // Set the cursor again, if there was one set before + if (mCursor) { + SetCursor(mCursorHotspot.x, mCursorHotspot.y, mCursor); + } + } void RenderingDevice::SetMaterial(const Material &material) { @@ -642,6 +647,24 @@ namespace gfx { mDevice->SetSamplerState(samplerIdx, D3DSAMP_ADDRESSV, state.addressV); } + void RenderingDevice::SetCursor(int hotspotX, int hotspotY, const gfx::TextureRef & texture) + { + // Save for device reset + mCursor = texture; + mCursorHotspot = { hotspotX, hotspotY }; + + auto deviceTexture = texture->GetDeviceTexture(); + CComPtr surface; + if (D3DLOG(deviceTexture->GetSurfaceLevel(0, &surface)) != D3D_OK) { + logger->error("Unable to get surface of cursor texture."); + return; + } + + if (D3DLOG(mDevice->SetCursorProperties(hotspotX, hotspotY, surface)) != D3D_OK) { + logger->error("Unable to set cursor properties."); + } + } + VertexBufferPtr RenderingDevice::CreateVertexBufferRaw(gsl::span data) { CComPtr result; diff --git a/TemplePlus/tig/tig_mouse.cpp b/TemplePlus/tig/tig_mouse.cpp index b4b6bea0b..2b3a2de2b 100644 --- a/TemplePlus/tig/tig_mouse.cpp +++ b/TemplePlus/tig/tig_mouse.cpp @@ -63,13 +63,6 @@ static bool SetCursorFromShaderId(int shaderId) { return false; } - auto deviceTexture = primaryTexture->GetDeviceTexture(); - CComPtr surface; - if (D3DLOG(deviceTexture->GetSurfaceLevel(0, &surface)) != D3D_OK) { - logger->error("Unable to get surface of cursor texture."); - return false; - } - int hotspotX = 0; int hotspotY = 0; @@ -81,10 +74,7 @@ static bool SetCursorFromShaderId(int shaderId) { hotspotY = primaryTexture->GetContentRect().height / 2; } - auto device = tig->GetRenderingDevice().GetDevice(); - if (D3DLOG(device->SetCursorProperties(hotspotX, hotspotY, surface)) != D3D_OK) { - logger->error("Unable to set cursor properties."); - } + tig->GetRenderingDevice().SetCursor(hotspotX, hotspotY, primaryTexture); return true; }