rename published_tick & world -> prev_tick & next_tick
This commit is contained in:
parent
787bb9e0d1
commit
e6d3506035
71
src/game.c
71
src/game.c
@ -15,18 +15,15 @@ GLOBAL struct {
|
||||
struct atomic_i32 game_thread_shutdown;
|
||||
struct sys_thread game_thread;
|
||||
|
||||
/* Double buffered game state */
|
||||
struct world prev_world; /* Last tick */
|
||||
struct world world;
|
||||
|
||||
/* Game thread input */
|
||||
struct sys_mutex game_cmds_mutex;
|
||||
struct arena game_cmds_arena;
|
||||
|
||||
/* Game thread output */
|
||||
struct sys_mutex published_tick_mutex;
|
||||
struct world published_tick;
|
||||
struct atomic_u64 published_tick_id;
|
||||
/* Ticks */
|
||||
struct sys_mutex prev_tick_mutex;
|
||||
struct atomic_u64 prev_tick_id;
|
||||
struct world prev_tick;
|
||||
struct world next_tick;
|
||||
} G = { 0 }, DEBUG_ALIAS(G, G_game);
|
||||
|
||||
/* ========================== *
|
||||
@ -48,15 +45,13 @@ struct game_startup_receipt game_startup(struct mixer_startup_receipt *mixer_sr,
|
||||
G.game_cmds_mutex = sys_mutex_alloc();
|
||||
G.game_cmds_arena = arena_alloc(GIGABYTE(64));
|
||||
|
||||
/* Initialize world */
|
||||
world_alloc(&G.world);
|
||||
/* Initialize ticks */
|
||||
world_alloc(&G.next_tick);
|
||||
world_alloc(&G.prev_tick);
|
||||
G.prev_tick_mutex = sys_mutex_alloc();
|
||||
arena_set_readonly(&G.prev_tick.entity_store->arena);
|
||||
|
||||
/* Initialize tick transmission */
|
||||
world_alloc(&G.published_tick);
|
||||
arena_set_readonly(&G.published_tick.entity_store->arena);
|
||||
G.published_tick_mutex = sys_mutex_alloc();
|
||||
|
||||
G.world.timescale = GAME_TIMESCALE;
|
||||
G.next_tick.timescale = GAME_TIMESCALE;
|
||||
G.game_thread = sys_thread_alloc(&game_thread_entry_point, NULL, STR("[P2] Game thread"));
|
||||
app_register_exit_callback(&game_shutdown);
|
||||
|
||||
@ -113,7 +108,7 @@ INTERNAL void spawn_test_entities(void)
|
||||
struct v2 size = V2(1, 1);
|
||||
f32 r = 0;
|
||||
|
||||
struct entity *e = entity_alloc_top(G.world.entity_store);
|
||||
struct entity *e = entity_alloc_top(G.next_tick.entity_store);
|
||||
entity_set_xform(e, XFORM_TRS(.t = pos, .r = r, .s = size));
|
||||
|
||||
e->sprite = sprite_tag_from_path(STR("res/graphics/tim.ase"));
|
||||
@ -141,7 +136,7 @@ INTERNAL void spawn_test_entities(void)
|
||||
|
||||
struct xform xf = XFORM_TRS(.t = pos, .r = r, .s = size);
|
||||
|
||||
struct entity *e = entity_alloc_top(G.world.entity_store);
|
||||
struct entity *e = entity_alloc_top(G.next_tick.entity_store);
|
||||
entity_set_local_xform(e, xf);
|
||||
|
||||
e->sprite = sprite_tag_from_path(STR("res/graphics/gun.ase"));
|
||||
@ -154,7 +149,7 @@ INTERNAL void spawn_test_entities(void)
|
||||
|
||||
/* Camera ent */
|
||||
{
|
||||
struct entity *e = entity_alloc_top(G.world.entity_store);
|
||||
struct entity *e = entity_alloc_top(G.next_tick.entity_store);
|
||||
entity_set_xform(e, XFORM_IDENT);
|
||||
|
||||
entity_enable_prop(e, ENTITY_PROP_CAMERA);
|
||||
@ -174,13 +169,13 @@ INTERNAL void spawn_test_entities(void)
|
||||
INTERNAL void publish_game_tick(void)
|
||||
{
|
||||
__prof;
|
||||
struct sys_lock lock = sys_mutex_lock_e(&G.published_tick_mutex);
|
||||
arena_set_readwrite(&G.published_tick.entity_store->arena);
|
||||
struct sys_lock lock = sys_mutex_lock_e(&G.prev_tick_mutex);
|
||||
arena_set_readwrite(&G.prev_tick.entity_store->arena);
|
||||
{
|
||||
world_copy_replace(&G.published_tick, &G.world);
|
||||
world_copy_replace(&G.prev_tick, &G.next_tick);
|
||||
}
|
||||
arena_set_readonly(&G.published_tick.entity_store->arena);
|
||||
atomic_u64_eval_exchange(&G.published_tick_id, G.published_tick.tick_id);
|
||||
arena_set_readonly(&G.prev_tick.entity_store->arena);
|
||||
atomic_u64_eval_exchange(&G.prev_tick_id, G.prev_tick.tick_id);
|
||||
sys_mutex_unlock(&lock);
|
||||
}
|
||||
|
||||
@ -194,13 +189,13 @@ INTERNAL void game_update(void)
|
||||
* Begin frame
|
||||
* ========================== */
|
||||
|
||||
++G.world.tick_id;
|
||||
G.world.tick_ts = sys_timestamp();
|
||||
G.world.dt = max_f64(0.0, (1.0 / GAME_FPS) * G.world.timescale);
|
||||
G.world.time += G.world.dt;
|
||||
++G.next_tick.tick_id;
|
||||
G.next_tick.tick_ts = sys_timestamp();
|
||||
G.next_tick.dt = max_f64(0.0, (1.0 / GAME_FPS) * G.next_tick.timescale);
|
||||
G.next_tick.time += G.next_tick.dt;
|
||||
|
||||
struct game_cmd_array game_cmds = pop_cmds(scratch.arena);
|
||||
struct entity_store *store = G.world.entity_store;
|
||||
struct entity_store *store = G.next_tick.entity_store;
|
||||
struct entity *root = entity_from_handle(store, store->root);
|
||||
struct sprite_scope *sprite_frame_scope = sprite_scope_begin();
|
||||
|
||||
@ -262,7 +257,7 @@ INTERNAL void game_update(void)
|
||||
struct sprite_sheet *sheet = sprite_sheet_from_tag_await(sprite_frame_scope, ent->sprite);
|
||||
struct sprite_sheet_span span = sprite_sheet_get_span(sheet, ent->sprite_span_name);
|
||||
|
||||
f64 time_in_frame = ent->animation_time_in_frame + G.world.dt;
|
||||
f64 time_in_frame = ent->animation_time_in_frame + G.next_tick.dt;
|
||||
u64 frame_index = ent->animation_frame;
|
||||
if (frame_index < span.start || frame_index > span.end) {
|
||||
frame_index = span.start;
|
||||
@ -380,7 +375,7 @@ INTERNAL void game_update(void)
|
||||
|
||||
/* ENTITY_PROP_TEST */
|
||||
if (entity_has_prop(ent, ENTITY_PROP_TEST)) {
|
||||
f32 t = ((f32)G.world.time);
|
||||
f32 t = ((f32)G.next_tick.time);
|
||||
struct v2 og = v2_mul(V2(math_cos(t), math_sin(t)), 3);
|
||||
f32 r = t * 3;
|
||||
struct v2 s = V2(1 + (math_fabs(math_sin(t * 5)) * 3), 1);
|
||||
@ -514,7 +509,7 @@ INTERNAL void game_update(void)
|
||||
* ========================== */
|
||||
|
||||
{
|
||||
f32 dt = (f32)G.world.dt;
|
||||
f32 dt = (f32)G.next_tick.dt;
|
||||
|
||||
/* Apply acceleration to velocity */
|
||||
struct v2 a = v2_mul(ent->acceleration, dt);
|
||||
@ -564,7 +559,7 @@ INTERNAL void game_update(void)
|
||||
|
||||
/* Lerp camera */
|
||||
if (ent->camera_applied_lerp_continuity_gen_plus_one == ent->camera_lerp_continuity_gen + 1) {
|
||||
f32 t = 1 - math_pow(2.f, -20.f * (f32)G.world.dt);
|
||||
f32 t = 1 - math_pow(2.f, -20.f * (f32)G.next_tick.dt);
|
||||
xf = xform_lerp(xf, ent->camera_xform_target, t);
|
||||
} else {
|
||||
/* Skip lerp */
|
||||
@ -580,7 +575,7 @@ INTERNAL void game_update(void)
|
||||
|
||||
if (entity_has_prop(ent, ENTITY_PROP_TEST_SOUND_EMITTER)) {
|
||||
struct mixer_desc desc = ent->sound_desc;
|
||||
desc.speed = G.world.timescale;
|
||||
desc.speed = G.next_tick.timescale;
|
||||
|
||||
desc.pos = entity_get_xform(ent).og;
|
||||
struct sound *sound = sound_load_async(ent->sound_name, 0);
|
||||
@ -619,7 +614,7 @@ INTERNAL void game_update(void)
|
||||
|
||||
struct entity *child = node.entity;
|
||||
|
||||
/* Calculate world xform */
|
||||
/* Calculate global xform */
|
||||
struct xform xf;
|
||||
if (child->cached_global_xform_dirty) {
|
||||
xf = xform_mul(node.parent_global_xform, child->local_xform);
|
||||
@ -691,14 +686,14 @@ INTERNAL SYS_THREAD_ENTRY_POINT_FUNC_DEF(game_thread_entry_point, arg)
|
||||
void game_get_latest_tick(struct world *dest)
|
||||
{
|
||||
__prof;
|
||||
struct sys_lock lock = sys_mutex_lock_e(&G.published_tick_mutex);
|
||||
world_copy_replace(dest, &G.published_tick);
|
||||
struct sys_lock lock = sys_mutex_lock_s(&G.prev_tick_mutex);
|
||||
world_copy_replace(dest, &G.prev_tick);
|
||||
sys_mutex_unlock(&lock);
|
||||
}
|
||||
|
||||
u64 game_get_latest_tick_id(void)
|
||||
{
|
||||
return atomic_u64_eval(&G.published_tick_id);
|
||||
return atomic_u64_eval(&G.prev_tick_id);
|
||||
}
|
||||
|
||||
void game_push_cmds(struct game_cmd_array cmd_array)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user