Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Code cleanup and consistency.
  • Loading branch information
michael-fadely committed Sep 8, 2015
1 parent 4a27d13 commit 0922dfe
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 36 deletions.
62 changes: 32 additions & 30 deletions SADXModLoader/AutoMipmap.cpp
@@ -1,8 +1,12 @@
#include "stdafx.h"

#include <d3dx8tex.h>
#include <SADXModLoader.h>

#include "AutoMipmap.h"

static bool mipmapsEnabled = false;

inline void SetSurface(IDirect3DTexture8* d3d_texture, NJS_TEXSURFACE* texsurface)
{
D3DSURFACE_DESC info;
Expand All @@ -17,24 +21,19 @@ inline void SetSurface(IDirect3DTexture8* d3d_texture, NJS_TEXSURFACE* texsurfac
/// </summary>
/// <param name="d3d_texture">The DirectX texture to apply mipmaps to.</param>
/// <param name="njs_texture">The Dremcast texture to receive the DirectX texture.</param>
/// <returns></returns>
static void __fastcall GenerateMipmaps_c(IDirect3DTexture8* d3d_texture, NJS_TEXMEMLIST* njs_texture)
{
if (d3d_texture == nullptr || njs_texture == nullptr)
return;

if (d3d_texture->GetLevelCount() < 2)
// TODO: Figure out how Chao palettized textures are handled.
Uint32 format = njs_texture->texinfo.texsurface.PixelFormat;
if (format == NJD_PIXELFORMAT_PALETTIZED_4BPP || format == NJD_PIXELFORMAT_PALETTIZED_8BPP || d3d_texture->GetLevelCount() < 2)
{
SetSurface(d3d_texture, &njs_texture->texinfo.texsurface);
return;
}

Uint32 format = njs_texture->texinfo.texsurface.PixelFormat;

// TODO: Figure out how Chao palettized textures are handled.
if (format == NJD_PIXELFORMAT_PALETTIZED_4BPP || format == NJD_PIXELFORMAT_PALETTIZED_8BPP)
PrintDebug("Palettized texture detected.\n");

// TODO: Consider different filtering. By default, it's D3DX_FILTER_BOX | D3DX_FILTER_DITHER
HRESULT result = D3DXFilterTexture(d3d_texture, nullptr, D3DX_DEFAULT, D3DX_DEFAULT);

Expand All @@ -44,29 +43,11 @@ static void __fastcall GenerateMipmaps_c(IDirect3DTexture8* d3d_texture, NJS_TEX
SetSurface(d3d_texture, &njs_texture->texinfo.texsurface);
}

void* GenerateMipmaps_ret = (void*)0x0078CD37;
static void __declspec(naked) GenerateMipmaps_asm()
{
// This could probably use some optimizing.
__asm
{
mov edx, esi
mov ecx, eax

push eax
call GenerateMipmaps_c
pop eax

jmp GenerateMipmaps_ret
}
}

bool mipmapsEnabled = false;
/// <summary>
/// Disables mipmaps for palettized textures as a work around.
/// </summary>
/// <param name="format">The texture's pixel format.</param>
static void _stdcall EnableMipmaps(uint32_t format)
static void __stdcall EnableMipmaps(uint32_t format)
{
if (format == NJD_PIXELFORMAT_PALETTIZED_4BPP || format == NJD_PIXELFORMAT_PALETTIZED_8BPP)
{
Expand All @@ -87,7 +68,26 @@ static void _stdcall EnableMipmaps(uint32_t format)
}
}

void* EnableMipmaps_ret = (void*)0x0078C889;
#pragma region Assembly

static void* GenerateMipmaps_return = (void*)0x0078CD37;
static void __declspec(naked) GenerateMipmaps_asm()
{
// This could probably use some optimizing.
__asm
{
mov edx, esi
mov ecx, eax

push eax
call GenerateMipmaps_c
pop eax

jmp GenerateMipmaps_return
}
}

static void* EnableMipmaps_return = (void*)0x0078C889;
static void __declspec(naked) EnableMipmaps_asm()
{
__asm
Expand All @@ -104,12 +104,14 @@ static void __declspec(naked) EnableMipmaps_asm()
push ebx
push ebp
mov ebp, [esp+1Ch+0Ch]
jmp EnableMipmaps_ret
jmp EnableMipmaps_return
}
}

#pragma endregion

void ConfigureAutoMipmaps()
{
WriteJump((void*)0x0078CD2A, GenerateMipmaps_asm); // Hooks the end of the function that converts PVRs to D3D textures
WriteJump((void*)0x0078C880, EnableMipmaps_asm);
WriteJump((void*)0x0078C880, EnableMipmaps_asm); // Toggles mipmaps off for palettized textures, on otherwise.
}
8 changes: 4 additions & 4 deletions SADXModLoader/FixFOV.cpp
Expand Up @@ -59,7 +59,7 @@ static void __cdecl SetHorizontalFOV_BAMS_hook(int bams)
last_bams = bams;
}

#pragma region assembly
#pragma region Assembly

void* setfov_return = (void*)0x00791251;
static void __declspec(naked) SetFOV()
Expand All @@ -78,9 +78,8 @@ static void __declspec(naked) SetFOV()
}

// HACK: Dirty hack to disable a write to ClippingRelated and keep the floating point stack balanced.
void* fix_to = (void*)0x00781529;
float dummy;

void* fix_to = (void*)0x00781529;
static void __declspec(naked) FixFloatStackPls()
{
__asm
Expand All @@ -90,6 +89,7 @@ static void __declspec(naked) FixFloatStackPls()
}
}

// Handles a case where the FOV is accessed directly. If somebody wants to name this, be my guest :V
void* dothething_return = (void*)0x0040872F;
static void __declspec(naked) dothething()
{
Expand Down Expand Up @@ -125,7 +125,7 @@ void ConfigureFOV()

SetHorizontalFOV_BAMS_hook(bams_default);

/* Stops the Pause Menu from using horizontal stretch in place of vertical stretch in coordinate calculation */
// Stops the Pause Menu from using horizontal stretch in place of vertical stretch in coordinate calculation
WriteData((uint8_t*)0x00457F69, (uint8_t)0xC4); // Elipse/Oval
WriteData((uint8_t*)0x004584EE, (uint8_t)0xC4); // Blue Transparent Box
WriteData((uint8_t*)0x0045802F, (uint8_t)0xC4); // Pause Menu Options
Expand Down
6 changes: 5 additions & 1 deletion SADXModLoader/TextureReplacement.cpp
Expand Up @@ -126,8 +126,10 @@ bool ParseIndex(const std::wstring& path, std::vector<CustomTextureEntry>& out)
return true;
}

#ifdef _DEBUG
if (!wasLoading)
PrintDebug("\tBuilding cache...\n");
#endif

wasLoading = LoadingFile;

Expand Down Expand Up @@ -202,7 +204,9 @@ void CheckCache()
{
if (wasLoading)
{
#ifdef _DEBUG
PrintDebug("\tClearing cache...\n");
#endif
entryCache.clear();
}

Expand Down Expand Up @@ -243,6 +247,7 @@ NJS_TEXMEMLIST* LoadTexture(const std::wstring& _path, uint32_t globalIndex, con
{
IDirect3DTexture8* d3dtexture;
// This loads the DDS/PNG/etc texture from disk.
// TODO: If/When auto-mipmaps are configurable, switch to *Ex and enable/disable mipmap generation accordingly.
HRESULT result = D3DXCreateTextureFromFile(Direct3D_Device, path.c_str(), &d3dtexture);

if (result != D3D_OK)
Expand Down Expand Up @@ -356,7 +361,6 @@ void __cdecl LoadPVM_C(const char* pvmName, NJS_TEXLIST* texList)
}

PrintDebug("Unable to locate PVM: %s\n", pvmName);
return;
}

#pragma endregion
Expand Down
5 changes: 4 additions & 1 deletion SADXModLoader/dllmain.cpp
Expand Up @@ -1869,7 +1869,6 @@ static void __cdecl InitMods(void)
WriteJump((void*)0x004210A0, LoadPVM_C); // Texture packs
WriteJump((void*)0x0077FC80, LoadPVRFile); // Texture packs
WriteJump((void*)0x004228E0, LoadPVR_wrapper); // Texture packs
ConfigureAutoMipmaps();

// Unprotect the .rdata section.
// TODO: Get .rdata address and length dynamically.
Expand All @@ -1881,6 +1880,10 @@ static void __cdecl InitMods(void)
WriteData((uint8_t*)0x0078B7D8, (uint8_t)0x02);
WriteData((uint8_t*)0x0078B7EC, (uint8_t)0x02);

// TODO: Make configurable
// Auto-Mipmaps
ConfigureAutoMipmaps();

// Map of files to replace and/or swap.
// This is done with a second map instead of sadx_fileMap directly
// in order to handle multiple mods.
Expand Down

0 comments on commit 0922dfe

Please sign in to comment.