Skip to content

Commit

Permalink
Fixes #215: Save the texture of the mouse cursor and restore it when …
Browse files Browse the repository at this point in the history
…the device has to be reset.
  • Loading branch information
shartte committed Apr 19, 2016
1 parent d555cdd commit 5553c7f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
8 changes: 8 additions & 0 deletions Infrastructure/include/graphics/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -193,6 +198,9 @@ namespace gfx {
CComPtr<IDirect3DSurface9> mSceneSurface;
CComPtr<IDirect3DSurface9> mSceneDepthSurface;

gfx::TextureRef mCursor;
XMINT2 mCursorHotspot;

std::list<ResourceListener*> mResourcesListeners;
bool mResourcesCreated = false;

Expand Down
23 changes: 23 additions & 0 deletions Infrastructure/src/graphics/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<IDirect3DSurface9> 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<const uint8_t> data) {
CComPtr<IDirect3DVertexBuffer9> result;

Expand Down
12 changes: 1 addition & 11 deletions TemplePlus/tig/tig_mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,6 @@ static bool SetCursorFromShaderId(int shaderId) {
return false;
}

auto deviceTexture = primaryTexture->GetDeviceTexture();
CComPtr<IDirect3DSurface9> 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;

Expand All @@ -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;
}

Expand Down

0 comments on commit 5553c7f

Please sign in to comment.