Compare commits

..

3 Commits

6 changed files with 48 additions and 18 deletions

View File

@@ -46,6 +46,7 @@ bool game_instance_host_session(Game_Instance *instance, const Game_Session_Sett
uint16_t host_player_id = 0xFFFF; uint16_t host_player_id = 0xFFFF;
if(!networking_try_host(&instance->game_networking, instance->game_session, settings, &host_player_id)) { if(!networking_try_host(&instance->game_networking, instance->game_session, settings, &host_player_id)) {
printf("Failed to host session.\n"); printf("Failed to host session.\n");
free(instance->game_session); free(instance->game_session);
instance->game_session = NULL; instance->game_session = NULL;
return false; return false;
@@ -82,10 +83,11 @@ bool game_instance_join_session(Game_Instance *instance, const char *session_id)
return false; return false;
} }
instance->game_session = (Game_Session *)calloc(1, sizeof(Game_Session)); // LEAK instance->game_session = (Game_Session *)calloc(1, sizeof(Game_Session));
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); free(instance->game_session);
instance->game_session = NULL;
return false; return false;
} }

View File

@@ -99,6 +99,17 @@ uint32_t floor_texture_variant(uint32_t x, uint32_t y, uint32_t seed, uint32_t n
return (wang_hash(x * 73856093u ^ y * 19349663u ^ seed)) % num_variants; return (wang_hash(x * 73856093u ^ y * 19349663u ^ seed)) % num_variants;
} }
Color get_tint_for_player_id(uint16_t player_id) {
switch(player_id) {
case 0: {
return (Color) { 255, 135, 102, 255 };
}
default: {
return (Color) { 82, 33, 110, 255 };
}
}
}
#define SHADOW_ALPHA 64 #define SHADOW_ALPHA 64
static void state_render(Presentation_State *state) { static void state_render(Presentation_State *state) {
@@ -151,7 +162,21 @@ static void state_render(Presentation_State *state) {
switch(entity->type) { switch(entity->type) {
case Entity_Type_Snake_Head: { case Entity_Type_Snake_Head: {
Color tint = (Color) { 255, 135, 102, 255 }; // TODO: SS - Get tint based on player ID.
Color tint = (Color) { 255, 255, 255, 255 };
// Set the snake's tint based on player-index.
for(uint16_t i = 0; i < sim_world->max_players; i++) {
Simulation_Player *player = &sim_world->players[i];
if(!player->active) {
continue;
}
if(player->entity_id == entity->id) {
tint = get_tint_for_player_id(i); // NOTE: SS - It's more like player-index. This needs to change if they're not sorted (but they are at the moment and I don't really plan on switching that up.)
break;
}
}
Vector2 origin = (Vector2) { GRID_CELL_SIZE/2, GRID_CELL_SIZE/2 }; Vector2 origin = (Vector2) { GRID_CELL_SIZE/2, GRID_CELL_SIZE/2 };
Entity *e = entity; Entity *e = entity;

View File

@@ -75,8 +75,6 @@ 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;
} }
@@ -89,12 +87,7 @@ static void state_render(Presentation_State *state) {
ctx->mode = Main_Menu_Mode_Multiplayer_Join; ctx->mode = Main_Menu_Mode_Multiplayer_Join;
memset(&ctx->session_id_input, 0, sizeof(ctx->session_id_input)); memset(&ctx->session_id_input, 0, sizeof(ctx->session_id_input));
ctx->session_id_input[0] = 'A'; // snprintf(&ctx->session_id_input[0], sizeof(ctx->session_id_input) + 1, "ABCDEF");
ctx->session_id_input[1] = 'B';
ctx->session_id_input[2] = 'C';
ctx->session_id_input[3] = 'D';
ctx->session_id_input[4] = 'E';
ctx->session_id_input[5] = 'F';
} }
if (GuiButton((Rectangle){ 64, 64 + (BUTTON_HEIGHT * 2), 128, BUTTON_HEIGHT }, "Back to Main Menu")) { if (GuiButton((Rectangle){ 64, 64 + (BUTTON_HEIGHT * 2), 128, BUTTON_HEIGHT }, "Back to Main Menu")) {
ctx->mode = Main_Menu_Mode_Home; ctx->mode = Main_Menu_Mode_Home;
@@ -136,7 +129,7 @@ static void state_render(Presentation_State *state) {
int pressed_button_index = GuiTextInputBox( int pressed_button_index = GuiTextInputBox(
(Rectangle) { 64, 64, 256, 128 }, // Bounds (Rectangle) { 64, 64, 256, 128 }, // Bounds
"Session-ID", // Title "Session-ID", // Title
NULL, // Message "6 characters expected. (ABCDEF)", // Message
"Join", // Buttons "Join", // Buttons
&ctx->session_id_input[0], // Text &ctx->session_id_input[0], // Text
sizeof(ctx->session_id_input) + 1, // Text max size sizeof(ctx->session_id_input) + 1, // Text max size

View File

@@ -82,6 +82,17 @@ static inline bool setup_api(Game_Networking *networking) {
return true; return true;
} }
static inline bool shutdown_api(Game_Networking *networking) {
if(networking->api == NULL) {
return false;
}
mp_api_destroy(networking->api);
networking->api = NULL;
return true;
}
static bool start_listening(Game_Networking *networking) { static bool start_listening(Game_Networking *networking) {
assert(networking != NULL); assert(networking != NULL);
assert(networking->api != NULL); assert(networking->api != NULL);
@@ -130,7 +141,7 @@ bool networking_try_host(Game_Networking *networking, Game_Session *session, Gam
); );
if(host_result != MP_API_OK) { if(host_result != MP_API_OK) {
printf("Failed to host; Got result: %i.\n", host_result); printf("Failed to host; Got result: %i.\n", host_result);
// TODO: SS - Shutdown API. shutdown_api(networking);
return false; return false;
} }
@@ -192,7 +203,7 @@ bool networking_try_join(Game_Networking *networking, Game_Session *session, con
&received_json // Received JSON-data. &received_json // Received JSON-data.
); );
if(join_result != MP_API_OK) { if(join_result != MP_API_OK) {
// TODO: SS - Shutdown API. shutdown_api(networking);
printf("Failed to join; Got result: %i.\n", join_result); printf("Failed to join; Got result: %i.\n", join_result);
return false; return false;
} }
@@ -215,7 +226,6 @@ bool networking_try_join(Game_Networking *networking, Game_Session *session, con
json_decref(received_json); json_decref(received_json);
} }
// TODO: SS - Stop listening.
assert(start_listening(networking)); assert(start_listening(networking));
// TODO: SS - Set up session based on what we got in the json. // TODO: SS - Set up session based on what we got in the json.
@@ -242,8 +252,7 @@ bool networking_stop(Game_Networking *networking) {
assert(stop_listening(networking)); assert(stop_listening(networking));
networking->listener_id = 0; networking->listener_id = 0;
mp_api_destroy(networking->api); shutdown_api(networking);
networking->api = NULL;
networking->game_session = NULL; networking->game_session = NULL;
networking->is_host = false; networking->is_host = false;

View File

@@ -25,7 +25,7 @@ typedef enum {
typedef struct { typedef struct {
bool active; bool active;
// TODO: SS - Maybe add an ID here. Entity_ID id;
Entity_Type type; Entity_Type type;

View File

@@ -78,6 +78,7 @@ bool game_world_create_entity(Game_World *world, Entity_Type type, uint16_t x, u
world->entities[id] = (Entity) { world->entities[id] = (Entity) {
.active = true, .active = true,
.id = id,
.type = type, .type = type,
.child = INVALID_ENTITY_ID .child = INVALID_ENTITY_ID
}; };
@@ -98,7 +99,7 @@ void game_world_destroy_entity(Game_World *world, Entity_ID entity_id) {
entity_id entity_id
); );
assert(entity != NULL); assert(entity != NULL);
entity->active = false; memset(&entity, 0, sizeof(Entity));
Grid_Cell *cell = grid_get_cell(&world->grid, entity->x, entity->y); Grid_Cell *cell = grid_get_cell(&world->grid, entity->x, entity->y);
assert(cell != NULL); assert(cell != NULL);