Skip to content

Commit

Permalink
#368 GBA mul (8-bit for rooms), div, branch & loads optims
Browse files Browse the repository at this point in the history
  • Loading branch information
XProger committed Nov 25, 2022
1 parent 39c9be9 commit a311245
Show file tree
Hide file tree
Showing 29 changed files with 353 additions and 428 deletions.
18 changes: 15 additions & 3 deletions src/fixed/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,20 @@ void Camera::move(Location &to, int32 speed)

vec3i d = to.pos - view.pos;

if (speed > 1) {
d /= speed;
if (speed > 1)
{
if (speed == 8)
{
d.x >>= 3;
d.y >>= 3;
d.z >>= 3;
}
else
{
d.x /= speed;
d.y /= speed;
d.z /= speed;
}
}

view.pos += d;
Expand Down Expand Up @@ -295,7 +307,7 @@ void Camera::updateFollow(ItemObj* item)

Location best = getBestLocation(false);

move(best, lastFixed ? speed : 12);
move(best, lastFixed ? speed : 8);
}

void Camera::updateCombat(ItemObj* item)
Expand Down
5 changes: 3 additions & 2 deletions src/fixed/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ int32 phd_atan(int32 x, int32 y)
swap(x, y);
}

return abs(atanTable[(y << 11) / x] + atanOctant[o]);
return abs(atanTable[(y << 11) / x] + atanOctant[o]); //@DIV
}

uint32 phd_sqrt(uint32 x)
Expand Down Expand Up @@ -1552,7 +1552,8 @@ void palSet(const uint16* palette, int32 gamma, int32 bright)

if (gamma || bright)
{
uint16* tmp = (uint16*)&gSpheres;
//STATIC_ASSERT(sizeof(gSpheres) >= 512);
uint16* tmp = (uint16*)gSpheres;

if (gamma) {
palGamma(pal, tmp, gamma);
Expand Down
16 changes: 13 additions & 3 deletions src/fixed/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,10 @@ X_INLINE int32 abs(int32 x) {

#if defined(__WIN32__) || defined(__GBA_WIN__)
#define ASSERT(x) { if (!(x)) { DebugBreak(); } }
#define STATIC_ASSERT(x) typedef char static_assert_##__COUNTER__[(x) ? 1 : -1]
#else
#define ASSERT(x)
#define STATIC_ASSERT(x)
#endif

#if defined(__GBA_WIN__)
Expand Down Expand Up @@ -590,13 +592,11 @@ struct vec3i {
X_INLINE vec3i operator + (const vec3i &v) const { return create(x + v.x, y + v.y, z + v.z); }
X_INLINE vec3i operator - (const vec3i &v) const { return create(x - v.x, y - v.y, z - v.z); }
X_INLINE vec3i operator * (int32 s) const { return create(x * s, y * s, z * s); }
X_INLINE vec3i operator / (int32 s) const { return create(x / s, y / s, z / s); }
X_INLINE bool operator == (const vec3i &v) const { return x == v.x && y == v.y && z == v.z; }
X_INLINE bool operator != (const vec3i &v) const { return x != v.x || y != v.y || z != v.z; }
X_INLINE vec3i& operator += (const vec3i &v) { x += v.x; y += v.y; z += v.z; return *this; }
X_INLINE vec3i& operator -= (const vec3i &v) { x -= v.x; y -= v.y; z -= v.z; return *this; }
X_INLINE vec3i& operator *= (int32 s) { x *= s; y *= s; z *= s; return *this; }
X_INLINE vec3i& operator /= (int32 s) { x /= s; y /= s; z /= s; return *this; }
};

#define _vec3i(x,y,z) vec3i::create(x, y, z)
Expand Down Expand Up @@ -2774,11 +2774,20 @@ void matrixFrame(const void* pos, const void* angles);
void matrixFrameLerp(const void* pos, const void* anglesA, const void* anglesB, int32 delta, int32 rate);
void matrixSetView(const vec3i &pos, int32 angleX, int32 angleY);

#if defined(__GBA__) || defined(__GBA_WIN__)
#define renderInit()
#define renderFree()
#define renderSwap()
#define renderLevelInit()
#define renderLevelFree()
#else
void renderInit();
void renderFree();
void renderSwap();
void renderLevelInit();
void renderLevelFree();
#endif

void setViewport(const RectMinMax &vp);
void setPaletteIndex(int32 index);
void clear();
Expand All @@ -2787,7 +2796,8 @@ void renderMesh(const Mesh* mesh);
void renderShadow(int32 x, int32 z, int32 sx, int32 sz);
void renderSprite(int32 vx, int32 vy, int32 vz, int32 vg, int32 index);
void renderGlyph(int32 vx, int32 vy, int32 index);
void renderBorder(int32 x, int32 y, int32 width, int32 height, int32 shade, int32 color1, int32 color2, int32 z);
void renderFill(int32 x, int32 y, int32 width, int32 height, int32 shade, int32 z);
void renderBorder(int32 x, int32 y, int32 width, int32 height, int32 color1, int32 color2, int32 z);
void renderBar(int32 x, int32 y, int32 width, int32 value, BarType type);
void renderBackground(const void* background);
void* copyBackground();
Expand Down
12 changes: 6 additions & 6 deletions src/fixed/draw.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ void drawNodesLerp(const ItemObj* item, const AnimFrame* frameA, const AnimFrame
#define DEF_TORSO_ANGLE_Y -832
#define DEF_TORSO_ANGLE_Z -192

const uint32 ZERO_POS[2] = { 0, 0 };
uint32 ZERO_POS[2] = { 0, 0 };

void drawLaraNodes(const ItemObj* lara, const AnimFrame* frameA)
{
Expand Down Expand Up @@ -433,7 +433,7 @@ void drawLaraNodes(const ItemObj* lara, const AnimFrame* frameA)
matrixSetBasis(matrixGet(), basis);
matrixRotateYXZ(arm->angle.x, arm->angle.y, arm->angle.z);
}
matrixFrame(&ZERO_POS, anglesArm[i]++);
matrixFrame(ZERO_POS, anglesArm[i]++);
drawMesh(*mesh++);

{ // JOINT_ARM_2
Expand Down Expand Up @@ -571,9 +571,9 @@ void drawLaraNodesLerp(const ItemObj* lara, const AnimFrame* frameA, const AnimF
bool useLerp = frameRateArm[i] > 1; // armed hands always use frameRate == 1 (i.e. useLerp == false)

if (useLerp) {
matrixFrameLerp(&ZERO_POS, anglesArmA[i]++, anglesArmB[i]++, frameDelta, frameRate);
matrixFrameLerp(ZERO_POS, anglesArmA[i]++, anglesArmB[i]++, frameDelta, frameRate);
} else {
matrixFrame(&ZERO_POS, anglesArmA[i]++);
matrixFrame(ZERO_POS, anglesArmA[i]++);
}
drawMesh(*mesh++);

Expand Down Expand Up @@ -920,17 +920,17 @@ void drawFPS()
drawText(2, 16, buf, TEXT_ALIGN_LEFT);
}

#ifdef PROFILING
void drawProfiling()
{
#ifdef PROFILING
for (int32 i = 0; i < CNT_MAX; i++)
{
char buf[32];
int2str(gCounters[i], buf);
drawText(2, 16 + 32 + i * 16, buf, TEXT_ALIGN_LEFT);
}
flush();
#endif
}
#endif

#endif
7 changes: 4 additions & 3 deletions src/fixed/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,11 @@ void gameRender()
flush();
}

#ifdef PROFILING
drawProfiling();

#ifndef PROFILE_SOUNDTIME
PROFILE_CLEAR();
#ifndef PROFILE_SOUNDTIME
PROFILE_CLEAR();
#endif
#endif
}

Expand Down
7 changes: 5 additions & 2 deletions src/fixed/inventory.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ struct Inventory
case ITEM_SCION_PICKUP_QUALOPEC :
case ITEM_SCION_PICKUP_DROP :
case ITEM_SCION_PICKUP_HOLDER : return ITEM_INV_SCION;
default : ;
}
return type;
}
Expand Down Expand Up @@ -313,6 +314,7 @@ struct Inventory
case ITEM_KEYHOLE_2 : return SLOT_KEY_ITEM_2;
case ITEM_KEYHOLE_3 : return SLOT_KEY_ITEM_3;
case ITEM_KEYHOLE_4 : return SLOT_KEY_ITEM_4;
default : ;
}
return SLOT_MAX;
}
Expand Down Expand Up @@ -1306,7 +1308,8 @@ struct Inventory
int32 h = optionsHeight;
int32 y = (FRAME_HEIGHT - h) / 2 - 12;

renderBorder((FRAME_WIDTH - w) / 2, y, w, h, 25, 14, 10, 2);
renderFill((FRAME_WIDTH - w) / 2 + 1, y + 1, w - 2, h - 2, 25, 2);
renderBorder((FRAME_WIDTH - w) / 2, y, w, h, 14, 10, 2);

w -= 4;
h = 18;
Expand All @@ -1317,7 +1320,7 @@ struct Inventory
const Option &opt = options[i];

if (optionIndex == i) {
renderBorder((FRAME_WIDTH - w) / 2, y, w, h, -1, 15, 15, 1);
renderBorder((FRAME_WIDTH - w) / 2, y, w, h, 15, 15, 1);
}

switch (opt.type)
Expand Down
57 changes: 48 additions & 9 deletions src/fixed/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ int32 ItemObj::getFrames(const AnimFrame* &frameA, const AnimFrame* &frameB, int
{
const Anim* anim = level.anims + animIndex;

if (anim->frameBegin == anim->frameEnd)
{
frameA = frameB = (AnimFrame*)(level.animFrames + (anim->frameOffset >> 1));
animFrameRate = 1;
return 0;
}

animFrameRate = anim->frameRate;

int32 frameSize = (sizeof(AnimFrame) >> 1) + (level.models[type].count << 1);
Expand All @@ -122,9 +129,31 @@ int32 ItemObj::getFrames(const AnimFrame* &frameA, const AnimFrame* &frameB, int
// int32 d = FixedInvU(animFrameRate);
// int32 indexA = frame * d >> 16;

int32 indexA = frame / animFrameRate;
int32 frameDelta = frame - indexA * animFrameRate;
int32 indexB = indexA + 1;
int32 indexA, indexB;
int32 frameDelta;

if (animFrameRate == 1)
{
indexA = frame;
frameDelta = frame - indexA;
}
else if (animFrameRate == 2)
{
indexA = frame >> 1;
frameDelta = frame - (indexA << 1);
}
else if (animFrameRate == 4)
{
indexA = frame >> 2;
frameDelta = frame - (indexA << 2);
}
else
{
indexA = frame / animFrameRate;
frameDelta = frame - (indexA * animFrameRate);
}

indexB = indexA + 1;

if (indexB * animFrameRate >= anim->frameEnd)
{
Expand Down Expand Up @@ -414,8 +443,9 @@ void ItemObj::animSkip(int32 stateBefore, int32 stateAfter, bool advance)
goalState = stateAfter;
}

#define ANIM_MOVE_LERP_POS (16)
#define ANIM_MOVE_LERP_ROT ANGLE(2)
#define ANIM_MOVE_LERP_POS_SHIFT 4
#define ANIM_MOVE_LERP_POS (1 << ANIM_MOVE_LERP_POS_SHIFT)
#define ANIM_MOVE_LERP_ROT ANGLE(2)

void ItemObj::animProcess(bool movement)
{
Expand Down Expand Up @@ -488,11 +518,20 @@ bool ItemObj::moveTo(const vec3i &point, ItemObj* item, bool lerp)

vec3i posDelta = p - pos;

int32 dist = phd_sqrt(X_SQR(posDelta.x) + X_SQR(posDelta.y) + X_SQR(posDelta.z));
int32 dist = X_SQR(posDelta.x) + X_SQR(posDelta.y) + X_SQR(posDelta.z);

if (dist > ANIM_MOVE_LERP_POS) {
pos += (posDelta * ANIM_MOVE_LERP_POS) / dist;
} else {
if (dist > ANIM_MOVE_LERP_POS * ANIM_MOVE_LERP_POS)
{
dist = phd_sqrt(dist) >> 1;
ASSERT(dist < DIV_TABLE_SIZE);
dist = FixedInvU(dist);

pos.x += posDelta.x * dist >> (16 + 1 - ANIM_MOVE_LERP_POS_SHIFT);
pos.y += posDelta.y * dist >> (16 + 1 - ANIM_MOVE_LERP_POS_SHIFT);
pos.z += posDelta.z * dist >> (16 + 1 - ANIM_MOVE_LERP_POS_SHIFT);
}
else
{
pos = p;
}

Expand Down
6 changes: 3 additions & 3 deletions src/fixed/lara.h
Original file line number Diff line number Diff line change
Expand Up @@ -3293,8 +3293,8 @@ struct Lara : ItemObj
if (frame)
{
if (anim == ANIM_PISTOLS_AIM) {
arm->angle.x -= arm->angle.x / frame;
arm->angle.y -= arm->angle.y / frame;
arm->angle.x -= arm->angle.x / frame; // @DIV
arm->angle.y -= arm->angle.y / frame; // @DIV
}

if (anim == ANIM_PISTOLS_FIRE) {
Expand Down Expand Up @@ -3620,7 +3620,7 @@ struct Lara : ItemObj
const AABBs &box = target->getBoundingBox(false);
vec3i p;
p.x = (box.minX + box.maxX) >> 1;
p.y = box.minY + (box.maxY - box.minY) / 3;
p.y = box.minY + (box.maxY - box.minY) / 3; // @DIV
p.z = (box.minZ + box.maxZ) >> 1;
int32 s, c;
sincos(target->angle.y, s, c);
Expand Down
36 changes: 22 additions & 14 deletions src/fixed/room.h
Original file line number Diff line number Diff line change
Expand Up @@ -667,19 +667,23 @@ bool traceX(const Location &from, Location &to, bool accurate)
if (!d.x)
return true;

d.y = (d.y << TRACE_SHIFT) / d.x;
d.z = (d.z << TRACE_SHIFT) / d.x;
int32 dx = abs(d.x) >> 3;
ASSERT(dx < DIV_TABLE_SIZE);
dx = FixedInvU(dx);

d.y = (d.y * dx) >> (16 + 3 - TRACE_SHIFT);
d.z = (d.z * dx) >> (16 + 3 - TRACE_SHIFT);

vec3i p = from.pos;

Room* room = from.room;

if (d.x < 0)
{
d.x = 1024;
d.x = -1024;
p.x &= ~1023;
p.y += d.y * (p.x - from.pos.x) >> TRACE_SHIFT;
p.z += d.z * (p.x - from.pos.x) >> TRACE_SHIFT;
p.y -= d.y * (p.x - from.pos.x) >> TRACE_SHIFT;
p.z -= d.z * (p.x - from.pos.x) >> TRACE_SHIFT;

while (p.x > to.pos.x)
{
Expand All @@ -690,7 +694,7 @@ bool traceX(const Location &from, Location &to, bool accurate)
TRACE_CHECK(nextRoom, p.x - 1, p.y, p.z);

room = nextRoom;
p -= d;
p += d;
}
}
else
Expand Down Expand Up @@ -725,19 +729,23 @@ bool traceZ(const Location &from, Location &to, bool accurate)
if (!d.z)
return true;

d.x = (d.x << TRACE_SHIFT) / d.z;
d.y = (d.y << TRACE_SHIFT) / d.z;
int32 dz = abs(d.z) >> 3;
ASSERT(dz < DIV_TABLE_SIZE);
dz = FixedInvU(dz);

d.x = (d.x * dz) >> (16 + 3 - TRACE_SHIFT);
d.y = (d.y * dz) >> (16 + 3 - TRACE_SHIFT);

vec3i p = from.pos;

Room* room = from.room;

if (d.z < 0)
{
d.z = 1024;
d.z = -1024;
p.z &= ~1023;
p.x += d.x * (p.z - from.pos.z) >> TRACE_SHIFT;
p.y += d.y * (p.z - from.pos.z) >> TRACE_SHIFT;
p.x -= d.x * (p.z - from.pos.z) >> TRACE_SHIFT;
p.y -= d.y * (p.z - from.pos.z) >> TRACE_SHIFT;

while (p.z > to.pos.z)
{
Expand All @@ -748,7 +756,7 @@ bool traceZ(const Location &from, Location &to, bool accurate)
TRACE_CHECK(nextRoom, p.x, p.y, p.z - 1);

room = nextRoom;
p -= d;
p += d;
}
}
else
Expand Down Expand Up @@ -816,8 +824,8 @@ bool trace(const Location &from, Location &to, bool accurate)
{
to.pos.y = h;
h -= from.pos.y;
to.pos.x = from.pos.x + (to.pos.x - from.pos.x) * h / dy;
to.pos.z = from.pos.z + (to.pos.z - from.pos.z) * h / dy;
to.pos.x = from.pos.x + (to.pos.x - from.pos.x) * h / dy; // @DIV
to.pos.z = from.pos.z + (to.pos.z - from.pos.z) * h / dy; // @DIV
return false;
}
}
Expand Down
Loading

0 comments on commit a311245

Please sign in to comment.