use keys for sprite lookups

This commit is contained in:
jacob 2025-11-06 15:10:10 -06:00
parent f7be7a9297
commit 545c698041
6 changed files with 115 additions and 41 deletions

View File

@ -779,23 +779,22 @@ f32 LerpAngleF32(f32 a, f32 b, f32 t)
i32 LerpI32(i32 val0, i32 val1, f32 t)
{
return val0 + RoundF32ToI32(((f32)val1 - (f32)val0 * t));
return val0 + RoundF32ToI32(((f32)val1 - (f32)val0) * t);
}
i64 LerpI64(i64 val0, i64 val1, f64 t)
{
return val0 + RoundF64ToI64(((f64)val1 - (f64)val0 * t));
return val0 + RoundF64ToI64(((f64)val1 - (f64)val0) * t);
}
i32 LerpU32(u32 val0, u32 val1, f32 t)
{
return val0 + (u64)RoundF32ToI32(((f32)val1 - (f32)val0 * t));
return val0 + (u64)RoundF32ToI32(((f32)val1 - (f32)val0) * t);
}
i64 LerpU64(u64 val0, u64 val1, f64 t)
{
return val0 + (u64)RoundF64ToI64(((f64)val1 - (f64)val0 * t));
return val0 + (u64)RoundF64ToI64(((f64)val1 - (f64)val0) * t);
}
////////////////////////////////////////////////////////////

View File

@ -2520,7 +2520,28 @@ JobDef(PP_UpdateSim, UNUSED sig, UNUSED key)
#if 0
if (IsSwappedIn())
{
TempArena scratch = BeginScratchNoConflict();
String encoded = SwappedStateFromName(scratch.arena, Lit("pp_snapshot"));
BB_Buff bb = BB_BuffFromString(encoded);
BB_Reader br = BB_ReaderFromBuff(&bb);
u64 tick = BB_ReadIBits(&br, 64);
if (tick > 0)
{
PP_EntKey player_id = { .uid = BB_ReadUid(&br) };
local_client->player_id = player_id;
user_input_client->player_id = player_id;
PP_Snapshot *ss = PP_AcquireSnapshot(local_client, PP_NilSnapshot(), tick);
PP_DecodeSnapshot(&br, ss);
}
EndScratch(scratch);
}
#endif
@ -2549,8 +2570,6 @@ JobDef(PP_UpdateSim, UNUSED sig, UNUSED key)
i64 last_publish_to_user_ns = 0;
i64 real_time_ns = 0;
i64 real_dt_ns = 0;
@ -3167,6 +3186,27 @@ JobDef(PP_UpdateSim, UNUSED sig, UNUSED key)
}
}
#if 0
if (IsSwappingOut())
{
TempArena scratch = BeginScratchNoConflict();
u64 max_size = Mebi(64);
u8 *bytes = PushStructsNoZero(scratch.arena, u8, max_size);
BB_Buff bb = BB_BuffFromString(STRING(max_size, bytes));
{
BB_Writer bw = BB_WriterFromBuff(&bb);
u64 tick = local_client->last_tick;
PP_Snapshot *ss = PP_SnapshotFromTick(local_client, tick);
BB_WriteUBits(&bw, tick, 64);
BB_WriteUid(&bw, local_client->player_id.uid);
PP_EncodeSnapshot(&bw, local_client, PP_NilSnapshot(), ss);
WriteSwappedState(Lit("pp_snapshot"), STRING(BB_GetNumBytesWritten(&bw), BB_GetWrittenRaw(&bw)));
}
EndScratch(scratch);
}
#endif
#if 0
PP_ReleaseClientStore(store);
PP_ReleaseAccel(&accel);

View File

@ -248,11 +248,11 @@ Struct(PP_Ent)
//- Sprite
ResourceKey sprite;
String sprite_span_name;
S_SpanKey sprite_span_key;
u32 sprite_tint;
Vec3 sprite_emittance;
String sprite_collider_slice; /* Collider will sync to bounds of this slice if set */
S_SliceKey sprite_collider_slice_key; /* Collider will sync to bounds of this slice if set */
Xform sprite_local_xform; /* Sprite transform in relation to ent */
@ -265,8 +265,8 @@ Struct(PP_Ent)
//- Attachment
/* PP_Prop_Attached */
/* Slice name on the parent ent's sprite to attach to */
String attach_slice;
/* Slice on the parent ent's sprite to attach to */
S_SliceKey attach_slice_key;
//- Equip
@ -351,9 +351,9 @@ Struct(PP_Ent)
Xform test_start_sprite_xform;
/* PP_Prop_SoundEmitterTest */
String sound_name;
MIX_TrackDesc sound_desc;
MIX_Handle sound_handle;
// String sound_name;
// MIX_TrackDesc sound_desc;
// MIX_Handle sound_handle;
//- Camera

View File

@ -39,7 +39,7 @@ PP_Ent *PP_SpawnTestSmg(PP_Ent *parent)
e->sprite = ResourceKeyFromStore(&PP_Resources, Lit("sprite/gun.ase"));
PP_EnableProp(e, PP_Prop_Attached);
e->attach_slice = Lit("attach.wep");
e->attach_slice_key = S_SliceKeyFromName(Lit("attach.wep"));
e->layer = PP_Layer_RelativeWeapon;
PP_EnableProp(e, PP_Prop_Smg);
@ -55,7 +55,7 @@ PP_Ent *PP_SpawnTestLauncher(PP_Ent *parent)
e->sprite = ResourceKeyFromStore(&PP_Resources, Lit("sprite/gun.ase"));
PP_EnableProp(e, PP_Prop_Attached);
e->attach_slice = Lit("attach.wep");
e->attach_slice_key = S_SliceKeyFromName(Lit("attach.wep"));
e->layer = PP_Layer_RelativeWeapon;
PP_EnableProp(e, PP_Prop_Launcher);
@ -71,7 +71,7 @@ PP_Ent *PP_SpawnTestChucker(PP_Ent *parent)
chucker->sprite = ResourceKeyFromStore(&PP_Resources, Lit("sprite/gun.ase"));
PP_EnableProp(chucker, PP_Prop_Attached);
chucker->attach_slice = Lit("attach.wep");
chucker->attach_slice_key = S_SliceKeyFromName(Lit("attach.wep"));
chucker->layer = PP_Layer_RelativeWeapon;
PP_EnableProp(chucker, PP_Prop_Chucker);
@ -85,7 +85,7 @@ PP_Ent *PP_SpawnTestChucker(PP_Ent *parent)
PP_EnableProp(zone, PP_Prop_ChuckerZone);
PP_EnableProp(zone, PP_Prop_Attached);
zone->attach_slice = Lit("out");
zone->attach_slice_key = S_SliceKeyFromName(Lit("out"));
PP_EnableProp(zone, PP_Prop_Sensor);
CLD_Shape collider = ZI;
@ -125,9 +125,9 @@ PP_Ent *PP_SpawnTestEmployee(PP_Ent *parent)
}
//e->sprite = ResourceKeyFromStore(PP_Resources, Lit("sprite/box_rounded.ase"));
//e->sprite_span_name = Lit("idle.unarmed");
//e->sprite_span_name = Lit("idle.one_handed");
e->sprite_span_name = Lit("idle.two_handed");
//e->sprite_span_key = S_SpanKeyFromName(Lit("idle.unarmed"));
//e->sprite_span_key = S_SpanKeyFromName(Lit("idle.one_handed"));
e->sprite_span_key = S_SpanKeyFromName(Lit("idle.two_handed"));
e->layer = PP_Layer_Shoulders;
e->local_collider.points[0] = VEC2(0, 0);
@ -1255,7 +1255,7 @@ void PP_StepSim(PP_SimStepCtx *ctx)
/* Update animation */
{
S_Span span = S_SpanFromName(sheet, ent->sprite_span_name);
S_Span span = S_SpanFromKey(sheet, ent->sprite_span_key);
if (ent->animation_last_frame_change_time_ns == 0)
{
ent->animation_last_frame_change_time_ns = SecondsFromNs(world->sim_time_ns);
@ -1291,7 +1291,7 @@ void PP_StepSim(PP_SimStepCtx *ctx)
#if 0
/* Update sprite local xform */
{
S_Slice pivot_slice = S_SliceFromName(sheet, Lit("pivot"), ent->animation_frame);
S_Slice pivot_slice = S_SliceFromKey(sheet, Lit("pivot"), ent->animation_frame);
Vec2 sprite_size = DivVec2(sheet->frame_size, (f32)PIXELS_PER_UNIT);
Vec2 dir = MulVec2Vec2(sprite_size, pivot_slice.dir);
@ -1306,10 +1306,10 @@ void PP_StepSim(PP_SimStepCtx *ctx)
#endif
/* Update collider from sprite */
if (ent->sprite_collider_slice.len > 0)
if (ent->sprite_collider_slice_key.hash != 0)
{
Xform cxf = ent->sprite_local_xform;
S_Slice slice = S_SliceFromName(sheet, ent->sprite_collider_slice, ent->animation_frame);
S_Slice slice = S_SliceFromKey(sheet, ent->sprite_collider_slice_key, ent->animation_frame);
ent->local_collider = CLD_ShapeFromQuad(MulXformQuad(cxf, QuadFromRect(slice.rect)));
}
@ -1364,7 +1364,7 @@ void PP_StepSim(PP_SimStepCtx *ctx)
Xform parent_sprite_xf = parent->sprite_local_xform;
S_Slice attach_slice = S_SliceFromName(parent_sheet, ent->attach_slice, parent->animation_frame);
S_Slice attach_slice = S_SliceFromKey(parent_sheet, ent->attach_slice_key, parent->animation_frame);
Vec2 attach_pos = MulXformV2(parent_sprite_xf, attach_slice.center);
Vec2 attach_dir = MulXformBasisV2(parent_sprite_xf, attach_slice.dir);
@ -1454,7 +1454,7 @@ void PP_StepSim(PP_SimStepCtx *ctx)
u32 animation_frame = ent->animation_frame;
S_Sheet *sheet = S_SheetFromResource(sprite);
Xform sprite_local_xform = ent->sprite_local_xform;
S_Slice out_slice = S_SliceFromName(sheet, Lit("out"), animation_frame);
S_Slice out_slice = S_SliceFromKey(sheet, S_SliceKeyFromName(Lit("out")), animation_frame);
Vec2 rel_pos = MulXformV2(sprite_local_xform, out_slice.center);
Vec2 rel_dir = MulXformBasisV2(sprite_local_xform, out_slice.dir);
@ -1503,7 +1503,7 @@ void PP_StepSim(PP_SimStepCtx *ctx)
u32 animation_frame = ent->animation_frame;
S_Sheet *sheet = S_SheetFromResource(sprite);
Xform sprite_local_xform = ent->sprite_local_xform;
S_Slice out_slice = S_SliceFromName(sheet, Lit("out"), animation_frame);
S_Slice out_slice = S_SliceFromKey(sheet, S_SliceKeyFromName(Lit("out")), animation_frame);
Vec2 rel_pos = MulXformV2(sprite_local_xform, out_slice.center);
Vec2 rel_dir = MulXformBasisV2(sprite_local_xform, out_slice.dir);
@ -1674,7 +1674,7 @@ void PP_StepSim(PP_SimStepCtx *ctx)
Vec2 sprite_hold_dir;
{
S_Sheet *sheet = S_SheetFromResource(ent->sprite);
S_Slice slice = S_SliceFromName(sheet, Lit("attach.wep"), ent->animation_frame);
S_Slice slice = S_SliceFromKey(sheet, S_SliceKeyFromName(Lit("attach.wep")), ent->animation_frame);
sprite_hold_pos = slice.center;
sprite_hold_dir = slice.dir;
}

View File

@ -385,21 +385,37 @@ S_Sheet *S_SheetFromResourceAsync(ResourceKey resource)
return result;
}
////////////////////////////////////////////////////////////
//~ Key helpers
S_SpanKey S_SpanKeyFromName(String name)
{
S_SpanKey result = ZI;
result.hash = HashFnv64(Fnv64Basis, name);
return result;
}
S_SliceKey S_SliceKeyFromName(String name)
{
S_SliceKey result = ZI;
result.hash = HashFnv64(Fnv64Basis, name);
return result;
}
////////////////////////////////////////////////////////////
//~ Sheet access operations
S_Span S_SpanFromName(S_Sheet *sheet, String name)
S_Span S_SpanFromKey(S_Sheet *sheet, S_SpanKey key)
{
S_Span result = ZI;
u32 bins_count = sheet->span_bins_count;
if (bins_count > 0)
{
u64 name_hash = HashFnv64(Fnv64Basis, name);
S_SpanBin *bin = &sheet->span_bins[name_hash % bins_count];
S_SpanBin *bin = &sheet->span_bins[key.hash % bins_count];
S_Span *span = bin->first;
for (; span; span = span->next_in_bin)
{
if (span->hash == name_hash)
if (span->hash == key.hash)
{
result = *span;
break;
@ -420,7 +436,7 @@ S_Frame S_FrameFromIndex(S_Sheet *sheet, u64 index)
return result;
}
S_Slice S_SliceFromName(S_Sheet *sheet, String name, u64 frame_index)
S_Slice S_SliceFromKey(S_Sheet *sheet, S_SliceKey key, u64 frame_index)
{
S_Slice result = ZI;
b32 match = 0;
@ -428,12 +444,11 @@ S_Slice S_SliceFromName(S_Sheet *sheet, String name, u64 frame_index)
u32 bins_count = sheet->slice_group_bins_count;
if (bins_count > 0 && sheet->frames_count > 0)
{
u64 name_hash = HashFnv64(Fnv64Basis, name);
S_SliceGroupBin *bin = &sheet->slice_group_bins[name_hash % bins_count];
S_SliceGroupBin *bin = &sheet->slice_group_bins[key.hash % bins_count];
S_SliceGroup *group = bin->first;
for (; group; group = group->next_in_bin)
{
if (group->hash == name_hash)
if (group->hash == key.hash)
{
result = group->slices[frame_index % sheet->frames_count];
match = 1;
@ -445,7 +460,8 @@ S_Slice S_SliceFromName(S_Sheet *sheet, String name, u64 frame_index)
/* Return 'pivot' by default */
if (!match)
{
if (EqString(name, Lit("pivot")))
S_SliceKey pivot_key = S_SliceKeyFromName(Lit("pivot"));
if (key.hash == pivot_key.hash)
{
/* 'pivot' slice does not exist, return center */
result.center = VEC2(0, 0);
@ -455,7 +471,7 @@ S_Slice S_SliceFromName(S_Sheet *sheet, String name, u64 frame_index)
}
else
{
result = S_SliceFromName(sheet, Lit("pivot"), frame_index);
result = S_SliceFromKey(sheet, pivot_key, frame_index);
}
}

View File

@ -1,3 +1,16 @@
////////////////////////////////////////////////////////////
//~ Key types
Struct(S_SpanKey)
{
u64 hash;
};
Struct(S_SliceKey)
{
u64 hash;
};
////////////////////////////////////////////////////////////
//~ Texture types
@ -157,9 +170,15 @@ S_Texture *S_TextureFromResourceAsync(ResourceKey resource);
S_Sheet *S_SheetFromResource(ResourceKey resource);
S_Sheet *S_SheetFromResourceAsync(ResourceKey resource);
////////////////////////////////////////////////////////////
//~ Key helpers
S_SpanKey S_SpanKeyFromName(String name);
S_SliceKey S_SliceKeyFromName(String name);
////////////////////////////////////////////////////////////
//~ Sheet access operations
S_Span S_SpanFromName(S_Sheet *sheet, String name);
S_Span S_SpanFromKey(S_Sheet *sheet, S_SpanKey key);
S_Frame S_FrameFromIndex(S_Sheet *sheet, u64 index);
S_Slice S_SliceFromName(S_Sheet *sheet, String name, u64 frame_index);
S_Slice S_SliceFromKey(S_Sheet *sheet, S_SliceKey key, u64 frame_index);