diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/game.c | 15 | ||||
| -rw-r--r-- | src/menu.c | 113 | ||||
| -rw-r--r-- | src/protocol.c | 8 |
3 files changed, 123 insertions, 13 deletions
@@ -29,7 +29,7 @@ static u32 get_session_id() { return (((time * 2654435789U) + time) * 2654435789U) + platform_get_processid(); } -void connect_to_server(char* ip, char* port) { +bool connect_to_server(char* ip, char* port) { client_incomming_allocator = create_allocator(MAX_NETWORK_BUFFER_SIZE); messages_received_on_client = array_create(sizeof(protocol_generic_message*)); @@ -51,23 +51,28 @@ void connect_to_server(char* ip, char* port) { player_id = get_session_id(); network_message message = create_protocol_get_id_up(player_id); add_message_to_outgoing_queuex(message, *global_state.client); + return true; } } + return false; } -void connect_to_game(char* ip, char* port) +bool connect_to_game(char* ip, char* port) { if (ip && port) { if (strcmp(ip, "127.0.0.1") == 0) { start_server(port); } - connect_to_server(ip, port); + if (!connect_to_server(ip, port)) { + return false; + } } log_info("STATE: GAMESTATE_PLAYING"); global_state.state = GAMESTATE_PLAYING; play_music(music_inside1); + return true; } void start_solo_game() @@ -188,6 +193,10 @@ void update_server(platform_window* window) { switch (msg->message->type) { + case MESSAGE_PING_UPSTREAM: { + add_message_to_outgoing_queuex(create_protocol_ping_downstream(), msg->client); + + } break; case MESSAGE_GET_ID_UPSTREAM: { protocol_get_id_upstream* m = (protocol_get_id_upstream*)msg->message; u32 new_id = get_id_from_ip(msg->client); @@ -1,7 +1,5 @@ #include "../include/menu.h" -#define PROGRAM_VERSION "0.1.0 | "__DATE__ - int current_res_index = 0; bool is_fullscreen = false; @@ -10,6 +8,7 @@ typedef enum t_menu_state { MENU_STATE_LEVEL_SELECT, MENU_STATE_CREDITS, MENU_STATE_SETTINGS, + MENU_STATE_JOIN_GAME, } menu_state; menu_state current_menu_state = MENU_STATE_MAIN; @@ -97,6 +96,13 @@ void draw_screen(platform_window* window) { play_sound(-1, wav_menu_hover); } selected_menu_option = 1; + + if (is_left_clicked()) { + play_sound(-1, wav_woosh); + current_menu_state = MENU_STATE_JOIN_GAME; + sec_since_state_change = 0.0f; + _global_keyboard.take_input = 1; + } } if (draw_menu_option(window, text_offset_x, text_offset_y + item_h*2, screen_w, item_h, "Settings", selected_menu_option == 2)) { @@ -203,7 +209,6 @@ void draw_level_select(platform_window* window) if (is_left_clicked()) { global_scene_state = SCENE_GAME; start_solo_game(); - // start game/go to lobby. } } else { @@ -249,20 +254,31 @@ static bool draw_settings_btn(platform_window* window, char* txt, int x, int y, { font* fnt = get_font(window, 1.0f); - int pad = 2; - renderer->render_rectangle(x, y, w, h, rgb(0,0,0)); - renderer->render_rectangle(x+pad, y+pad, w-pad*2, h-pad*2, rgb(255,255,255)); - renderer->render_text(fnt, x+(w/2)-(fnt->px_h/2), y+(h/2)-(fnt->px_h/2), txt, rgb(0,0,0)); - + static int was_hovered = 0; + bool result = false; if (_global_mouse.x + _global_camera.x > x && _global_mouse.x + _global_camera.x < x + w && _global_mouse.y + _global_camera.y > y && _global_mouse.y + _global_camera.y < y + h) { - return true; + result = true; + + if (result && was_hovered == 0) { + play_sound(-1, wav_menu_hover); + } + + was_hovered = x+y; + } + else if (was_hovered == x+y) { + was_hovered = 0; } - return false; + int pad = 2; + renderer->render_rectangle(x, y, w, h, rgba(0,0,0, result ? 240 : 160)); + renderer->render_rectangle(x+pad, y+pad, w-pad*2, h-pad*2, rgba(255,255,255, result ? 240 : 160)); + renderer->render_text(fnt, x+(w/2)-(fnt->px_h/2), y+(h/2)-(fnt->px_h/2), txt, rgb(0,0,0)); + + return result; } void draw_settings(platform_window* window) @@ -352,6 +368,78 @@ void draw_settings(platform_window* window) renderable_rec = draw_screen_change_animation(window, 1.0f); } +void draw_join_game(platform_window* window) +{ + if (_global_keyboard.input_text_len >= 20) { + _global_keyboard.input_text[19] = 0; + keyboard_set_input_text(_global_keyboard.input_text); + } + + color txt = rgb(102, 255, 102); + int tb_offset = 100; + int tb_w = renderable_rec.w - (tb_offset*2); + int tb_h = 50; + int tb_pad = 2; + int render_y = renderable_rec.y + tb_offset; + + renderer->render_image(img_splash_art2, renderable_rec.x, renderable_rec.y, renderable_rec.w, renderable_rec.h); + renderer->render_rectangle(renderable_rec.x, renderable_rec.y, renderable_rec.w, renderable_rec.h, rgba(40,40,40, 200)); + + char* join_game_text = "Join Game"; + font* fnt = get_font(window, 1.5f); + int text_w = renderer->calculate_text_width(fnt, join_game_text); + int text_y = renderable_rec.y + 30; + + renderer->render_text(fnt, renderable_rec.x + (renderable_rec.w/2)-(text_w/2), text_y, join_game_text, txt); + + render_y += 30; + + vec4 tb_rec = (vec4){renderable_rec.x + tb_offset, render_y, tb_w, tb_h}; + + renderer->render_rectangle(tb_rec.x, tb_rec.y, tb_rec.w, tb_rec.h, rgba(0,0,0, 160)); + renderer->render_rectangle(tb_rec.x+tb_pad, tb_rec.y+tb_pad, tb_rec.w-tb_pad*2, tb_rec.h-tb_pad*2, rgba(255,255,255, 160)); + + int txt_x = renderer->render_text(fnt, tb_rec.x+tb_pad + 20, tb_rec.y+tb_pad + (tb_h/2) - (fnt->px_h/2), _global_keyboard.input_text, txt); + renderer->render_rectangle(tb_rec.x+tb_pad + 20+txt_x, tb_rec.y+tb_pad + 10, 2, tb_h - 25, txt); + + text_y += 200; + int item_h = 40; + + select_animation_duration += update_delta; + + static bool btn_hovered = 0; + bool do_join = 0; + if (draw_menu_option(window, renderable_rec.x, text_y, renderable_rec.w, item_h, "Join", btn_hovered)) { + if (btn_hovered != 1) { + select_animation_duration = 0.0f; + play_sound(-1, wav_menu_hover); + } + btn_hovered = 1; + + if (is_left_clicked()) { + play_sound(-1, wav_woosh); + do_join = 1; + } + } + else { + btn_hovered = 0; + } + + if (keyboard_is_key_pressed(KEY_ENTER) || do_join) { + char* ip = _global_keyboard.input_text; + if (_global_keyboard.input_text_len > 0) { + if (connect_to_game(ip, DEFAULT_PORT)) { + global_scene_state = SCENE_GAME; + } + else { + log_infox("Failed to connect to %s", ip); + } + } + } + + renderable_rec = draw_screen_change_animation(window, 1.0f); +} + void update_menu(platform_window* window) { sec_since_state_change += update_delta; @@ -368,9 +456,14 @@ void update_menu(platform_window* window) else if (current_menu_state == MENU_STATE_SETTINGS) { draw_settings(window); } + else if (current_menu_state == MENU_STATE_JOIN_GAME) { + draw_join_game(window); + } if (keyboard_is_key_down(KEY_ESCAPE)) { + _global_keyboard.take_input = 0; + keyboard_set_input_text(""); play_sound(-1, wav_woosh); current_menu_state = MENU_STATE_MAIN; if (sec_since_state_change >= menu_state_change_duration) diff --git a/src/protocol.c b/src/protocol.c index b6c03ee..cfcbd10 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -11,6 +11,14 @@ network_message create_protocol_get_id_up(u32 id) return network_create_message((u8*)buf, sizeof(protocol_get_id_upstream), MAX_NETWORK_BUFFER_SIZE); } +network_message create_protocol_ping_downstream() +{ + protocol_ping_downstream *buf = alloc_network_message(protocol_ping_downstream); + buf->type = MESSAGE_PING_DOWNSTREAM; + snprintf(buf->program_version, 30, "%s", PROGRAM_VERSION); + return network_create_message((u8*)buf, sizeof(protocol_ping_downstream), MAX_NETWORK_BUFFER_SIZE); +} + network_message create_protocol_get_id_down(u32 id) { protocol_get_id_downstream *buf = alloc_network_message(protocol_get_id_downstream); |
