From f20dfd8240c5cab969b25a3c1e986b28c97f6898 Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Sun, 12 May 2024 23:08:00 +0200 Subject: main menu work --- src/asset_defs.c | 8 +++- src/audio.c | 2 +- src/menu.c | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 src/menu.c (limited to 'src') diff --git a/src/asset_defs.c b/src/asset_defs.c index 9e29b10..cc7ac6b 100644 --- a/src/asset_defs.c +++ b/src/asset_defs.c @@ -4,7 +4,11 @@ #include "../data/fonts/aleo.h" void load_menu_assets() { // Assets loaded at game start - img_splash_art1 = assets_load_image_from_file("data/imgs/ui/splash1.png"); + img_menu_screen = assets_load_image_from_file("data/imgs/ui/menu_screen.png"); + img_splash_art2 = assets_load_image_from_file("data/imgs/ui/splash2.png"); + + wav_menu_hover = Mix_LoadWAV("data/sounds/menu_hover.wav"); + music_menu = Mix_LoadMUS("data/sounds/menu_ambient.mp3"); fnt_52 = assets_load_font(Aleo_Regular, Aleo_Regular + Aleo_Regular_Size, 52); fnt_48 = assets_load_font(Aleo_Regular, Aleo_Regular + Aleo_Regular_Size, 48); @@ -19,6 +23,8 @@ void load_menu_assets() { // Assets loaded at game start fnt_12 = assets_load_font(Aleo_Regular, Aleo_Regular + Aleo_Regular_Size, 12); fnt_8 = assets_load_font(Aleo_Regular, Aleo_Regular + Aleo_Regular_Size, 8); fnt_4 = assets_load_font(Aleo_Regular, Aleo_Regular + Aleo_Regular_Size, 4); + + img_splash_art1 = assets_load_image_from_file("data/imgs/ui/splash1.png"); } void load_assets() { // Assets loaded at match start. diff --git a/src/audio.c b/src/audio.c index 1363fa7..39c3557 100644 --- a/src/audio.c +++ b/src/audio.c @@ -151,7 +151,7 @@ void play_sound(int channel, Mix_Chunk* wav) { void play_music(Mix_Music* music) { Mix_FadeInMusic(music, -1, 2000); - Mix_VolumeMusic(MIX_MAX_VOLUME/6); + Mix_VolumeMusic(MIX_MAX_VOLUME/8); } void play_positioned_sound(int channel, Mix_Chunk* wav, vec3f pos, float max_audible_dist) { diff --git a/src/menu.c b/src/menu.c new file mode 100644 index 0000000..1173c42 --- /dev/null +++ b/src/menu.c @@ -0,0 +1,120 @@ +#include "../include/menu.h" + +void draw_background(platform_window* window) +{ + image* img = img_splash_art2; + int imgw = img->width * (window->height/(float)img->height); + int imgh = window->height; + + int imgx = (window->width - imgw) / 2 + _global_camera.x; + int imgy = 0 + _global_camera.y; + renderer->render_image(img, imgx, imgy, imgw, imgh); +} + +float select_animation_duration = 0.0f; +static bool draw_menu_option(platform_window* window, int x, int y, int w, int h, char* text, bool is_hovered) +{ + color txt = rgb(102, 255, 102); + color txt_bg = rgb(90, 176, 90); + + if (is_hovered) { + renderer->render_rectangle(x, y, w, h, rgba(245,245,245, 100)); + //txt = rgb(255,255,255); + } + + font* fnt = get_font(window, 1.0f); + int offset_x = (w/2) - (renderer->calculate_text_width(fnt, text)/2); + + if (is_hovered && (select_animation_duration*100) < 2*M_PI) offset_x += sin(select_animation_duration*100.0f)*10.0f; + + renderer->render_text(fnt, x+offset_x + 1, y + (h/2) - (fnt->px_h/2) - 1, text, txt_bg); + renderer->render_text(fnt, x+offset_x, y + (h/2) - (fnt->px_h/2), text, txt); + + 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; + } + + return false; +} + +int selected_menu_option = 0; +void draw_screen(platform_window* window) { + image* img = img_menu_screen; + int imgw = img->width * (window->height/(float)img->height); + int imgh = window->height; + + int imgx = (window->width - imgw) / 2 + _global_camera.x; + int imgy = 0 + _global_camera.y; + + int text_offset_x = imgx + 1540 * (imgw/(float)img->width); + int text_offset_y = imgy + 475 * (imgh/(float)img->height); + + int screen_w = 900 * (imgw/(float)img->width); + int screen_h = 560 * (imgh/(float)img->height); + + renderer->render_image(img_splash_art2, text_offset_x, text_offset_y, screen_w, screen_h); + renderer->render_rectangle(text_offset_x, text_offset_y, screen_w, screen_h, rgba(40,40,40, 200)); + + select_animation_duration += update_delta; + + int item_h = screen_h / 6; + if (draw_menu_option(window, text_offset_x, text_offset_y + item_h*0, screen_w, item_h, "Play Solo", selected_menu_option == 0)) { + if (selected_menu_option != 0) { + select_animation_duration = 0.0f; + play_sound(-1, wav_menu_hover); + } + selected_menu_option = 0; + } + + if (draw_menu_option(window, text_offset_x, text_offset_y + item_h*1, screen_w, item_h, "Host Game", selected_menu_option == 1)) { + if (selected_menu_option != 1) { + select_animation_duration = 0.0f; + play_sound(-1, wav_menu_hover); + } + selected_menu_option = 1; + } + + if (draw_menu_option(window, text_offset_x, text_offset_y + item_h*2, screen_w, item_h, "Join Game", selected_menu_option == 2)) { + if (selected_menu_option != 2) { + select_animation_duration = 0.0f; + play_sound(-1, wav_menu_hover); + } + selected_menu_option = 2; + } + + if (draw_menu_option(window, text_offset_x, text_offset_y + item_h*3, screen_w, item_h, "Settings", selected_menu_option == 3)) { + if (selected_menu_option != 3) { + select_animation_duration = 0.0f; + play_sound(-1, wav_menu_hover); + } + selected_menu_option = 3; + } + + if (draw_menu_option(window, text_offset_x, text_offset_y + item_h*4, screen_w, item_h, "Credits", selected_menu_option == 4)) { + if (selected_menu_option != 4) { + select_animation_duration = 0.0f; + play_sound(-1, wav_menu_hover); + } + selected_menu_option = 4; + } + + if (draw_menu_option(window, text_offset_x, text_offset_y + item_h*5, screen_w, item_h, "Quit", selected_menu_option == 5)) { + if (selected_menu_option != 5) { + select_animation_duration = 0.0f; + play_sound(-1, wav_menu_hover); + } + selected_menu_option = 5; + } + + renderer->render_image(img, imgx, imgy, imgw, imgh); +} + +void update_menu(platform_window* window) +{ + //draw_background(window); + draw_screen(window); +} \ No newline at end of file -- cgit v1.2.3-70-g09d2