Skip to content

Commit

Permalink
Add free camera impl for TR8
Browse files Browse the repository at this point in the history
  • Loading branch information
TheIndra55 committed Jan 28, 2024
1 parent 0591fa7 commit 5006de2
Show file tree
Hide file tree
Showing 11 changed files with 275 additions and 18 deletions.
14 changes: 14 additions & 0 deletions src/cdc/math/Math.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "Math.h"

cdc::Vector3 cdc::Mul3x3(Matrix* matA, Vector3* vecB)
{
cdc::Vector3 result;

result.vec128 = _mm_add_ps(
_mm_mul_ps(matA->col0.vec128, _mm_shuffle_ps(vecB->vec128, vecB->vec128, 0)),
_mm_add_ps(
_mm_mul_ps(matA->col1.vec128, _mm_shuffle_ps(vecB->vec128, vecB->vec128, 85)),
_mm_mul_ps(matA->col2.vec128, _mm_shuffle_ps(vecB->vec128, vecB->vec128, 170))));

return result;
}
9 changes: 9 additions & 0 deletions src/cdc/math/Math.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include "Vector.h"
#include "Matrix.h"

namespace cdc
{
Vector3 Mul3x3(Matrix* matA, Vector3* vecB);
}
10 changes: 10 additions & 0 deletions src/cdc/math/Matrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "Matrix.h"
#include "util/Hooking.h"

void cdc::Matrix::Build(Euler* euler)
{
// Too lazy to implement myself
auto addr = GET_ADDRESS(0x000000, 0x000000, 0x49DD70);

Hooking::ThisCall(addr, this, euler);
}
10 changes: 6 additions & 4 deletions src/cdc/math/Matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ namespace cdc
class Matrix
{
public:
cdc::Vector col0;
cdc::Vector col1;
cdc::Vector col2;
cdc::Vector col3;
Vector col0;
Vector col1;
Vector col2;
Vector col3;

void Build(Euler* euler);
};
}
13 changes: 12 additions & 1 deletion src/game/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@

Camera* CAMERA_GetCamera()
{
#ifndef TR8
return (Camera*)GET_ADDRESS(0x000000, 0x850670, 0x000000);
#else
return *(Camera**)0xE80534;
#endif
}

void CAMERA_CalcPosition(cdc::Vector3* position, cdc::Vector3* base, cdc::Euler* rotation, float distance)
{
auto addr = GET_ADDRESS(0x48D8B0, 0x491320, 0x000000);

Hooking::Call(addr, position, base, rotation, distance);
}
}

#ifdef TR8
void CAMERA_SetMode(int mode)
{
Hooking::Call(0x5F39F0, mode);
}
#endif
16 changes: 15 additions & 1 deletion src/game/Camera.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#pragma once

#include "cdc/math/Vector.h"
#include "cdc/math/Matrix.h"

#ifndef TR8
struct CameraCore
{
cdc::Vector3 position;
Expand All @@ -18,6 +20,18 @@ struct Camera : CameraCore
int lock;
__int16 mode;
};
#else
struct Camera
{
char pad1[16];

cdc::Matrix transform;
};
#endif

Camera* CAMERA_GetCamera();
void CAMERA_CalcPosition(cdc::Vector3* position, cdc::Vector3* base, cdc::Euler* rotation, float distance);
void CAMERA_CalcPosition(cdc::Vector3* position, cdc::Vector3* base, cdc::Euler* rotation, float distance);

#ifdef TR8
void CAMERA_SetMode(int mode);
#endif
63 changes: 63 additions & 0 deletions src/input/Input.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,76 @@
#pragma once

enum INPUTACTION
{
INPUTACTION_NONE,

INPUTACTION_UP,
INPUTACTION_DOWN,
INPUTACTION_LEFT,
INPUTACTION_RIGHT,
INPUTACTION_DPAD_UP,
INPUTACTION_DPAD_DOWN,
INPUTACTION_DPAD_LEFT,
INPUTACTION_DPAD_RIGHT,
INPUTACTION_START,
INPUTACTION_SELECT,

#ifndef TR8
INPUTACTION_TRIANGLE = 16,
INPUTACTION_SQUARE = 17,
INPUTACTION_CIRCLE = 18,
INPUTACTION_CROSS = 19,
INPUTACTION_L1 = 20,
INPUTACTION_R1 = 21,
INPUTACTION_L2 = 22,
INPUTACTION_R2 = 23,

// Kinda hack, instead we should probably see how to register an input action
// which would also allow rebinding of them, but this will work for now
INPUTACTION_CAMERA_UP = INPUTACTION_TRIANGLE,
INPUTACTION_CAMERA_DOWN = INPUTACTION_SQUARE,
INPUTACTION_CAMERA_FAST = INPUTACTION_CIRCLE,
INPUTACTION_CAMERA_SLOW = INPUTACTION_R2,
#else
INPUTACTION_TRIANGLE = 11,
INPUTACTION_SQUARE = 12,
INPUTACTION_CIRCLE = 13,
INPUTACTION_CROSS = 14,
INPUTACTION_L1 = 15,
INPUTACTION_R1 = 16,
INPUTACTION_L2 = 17,
INPUTACTION_R2 = 18,

INPUTACTION_CAMERA_UP = INPUTACTION_TRIANGLE,
INPUTACTION_CAMERA_DOWN = INPUTACTION_SQUARE,
INPUTACTION_CAMERA_FAST = INPUTACTION_L2,
INPUTACTION_CAMERA_SLOW = INPUTACTION_CIRCLE,
#endif
};

enum INPUTAXIS
{
INPUTAXIS_LANALOGUEX,
INPUTAXIS_LANALOGUEY,
INPUTAXIS_RANALOGUEX,
INPUTAXIS_RANALOGUEY,

INPUTAXIS_MOUSEX = 16,
INPUTAXIS_MOUSEY = 17,
};

class InputActionMapper
{
public:
struct ActionResult
{
unsigned int state;

#ifndef TR8
char pad1[20];
#else
char pad1[44];
#endif
};

ActionResult* m_pActionResults;
Expand Down
31 changes: 21 additions & 10 deletions src/modules/camera/FreeCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ void FreeCameraBase::OnControl()
auto gameTracker = Game::GetGameTracker();

// Camera rotation based on mouse/controller axis
auto rotX = input->GetAxisValue(17) * gameTracker->timeMult;
auto rotZ = input->GetAxisValue(16) * gameTracker->timeMult;
auto rotX = input->GetAxisValue(INPUTAXIS_MOUSEY) * gameTracker->timeMult;
auto rotZ = input->GetAxisValue(INPUTAXIS_MOUSEX) * gameTracker->timeMult;

Rotate(rotX, rotZ);

Expand All @@ -57,40 +57,51 @@ void FreeCameraBase::OnControl()
}

// Change the speed depending on if shift/alt is pressed
auto shift = Input::IsInputActionPressed(18);
auto control = Input::IsInputActionPressed(23);
auto shift = Input::IsInputActionPressed(INPUTACTION_CAMERA_FAST);
auto control = Input::IsInputActionPressed(INPUTACTION_CAMERA_SLOW);

auto speed = shift ? 200.f : control ? 20.f : 80.f;

// Camera forward/backward
if (Input::IsInputActionPressed(1) || Input::IsInputActionPressed(2))
if (Input::IsInputActionPressed(INPUTACTION_UP) || Input::IsInputActionPressed(INPUTACTION_DOWN))
{
auto distance = Input::IsInputActionPressed(1) ? -speed : speed;
auto distance = Input::IsInputActionPressed(INPUTACTION_UP) ? -speed : speed;

MoveForward(distance * gameTracker->timeMult);
}

// Camera left/right
if (Input::IsInputActionPressed(3) || Input::IsInputActionPressed(4))
if (Input::IsInputActionPressed(INPUTACTION_LEFT) || Input::IsInputActionPressed(INPUTACTION_RIGHT))
{
auto distance = Input::IsInputActionPressed(3) ? -speed : speed;
auto distance = Input::IsInputActionPressed(INPUTACTION_LEFT) ? -speed : speed;

MoveLeft(distance * gameTracker->timeMult);
}

// Camera up/down
if (Input::IsInputActionPressed(16) || Input::IsInputActionPressed(17))
if (Input::IsInputActionPressed(INPUTACTION_CAMERA_UP) || Input::IsInputActionPressed(INPUTACTION_CAMERA_DOWN))
{
auto distance = Input::IsInputActionPressed(16) ? -speed : speed;
auto distance = Input::IsInputActionPressed(INPUTACTION_CAMERA_UP) ? -speed : speed;

MoveUp(distance * gameTracker->timeMult);
}
}

#include "Hook.h"
#include "modules/Log.h"

void FreeCameraBase::OnLoop()
{
if (m_mode == Enabled)
{
OnControl();
}

for (int i = 0; i < 100; i++)
{
if (Input::IsInputActionPressed(i))
{
Hook::GetInstance().GetModule<Log>()->WriteLine("%d", i);
}
}
}
31 changes: 30 additions & 1 deletion src/modules/camera/FreeCamera.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "modules/Module.h"
#include "cdc/math/Vector.h"

// Base class for both free camera implementations
class FreeCameraBase : public Module
Expand Down Expand Up @@ -36,6 +37,8 @@ class FreeCameraBase : public Module
void OnLoop();
};

#ifndef TR8

// Free camera implementation for Legend and Anniversary
class LegendCamera : public FreeCameraBase
{
Expand All @@ -49,4 +52,30 @@ class LegendCamera : public FreeCameraBase
void MoveUp(float distance);
};

using FreeCamera = LegendCamera;
#else

// Free camera implementation for Underworld
class UnderworldCamera : public FreeCameraBase
{
private:
cdc::Vector m_position = { };
cdc::Euler m_rotation = { };

protected:
void ToggleMode();
void OnControl();

void Rotate(float x, float z);
void Rotate(float y);
void MoveForward(float distance);
void MoveLeft(float distance);
void MoveUp(float distance);
};

#endif

#ifndef TR8
using FreeCamera = LegendCamera;
#else
using FreeCamera = UnderworldCamera;
#endif
6 changes: 5 additions & 1 deletion src/modules/camera/Legend.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#ifndef TR8

#define _USE_MATH_DEFINES
#include <cmath>

Expand Down Expand Up @@ -61,4 +63,6 @@ void LegendCamera::MoveUp(float distance)
auto camera = CAMERA_GetCamera();

camera->position.z += distance;
}
}

#endif
Loading

0 comments on commit 5006de2

Please sign in to comment.