Added some comments and made the debug-info-text display more info + more relevant info

This commit is contained in:
2025-12-16 11:31:30 +01:00
parent 4ea71838b9
commit fd2dbf232d
5 changed files with 72 additions and 17 deletions

View File

@@ -45,16 +45,18 @@ bool game_instance_host_session(Game_Instance *instance, const Game_Session_Sett
instance->game_session = NULL; instance->game_session = NULL;
return false; return false;
} }
instance->local_player_id = host_player_id; instance->local_player_id = host_player_id;
} }
else { else {
game_session_init(instance->game_session, settings); game_session_init(instance->game_session, settings);
for(uint32_t i = 0; i < settings.max_players; i++) { for(uint32_t i = 0; i < settings.max_players; i++) {
// When playing locally, create the player(s) here. // When playing locally/offline, create the player(s) here.
assert(game_session_create_player(instance->game_session, &instance->local_player_id)); // TODO: SS - Fix me! Always assigning the newest player to be the active 'local_player_id' which isn't correct. assert(game_session_create_player(instance->game_session, &instance->local_player_id)); // TODO: SS - Fix me! Always assigning the newest player to be the active 'local_player_id' which isn't correct.
} }
printf("Created %u local players.\n", settings.max_players);
} }
return true; return true;
@@ -78,12 +80,43 @@ bool game_instance_join_session(Game_Instance *instance, const char *session_id)
instance->game_session = (Game_Session *)calloc(1, sizeof(Game_Session)); // LEAK instance->game_session = (Game_Session *)calloc(1, sizeof(Game_Session)); // LEAK
if(!networking_try_join(&instance->game_networking, instance->game_session, session_id)) { if(!networking_try_join(&instance->game_networking, instance->game_session, session_id)) {
free(instance->game_session);
return false; return false;
} }
return true; return true;
} }
void game_instance_stop_session(Game_Instance *instance) {
assert(instance != NULL);
assert(instance->game_session != NULL);
Game_Session *session = instance->game_session;
Game_Networking *networking = &instance->game_networking;
printf("Stopping session ...\n");
if(session->settings.online) { // Are we online? Offline?
printf("Session is online (session-id: '%s', host? %s).\n", networking->session_id, networking->is_host ? "true" : "false");
if(networking->is_host) {
// Stop hosting.
networking_stop_hosting(networking);
}
else {
// Disconnect from the host.
assert(false); // TEMP
}
}
else {
printf("Session is offline/local.\n");
}
game_session_dispose(instance->game_session);
free(instance->game_session);
instance->game_session = NULL;
}
bool game_instance_push_local_input(Game_Instance *instance, Simulation_Game_Input input) { bool game_instance_push_local_input(Game_Instance *instance, Simulation_Game_Input input) {
return squeue_push(&instance->local_input_queue, (void *)&input); return squeue_push(&instance->local_input_queue, (void *)&input);
} }

View File

@@ -9,7 +9,7 @@
#include "shared/squeue.h" #include "shared/squeue.h"
typedef struct { typedef struct {
uint16_t local_player_id; uint16_t local_player_id; // TODO: SS - We need to modify this to support local multiplayer.
SQueue local_input_queue; SQueue local_input_queue;
double simulation_accumulator; double simulation_accumulator;
@@ -24,7 +24,7 @@ void game_instance_dispose(Game_Instance *instance);
bool game_instance_host_session(Game_Instance *instance, const Game_Session_Settings settings); bool game_instance_host_session(Game_Instance *instance, const Game_Session_Settings settings);
bool game_instance_join_session(Game_Instance *instance, const char *session_id); bool game_instance_join_session(Game_Instance *instance, const char *session_id);
// TODO: SS - stop_session() void game_instance_stop_session(Game_Instance *instance);
bool game_instance_push_local_input(Game_Instance *instance, Simulation_Game_Input input); bool game_instance_push_local_input(Game_Instance *instance, Simulation_Game_Input input);
bool game_instance_pop_local_input(Game_Instance *instance, Simulation_Game_Input *out_input); bool game_instance_pop_local_input(Game_Instance *instance, Simulation_Game_Input *out_input);

View File

@@ -279,30 +279,46 @@ static void state_render(Presentation_State *state) {
// TODO: SS - Switch on 'sim_world->match_state'. // TODO: SS - Switch on 'sim_world->match_state'.
if(ctx->debug_draw_session_details) { if(ctx->debug_draw_session_details) {
char buf[1024]; char buf[2048];
snprintf(&buf[0], sizeof(buf),
// Add generic info about the game to the buffer.
uint32_t i = snprintf(&buf[0], sizeof(buf),
"Match-state: %i\n" "Match-state: %i\n"
"Tick: %lu\n" "Tick: %lu\n"
"\n" "\n"
"Seed: %u\n" "Seed: %u\n"
"Level: %ux%u\n" "Level: %ux%u\n"
"Tickrate: %.2f\n" "Tickrate: %.2f\n"
"\n"
// TODO: SS - This should only be shown when playing online. Other things should be shown in the cases below;
// When playing offline/singleplayer you can only be one player.
// When playing local-multiplayer, you control multiple players from the same computer/client/instance.
"Local player: %u (host? %s)\n"
"Player count: %u / %u\n"
, ,
sim_world->match_state, sim_world->tick, sim_world->match_state, sim_world->tick,
sim_world->game_world->seed, sim_world->game_world->seed,
sim_world->game_world->grid.width, sim_world->game_world->grid.height, sim_world->game_world->grid.width, sim_world->game_world->grid.height,
session->settings.tickrate, session->settings.tickrate
instance->local_player_id, instance->local_player_id == 0 ? "true" : "false", // NOTE: SS - You're the host if you're player 0.
game_session_get_amount_of_active_players(session), sim_world->max_players
); );
// Now, write info specific to whether you're online or offline.
if(session->settings.online) {
Game_Networking *networking = &instance->game_networking;
snprintf(&buf[i], sizeof(buf),
"\n"
"[Online]\n"
"Host: %s\n"
"Session-id: %s\n"
"Client-id: %s\n"
"\n"
"Local player: %u\n"
"Player count: %u / %u\n"
,
networking->is_host ? "true" : "false",
networking->session_id,
networking->local_client_id,
instance->local_player_id,
game_session_get_amount_of_active_players(session), sim_world->max_players
);
}
// Finally, draw the buffer to the screen (with nice shadowing!).
DrawText(buf, 17, 17, 8, BLACK); DrawText(buf, 17, 17, 8, BLACK);
DrawText(buf, 16, 16, 8, WHITE); DrawText(buf, 16, 16, 8, WHITE);
} }
@@ -321,6 +337,7 @@ static void state_exit(Presentation_State *state) {
UnloadTexture(ctx->texture_snake_head); UnloadTexture(ctx->texture_snake_head);
UnloadTexture(ctx->texture_snake_body); UnloadTexture(ctx->texture_snake_body);
game_instance_stop_session(ctx->game_instance);
ctx->game_instance = NULL; ctx->game_instance = NULL;
} }

View File

@@ -135,6 +135,7 @@ bool networking_try_host(Game_Networking *networking, Game_Session *session, Gam
} }
printf("Started hosting session '%s', local_client_id is '%s'.\n", session_id, local_client_id); printf("Started hosting session '%s', local_client_id is '%s'.\n", session_id, local_client_id);
networking->is_host = true;
networking->game_session = session; networking->game_session = session;
networking->session_id = session_id; networking->session_id = session_id;
networking->local_client_id = local_client_id; networking->local_client_id = local_client_id;
@@ -198,6 +199,7 @@ bool networking_try_join(Game_Networking *networking, Game_Session *session, con
assert(strcmp(result_session_id, session_id) == 0); assert(strcmp(result_session_id, session_id) == 0);
printf("Joined session '%s', local_client_id is '%s'.\n", result_session_id, local_client_id); printf("Joined session '%s', local_client_id is '%s'.\n", result_session_id, local_client_id);
networking->is_host = true;
networking->game_session = session; networking->game_session = session;
networking->session_id = result_session_id; networking->session_id = result_session_id;
networking->local_client_id = local_client_id; networking->local_client_id = local_client_id;
@@ -238,7 +240,8 @@ bool networking_stop_hosting(Game_Networking *networking) {
mp_api_destroy(networking->api); mp_api_destroy(networking->api);
networking->api = NULL; networking->api = NULL;
networking->session = NULL; networking->game_session = NULL;
networking->is_host = false;
if(networking->session_id != NULL) { if(networking->session_id != NULL) {
free(networking->session_id); free(networking->session_id);

View File

@@ -9,6 +9,8 @@
typedef struct { typedef struct {
MultiplayerApi *api; MultiplayerApi *api;
bool is_host;
char *session_id; char *session_id;
char *local_client_id; char *local_client_id;