Permalink
Browse files

Cleanup Camera.c (allows for more camers to be added)

  • Loading branch information...
UnknownShadow200 committed Oct 11, 2018
1 parent c3f0585 commit 0567fec1142dca9ed7a54b4b1935ed707f788850
Showing with 60 additions and 49 deletions.
  1. +53 −48 src/Camera.c
  2. +7 −1 src/Camera.h
View
@@ -9,10 +9,11 @@
#include "Input.h"
Vector2 cam_rotOffset;
struct Camera Camera_Cameras[3];
int Camera_ActiveIndex;
#define Cam_IsForward_Third() (Camera_ActiveIndex == 2)
bool cam_isForwardThird;
/*########################################################################################################################*
*--------------------------------------------------Perspective camera-----------------------------------------------------*
*#########################################################################################################################*/
static void PerspectiveCamera_GetProjection(struct Matrix* proj) {
float fovy = Game_Fov * MATH_DEG2RAD;
float aspectRatio = (float)Game_Width / (float)Game_Height;
@@ -55,9 +56,9 @@ static void PerspectiveCamera_RegrabMouse(void) {
#define CAMERA_SLIPPERY 0.97f
#define CAMERA_ADJUST 0.025f
float speedX = 0.0f, speedY = 0.0f;
static Vector2 PerspectiveCamera_GetMouseDelta(void) {
float sensitivity = CAMERA_SENSI_FACTOR * Game_MouseSensitivity;
static float speedX, speedY;
if (Game_SmoothCamera) {
speedX += cam_delta.X * CAMERA_ADJUST;
@@ -126,15 +127,10 @@ static void PerspectiveCamera_CalcViewBobbing(float t, float velTiltScale) {
Matrix_MulBy(&Camera_TiltM, &Camera_velX);
}
static void PerspectiveCamera_Init(struct Camera* cam) {
cam->GetProjection = PerspectiveCamera_GetProjection;
cam->GetView = PerspectiveCamera_GetView;
cam->UpdateMouse = PerspectiveCamera_UpdateMouse;
cam->RegrabMouse = PerspectiveCamera_RegrabMouse;
cam->GetPickedBlock = PerspectiveCamera_GetPickedBlock;
}
/*########################################################################################################################*
*---------------------------------------------------First person camera---------------------------------------------------*
*#########################################################################################################################*/
static Vector2 FirstPersonCamera_GetOrientation(void) {
struct Entity* p = &LocalPlayer_Instance.Base;
Vector2 ori = { p->HeadY * MATH_DEG2RAD, p->HeadX * MATH_DEG2RAD };
@@ -154,29 +150,32 @@ static Vector3 FirstPersonCamera_GetPosition(float t) {
}
static bool FirstPersonCamera_Zoom(float amount) { return false; }
static void FirstPersonCamera_Init(struct Camera* cam) {
PerspectiveCamera_Init(cam);
cam->IsThirdPerson = false;
cam->GetOrientation = FirstPersonCamera_GetOrientation;
cam->GetPosition = FirstPersonCamera_GetPosition;
cam->Zoom = FirstPersonCamera_Zoom;
}
struct Camera Camera_FirstPerson = {
false, NULL,
PerspectiveCamera_GetProjection, PerspectiveCamera_GetView,
FirstPersonCamera_GetOrientation, FirstPersonCamera_GetPosition,
PerspectiveCamera_UpdateMouse, PerspectiveCamera_RegrabMouse,
PerspectiveCamera_GetPickedBlock, FirstPersonCamera_Zoom,
};
/*########################################################################################################################*
*---------------------------------------------------Third person camera---------------------------------------------------*
*#########################################################################################################################*/
float dist_third = 3.0f, dist_forward = 3.0f;
static Vector2 ThirdPersonCamera_GetOrientation(void) {
struct Entity* p = &LocalPlayer_Instance.Base;
Vector2 v = { p->HeadY * MATH_DEG2RAD, p->HeadX * MATH_DEG2RAD };
if (Cam_IsForward_Third()) { v.X += MATH_PI; v.Y = -v.Y; }
if (cam_isForwardThird) { v.X += MATH_PI; v.Y = -v.Y; }
v.X += cam_rotOffset.X * MATH_DEG2RAD;
v.Y += cam_rotOffset.Y * MATH_DEG2RAD;
return v;
}
static Vector3 ThirdPersonCamera_GetPosition(float t) {
float dist = Cam_IsForward_Third() ? dist_forward : dist_third;
float dist = cam_isForwardThird ? dist_forward : dist_third;
PerspectiveCamera_CalcViewBobbing(t, dist);
struct Entity* p = &LocalPlayer_Instance.Base;
@@ -192,44 +191,50 @@ static Vector3 ThirdPersonCamera_GetPosition(float t) {
}
static bool ThirdPersonCamera_Zoom(float amount) {
float* dist = Cam_IsForward_Third() ? &dist_forward : &dist_third;
float* dist = cam_isForwardThird ? &dist_forward : &dist_third;
float newDist = *dist - amount;
*dist = max(newDist, 2.0f);
return true;
}
static void ThirdPersonCamera_Init(struct Camera* cam) {
PerspectiveCamera_Init(cam);
cam->IsThirdPerson = true;
cam->GetOrientation = ThirdPersonCamera_GetOrientation;
cam->GetPosition = ThirdPersonCamera_GetPosition;
cam->Zoom = ThirdPersonCamera_Zoom;
}
struct Camera Camera_ThirdPerson = {
true, NULL,
PerspectiveCamera_GetProjection, PerspectiveCamera_GetView,
ThirdPersonCamera_GetOrientation, ThirdPersonCamera_GetPosition,
PerspectiveCamera_UpdateMouse, PerspectiveCamera_RegrabMouse,
PerspectiveCamera_GetPickedBlock, ThirdPersonCamera_Zoom,
};
struct Camera Camera_ForwardThird = {
true, NULL,
PerspectiveCamera_GetProjection, PerspectiveCamera_GetView,
ThirdPersonCamera_GetOrientation, ThirdPersonCamera_GetPosition,
PerspectiveCamera_UpdateMouse, PerspectiveCamera_RegrabMouse,
PerspectiveCamera_GetPickedBlock, ThirdPersonCamera_Zoom,
};
/*########################################################################################################################*
*-----------------------------------------------------General camera------------------------------------------------------*
*#########################################################################################################################*/
void Camera_Init(void) {
FirstPersonCamera_Init(&Camera_Cameras[0]);
ThirdPersonCamera_Init(&Camera_Cameras[1]);
ThirdPersonCamera_Init(&Camera_Cameras[2]);
Camera_Active = &Camera_Cameras[0];
Camera_ActiveIndex = 0;
Camera_FirstPerson.Next = &Camera_ThirdPerson;
Camera_ThirdPerson.Next = &Camera_ForwardThird;
Camera_ForwardThird.Next = &Camera_FirstPerson;
Camera_Active = &Camera_FirstPerson;
}
void Camera_CycleActive(void) {
if (Game_ClassicMode) return;
Camera_Active = Camera_Active->Next;
cam_isForwardThird = Camera_Active == &Camera_ForwardThird;
int i = Camera_ActiveIndex;
i = (i + 1) % Array_Elems(Camera_Cameras);
struct LocalPlayer* player = &LocalPlayer_Instance;
if (!player->Hacks.CanUseThirdPersonCamera || !player->Hacks.Enabled) { i = 0; }
struct LocalPlayer* p = &LocalPlayer_Instance;
if (!p->Hacks.CanUseThirdPersonCamera || !p->Hacks.Enabled) {
Camera_Active = &Camera_FirstPerson;
}
Camera_Active = &Camera_Cameras[i];
Camera_ActiveIndex = i;
/* reset rotation offset when changing cameras */
cam_rotOffset.X = 0.0f; cam_rotOffset.Y = 0.0f;
Game_UpdateProjection();
}
View
@@ -6,6 +6,7 @@
Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3
*/
struct PickedPos;
struct Camera;
/* Tilt effect applied to the camera. */
struct Matrix Camera_TiltM;
@@ -15,6 +16,9 @@ float Camera_BobbingVer, Camera_BobbingHor;
struct Camera {
/* Whether this camera is third person. (i.e. not allowed when -thirdperson in MOTD) */
bool IsThirdPerson;
/* Next camera in linked list of cameras. */
struct Camera* Next;
/* Calculates the current projection matrix of this camera. */
void (*GetProjection)(struct Matrix* proj);
/* Calculates the current modelview matrix of this camera. */
@@ -25,6 +29,8 @@ struct Camera {
/* Returns the current interpolated position of the camera. */
Vector3 (*GetPosition)(float t);
/* Called to update the camera's state. */
/* Typically, this is used to adjust yaw/pitch based on mouse movement. */
void (*UpdateMouse)(void);
/* Called when user closes all menus, and is interacting with camera again. */
/* Typically, this is used to move mouse cursor to centre of the window. */
@@ -38,7 +44,7 @@ struct Camera {
/* Camera user is currently using. */
struct Camera* Camera_Active;
/* Initialises the list of cameras. */
/* Initialises the default cameras. */
void Camera_Init(void);
/* Switches to the next camera in the list. */
void Camera_CycleActive(void);

0 comments on commit 0567fec

Please sign in to comment.