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; 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 instance->game_session = (Game_Session *)calloc(1, sizeof(Game_Session)); // LEAK
if(settings.online) { if(settings.online) {
@@ -98,15 +103,7 @@ void game_instance_stop_session(Game_Instance *instance) {
if(session->settings.online) { // Are we online? Offline? 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"); printf("Session is online (session-id: '%s', host? %s).\n", networking->session_id, networking->is_host ? "true" : "false");
networking_stop(networking);
if(networking->is_host) {
// Stop hosting.
networking_stop_hosting(networking);
}
else {
// Disconnect from the host.
assert(false); // TEMP
}
} }
else { else {
printf("Session is offline/local.\n"); printf("Session is offline/local.\n");

View File

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

View File

@@ -227,16 +227,20 @@ bool networking_try_join(Game_Networking *networking, Game_Session *session, con
return true; return true;
} }
bool networking_stop_hosting(Game_Networking *networking) { bool networking_stop(Game_Networking *networking) {
assert(networking != NULL); assert(networking != NULL);
if(networking->api == 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; 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); map_deinit(&networking->client_id_to_player_index);
assert(stop_listening(networking)); assert(stop_listening(networking));
networking->listener_id = 0;
mp_api_destroy(networking->api); mp_api_destroy(networking->api);
networking->api = NULL; networking->api = NULL;
@@ -252,6 +256,6 @@ bool networking_stop_hosting(Game_Networking *networking) {
networking->local_client_id = NULL; networking->local_client_id = NULL;
} }
printf("Stopped hosting.\n"); printf("Stopped networking.\n");
return true; 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_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_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 #endif