Slight reconstruction/abstraction. Getting close to connecting to other sessions etc.
This commit is contained in:
87
src/instance/game_instance.c
Normal file
87
src/instance/game_instance.c
Normal file
@@ -0,0 +1,87 @@
|
||||
#include "game_instance.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#define INPUT_QUEUE_CAPACITY 8
|
||||
|
||||
void game_instance_init(Game_Instance *instance) {
|
||||
instance->local_player_index = 0xFFFF;
|
||||
// TODO: SS - Init input-queue.
|
||||
instance->simulation_accumulator = 0.0f; // TODO: SS - Should probably be moved to the Ingame context.
|
||||
instance->game_session = NULL;
|
||||
memset(&instance->game_networking, 0, sizeof(Game_Networking));
|
||||
|
||||
squeue_init(&instance->local_input_queue, 4, sizeof(Simulation_Game_Input));
|
||||
}
|
||||
|
||||
void game_instance_dispose(Game_Instance *instance) {
|
||||
squeue_free(&instance->local_input_queue);
|
||||
}
|
||||
|
||||
bool game_instance_host_session(Game_Instance *instance, const Game_Session_Settings settings) {
|
||||
assert(instance != NULL);
|
||||
|
||||
printf("Trying to host an %s session ...\n", settings.online ? "online" : "offline");
|
||||
|
||||
if(instance->game_session != NULL) {
|
||||
printf("Failed. A session is already active.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
instance->game_session = (Game_Session *)calloc(1, sizeof(Game_Session));
|
||||
|
||||
if(settings.online) {
|
||||
if(instance->game_networking.api != NULL) {
|
||||
printf("Failed. Network is already active.\n");
|
||||
free(instance->game_session);
|
||||
instance->game_session = NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!networking_try_host(&instance->game_networking, instance->game_session, settings)) {
|
||||
printf("Failed to host session.\n");
|
||||
free(instance->game_session);
|
||||
instance->game_session = NULL;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
game_session_init(instance->game_session, settings);
|
||||
}
|
||||
|
||||
assert(game_session_create_player(instance->game_session, &instance->local_player_index)); // Create the host (you!).
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool game_instance_join_session(Game_Instance *instance, const char *session_id) {
|
||||
assert(instance != NULL);
|
||||
|
||||
printf("Trying to join a session ...\n");
|
||||
|
||||
if(instance->game_session != NULL) {
|
||||
printf("Failed. A session is already active.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if(instance->game_networking.api != NULL) {
|
||||
printf("Failed. Network is already active.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool game_instance_push_local_input(Game_Instance *instance, Simulation_Game_Input input) {
|
||||
return squeue_push(&instance->local_input_queue, (void *)&input);
|
||||
}
|
||||
|
||||
bool game_instance_pop_local_input(Game_Instance *instance, Simulation_Game_Input *out_input) {
|
||||
Simulation_Game_Input input = {0};
|
||||
if(!squeue_pop(&instance->local_input_queue, (void *)&input)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*out_input = input;
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user