Skip to content

Commit

Permalink
Adding initial PNG/GIF/BMP texture support.
Browse files Browse the repository at this point in the history
  • Loading branch information
MainMemory committed Nov 27, 2017
1 parent a2388f6 commit 5a85998
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions SonicRModLoader/FileReplacement.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <MMSystem.h>
#include "FileMap.hpp"

extern FileMap fileMap;
Expand Down
2 changes: 2 additions & 0 deletions SonicRModLoader/SonicRModLoader.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
<ClInclude Include="Music.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" />
<ClInclude Include="Textures.h" />
<ClInclude Include="Widescreen.h" />
</ItemGroup>
<ItemGroup>
Expand All @@ -117,6 +118,7 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="Textures.cpp" />
<ClCompile Include="Widescreen.cpp" />
</ItemGroup>
<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions SonicRModLoader/SonicRModLoader.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
<ClInclude Include="Widescreen.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Textures.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="stdafx.cpp">
Expand Down Expand Up @@ -80,5 +83,8 @@
<ClCompile Include="Widescreen.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Textures.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
38 changes: 38 additions & 0 deletions SonicRModLoader/Textures.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "stdafx.h"
#include <algorithm>
#include <Shlwapi.h>
#include <GdiPlus.h>
#include "TextConv.hpp"
#include "FileReplacement.h"
#include "FileSystem.h"
#include "Textures.h"
#include "SonicRModLoader.h"

using std::string;
using namespace Gdiplus;

const char *extensions[] = { ".png", ".gif", ".bmp" };

signed int __cdecl D3D_ReadTPageRGB_r(const char *FilePath, char *Buffer, int BytesPerChannel)
{
string fp = FilePath;
for (size_t i = 0; i < LengthOfArray(extensions); i++)
{
ReplaceFileExtension(fp, extensions[i]);
string f2 = fileMap.replaceFile(fp.c_str());
if (FileExists(f2))
{
Bitmap *bmp = Bitmap::FromFile(MBStoUTF16(f2, CP_ACP).c_str());
Rect rect(0, 0, bmp->GetWidth(), bmp->GetHeight());
BitmapData bmpd;
bmp->LockBits(&rect, ImageLockModeRead, PixelFormat24bppRGB, &bmpd);
memcpy(Buffer, bmpd.Scan0, abs(bmpd.Stride) * bmpd.Height);
bmp->UnlockBits(&bmpd);
delete bmp;
for (int p = 0; p < BytesPerChannel; p++)
std::swap(Buffer[p * 3], Buffer[p * 3 + 2]);
return 1;
}
}
return D3D_ReadTPageRGB(FilePath, Buffer, BytesPerChannel);
}
3 changes: 3 additions & 0 deletions SonicRModLoader/Textures.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

signed int D3D_ReadTPageRGB_r(const char * FilePath, char * Buffer, int BytesPerChannel);
4 changes: 4 additions & 0 deletions SonicRModLoader/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "Events.h"
#include "Music.h"
#include "Widescreen.h"
#include "Textures.h"
#include "SonicRModLoader.h"

using std::ifstream;
Expand Down Expand Up @@ -395,6 +396,9 @@ int __stdcall InitMods(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmd
WriteCall((void *)0x43E901, D3D_Render2DObject_AlignLeft); // /
WriteCall((void *)0x43E928, D3D_Render2DObject_AlignLeft); // 5

// Textures
WriteCall((void*)0x407A30, D3D_ReadTPageRGB_r);

// Map of files to replace and/or swap.
// This is done with a second map instead of fileMap directly
// in order to handle multiple mods.
Expand Down
5 changes: 5 additions & 0 deletions SonicRModLoader/include/SonicRModLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ DataPointer(WNDCLASSA, WndClass, 0x4B5398);
DataPointer(HINSTANCE, hInstance, 0x4B53C4);
DataPointer(HWND, hWnd, 0x4B53C8);
DataPointer(int, CurrentMusicTrack, 0x502360);
DataArray(char *, TPageBuffers, 0x5D359C, 1);
DataPointer(int, Windowed, 0x5EDD24);
DataPointer(int, FrameEndTime, 0x7349F8);
DataPointer(int, FrameStartTime, 0x7356B0);
Expand All @@ -59,6 +60,10 @@ static void(__cdecl *(__cdecl *const DisplayFatalErrorWithTrace)(char *file, int
static void(__cdecl *(__cdecl *const PrintDebugWithTrace)(char *file, int line))(char *, ...) = (decltype(PrintDebugWithTrace))0x404BC0;
FunctionPointer(int, GetTime, (), 0x404D30);
FunctionPointer(int, PrintDebug, (const char *fmt, ...), 0x404D80);
FunctionPointer(int, D3D_ReadTPageRGB, (const char *FilePath, char *Buffer, int BytesPerChannel), 0x407950);
FunctionPointer(void, D3D_LoadTPageRGB, (int page, char *filename), 0x407A00);
FunctionPointer(void, CreateTPageBuffer, (int page), 0x442720);
FunctionPointer(void, D3D_ReleaseTexture, (int page), 0x4427D0);
VoidFunc(CalculateClockSpeed, 0x4332B0);
FunctionPointer(void, ProcessCommandLine, (const char *a1), 0x433400);
FunctionPointer(int, MainGameLoop, (), 0x43AA60);
Expand Down

0 comments on commit 5a85998

Please sign in to comment.