power_play/src/pp/pp.h
2025-10-25 10:27:47 -05:00

340 lines
8.8 KiB
C

////////////////////////////////////////////////////////////
//~ Binds
//- Bind kinds
Enum(BindKind)
{
BindKind_None,
BindKind_MoveUp,
BindKind_MoveDown,
BindKind_MoveLeft,
BindKind_MoveRight,
BindKind_Walk,
BindKind_Fire,
BindKind_AltFire,
BindKind_TestTile,
BindKind_DebugClear,
BindKind_DebugSpawn1,
BindKind_DebugSpawn2,
BindKind_DebugSpawn3,
BindKind_DebugSpawn4,
BindKind_DebugWalls,
BindKind_DebugFollow,
BindKind_DebugDraw,
BindKind_DebugConsole,
BindKind_DebugCamera,
BindKind_DebugPause,
BindKind_DebugStep,
BindKind_DebugDrag,
BindKind_DebugDelete,
BindKind_DebugTeleport,
BindKind_DebugExplode,
BindKind_DebugToggleTopmost,
BindKind_FullscreenMod,
BindKind_Fullscreen,
BindKind_ZoomIn,
BindKind_ZoomOut,
BindKind_Pan,
#if RtcIsEnabled
/* Debug */
BindKind_ResetDebugSteps,
BindKind_IncrementDebugSteps,
BindKind_DecrementDebugSteps,
#endif
BindKind_Count
};
//- Test bindings
/* TODO: Remove this */
Global Readonly BindKind g_binds[Btn_Count] = {
[Btn_W] = BindKind_MoveUp,
[Btn_S] = BindKind_MoveDown,
[Btn_A] = BindKind_MoveLeft,
[Btn_D] = BindKind_MoveRight,
[Btn_M1] = BindKind_Fire,
[Btn_M2] = BindKind_AltFire,
#if 0
[Btn_Alt] = BindKind_Walk,
#endif
/* Testing */
[Btn_Z] = BindKind_TestTile,
[Btn_M5] = BindKind_DebugDrag,
[Btn_M4] = BindKind_DebugDelete,
[Btn_F] = BindKind_DebugExplode,
[Btn_T] = BindKind_DebugTeleport,
[Btn_C] = BindKind_DebugClear,
[Btn_1] = BindKind_DebugSpawn1,
[Btn_2] = BindKind_DebugSpawn2,
[Btn_3] = BindKind_DebugSpawn3,
[Btn_4] = BindKind_DebugSpawn4,
[Btn_G] = BindKind_DebugWalls,
[Btn_N] = BindKind_DebugStep,
[Btn_Q] = BindKind_DebugFollow,
[Btn_F1] = BindKind_DebugPause,
[Btn_F2] = BindKind_DebugCamera,
[Btn_F3] = BindKind_DebugDraw,
[Btn_F4] = BindKind_DebugToggleTopmost,
[Btn_GraveAccent] = BindKind_DebugConsole,
[Btn_Alt] = BindKind_FullscreenMod,
[Btn_Enter] = BindKind_Fullscreen,
[Btn_MWheelUp] = BindKind_ZoomIn,
[Btn_MWheelDown] = BindKind_ZoomOut,
[Btn_M3] = BindKind_Pan,
#if RtcIsEnabled
[Btn_ForwardSlash] = BindKind_ResetDebugSteps,
[Btn_Comma] = BindKind_DecrementDebugSteps,
[Btn_Period] = BindKind_IncrementDebugSteps
#endif
};
////////////////////////////////////////////////////////////
//~ Stats
Struct(SecondsStat)
{
u64 last_second_start;
u64 last_second_end;
u64 last_second;
};
////////////////////////////////////////////////////////////
//~ Console log
Struct(ConsoleLog)
{
String msg;
i32 level;
i32 color_index;
P_DateTime datetime;
i64 time_ns;
Rect bounds;
ConsoleLog *prev;
ConsoleLog *next;
};
////////////////////////////////////////////////////////////
//~ Sim decode queue
Struct(DecodeQueueNode)
{
Client *client;
u64 tick;
u64 base_tick;
String tmp_encoded;
DecodeQueueNode *next;
};
Struct(DecodeQueue)
{
DecodeQueueNode *first;
DecodeQueueNode *last;
};
////////////////////////////////////////////////////////////
//~ State types
Struct(BindState)
{
b32 is_held; /* Is this bind held down this frame */
u32 num_presses; /* How many times was this bind's pressed since last frame */
u32 num_repeats; /* How many times was this bind's key repeated since last frame */
u32 num_presses_and_repeats; /* Same as `num_presses` but includes key repeats as well */
u32 num_releases; /* How many times was this bind released since last frame */
};
Struct(SharedUserState)
{
Atomic32 shutdown;
Fence shutdown_jobs_fence;
u64 shutdown_jobs_count;
Arena *arena;
String connect_address_str;
ClientStore *user_client_store;
Client *user_unblended_client; /* Contains snapshots received from local sim */
Client *user_blended_client; /* Contains single snapshot from result of blending local sim snapshots */
Snapshot *ss_blended; /* Points to blended snapshot contained in blended client */
//- Usage stats
i64 last_second_reset_ns;
SecondsStat net_bytes_read;
SecondsStat net_bytes_sent;
//- Renderer gbuffers
GPU_Resource *albedo;
GPU_Resource *emittance;
GPU_Resource *emittance_flood_read;
GPU_Resource *emittance_flood_target;
GPU_Resource *shade_read;
GPU_Resource *shade_target;
//- Renderer transient buffers
GPU_TransientBuffer material_instances_tbuff;
GPU_TransientBuffer grids_tbuff;
Arena *material_instances_arena;
Arena *grids_arena;
//- Renderer state
RandState frame_rand;
u64 frame_index;
i64 gpu_render_fence_target;
//- Bind state
BindState bind_states[BindKind_Count];
//- Debug console
Mutex console_logs_mutex;
Arena *console_logs_arena;
ConsoleLog *first_console_log;
ConsoleLog *last_console_log;
i32 console_log_color_indices[P_LogLevel_Count];
f32 console_logs_height;
b32 debug_console;
//- Window -> user
Mutex sys_window_events_mutex;
Arena *sys_window_events_arena;
//- User -> sim
Mutex user_sim_cmd_mutex;
ControlData user_sim_cmd_control;
EntityId user_hovered_ent;
u64 last_user_sim_cmd_gen;
u64 user_sim_cmd_gen;
Atomic32 user_paused;
Atomic32 user_paused_steps;
//- Sim -> user
Mutex local_to_user_client_mutex;
ClientStore *local_to_user_client_store;
Client *local_to_user_client;
i64 local_to_user_client_publish_dt_ns;
i64 local_to_user_client_publish_time_ns;
//- Local sim -> user rolling window of publish time deltas
i64 last_local_to_user_snapshot_published_at_ns;
i64 average_local_to_user_snapshot_publish_dt_ns;
i64 local_sim_predicted_time_ns; /* Calculated from <last local sim to user pubilsh time> + <time since last local sim to user publish> */
i64 render_time_target_ns; /* Claculated from <local_sim_rpedicted_time_ns> - <render interp delay> */
i64 render_time_ns; /* Incremented at a constant rate based on average local to user publish delta, but snaps to render_time_target_ns if it gets too distant */
u64 local_sim_last_known_tick;
i64 local_sim_last_known_time_ns;
i64 real_dt_ns;
i64 real_time_ns;
//////////////////////////////
//- Persist start
StructRegion(PERSIST_START);
//- Window
WND_Settings window_settings;
//- Debug camera
EntityId debug_following;
b32 debug_camera;
b32 debug_camera_panning;
Vec2 debug_camera_pan_start;
b32 debug_draw;
//- Per frame
Vec2I32 screen_size;
Vec2 screen_cursor;
Xform ui_to_screen_xf;
Vec2I32 ui_size;
Vec2 ui_cursor;
Xform render_to_ui_xf;
Vec2I32 render_size;
Xform world_to_render_xf;
Xform world_to_ui_xf;
Vec2 world_cursor;
Vec2 focus_send;
StructRegion(PERSIST_END);
//- Persist end
//////////////////////////////
} extern shared_user_state;
////////////////////////////////////////////////////////////
//~ Swap types
Struct(SwappedUserState)
{
SharedUserState s;
};
////////////////////////////////////////////////////////////
//~ Startup
void StartupUser(void);
////////////////////////////////////////////////////////////
//~ Shutdown
ExitFuncDef(ShutdownUser);
////////////////////////////////////////////////////////////
//~ Ui helpers
void PushGameUiStyle(void);
void DrawDebugXform(Xform xf, u32 color_x, u32 color_y);
void DrawDebugMovement(Entity *ent);
String DebugStringFromEntity(Arena *arena, Entity *ent);
////////////////////////////////////////////////////////////
//~ Console draw operations
P_LogEventCallbackFuncDef(ConsoleLogCallback, log);
void DrawDebugConsole(i32 level, b32 minimized);
////////////////////////////////////////////////////////////
//~ Gpu buffer helpers
GPU_Resource *AcquireGbuffer(GPU_Format format, Vec2I32 size);
////////////////////////////////////////////////////////////
//~ Entity sorting
MergesortCompareFuncDef(EntitySortCmp, arg_a, arg_b, _);
////////////////////////////////////////////////////////////
//~ User update
void UpdateUser(void);
////////////////////////////////////////////////////////////
//~ User update job
JobDecl(UpdateUserOrSleep, EmptySig);
////////////////////////////////////////////////////////////
//~ User input cmds
void GenerateuserInputCmds(Client *user_input_client, u64 tick);
////////////////////////////////////////////////////////////
//~ Sim update job
JobDecl(UpdateSim, EmptySig);