diff --git a/src/instance/game_instance.c b/src/instance/game_instance.c index dd84832..c2d43cc 100644 --- a/src/instance/game_instance.c +++ b/src/instance/game_instance.c @@ -45,16 +45,18 @@ bool game_instance_host_session(Game_Instance *instance, const Game_Session_Sett instance->game_session = NULL; return false; } - + instance->local_player_id = host_player_id; } else { game_session_init(instance->game_session, settings); 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. } + + printf("Created %u local players.\n", settings.max_players); } 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 if(!networking_try_join(&instance->game_networking, instance->game_session, session_id)) { + free(instance->game_session); return false; } 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) { return squeue_push(&instance->local_input_queue, (void *)&input); } diff --git a/src/instance/game_instance.h b/src/instance/game_instance.h index 1b6b456..bc9feb2 100644 --- a/src/instance/game_instance.h +++ b/src/instance/game_instance.h @@ -9,7 +9,7 @@ #include "shared/squeue.h" 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; 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_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_pop_local_input(Game_Instance *instance, Simulation_Game_Input *out_input); diff --git a/src/presentation/states/state_ingame.c b/src/presentation/states/state_ingame.c index 474706e..29bf4db 100644 --- a/src/presentation/states/state_ingame.c +++ b/src/presentation/states/state_ingame.c @@ -279,30 +279,46 @@ static void state_render(Presentation_State *state) { // TODO: SS - Switch on 'sim_world->match_state'. if(ctx->debug_draw_session_details) { - char buf[1024]; - snprintf(&buf[0], sizeof(buf), + char buf[2048]; + + // Add generic info about the game to the buffer. + uint32_t i = snprintf(&buf[0], sizeof(buf), "Match-state: %i\n" "Tick: %lu\n" "\n" "Seed: %u\n" "Level: %ux%u\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->game_world->seed, sim_world->game_world->grid.width, sim_world->game_world->grid.height, - 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 + session->settings.tickrate ); + + // 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, 16, 16, 8, WHITE); } @@ -321,6 +337,7 @@ static void state_exit(Presentation_State *state) { UnloadTexture(ctx->texture_snake_head); UnloadTexture(ctx->texture_snake_body); + game_instance_stop_session(ctx->game_instance); ctx->game_instance = NULL; } diff --git a/src/session/networking.c b/src/session/networking.c index 466ac5a..dc3f4e7 100644 --- a/src/session/networking.c +++ b/src/session/networking.c @@ -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); + networking->is_host = true; networking->game_session = session; networking->session_id = session_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); 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->session_id = result_session_id; networking->local_client_id = local_client_id; @@ -238,7 +240,8 @@ bool networking_stop_hosting(Game_Networking *networking) { mp_api_destroy(networking->api); networking->api = NULL; - networking->session = NULL; + networking->game_session = NULL; + networking->is_host = false; if(networking->session_id != NULL) { free(networking->session_id); diff --git a/src/session/networking.h b/src/session/networking.h index d2db014..810d8b9 100644 --- a/src/session/networking.h +++ b/src/session/networking.h @@ -9,6 +9,8 @@ typedef struct { MultiplayerApi *api; + bool is_host; + char *session_id; char *local_client_id;