-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCore.cpp
More file actions
328 lines (299 loc) · 10.8 KB
/
Core.cpp
File metadata and controls
328 lines (299 loc) · 10.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
////////////////////////////////////////////////////////////////////////////////////////////////////////
// Part of Injectable Generic Camera System
// Copyright(c) 2016, Frans Bouma
// All rights reserved.
// https://github.com/FransBouma/InjectableGenericCameraSystem
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met :
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and / or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "input.h"
#include "Camera.h"
#include "InterceptorHelper.h"
#include "Gamepad.h"
#include "Utils.h"
using namespace std;
//--------------------------------------------------------------------------------------------------------------------------------
// data shared with asm functions. This is allocated here, 'C' style and not in some datastructure as passing that to
// MASM is rather tedious.
extern "C" {
byte _cameraEnabled = 0;
byte _timeStopped = 0;
}
//--------------------------------------------------------------------------------------------------------------------------------
// local data
static Camera* _camera=NULL;
static Gamepad* _gamePad = NULL;
static Console* _consoleWrapper=NULL;
static LPBYTE _hostImageAddress = NULL;
static bool _cameraMovementLocked = false;
static bool _cameraStructFound = false;
static float _lookDirectionInverter = 1.0f;
static WNDPROC _oldMainWindowWndProc = 0;
//--------------------------------------------------------------------------------------------------------------------------------
// Local function definitions
void MainLoop();
void UpdateFrame();
void HandleUserInput();
void InitCamera();
void WriteNewCameraValuesToCameraStructs();
void DisplayHelp();
//--------------------------------------------------------------------------------------------------------------------------------
// Implementations
void SystemStart(HMODULE hostBaseAddress, Console c)
{
g_systemActive = true;
_consoleWrapper = &c;
_hostImageAddress = (LPBYTE)hostBaseAddress;
_gamePad = new Gamepad(0);
_gamePad->setInvertLStickY(CONTROLLER_Y_INVERT);
_gamePad->setInvertRStickY(CONTROLLER_Y_INVERT);
DisplayHelp();
// initialization
SetCameraStructInterceptorHook(_hostImageAddress);
InitCamera();
SetCameraWriteInterceptorHooks(_hostImageAddress);
// done initializing, start main loop of camera. We won't return from this
MainLoop();
delete _gamePad;
}
// Core loop of the system
void MainLoop()
{
while (g_systemActive)
{
Sleep(FRAME_SLEEP);
UpdateFrame();
}
}
// updates the data and camera for a frame
void UpdateFrame()
{
HandleUserInput();
WriteNewCameraValuesToCameraStructs();
}
void HandleUserInput()
{
_gamePad->update();
bool altPressed = KeyDown(VK_MENU) || KeyDown(VK_RMENU);
bool controlPressed = KeyDown(VK_CONTROL) || KeyDown(VK_RCONTROL);
if (KeyDown(IGCS_KEY_HELP) && altPressed)
{
DisplayHelp();
// wait for 350ms to avoid fast keyboard hammering displaying multiple times the help!
Sleep(350);
}
if (KeyDown(IGCS_KEY_INVERT_Y_LOOK))
{
_lookDirectionInverter = -_lookDirectionInverter;
if (_lookDirectionInverter < 0)
{
_consoleWrapper->WriteLine("Y look direction is inverted");
}
else
{
_consoleWrapper->WriteLine("Y look direction is normal");
}
// wait for 350ms to avoid fast keyboard hammering switching look directive multiple times
Sleep(350);
}
if(!_cameraStructFound)
{
// camera not found yet, can't proceed.
return;
}
if(KeyDown(IGCS_KEY_CAMERA_ENABLE))
{
if (_cameraEnabled)
{
_consoleWrapper->WriteLine("Camera disabled");
}
else
{
_consoleWrapper->WriteLine("Camera enabled");
_camera->ResetAngles();
}
_cameraEnabled = _cameraEnabled == 0 ? (byte)1 : (byte)0;
// wait for 350ms to avoid fast keyboard hammering disabling the camera right away
Sleep(350);
}
if (KeyDown(IGCS_KEY_FOV_DECREASE))
{
}
if (KeyDown(IGCS_KEY_FOV_INCREASE))
{
}
if (KeyDown(IGCS_KEY_FOV_RESET))
{
}
//////////////////////////////////////////////////// CAMERA
if (!_cameraEnabled)
{
// camera is disabled. We simply disable all input to the camera movement.
return;
}
_camera->ResetDeltas();
float multiplier = altPressed ? FASTER_MULTIPLIER : controlPressed ? SLOWER_MULTIPLIER : 1.0f;
if (KeyDown(IGCS_KEY_CAMERA_LOCK))
{
if (_cameraMovementLocked)
{
_consoleWrapper->WriteLine("Camera movement unlocked");
}
else
{
_consoleWrapper->WriteLine("Camera movement locked");
}
_cameraMovementLocked = !_cameraMovementLocked;
// wait 350 ms to avoid fast keyboard hammering unlocking/locking the camera movement right away
Sleep(350);
}
if (_cameraMovementLocked)
{
// no movement allowed, simply return
return;
}
if(KeyDown(IGCS_KEY_MOVE_FORWARD))
{
_camera->MoveForward(multiplier);
}
if(KeyDown(IGCS_KEY_MOVE_BACKWARD))
{
_camera->MoveForward(-multiplier);
}
if (KeyDown(IGCS_KEY_MOVE_RIGHT))
{
_camera->MoveRight(multiplier);
}
if (KeyDown(IGCS_KEY_MOVE_LEFT))
{
_camera->MoveRight(-multiplier);
}
if (KeyDown(IGCS_KEY_MOVE_UP))
{
_camera->MoveUp(multiplier);
}
if (KeyDown(IGCS_KEY_MOVE_DOWN))
{
_camera->MoveUp(-multiplier);
}
if (KeyDown(IGCS_KEY_ROTATE_DOWN))
{
_camera->Pitch(multiplier * _lookDirectionInverter);
}
if (KeyDown(IGCS_KEY_ROTATE_UP))
{
_camera->Pitch((-multiplier) * _lookDirectionInverter);
}
if (KeyDown(IGCS_KEY_ROTATE_RIGHT))
{
_camera->Yaw(multiplier);
}
if (KeyDown(IGCS_KEY_ROTATE_LEFT))
{
_camera->Yaw(-multiplier);
}
if (KeyDown(IGCS_KEY_TILT_LEFT))
{
_camera->Roll(multiplier);
}
if (KeyDown(IGCS_KEY_TILT_RIGHT))
{
_camera->Roll(-multiplier);
}
// gamepad
if (_gamePad->isConnected())
{
multiplier = _gamePad->isButtonPressed(IGCS_BUTTON_FASTER) ? FASTER_MULTIPLIER : _gamePad->isButtonPressed(IGCS_BUTTON_SLOWER) ? SLOWER_MULTIPLIER : multiplier;
vec2 rightStickPosition = _gamePad->getRStickPosition();
_camera->Pitch(rightStickPosition.y * multiplier * _lookDirectionInverter);
_camera->Yaw(rightStickPosition.x * multiplier);
vec2 leftStickPosition = _gamePad->getLStickPosition();
_camera->MoveForward(-leftStickPosition.y * multiplier);
_camera->MoveRight(leftStickPosition.x * multiplier);
_camera->MoveUp((_gamePad->getLTrigger() - _gamePad->getRTrigger()) * multiplier);
if (_gamePad->isButtonPressed(IGCS_BUTTON_TILT_LEFT))
{
_camera->Roll(multiplier);
}
if (_gamePad->isButtonPressed(IGCS_BUTTON_TILT_RIGHT))
{
_camera->Roll(-multiplier);
}
if (_gamePad->isButtonPressed(IGCS_BUTTON_FOV_DECREASE))
{
}
if (_gamePad->isButtonPressed(IGCS_BUTTON_FOV_INCREASE))
{
}
}
}
void WriteNewCameraValuesToCameraStructs()
{
if (!_cameraEnabled)
{
return;
}
// calculate new camera values. We don't use the current quaternion at the moment as it bugs in some scenes where the quaternion is the one used in some effect layer.
// so we calculate one from scratch.
XMVECTOR newLookQuaternion = _camera->CalculateLookQuaternion();
XMFLOAT3 currentCoords = GetCurrentCameraCoords();
XMFLOAT3 newCoords = _camera->CalculateNewCoords(currentCoords, newLookQuaternion);
WriteNewCameraValuesToGameData(newLookQuaternion, newCoords);
}
void InitCamera()
{
_consoleWrapper->WriteLine("Waiting for camera struct interception...");
WaitForCameraStructAddresses();
_cameraStructFound = true;
_consoleWrapper->WriteLine("Camera found.");
_camera = new Camera();
//_camera->SetPitch(INITIAL_PITCH_RADIANS);
//_camera->SetRoll(INITIAL_ROLL_RADIANS);
//_camera->SetYaw(INITIAL_YAW_RADIANS);
}
void DisplayHelp()
{
//0 1 2 3 4 5 6 7
//01234567890123456789012345678901234567890123456789012345678901234567890123456789
_consoleWrapper->WriteLine("---[IGCS Help]----------------------------------------------------------------", CONSOLE_WHITE);
_consoleWrapper->WriteLine("INS : Enable/Disable camera");
_consoleWrapper->WriteLine("HOME : Lock/unlock camera movement");
_consoleWrapper->WriteLine("ALT+rotate/move : Faster rotate / move");
_consoleWrapper->WriteLine("CTRL+rotate/move : Slower rotate / move");
_consoleWrapper->WriteLine("Controller Y-button + left/right-stick : Faster rotate / move");
_consoleWrapper->WriteLine("Controller X-button + left/right-stick : Slower rotate / move");
_consoleWrapper->WriteLine("Arrow up/down or right-stick : Rotate camera up/down");
_consoleWrapper->WriteLine("Arrow left/right or right-stick : Rotate camera left/right");
_consoleWrapper->WriteLine("Numpad 8/Numpad 5 or left-stick : Move camera forward/backward");
_consoleWrapper->WriteLine("Numpad 4/Numpad 6 or left-stick : Move camera left / right");
_consoleWrapper->WriteLine("Numpad 7/Numpad 9 or left/right trigger : Move camera up / down");
_consoleWrapper->WriteLine("Numpad 1/Numpad 3 or d-pad left/right : Tilt camera left / right");
_consoleWrapper->WriteLine("Numpad +/Numpad - or d-pad up/down : Increase / decrease FoV");
_consoleWrapper->WriteLine("Numpad * : Reset FoV");
_consoleWrapper->WriteLine("Numpad 0 : Pause / Continue game (See readme!)");
_consoleWrapper->WriteLine("Numpad / : Toggle Y look direction");
_consoleWrapper->WriteLine("ALT+H : This help");
_consoleWrapper->WriteLine("------------------------------------------------------------------------------", CONSOLE_WHITE);
_consoleWrapper->WriteLine("Only use this camera in single player!");
_consoleWrapper->WriteLine("------------------------------------------------------------------------------", CONSOLE_WHITE);
}