power_play/src/pp/pp_core.h
2025-08-01 04:36:37 -05:00

303 lines
8.1 KiB
C

////////////////////////////////
//~ Binds
//- Bind kinds
typedef i32 BindKind; enum
{
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[P_Btn_Count] = {
[P_Btn_W] = BindKind_MoveUp,
[P_Btn_S] = BindKind_MoveDown,
[P_Btn_A] = BindKind_MoveLeft,
[P_Btn_D] = BindKind_MoveRight,
[P_Btn_M1] = BindKind_Fire,
[P_Btn_M2] = BindKind_AltFire,
#if 0
[P_Btn_Alt] = BindKind_Walk,
#endif
/* Testing */
[P_Btn_Z] = BindKind_TestTile,
[P_Btn_M5] = BindKind_DebugDrag,
[P_Btn_M4] = BindKind_DebugDelete,
[P_Btn_F] = BindKind_DebugExplode,
[P_Btn_T] = BindKind_DebugTeleport,
[P_Btn_C] = BindKind_DebugClear,
[P_Btn_1] = BindKind_DebugSpawn1,
[P_Btn_2] = BindKind_DebugSpawn2,
[P_Btn_3] = BindKind_DebugSpawn3,
[P_Btn_4] = BindKind_DebugSpawn4,
[P_Btn_G] = BindKind_DebugWalls,
[P_Btn_N] = BindKind_DebugStep,
[P_Btn_Q] = BindKind_DebugFollow,
[P_Btn_F1] = BindKind_DebugPause,
[P_Btn_F2] = BindKind_DebugCamera,
[P_Btn_F3] = BindKind_DebugDraw,
[P_Btn_F4] = BindKind_DebugToggleTopmost,
[P_Btn_GraveAccent] = BindKind_DebugConsole,
[P_Btn_Alt] = BindKind_FullscreenMod,
[P_Btn_Enter] = BindKind_Fullscreen,
[P_Btn_MWheelUp] = BindKind_ZoomIn,
[P_Btn_MWheelDown] = BindKind_ZoomOut,
[P_Btn_M3] = BindKind_Pan,
#if RtcIsEnabled
[P_Btn_ForwardSlash] = BindKind_ResetDebugSteps,
[P_Btn_Comma] = BindKind_DecrementDebugSteps,
[P_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;
};
////////////////////////////////
//~ Shared state
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;
P_Counter shutdown_job_counters;
P_Window *window;
GPU_Swapchain *swapchain;
struct sim_ctx *local_sim_ctx;
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;
//- Gpu resources
GPU_RenderSig *render_sig;
BindState bind_states[BindKind_Count];
//- Debug camera
EntityId debug_following;
b32 debug_camera;
b32 debug_camera_panning;
Vec2 debug_camera_pan_start;
b32 debug_draw;
//- Debug console
P_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
P_Mutex sys_window_events_mutex;
Arena *sys_window_events_arena;
//- User -> sim
P_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
P_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;
//- Per frame
Vec2 screen_size;
Vec2 screen_cursor;
Xform ui_to_screen_xf;
Vec2 ui_size;
Vec2 ui_cursor;
Xform render_to_ui_xf;
Vec2 render_size;
Xform world_to_render_xf;
Xform world_to_ui_xf;
Vec2 world_cursor;
Vec2 focus_send;
};
extern SharedUserState shared_user_state;
////////////////////////////////
//~ Startup
Struct(UserStartupReceipt) { i32 _; };
UserStartupReceipt StartupUser(F_StartupReceipt *font_sr,
S_StartupReceipt *sprite_sr,
D_StartupReceipt *draw_sr,
AC_StartupReceipt *asset_cache_sr,
SND_StartupReceipt *sound_sr,
MIX_StartupReceipt *mixer_sr,
SimStartupReceipt *sim_sr,
String connect_address_str);
////////////////////////////////
//~ Shutdown
P_ExitFuncDef(ShutdownUser);
////////////////////////////////
//~ Debug draw operations
void DebugDrawXform(Xform xf, u32 color_x, u32 color_y);
void DebugDrawMovement(Entity *ent);
String DebugStringFromEntity(Arena *arena, Entity *ent);
////////////////////////////////
//~ Console draw operations
P_LogEventCallbackFuncDef(ConsoleLogCallback, log);
void DebugDrawConsole(i32 level, b32 minimized);
////////////////////////////////
//~ Entity sortign
MergesortCompareFuncDef(EntitySortCmp, arg_a, arg_b, _);
////////////////////////////////
//~ User update
void UpdateUser(P_Window *window);
P_JobDef(UpdateUserJob, _);
////////////////////////////////
//~ User input cmds
void GenerateuserInputCmds(Client *user_input_client, u64 tick);
////////////////////////////////
//~ Sim update
P_JobDef(SimJob, _);