Skip to content

Commit

Permalink
lightgun xhair: centralize logic in crosshairNeeded()
Browse files Browse the repository at this point in the history
  • Loading branch information
flyinghead committed Dec 13, 2024
1 parent 686f08e commit 421245a
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 87 deletions.
6 changes: 0 additions & 6 deletions core/hw/maple/maple_cfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,12 +405,6 @@ void mcfg_CreateDevices()
die("Unknown system");
break;
}
if (settings.platform.isArcade() && !settings.input.fourPlayerGames)
{
// No known 4-player lightgun/touchscreen game so far
config::CrosshairColor[2].override(0);
config::CrosshairColor[3].override(0);
}
vmuDigest();
}

Expand Down
53 changes: 25 additions & 28 deletions core/rend/dx11/dx11_overlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,39 +142,36 @@ void DX11Overlay::draw(u32 width, u32 height, bool vmu, bool crosshair)
quad.draw(vmuTextureViews[i], samplers->getSampler(false));
}
}
if (crosshair && crosshairsNeeded())
if (crosshair)
{
if (!xhairTexture)
for (u32 i = 0; i < config::CrosshairColor.size(); i++)
{
const u32* texData = getCrosshairTextureData();
D3D11_TEXTURE2D_DESC desc{};
desc.Width = 16;
desc.Height = 16;
desc.ArraySize = 1;
desc.SampleDesc.Count = 1;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.MipLevels = 1;

if (SUCCEEDED(device->CreateTexture2D(&desc, nullptr, &xhairTexture.get())))
if (!crosshairNeeded(i))
continue;
if (!xhairTexture)
{
D3D11_SHADER_RESOURCE_VIEW_DESC viewDesc{};
viewDesc.Format = desc.Format;
viewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
viewDesc.Texture2D.MipLevels = desc.MipLevels;
device->CreateShaderResourceView(xhairTexture, &viewDesc, &xhairTextureView.get());
const u32* texData = getCrosshairTextureData();
D3D11_TEXTURE2D_DESC desc{};
desc.Width = 16;
desc.Height = 16;
desc.ArraySize = 1;
desc.SampleDesc.Count = 1;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.MipLevels = 1;

if (SUCCEEDED(device->CreateTexture2D(&desc, nullptr, &xhairTexture.get())))
{
D3D11_SHADER_RESOURCE_VIEW_DESC viewDesc{};
viewDesc.Format = desc.Format;
viewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
viewDesc.Texture2D.MipLevels = desc.MipLevels;
device->CreateShaderResourceView(xhairTexture, &viewDesc, &xhairTextureView.get());

deviceContext->UpdateSubresource(xhairTexture, 0, nullptr, texData, 16 * 4, 16 * 4 * 16);
deviceContext->UpdateSubresource(xhairTexture, 0, nullptr, texData, 16 * 4, 16 * 4 * 16);
}
}
}
for (u32 i = 0; i < config::CrosshairColor.size(); i++)
{
if (config::CrosshairColor[i] == 0)
continue;
if (settings.platform.isConsole()
&& config::MapleMainDevices[i] != MDT_LightGun)
continue;

auto [x, y] = getCrosshairPosition(i);
#ifdef LIBRETRO
Expand Down
10 changes: 4 additions & 6 deletions core/rend/dx11/dx11context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,19 +253,17 @@ void DX11Context::EndImGuiFrame()
{
if (pDevice && pDeviceContext && renderTargetView)
{
if (!overlayOnly)
if (overlayOnly) {
overlay.draw(settings.display.width, settings.display.height, config::FloatVMUs, true);
}
else
{
pDeviceContext->OMSetRenderTargets(1, &renderTargetView.get(), nullptr);
const FLOAT black[4] { 0.f, 0.f, 0.f, 1.f };
pDeviceContext->ClearRenderTargetView(renderTargetView, black);
if (renderer != nullptr)
renderer->RenderLastFrame();
}
if (overlayOnly)
{
if (crosshairsNeeded() || config::FloatVMUs)
overlay.draw(settings.display.width, settings.display.height, config::FloatVMUs, true);
}
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
}
frameRendered = true;
Expand Down
43 changes: 20 additions & 23 deletions core/rend/dx9/d3d_overlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,35 +87,32 @@ void D3DOverlay::draw(u32 width, u32 height, bool vmu, bool crosshair)
drawQuad(rect, D3DCOLOR_ARGB(192, 255, 255, 255));
}
}
if (crosshair && crosshairsNeeded())
if (crosshair)
{
if (!xhairTexture)
for (u32 i = 0; i < config::CrosshairColor.size(); i++)
{
const u32* texData = getCrosshairTextureData();
device->CreateTexture(16, 16, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &xhairTexture.get(), 0);
D3DLOCKED_RECT rect;
if (SUCCEEDED(xhairTexture->LockRect(0, &rect, nullptr, 0)))
if (!crosshairNeeded(i))
continue;

if (!xhairTexture)
{
if (rect.Pitch == 16 * sizeof(u32))
memcpy(rect.pBits, texData, 16 * 16 * sizeof(u32));
else
const u32* texData = getCrosshairTextureData();
device->CreateTexture(16, 16, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &xhairTexture.get(), 0);
D3DLOCKED_RECT rect;
if (SUCCEEDED(xhairTexture->LockRect(0, &rect, nullptr, 0)))
{
u8 *dst = (u8 *) rect.pBits;
for (int y = 0; y < 16; y++)
memcpy(dst + y * rect.Pitch, texData + y * 16, 16 * sizeof(u32));
if (rect.Pitch == 16 * sizeof(u32))
memcpy(rect.pBits, texData, 16 * 16 * sizeof(u32));
else
{
u8 *dst = (u8 *) rect.pBits;
for (int y = 0; y < 16; y++)
memcpy(dst + y * rect.Pitch, texData + y * 16, 16 * sizeof(u32));
}
xhairTexture->UnlockRect(0);
}
xhairTexture->UnlockRect(0);
}
}
device->SetTexture(0, xhairTexture);
for (u32 i = 0; i < config::CrosshairColor.size(); i++)
{
if (config::CrosshairColor[i] == 0)
continue;
if (settings.platform.isConsole()
&& config::MapleMainDevices[i] != MDT_LightGun)
continue;

device->SetTexture(0, xhairTexture);
auto [x, y] = getCrosshairPosition(i);
float halfWidth = config::CrosshairSize * settings.display.uiScale / 2.f;
RECT rect { (long) (x - halfWidth), (long) (y - halfWidth), (long) (x + halfWidth), (long) (y + halfWidth) };
Expand Down
5 changes: 1 addition & 4 deletions core/rend/dx9/dxcontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,7 @@ void DXContext::EndImGuiFrame()
if (SUCCEEDED(pDevice->BeginScene()))
{
if (overlayOnly)
{
if (crosshairsNeeded() || config::FloatVMUs)
overlay.draw(settings.display.width, settings.display.height, config::FloatVMUs, true);
}
overlay.draw(settings.display.width, settings.display.height, config::FloatVMUs, true);
ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData());
pDevice->EndScene();
}
Expand Down
11 changes: 2 additions & 9 deletions core/rend/gles/gldraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1015,12 +1015,6 @@ static void updateLightGunTexture()

static void drawGunCrosshair(u8 port, int width, int height)
{
if (config::CrosshairColor[port] == 0)
return;
if (settings.platform.isConsole()
&& config::MapleMainDevices[port] != MDT_LightGun)
return;

auto [x, y] = getCrosshairPosition(port);
#ifdef LIBRETRO
float halfWidth = lightgun_crosshair_size / 2.f / config::ScreenStretching * 100.f * config::RenderResolution / 480.f;
Expand Down Expand Up @@ -1073,10 +1067,9 @@ void drawVmusAndCrosshairs(int width, int height)
drawVmuTexture(i, width, height);
}

if (crosshairsNeeded()) {
for (int i = 0 ; i < 4 ; i++)
for (int i = 0 ; i < 4 ; i++)
if (crosshairNeeded(i))
drawGunCrosshair(i, width, height);
}
glCheck();
}

Expand Down
19 changes: 14 additions & 5 deletions core/rend/osd.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,23 @@ void push_vmu_screen(int bus_id, int bus_port, u8* buffer);
const u32 *getCrosshairTextureData();
std::pair<float, float> getCrosshairPosition(int playerNum);

static inline bool crosshairsNeeded()
static inline bool crosshairNeeded(int port)
{
if (config::CrosshairColor[0] == 0 && config::CrosshairColor[1] == 0
&& config::CrosshairColor[2] == 0 && config::CrosshairColor[3] == 0)
if (port < 0 || port >= 4)
return false;
if (settings.platform.isArcade() && !settings.input.lightgunGame)
// not a lightgun game
if (config::CrosshairColor[port] == 0)
return false;
if (settings.platform.isArcade())
{
// Arcade game: only for lightgun games and P1 or P2 (no known 4-player lightgun or touchscreen game for now)
if (!settings.input.lightgunGame || (port >= 2 && !settings.input.fourPlayerGames))
return false;
}
else {
// Console game
if (config::MapleMainDevices[port] != MDT_LightGun)
return false;
}
return true;
}

Expand Down
1 change: 0 additions & 1 deletion core/rend/vulkan/oit/oit_drawer.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ class OITDrawer : public BaseDrawer
SamplerManager *samplerManager = nullptr;
OITBuffers *oitBuffers = nullptr;
bool needAttachmentTransition = false;
bool needDepthTransition = false;
OITDescriptorSets descriptorSets;
vk::Buffer curMainBuffer;
bool dithering = false;
Expand Down
7 changes: 2 additions & 5 deletions core/rend/vulkan/overlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,19 +212,16 @@ void VulkanOverlay::Draw(vk::CommandBuffer commandBuffer, vk::Extent2D viewport,
drawers[i]->Draw(commandBuffer, vmuTextures[i]->GetImageView(), vtx, true, color);
}
}
if (crosshair && crosshairsNeeded())
if (crosshair)
{
pipeline->BindPipeline(commandBuffer);
bool imageViewBound = false;
for (size_t i = 0; i < config::CrosshairColor.size(); i++)
{
if (config::CrosshairColor[i] == 0)
continue;
if (settings.platform.isConsole() && config::MapleMainDevices[i] != MDT_LightGun)
if (!crosshairNeeded(i))
continue;

auto [x, y] = getCrosshairPosition(i);

#ifdef LIBRETRO
float w = lightgun_crosshair_size * scaling / config::ScreenStretching * 100.f;
float h = lightgun_crosshair_size * scaling;
Expand Down

0 comments on commit 421245a

Please sign in to comment.