Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor but vital fixes :P #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/IDirect3DDevice9Hook.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "IDirect3DDevice9Hook.h"
#include <algorithm>

/*** IUnknown methods ***/
STDCALL(IDirect3DDevice9Hook::QueryInterface)(THIS_ REFIID riid, void** ppvObj){
Expand Down Expand Up @@ -28,6 +29,9 @@ STDCALL(IDirect3DDevice9Hook::EvictManagedResources)(THIS){
}

STDCALL(IDirect3DDevice9Hook::GetDirect3D)(THIS_ IDirect3D9** ppD3D9){
if (!pD3D) {
return DEVICE->GetDirect3D(ppD3D9);
}
*ppD3D9 = pD3D;
return D3D_OK;
}
Expand Down Expand Up @@ -298,6 +302,70 @@ STDCALL(IDirect3DDevice9Hook::GetSamplerState)(THIS_ DWORD Sampler, D3DSAMPLERST
}

STDCALL(IDirect3DDevice9Hook::SetSamplerState)(THIS_ DWORD Sampler, D3DSAMPLERSTATETYPE Type, DWORD Value){
const int anisotropicFiltering = 1; // TODO: config...

static bool anisotropyDetectNeeded = true;
static int nMaxAnisotropy = 16;

// Disable AntiAliasing when using point filtering
//if (Config.AntiAliasing)
{
if (Type == D3DSAMP_MINFILTER || Type == D3DSAMP_MAGFILTER)
{
if (Value == D3DTEXF_NONE || Value == D3DTEXF_POINT)
{
DEVICE->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
}
else
{
DEVICE->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
}
}
}

// detect max anisotropy
if (anisotropyDetectNeeded && (Type == D3DSAMP_MAXANISOTROPY || ((Type == D3DSAMP_MINFILTER || Type == D3DSAMP_MAGFILTER) && Value == D3DTEXF_LINEAR)))
{
anisotropyDetectNeeded = false;

D3DCAPS9 Caps;
ZeroMemory(&Caps, sizeof(D3DCAPS9));
if (SUCCEEDED(DEVICE->GetDeviceCaps(&Caps)))
{
nMaxAnisotropy = (anisotropicFiltering == 1) ? Caps.MaxAnisotropy : std::min((DWORD)anisotropicFiltering, Caps.MaxAnisotropy);
}

if (nMaxAnisotropy && SUCCEEDED(DEVICE->SetSamplerState(Sampler, D3DSAMP_MAXANISOTROPY, nMaxAnisotropy)))
{
LogFile::Format("Setting Anisotropy Filtering at %dx\n", nMaxAnisotropy);
}
else
{
nMaxAnisotropy = 0;
LogFile::WriteLine("Failed to enable Anisotropy Filtering!");
}
}

// enable anisotropic filtering
if (nMaxAnisotropy)
{
if (Type == D3DSAMP_MAXANISOTROPY)
{
if (SUCCEEDED(DEVICE->SetSamplerState(Sampler, D3DSAMP_MAXANISOTROPY, nMaxAnisotropy)))
{
return D3D_OK;
}
}
else if ((Type == D3DSAMP_MINFILTER || Type == D3DSAMP_MAGFILTER) && Value == D3DTEXF_LINEAR)
{
if (SUCCEEDED(DEVICE->SetSamplerState(Sampler, D3DSAMP_MAXANISOTROPY, nMaxAnisotropy)) &&
SUCCEEDED(DEVICE->SetSamplerState(Sampler, Type, D3DTEXF_ANISOTROPIC)))
{
return D3D_OK;
}
}
}

return DEVICE->SetSamplerState(Sampler, Type, Value);
}

Expand Down
36 changes: 23 additions & 13 deletions src/patches.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,16 +541,18 @@ class effectManagerHandler : public effectManager {

int InitShaderFX()
{
bool loadedFromChunk = false;

if (gameversion == __DRIV3R_V100)
{
/* Loads shaders from AllShaders.chunk */
CFileChunker shadersChunk = CFileChunker();

int errorCode = 0;

LogFile::WriteLine("Loading shaders chunk...");
if (shadersChunk.OpenChunks("D3Hook:shaders\\AllShaders.chunk"))
{
int errorCode = 0;

int nShaders = shadersChunk.GetChunkCount();

LogFile::Format(" - Count: %d\n", nShaders);
Expand Down Expand Up @@ -585,18 +587,20 @@ class effectManagerHandler : public effectManager {
}

shadersChunk.Release();

// something went wrong, abort
if (errorCode != 0)
return errorCode;

loadedFromChunk = true;
}
else
{
MessageBoxW(hamster::GetMainWindow(), L"FATAL ERROR: Cannot open the shaders file!", L"D3Hook", MB_OK | MB_ICONERROR);
return E_FAIL;
LogFile::WriteLine("ERROR: Cannot open the shaders file located at 'D3Hook:shaders\\AllShaders.chunk'");
}

// something went wrong, abort
if (errorCode != 0)
return errorCode;
}
else

if(!loadedFromChunk)
{
for (auto shader : shader_programs) {
/* Loads shader from EXE */
Expand Down Expand Up @@ -1343,7 +1347,9 @@ class rendererHandler : public renderer {
});

// quick and dirty way to try fixing mipmaps
manager->globalstate.addSamplerState({ D3DSAMP_MIPMAPLODBIAS, -1.0f });
manager->globalstate.addSamplerState({ D3DSAMP_MIPMAPLODBIAS, -1.25f });
manager->globalstate.addSamplerState({ D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC });
manager->globalstate.addSamplerState({ D3DSAMP_MAGFILTER, D3DTEXF_ANISOTROPIC });
}

void registerStateParams(void) {
Expand Down Expand Up @@ -1743,6 +1749,11 @@ class CFileSystemPC {
if (!_strnicmp(vPath, "D3Hook:", 7)) {
std::sprintf(buffer, "%s%s", toolBase.c_str(), &vPath[7]);
}
else if (!_strnicmp(vPath, "LANG:TERR:", 10)) { // totally strange case but we have to handle it
std::sprintf(buffer, "%sTERRITORY\\%s\\LOCALE\\%s\\%s", gamePath,
config->GetTerritoryString(),
config->GetTextLanguageString(), &vPath[10]);
}
else if (!_strnicmp(vPath, "TERR:", 5)) {
std::sprintf(buffer, "%sTERRITORY\\%s\\%s", gamePath,
config->GetTerritoryString(), &vPath[5]);
Expand Down Expand Up @@ -1778,9 +1789,8 @@ class CFileSystemPC {
}

if (GetFileAttributesA(buffer) == INVALID_FILE_ATTRIBUTES) {
char pathBuf[512]{};
sprintf(pathBuf, "Game requested non existent file at %s", buffer);
MessageBoxA(nullptr, pathBuf, "D3Hook", MB_ICONWARNING | MB_OK);
LogFile::Format("WARNING: Game requested non existent file at %s", buffer);
LogFile::AppendLine();
}

return _strupr(buffer);
Expand Down