Skip to content

Commit

Permalink
Add support for SetBackground() / GetBackground()
Browse files Browse the repository at this point in the history
  • Loading branch information
elishacloud committed Jul 31, 2024
1 parent 221a13c commit c67a90c
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 56 deletions.
2 changes: 1 addition & 1 deletion Dllmain/BuildNo.rc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define BUILD_NUMBER 7076
#define BUILD_NUMBER 7077
35 changes: 28 additions & 7 deletions ddraw/IDirect3DDeviceX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,11 +545,19 @@ HRESULT m_IDirect3DDeviceX::SwapTextureHandles(LPDIRECT3DTEXTURE2 lpD3DTex1, LPD
}

// If handles are found, swap them
std::swap(it1->second, it2->second);
DWORD Handle1 = it1->first, Handle2 = it2->first;
SetTextureHandle(Handle1, pTextureX2);
SetTextureHandle(Handle2, pTextureX1);

// Update handles associated with textures
pTextureX1->SetHandle(it1->first);
pTextureX2->SetHandle(it2->first);
pTextureX1->SetHandle(Handle2);
pTextureX2->SetHandle(Handle1);

// If texture handle is set then use new texture
if (rsTextureHandle == Handle1 || rsTextureHandle == Handle2)
{
SetRenderState(D3DRENDERSTATE_TEXTUREHANDLE, rsTextureHandle);
}

return D3D_OK;
}
Expand Down Expand Up @@ -1561,8 +1569,17 @@ HRESULT m_IDirect3DDeviceX::SetCurrentViewport(LPDIRECT3DVIEWPORT3 lpd3dViewport
{
lpCurrentViewport = lpd3dViewport;

lpCurrentViewport->QueryInterface(IID_GetInterfaceX, (LPVOID*)&lpCurrentViewportX);

lpCurrentViewport->AddRef();
}

BOOL Valid = FALSE;
D3DMATERIALHANDLE hMat = NULL;
if (SUCCEEDED(lpd3dViewport->GetBackground(&hMat, &Valid)) && Valid)
{
SetLightState(D3DLIGHTSTATE_MATERIAL, hMat);
}
}

return hr;
Expand Down Expand Up @@ -2002,7 +2019,7 @@ HRESULT m_IDirect3DDeviceX::GetLightState(D3DLIGHTSTATETYPE dwLightStateType, LP
switch (dwLightStateType)
{
case D3DLIGHTSTATE_MATERIAL:
*lpdwLightState = lsMaterial;
*lpdwLightState = lsMaterialHandle;
return D3D_OK;
case D3DLIGHTSTATE_AMBIENT:
RenderState = D3DRENDERSTATE_AMBIENT;
Expand Down Expand Up @@ -2065,7 +2082,7 @@ HRESULT m_IDirect3DDeviceX::SetLightState(D3DLIGHTSTATETYPE dwLightStateType, DW

if (dwLightState == NULL)
{
Material = defaultMaterial;
ConvertMaterial(Material, *(D3DMATERIAL7*)&DefaultMaterial);
}
else if (MaterialHandleMap.find(dwLightState) != MaterialHandleMap.end())
{
Expand Down Expand Up @@ -2099,7 +2116,7 @@ HRESULT m_IDirect3DDeviceX::SetLightState(D3DLIGHTSTATETYPE dwLightStateType, DW
SetRenderState(D3DRENDERSTATE_TEXTUREHANDLE, Material.hTexture);
}

lsMaterial = dwLightState;
lsMaterialHandle = dwLightState;

return D3D_OK;
}
Expand Down Expand Up @@ -4111,7 +4128,7 @@ void m_IDirect3DDeviceX::SetDefaults()
D3DClipStatus = {};

// Light states
lsMaterial = 0;
lsMaterialHandle = NULL;

// Render states
rsAntiAliasChanged = true;
Expand All @@ -4134,6 +4151,10 @@ void m_IDirect3DDeviceX::SetDefaults()
SetTextureStageState(4, D3DTSS_TEXCOORDINDEX, 0);
SetTextureStageState(5, D3DTSS_TEXCOORDINDEX, 0);
SetTextureStageState(6, D3DTSS_TEXCOORDINDEX, 0);

// Get default structures
(*d3d9Device)->GetMaterial(&DefaultMaterial);
(*d3d9Device)->GetViewport(&DefaultViewport);
}

inline void m_IDirect3DDeviceX::SetDrawStates(DWORD dwVertexTypeDesc, DWORD& dwFlags, DWORD DirectXVersion)
Expand Down
13 changes: 12 additions & 1 deletion ddraw/IDirect3DDeviceX.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class m_IDirect3DDeviceX : public IUnknown, public AddressLookupTableDdrawObject
LPDIRECT3DDEVICE9 *d3d9Device = nullptr;
LPDIRECT3DPIXELSHADER9* colorkeyPixelShader = nullptr;
LPDIRECT3DVIEWPORT3 lpCurrentViewport = nullptr;
m_IDirect3DViewportX* lpCurrentViewportX = nullptr;

struct {
DWORD rsClipping = 0;
Expand All @@ -49,7 +50,7 @@ class m_IDirect3DDeviceX : public IUnknown, public AddressLookupTableDdrawObject
D3DCLIPSTATUS D3DClipStatus;

// Light states
DWORD lsMaterial;
DWORD lsMaterialHandle;

// Render states
bool rsAntiAliasChanged;
Expand All @@ -67,6 +68,10 @@ class m_IDirect3DDeviceX : public IUnknown, public AddressLookupTableDdrawObject
DWORD rsColorKeyEnabled;
DWORD ssMipFilter[MaxTextureStages] = {};

// Default settings
D3DMATERIAL9 DefaultMaterial = {};
D3DVIEWPORT9 DefaultViewport = {};

// SetTexture array
LPDIRECTDRAWSURFACE7 CurrentRenderTarget = nullptr;
m_IDirectDrawSurfaceX* CurrentTextureSurfaceX[MaxTextureStages] = {};
Expand Down Expand Up @@ -274,13 +279,19 @@ class m_IDirect3DDeviceX : public IUnknown, public AddressLookupTableDdrawObject
ULONG Release(DWORD DirectXVersion);
bool IsDeviceInScene() { return IsInScene; }

// Viewport functions
inline void GetDefaultViewport(D3DVIEWPORT9& Viewport) { Viewport = DefaultViewport; }
inline bool CheckIfViewportSet(m_IDirect3DViewportX* pViewport) { return (pViewport == lpCurrentViewportX); }

// Texture handle function
void ReleaseTextureHandle(m_IDirect3DTextureX* lpTexture);
HRESULT SetTextureHandle(DWORD tHandle, m_IDirect3DTextureX* lpTexture);

// Material handle function
inline void GetDefaultMaterial(D3DMATERIAL9& Material) { Material = DefaultMaterial; }
void ReleaseMaterialHandle(m_IDirect3DMaterialX* lpMaterial);
HRESULT SetMaterialHandle(D3DMATERIALHANDLE mHandle, m_IDirect3DMaterialX* lpMaterial);
inline bool CheckIfMaterialSet(D3DMATERIALHANDLE mHandle) { return (mHandle == lsMaterialHandle); }

// Light index function
void ReleaseLightInterface(m_IDirect3DLight* lpLight);
Expand Down
36 changes: 24 additions & 12 deletions ddraw/IDirect3DMaterialX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,22 +174,17 @@ HRESULT m_IDirect3DMaterialX::SetMaterial(LPD3DMATERIAL lpMat)
return DDERR_GENERIC;
}

// If material is in use then set new material
if (mHandle)
// If current material is set then use new material
if (mHandle && (*D3DDeviceInterface)->CheckIfMaterialSet(mHandle))
{
D3DMATERIALHANDLE Handle = 0;
if (SUCCEEDED((*D3DDeviceInterface)->GetLightState(D3DLIGHTSTATE_MATERIAL, &Handle)))
if (FAILED((*D3DDeviceInterface)->SetMaterial(lpMat)))
{
if (mHandle == Handle)
{
if (FAILED((*D3DDeviceInterface)->SetMaterial(lpMat)))
{
return DDERR_GENERIC;
}
}
return DDERR_GENERIC;
}
}

IsMaterialSet = true;

Material = *lpMat;

return D3D_OK;
Expand Down Expand Up @@ -220,7 +215,24 @@ HRESULT m_IDirect3DMaterialX::GetMaterial(LPD3DMATERIAL lpMat)
return DDERR_INVALIDPARAMS;
}

*lpMat = Material;
if (IsMaterialSet)
{
*lpMat = Material;
}
else
{
if (!D3DDeviceInterface || !*D3DDeviceInterface)
{
LOG_LIMIT(100, __FUNCTION__ << " Error: no D3DirectDevice interface!");
return DDERR_GENERIC;
}

D3DMATERIAL7 Material7 = {};

(*D3DDeviceInterface)->GetDefaultMaterial(*(D3DMATERIAL9*)&Material7);

ConvertMaterial(*lpMat, Material7);
}

return D3D_OK;
}
Expand Down
3 changes: 2 additions & 1 deletion ddraw/IDirect3DMaterialX.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class m_IDirect3DMaterialX : public IUnknown, public AddressLookupTableDdrawObje

// Convert Material
m_IDirect3DDeviceX **D3DDeviceInterface = nullptr;
D3DMATERIAL Material = defaultMaterial;
bool IsMaterialSet = false;
D3DMATERIAL Material = {};
D3DMATERIALHANDLE mHandle = 0;

// Store d3d material version wrappers
Expand Down
28 changes: 16 additions & 12 deletions ddraw/IDirect3DTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,6 @@

#include "ddraw.h"

// Define a default material with neutral properties
D3DMATERIAL defaultMaterial = {
sizeof(D3DMATERIAL),
{ 0.0f, 0.0f, 0.0f, 1.0f }, // Diffuse color (black)
{ 0.0f, 0.0f, 0.0f, 1.0f }, // Ambient color (black)
{ 0.0f, 0.0f, 0.0f, 1.0f }, // Specular color (black)
{ 0.0f, 0.0f, 0.0f, 1.0f }, // Emissive color (black)
0.0f, // Power (shininess)
0, // Texture handle
0 // Ramp size
};

void ConvertLight(D3DLIGHT7& Light7, D3DLIGHT& Light)
{
if (Light.dwSize != sizeof(D3DLIGHT) && Light.dwSize != sizeof(D3DLIGHT2))
Expand Down Expand Up @@ -60,6 +48,22 @@ void ConvertLight(D3DLIGHT7& Light7, D3DLIGHT& Light)
}
}

void ConvertMaterial(D3DMATERIAL& Material, D3DMATERIAL7& Material7)
{
if (Material.dwSize != sizeof(D3DMATERIAL))
{
LOG_LIMIT(100, __FUNCTION__ << " Error: Incorrect dwSize: " << Material.dwSize);
return;
}
Material.dcvDiffuse = Material7.dcvDiffuse;
Material.dcvAmbient = Material7.dcvAmbient;
Material.dcvSpecular = Material7.dcvSpecular;
Material.dcvEmissive = Material7.dcvEmissive;
Material.dvPower = Material7.dvPower;
Material.hTexture = NULL;
Material.dwRampSize = 0;
}

void ConvertMaterial(D3DMATERIAL7 &Material7, D3DMATERIAL &Material)
{
if (Material.dwSize != sizeof(D3DMATERIAL))
Expand Down
3 changes: 1 addition & 2 deletions ddraw/IDirect3DTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ typedef enum _D3DSURFACETYPE {
D3DTYPE_DEPTHBUFFER = 4
} D3DSURFACETYPE;

extern D3DMATERIAL defaultMaterial;

void ConvertLight(D3DLIGHT7& Light7, D3DLIGHT& Light);
void ConvertMaterial(D3DMATERIAL& Material, D3DMATERIAL7& Material7);
void ConvertMaterial(D3DMATERIAL7 &Material7, D3DMATERIAL &Material);
void ConvertViewport(D3DVIEWPORT &ViewPort, D3DVIEWPORT2 &ViewPort2);
void ConvertViewport(D3DVIEWPORT2 &ViewPort2, D3DVIEWPORT &ViewPort);
Expand Down
Loading

0 comments on commit c67a90c

Please sign in to comment.