network_stop() and shortcuts to singleplayer and local multiplayer.

This commit is contained in:
2025-12-16 12:14:38 +01:00
parent 8cdbd5b162
commit 168278faf7
4 changed files with 47 additions and 49 deletions

View File

@@ -28,6 +28,11 @@ bool game_instance_host_session(Game_Instance *instance, const Game_Session_Sett
return false;
}
if(settings.max_players == 0) {
printf("Failed. Max-players is not allowed to be 0.\n");
return false;
}
instance->game_session = (Game_Session *)calloc(1, sizeof(Game_Session)); // LEAK
if(settings.online) {
@@ -98,15 +103,7 @@ void game_instance_stop_session(Game_Instance *instance) {
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
}
networking_stop(networking);
}
else {
printf("Session is offline/local.\n");

View File

@@ -14,44 +14,39 @@ static void state_enter(Presentation_State *state) {
(void)ctx;
ctx->mode = Main_Menu_Mode_Home;
// if(ctx->game_session != NULL) {
// if(ctx->game_session->is_singleplayer) {
// }
// else {
// if(ctx->game_session->is_host) {
// networking_stop_hosting(ctx->game_networking);
// }
// else {
// }
// }
// game_session_dispose(ctx->game_session);
// ctx->game_session = NULL;
// }
}
static void state_tick(Presentation_State *state) {
Presentation_State_Main_Menu_Context *ctx = (Presentation_State_Main_Menu_Context *)state->context;
// { // DEBUG
// if(IsKeyPressed(KEY_P)) {
// game_session_init_default_settings(true, &ctx->session_settings);
// assert(ctx->game_session == NULL);
// ctx->game_session = (Game_Session *)calloc(1, sizeof(Game_Session));
// game_session_init(
// ctx->game_session,
// ctx->session_settings
// );
// ctx->ingame_ctx->game_instance = ctx->game_instance;
// presentation_state_machine_go_to(&presentation_state_ingame);
// }
// }
{ // DEBUG
if(IsKeyPressed(KEY_P)) {
printf("Shortcut to singleplayer.\n");
game_session_init_default_settings(false, &ctx->session_settings);
ctx->session_settings.max_players = 1;
if(game_instance_host_session(ctx->game_instance, ctx->session_settings)) {
ctx->ingame_ctx->game_instance = ctx->game_instance;
presentation_state_machine_go_to(&presentation_state_ingame);
}
else {
printf("Failed to play.\n");
}
}
else if(IsKeyPressed(KEY_L)) {
printf("Shortcut to local multiplayer (2 players).\n");
game_session_init_default_settings(false, &ctx->session_settings);
ctx->session_settings.max_players = 2;
if(game_instance_host_session(ctx->game_instance, ctx->session_settings)) {
ctx->ingame_ctx->game_instance = ctx->game_instance;
presentation_state_machine_go_to(&presentation_state_ingame);
}
else {
printf("Failed to play.\n");
}
}
}
}
static void state_render(Presentation_State *state) {
@@ -80,6 +75,8 @@ static void state_render(Presentation_State *state) {
case Main_Menu_Mode_Singleplayer: {
ctx->mode = Main_Menu_Mode_Game_Setup;
game_session_init_default_settings(false, &ctx->session_settings);
ctx->ingame_ctx->game_instance = ctx->game_instance;
presentation_state_machine_go_to(&presentation_state_ingame);
break;
}

View File

@@ -227,17 +227,21 @@ bool networking_try_join(Game_Networking *networking, Game_Session *session, con
return true;
}
bool networking_stop_hosting(Game_Networking *networking) {
bool networking_stop(Game_Networking *networking) {
assert(networking != NULL);
if(networking->api == NULL) {
printf("Failed to stop hosting; API is not set up.\n");
printf("Failed to stop networking; API is not set up.\n");
return false;
}
// TODO: SS - MP_API (preferably) should send a 'hey the server has shut down' message to all connected clients.
// If not, we might need to do it by having the host send a Game-message to all clients.
map_deinit(&networking->client_id_to_player_index);
assert(stop_listening(networking));
networking->listener_id = 0;
mp_api_destroy(networking->api);
networking->api = NULL;
networking->game_session = NULL;
@@ -252,6 +256,6 @@ bool networking_stop_hosting(Game_Networking *networking) {
networking->local_client_id = NULL;
}
printf("Stopped hosting.\n");
printf("Stopped networking.\n");
return true;
}

View File

@@ -23,6 +23,6 @@ typedef struct {
bool networking_try_host(Game_Networking *networking, Game_Session *session, Game_Session_Settings settings, uint16_t *out_host_player_id);
bool networking_try_join(Game_Networking *networking, Game_Session *session, const char *session_id);
bool networking_stop_hosting(Game_Networking *networking);
bool networking_stop(Game_Networking *networking);
#endif