Skip to content

Commit

Permalink
lara: introduce common hit frame logic
Browse files Browse the repository at this point in the history
This replaces several duplicated switch statements for checking if Lara
is in her hit animation with a common routine in TRX.
  • Loading branch information
lahm86 committed Jan 25, 2025
1 parent cd19c4d commit 1e8b36c
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 117 deletions.
22 changes: 22 additions & 0 deletions src/libtrx/game/lara/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,25 @@ void Lara_SetMesh(const LARA_MESH mesh, OBJECT_MESH *const mesh_ptr)
{
Lara_GetLaraInfo()->mesh_ptrs[mesh] = mesh_ptr;
}

const ANIM_FRAME *Lara_GetHitFrame(const ITEM *const item)
{
const LARA_INFO *const lara = Lara_GetLaraInfo();
if (lara->hit_direction < 0) {
return NULL;
}

// clang-format off
LARA_ANIMATION anim_idx;
switch (lara->hit_direction) {
case DIR_EAST: anim_idx = LA_HIT_LEFT; break;
case DIR_SOUTH: anim_idx = LA_HIT_BACK; break;
case DIR_WEST: anim_idx = LA_HIT_RIGHT; break;
default: anim_idx = LA_HIT_FRONT; break;
}
// clang-format on

const OBJECT *const object = Object_GetObject(item->object_id);
const ANIM *const anim = Object_GetAnim(object, anim_idx);
return &anim->frame_ptr[lara->hit_frame];
}
1 change: 1 addition & 0 deletions src/libtrx/include/libtrx/game/lara/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ void Lara_Animate(ITEM *item);
void Lara_SwapSingleMesh(LARA_MESH mesh, GAME_OBJECT_ID object_id);
OBJECT_MESH *Lara_GetMesh(LARA_MESH mesh);
void Lara_SetMesh(LARA_MESH mesh, OBJECT_MESH *mesh_ptr);
const ANIM_FRAME *Lara_GetHitFrame(const ITEM *item);
4 changes: 2 additions & 2 deletions src/libtrx/include/libtrx/game/lara/enum_tr1.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ typedef enum {
LA_GRAB_LEDGE_IN = 150,
LA_HIT_FRONT = 125,
LA_HIT_BACK = 126,
LA_HIT_RIGHT = 127,
LA_HIT_LEFT = 128,
LA_HIT_LEFT = 127,
LA_HIT_RIGHT = 128,
LA_SURF_CLIMB_MEDIUM = 169,
LA_WADE = 170,
LA_SURF_TO_WADE = 178,
Expand Down
27 changes: 3 additions & 24 deletions src/tr1/game/lara/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ static void M_DrawMesh(

void Lara_Draw(ITEM *item)
{
OBJECT *object;
ANIM_FRAME *frame;
ANIM_FRAME *frmptr[2];
MATRIX saved_matrix;

Expand All @@ -53,32 +51,13 @@ void Lara_Draw(ITEM *item)
}
}

object = &g_Objects[item->object_id];
if (g_Lara.hit_direction >= 0) {
switch (g_Lara.hit_direction) {
default:
case DIR_NORTH:
frame = Object_GetAnim(object, LA_HIT_FRONT)->frame_ptr;
break;
case DIR_EAST:
frame = Object_GetAnim(object, LA_HIT_RIGHT)->frame_ptr;
break;
case DIR_SOUTH:
frame = Object_GetAnim(object, LA_HIT_BACK)->frame_ptr;
break;
case DIR_WEST:
frame = Object_GetAnim(object, LA_HIT_LEFT)->frame_ptr;
break;
}

frame += g_Lara.hit_frame;
} else {
frame = frmptr[0];
}
const ANIM_FRAME *const hit_frame = Lara_GetHitFrame(item);
const ANIM_FRAME *const frame = hit_frame == NULL ? frmptr[0] : hit_frame;

// save matrix for hair
saved_matrix = *g_MatrixPtr;

const OBJECT *const object = Object_GetObject(item->object_id);
Output_DrawShadow(object->shadow_size, &frame->bounds, item);

Matrix_Push();
Expand Down
36 changes: 7 additions & 29 deletions src/tr1/game/lara/hair.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "global/vars.h"

#include <libtrx/config.h>
#include <libtrx/game/lara/common.h>
#include <libtrx/game/math.h>
#include <libtrx/game/matrix.h>
#include <libtrx/utils.h>
Expand Down Expand Up @@ -84,10 +85,7 @@ void Lara_Hair_Control(void)
return;
}

bool in_cutscene;
OBJECT *object;
int32_t distance;
ANIM_FRAME *frame;
const OBJECT_MESH *mesh;
int16_t room_num;
ANIM_FRAME *frmptr[2];
Expand All @@ -104,32 +102,11 @@ void Lara_Hair_Control(void)
int32_t y;
int32_t z;

in_cutscene = m_LaraType != O_LARA;
object = &g_Objects[m_LaraType];

if (!in_cutscene && g_Lara.hit_direction >= 0) {
LARA_ANIMATION hit_anim;
switch (g_Lara.hit_direction) {
case DIR_NORTH:
hit_anim = LA_HIT_FRONT;
break;

case DIR_SOUTH:
hit_anim = LA_HIT_BACK;
break;

case DIR_EAST:
hit_anim = LA_HIT_RIGHT;
break;

default:
hit_anim = LA_HIT_LEFT;
break;
}

frame = Object_GetAnim(object, hit_anim)->frame_ptr;
frame += g_Lara.hit_frame;

const bool in_cutscene = m_LaraType != O_LARA;
const ANIM_FRAME *const hit_frame = Lara_GetHitFrame(g_LaraItem);
const ANIM_FRAME *frame;
if (!in_cutscene && hit_frame != NULL) {
frame = hit_frame;
frac = 0;
} else {
frame = Item_GetBestFrame(g_LaraItem);
Expand All @@ -141,6 +118,7 @@ void Lara_Hair_Control(void)
g_LaraItem->pos.x, g_LaraItem->pos.y, g_LaraItem->pos.z);
Matrix_Rot16(g_LaraItem->rot);

const OBJECT *const object = Object_GetObject(m_LaraType);
const ANIM_BONE *bone = Object_GetBone(object, 0);
if (frac) {
const XYZ_16 *const mesh_rots_1 = frmptr[0]->mesh_rots;
Expand Down
21 changes: 4 additions & 17 deletions src/tr2/game/lara/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "game/random.h"
#include "global/vars.h"

#include <libtrx/game/lara/common.h>
#include <libtrx/game/matrix.h>

static void M_DrawBodyPart(
Expand All @@ -30,7 +31,6 @@ static void M_DrawBodyPart(

void Lara_Draw(const ITEM *const item)
{
ANIM_FRAME *frame;
MATRIX saved_matrix;

const int32_t top = g_PhdWinTop;
Expand All @@ -53,23 +53,10 @@ void Lara_Draw(const ITEM *const item)
}
}

const OBJECT *const object = &g_Objects[item->object_id];
if (g_Lara.hit_direction < 0) {
frame = frames[0];
} else {
// clang-format off
LARA_ANIMATION anim_idx;
switch (g_Lara.hit_direction) {
case DIR_EAST: anim_idx = LA_HIT_LEFT; break;
case DIR_SOUTH: anim_idx = LA_HIT_BACK; break;
case DIR_WEST: anim_idx = LA_HIT_RIGHT; break;
default: anim_idx = LA_HIT_FRONT; break;
}
// clang-format on
const ANIM *const anim = Object_GetAnim(object, anim_idx);
frame = &anim->frame_ptr[g_Lara.hit_frame];
}
const ANIM_FRAME *const hit_frame = Lara_GetHitFrame(item);
const ANIM_FRAME *const frame = hit_frame == NULL ? frames[0] : hit_frame;

const OBJECT *const object = Object_GetObject(item->object_id);
if (g_Lara.skidoo == NO_ITEM) {
Output_InsertShadow(object->shadow_size, &frame->bounds, item);
}
Expand Down
28 changes: 6 additions & 22 deletions src/tr2/game/lara/hair.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "game/room.h"
#include "global/vars.h"

#include <libtrx/game/lara/common.h>
#include <libtrx/game/math.h>
#include <libtrx/game/matrix.h>
#include <libtrx/utils.h>
Expand Down Expand Up @@ -215,32 +216,15 @@ void Lara_Hair_Control(const bool in_cutscene)
const ANIM_FRAME *frame_2;
int32_t frac;
int32_t rate;
if (g_Lara.hit_direction < 0) {
const ANIM_FRAME *const hit_frame = Lara_GetHitFrame(g_LaraItem);
if (hit_frame != NULL) {
frame_1 = hit_frame;
frac = 0;
} else {
ANIM_FRAME *frmptr[2];
frac = Item_GetFrames(g_LaraItem, frmptr, &rate);
frame_1 = frmptr[0];
frame_2 = frmptr[1];
} else {
LARA_ANIMATION lara_anim;
switch (g_Lara.hit_direction) {
case DIR_EAST:
lara_anim = LA_HIT_LEFT;
break;
case DIR_SOUTH:
lara_anim = LA_HIT_BACK;
break;
case DIR_WEST:
lara_anim = LA_HIT_RIGHT;
break;
default:
lara_anim = LA_HIT_FRONT;
break;
}

const OBJECT *const object = Object_GetObject(g_LaraItem->object_id);
const ANIM *const anim = Object_GetAnim(object, lara_anim);
frame_1 = &anim->frame_ptr[g_Lara.hit_frame];
frac = 0;
}

Matrix_PushUnit();
Expand Down
27 changes: 4 additions & 23 deletions src/tr2/game/lara/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -835,29 +835,9 @@ void Lara_GetJointAbsPosition(XYZ_32 *vec, int32_t joint)
}
}

const ANIM_FRAME *frame_ptr = NULL;
const OBJECT *obj = &g_Objects[g_LaraItem->object_id];
if (g_Lara.hit_direction >= 0) {
LARA_ANIMATION anim_num;
switch (g_Lara.hit_direction) {
case DIR_EAST:
anim_num = LA_HIT_RIGHT;
break;
case DIR_SOUTH:
anim_num = LA_HIT_BACK;
break;
case DIR_WEST:
anim_num = LA_HIT_LEFT;
break;
default:
anim_num = LA_HIT_FRONT;
break;
}
const ANIM *anim = Object_GetAnim(obj, anim_num);
frame_ptr = &anim->frame_ptr[g_Lara.hit_frame];
} else {
frame_ptr = frmptr[0];
}
const ANIM_FRAME *const hit_frame = Lara_GetHitFrame(g_LaraItem);
const ANIM_FRAME *const frame_ptr =
hit_frame == NULL ? frmptr[0] : hit_frame;

Matrix_PushUnit();
g_MatrixPtr->_03 = 0;
Expand All @@ -866,6 +846,7 @@ void Lara_GetJointAbsPosition(XYZ_32 *vec, int32_t joint)
Matrix_Rot16(g_LaraItem->rot);

const XYZ_16 *mesh_rots = frame_ptr->mesh_rots;
const OBJECT *const obj = Object_GetObject(g_LaraItem->object_id);
const ANIM_BONE *bone = Object_GetBone(obj, 0);

Matrix_TranslateRel16(frame_ptr->offset);
Expand Down

0 comments on commit 1e8b36c

Please sign in to comment.