-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphics.cpp
More file actions
363 lines (301 loc) · 13.8 KB
/
Graphics.cpp
File metadata and controls
363 lines (301 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <dwmapi.h>
#include <versionhelpers.h>
#include "Patcher.h"
#include "Util.h"
#include "Tethys/Common/Library.h"
#include "Tethys/API/TethysGame.h"
#include "Tethys/API/GameMap.h"
#include "Tethys/Game/MineManager.h"
#include "Tethys/UI/GameFrame.h"
#include "Tethys/Resource/SpriteManager.h"
#include "Tethys/Resource/GFXSurface.h"
#include "Tethys/Resource/GFXBitmap.h"
#include "Tethys/Resource/CConfig.h"
#include <algorithm>
using namespace Tethys;
using namespace TethysAPI;
using namespace Tethys::TethysUtil;
using namespace Patcher::Util;
using namespace Patcher::Registers;
// =====================================================================================================================
// Disable DirectDraw mode, always forcing windowed mode (pure GDI mode). Also fixes crash bugs with 4k resolutions and
// when the game window is bigger than the current map.
bool SetWindowFix(
bool enable)
{
static Patcher::PatchContext patcher;
bool success = true;
if (enable) {
// Bypass 16-bit color check and DirectDraw::SetDisplayMode()
// In TApp::Init()
patcher.WriteBytes(0x485C06, { 0xEB, 0x21 }); // je => jmp 0x23
patcher.WriteBytes(0x486072, { 0xEB, 0x7E }); // je => jmp 0x80
// Dynamically allocate bit vectors that store what parts of the screen to redraw to avoid crashes on 4k displays.
// In Viewport::Init()
patcher.LowLevelHook(0x46F1D2, [](Esi<Viewport*> pThis, Ecx<size_t>& counter) {
// Try to allocate bit vectors only once, and make them the virtual screen size in case the game is resized.
// The game can behave strangely (e.g. flickering structure shadows, missing sprites) in certain cases at high
// resolutions if the buffers get reallocated mid-game. Assume there is only one active Viewport (detail pane's).
static const int screenX = ::GetSystemMetrics(SM_CXVIRTUALSCREEN);
static const int screenY = ::GetSystemMetrics(SM_CYVIRTUALSCREEN);
static const size_t bitVecSize = (((screenX * screenY) + 31) / 32) / 8; // Round up to nearest tile size (32 px)
static auto*const pBitVectors = static_cast<uint8*>(OP2Alloc(bitVecSize * 5));
static const auto cleanup = atexit([] { OP2Free(pBitVectors); });
pThis->pRedrawBitVector_ = &pBitVectors[0 * bitVecSize];
pThis->pOldRedrawBitVector_ = &pBitVectors[1 * bitVecSize];
pThis->pLightBitVector_ = &pBitVectors[2 * bitVecSize];
pThis->pOldLightBitVector_ = &pBitVectors[3 * bitVecSize];
pThis->pUnknownBitVector_ = &pBitVectors[4 * bitVecSize];
memset(pBitVectors, 0, bitVecSize * 5);
// Skip the original code that zeroes the fixed size buffers
// Set ecx = 0 to negate the effects of a rep stosb instruction when we jump back
counter = 0;
return 0x46F229;
});
static bool isDetailPaneSmall = false;
// Prevent detail pane from getting larger than the map size by hooking the DeferWindowPos call that sets its size.
// In GameFrame::RepositionWindowOnResize()
patcher.HookCall(0x4999FC, StdcallLambdaPtr(
[](HDWP hWinPosInfo, HWND hWnd, HWND hWndInsertAfter, int x, int y, int cx, int cy, UINT uFlags) -> HDWP {
const MapRect clipRect = GameMap::GetClipRect();
const int maxDisplayableMapWidth = 32 * (std::max)(64, clipRect.x2 - clipRect.x1 + 1);
// ** TODO Displaying the last row of the map causes severe glitches similar to the window being too tall, so we
// subtract one tile. We assume the map is 64x64 minimum.
const int maxDisplayableMapHeight = 32 * (std::max)(63, clipRect.y2 - clipRect.y1 + 1);
const int origCx = cx;
const int origCy = cy;
cx = (std::min)(cx, maxDisplayableMapWidth);
cy = (std::min)(cy, maxDisplayableMapHeight);
// Center the detail pane in the original area.
if (cx < origCx) {
x += (origCx - cx) / 2;
}
if (cy < origCy) {
y += (origCy - cy) / 2;
}
isDetailPaneSmall = (cx < origCx) || (cy < origCy);
return ::DeferWindowPos(hWinPosInfo, hWnd, hWndInsertAfter, x, y, cx, cy, uFlags);
}));
// Repaint the background with a black color to handle when the detail pane is smaller than the actual window.
// In GameFrame::OnPaint()
patcher.LowLevelHook(0x499CA8, [](Esi<GameFrame*> pThis, Eax<HDC> hDc) {
if (isDetailPaneSmall) {
RECT frameRect;
::GetClientRect(pThis->hWnd_, &frameRect);
::PatBlt(hDc, 0, 0, frameRect.right, frameRect.bottom, BLACKNESS);
}
});
// Force a window resize on game load.
// In GameImpl::PrepareGame()
patcher.LowLevelHook(0x48990D, [] { ::SendMessage(g_gameFrame.hWnd_, WM_SIZE, 0, 0); });
success = (patcher.GetStatus() == PatcherStatus::Ok);
}
if ((enable == false) || (success == false)) {
success &= (patcher.RevertAll() == PatcherStatus::Ok);
}
return success;
}
// =====================================================================================================================
// Returns if Windows Desktop Window Manager Compositioning is enabled.
// Never enabled in WinXP, may be enabled or disabled in Vista and Win7, always enabled in Win8 and Win10+.
static bool IsDwmCompositionEnabled(
const Library& dwmApi)
{
BOOL dwmEnabled = IsWindows8OrGreater();
if (dwmEnabled == false) {
static auto*const pfnIsCompositionEnabled = dwmApi.Get<HRESULT WINAPI(BOOL*)>("DwmIsCompositionEnabled");
if (pfnIsCompositionEnabled != nullptr) {
pfnIsCompositionEnabled(&dwmEnabled);
}
}
return dwmEnabled;
}
// =====================================================================================================================
// Fixes various issues related to DWM desktop compositioning on Vista and newer, such as issues where there is visible
// stuttering, and where the Aero frame is drawn over the game frame.
bool SetDwmFix(
bool enable)
{
static Patcher::PatchContext patcher;
bool success = true;
if (enable) {
static Library dwmApi("dwmapi.dll");
int forceRedraw = g_configFile.GetInt("Game", "ForceFullRedraw", -1);
if (forceRedraw == -1) {
forceRedraw = IsDwmCompositionEnabled(dwmApi) || IsVirtualMachine(); // XP VM running on Vista+ host is affected
g_configFile.SetInt("Game", "ForceFullRedraw", forceRedraw);
}
if (forceRedraw) {
// Workaround the redraw stuttering issue by forcing the entire detail pane to be redrawn every frame.
// ** TODO Implement Direct2D presents to fix stuttering issues with redrawing the other parts of the UI?
// In DetailPane::OnIdle()
patcher.LowLevelHook(0x407EE3, [](Ecx<Viewport*> pViewport)
{ pViewport->MarkForRedraw({ 0, 0, pViewport->height_, pViewport->width_ }); });
}
static auto*const pfnDwmSetWindowAttribute =
dwmApi.Get<HRESULT WINAPI(HWND, DWORD, LPCVOID, DWORD)>("DwmSetWindowAttribute");
// Prevent the default Aero window frame from being drawn over the game's custom frame.
if (pfnDwmSetWindowAttribute != nullptr) {
// In IWnd::CreateEx()
patcher.LowLevelHook(0x4316A4, [](Eax<HWND> hWnd) {
const DWMNCRENDERINGPOLICY policy = DWMNCRP_DISABLED;
pfnDwmSetWindowAttribute(hWnd, DWMWA_NCRENDERING_POLICY, &policy, sizeof(policy));
});
}
success = (patcher.GetStatus() == PatcherStatus::Ok);
}
if ((enable == false) || (success == false)) {
success &= (patcher.RevertAll() == PatcherStatus::Ok);
}
return success;
}
// =====================================================================================================================
// Sets the main thread to be DPI-unaware, to negate the GOG distro exe's manifest settings. Win10+ only.
bool SetDpiFix(
bool enable)
{
bool success = true;
static Library user32("User32.dll");
static auto*const pfnSetThreadDpiAwarenessContext =
user32.Get<DPI_AWARENESS_CONTEXT WINAPI(DPI_AWARENESS_CONTEXT)>("SetThreadDpiAwarenessContext");
static DPI_AWARENESS_CONTEXT oldCtx = {};
if (pfnSetThreadDpiAwarenessContext != nullptr) {
if (enable) {
int dpiAwareness = g_configFile.GetInt("Game", "DPIAwareness", INT_MAX);
if (dpiAwareness == INT_MAX) {
dpiAwareness = int(DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED); // Default to DPI-unaware with GDI scaling
g_configFile.SetInt("Game", "DPIAwareness", dpiAwareness);
}
if (dpiAwareness != 0) {
oldCtx = pfnSetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT(dpiAwareness));
success = (oldCtx != NULL);
if (success) {
static const auto cleanup = atexit([] { SetDpiFix(false); });
}
}
}
else if (oldCtx != NULL) {
success = (pfnSetThreadDpiAwarenessContext(oldCtx) != NULL);
}
}
return true;
}
// =====================================================================================================================
// Forces game to be rendered as often as possible and not just during game sim cycle updates.
// ** TODO Fix scrolling speed handled by DetailPane and MiniMapPane, test for possible desyncs?
bool SetFpsPatch(
bool enable)
{
static Patcher::PatchContext patcher;
bool success = true;
if (enable) {
static int noRenderFpsLimit = g_configFile.GetInt("Game", "NoRenderFPSLimit", -1);
if (noRenderFpsLimit == -1) {
noRenderFpsLimit = 0;
g_configFile.SetInt("Game", "NoRenderFPSLimit", noRenderFpsLimit);
}
static int timeMsPassed = 0;
// In GameFrame::OnIdle()
patcher.LowLevelHook(0x49C376, [](Eax<uint32> relTimeMs, Esi<GameFrame*> pThis) {
if (noRenderFpsLimit && (pThis->iniSettings_.frameSkip <= 1)) {
timeMsPassed = relTimeMs;
pThis->detailPane_.OnIdle();
pThis->miniMapPane_.OnIdle();
pThis->commandPane_.OnIdle();
timeMsPassed = 0;
}
});
// In DetailPane::HandleScrolling()
patcher.LowLevelHook(0x407B3F, [](Eax<uint32>& actualScrollRate) { return (timeMsPassed != 0) ? 0x407E3C : 0; });
// Prevent fumarole ambient sound from being played many times
// In Fumarole::Draw()
patcher.LowLevelHook(0x405AA2, [] { return (timeMsPassed != 0) ? 0x405AB6 : 0; });
// Prevent meteor defense sounds from being played many times
// In Meteor::Draw(), EMPMissile::Draw()
patcher.LowLevelHook(0x44ACB9, [] { return (timeMsPassed != 0) ? 0x44ACEC : 0; });
patcher.LowLevelHook(0x48075B, [] { return (timeMsPassed != 0) ? 0x48078E : 0; });
success = (patcher.GetStatus() == PatcherStatus::Ok);
}
if ((enable == false) || (success == false)) {
success &= (patcher.RevertAll() == PatcherStatus::Ok);
}
return success;
}
// =====================================================================================================================
// Replaces translucency effects with real alpha blending, instead of emulated with checkerboard patterns.
// ** TODO Currently only affects Acid Cloud drawing
bool SetAlphaBlendPatch(
bool enable)
{
static Patcher::PatchContext patcher;
bool success = true;
if (enable) {
// Hook acid cloud drawing code.
patcher.LowLevelHook(*OP2Mem<void**>(0x586521), [](
Ebp<Rgb555[256]> palette, Ecx<uint32> width, Edi<Rgb555*>& pDstOut, Esi<uint8*>& pSrcPaletteIdxOut) {
Rgb555* pDst = pDstOut;
uint8* pSrcPaletteIdx = pSrcPaletteIdxOut;
for (uint32 i = width; i > 0; --i, ++pDst, ++pSrcPaletteIdx) {
if (*pSrcPaletteIdx != 0) {
// 50% alpha blend.
const auto& src = palette[*pSrcPaletteIdx];
auto dst = *pDst;
dst = {
uint16((dst.b / 2) + (src.b / 2)), uint16((dst.g / 2) + (src.g / 2)), uint16((dst.r / 2) + (src.r / 2))
};
pDst->u16All = dst.u16All;
}
}
pSrcPaletteIdxOut = pSrcPaletteIdx;
pDstOut = pDst;
return 0x5868B0;
});
success = (patcher.GetStatus() == PatcherStatus::Ok);
}
if ((enable == false) || (success == false)) {
success &= (patcher.RevertAll() == PatcherStatus::Ok);
}
return success;
}
// =====================================================================================================================
// Alters the mining beacon animation based on the mine's OreVariant.
bool SetMineVariantVisibilityPatch(
bool enable)
{
static Patcher::PatchContext patcher;
bool success = true;
if (enable) {
patcher.LowLevelHook(
0x4054AC,
[](Esi<MapObj::MiningBeacon*> pThis, Edi<int> animIndex, Eax<int> numFrames, Edx<int>& frame, Ebp<int>& pixelX) {
int curFrame = -1;
if (pThis->IsSurveyed(TethysGame::LocalPlayer())) {
switch (MineManager::GetInstance()->GetOreVariant(pThis->mineYield_, pThis->mineVariant_)) {
case OreVariant::Low:
// Low variant (no animation)
curFrame = 0;
break;
case OreVariant::Mid:
// Mid variant (slower animation)
curFrame = (TethysGame::Tick() % (numFrames * 4)) / 2;
if (curFrame >= numFrames) {
curFrame = 0;
}
break;
default:
break;
}
}
frame = (curFrame != -1) ? curFrame : (TethysGame::Tick() % numFrames);
pixelX = pThis->pixelX_;
return 0x4054B6;
});
success = (patcher.GetStatus() == PatcherStatus::Ok);
}
if ((enable == false) || (success == false)) {
success &= (patcher.RevertAll() == PatcherStatus::Ok);
}
return success;
}