From 59b8c535ba6bffa7c6f33135b9ce378af0cb0b0e Mon Sep 17 00:00:00 2001 From: iWas-Coder Date: Thu, 25 Apr 2024 13:55:40 +0200 Subject: [PATCH] launcher: run game client/server in separate thread Also, played with some conditional-based rendering for the end user to have a better experience. --- include/sk_client.h | 2 +- include/sk_server.h | 2 +- src/sk_launcher.rs | 53 +++++++++++++++++++++++++++++++++++--------- src/sk_scene_intro.c | 2 ++ src/sk_state.c | 2 ++ 5 files changed, 48 insertions(+), 13 deletions(-) diff --git a/include/sk_client.h b/include/sk_client.h index 5286618..eaa3fad 100644 --- a/include/sk_client.h +++ b/include/sk_client.h @@ -23,6 +23,6 @@ #include -#define SK_CLIENT_NAME "sparky-client" +#define SK_CLIENT_NAME "sparky::client" u8 sk_client_run(const char *ip); diff --git a/include/sk_server.h b/include/sk_server.h index 25b4053..9e2ab78 100644 --- a/include/sk_server.h +++ b/include/sk_server.h @@ -23,7 +23,7 @@ #include -#define SK_SERVER_NAME "sparky-server" +#define SK_SERVER_NAME "sparky::server" #define SK_SERVER_PORT 27015 #define SK_SERVER_TICK_RATE 128 #define SK_SERVER_MSG_MAX_SIZE 1024 diff --git a/src/sk_launcher.rs b/src/sk_launcher.rs index 15b1f5b..07832bd 100644 --- a/src/sk_launcher.rs +++ b/src/sk_launcher.rs @@ -22,12 +22,18 @@ extern crate libc; extern crate eframe; +use std::thread; +use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; use libc::c_char; use eframe::egui; struct Launcher { curr_tab: String, - ip: String + online_mode: bool, + ip: String, + is_client_running: Arc, + is_server_running: Arc } extern "C" { @@ -43,7 +49,10 @@ impl Default for Launcher { fn default() -> Self { Self { curr_tab: "Modules".to_owned(), - ip: "".to_owned() + online_mode: false, + ip: "".to_owned(), + is_client_running: Arc::new(AtomicBool::new(false)), + is_server_running: Arc::new(AtomicBool::new(false)) } } } @@ -65,20 +74,42 @@ impl eframe::App for Launcher { egui::CentralPanel::default().show(ctx, |_| { if self.curr_tab == SK_LAUNCHER_TAB_MODULES { egui::Window::new("Client").show(ctx, |ui| { - ui.horizontal(|ui| { - let ip_label = ui.label("IP: "); - ui.text_edit_singleline(&mut self.ip).labelled_by(ip_label.id); - }); + if self.is_client_running.load(Ordering::SeqCst) { ui.set_enabled(false); } + ui.checkbox(&mut self.online_mode, "Online mode"); + if self.online_mode { + ui.horizontal(|ui| { + let ip_label = ui.label("IP: "); + ui.text_edit_singleline(&mut self.ip).labelled_by(ip_label.id); + }); + } if ui.button("Play").clicked() { - if self.ip.is_empty() { unsafe { sk_client_run(std::ptr::null()); } } + self.is_client_running.store(true, Ordering::SeqCst); + let is_client_running = Arc::clone(&self.is_client_running); + if self.ip.is_empty() { + thread::spawn(move || { + unsafe { sk_client_run(std::ptr::null()); } + is_client_running.store(false, Ordering::SeqCst); + }); + } else { let ip_addr = std::ffi::CString::new(self.ip.clone()).unwrap(); - unsafe { sk_client_run(ip_addr.as_ptr()); } + thread::spawn(move || { + unsafe { sk_client_run(ip_addr.as_ptr()); } + is_client_running.store(false, Ordering::SeqCst); + }); } } }); egui::Window::new("Server").show(ctx, |ui| { - if ui.button("Start").clicked() { unsafe { sk_server_run(); } } + if self.is_server_running.load(Ordering::SeqCst) { ui.set_enabled(false); } + if ui.button("Start").clicked() { + self.is_server_running.store(true, Ordering::SeqCst); + let is_server_running = Arc::clone(&self.is_server_running); + thread::spawn(move || { + unsafe { sk_server_run(); } + is_server_running.store(false, Ordering::SeqCst); + }); + } }); } else if self.curr_tab == SK_LAUNCHER_TAB_LOG { @@ -93,8 +124,8 @@ pub extern "C" fn sk_launcher_run() -> u8 { println!("INFO: Initializing {}", SK_LAUNCHER_NAME); let options = eframe::NativeOptions { viewport: egui::ViewportBuilder::default() - .with_inner_size([800.0, 600.0]) - .with_min_inner_size([300.0, 220.0]), + .with_inner_size([600.0, 400.0]) + .with_min_inner_size([600.0, 400.0]), ..Default::default() }; let _ = eframe::run_native( diff --git a/src/sk_scene_intro.c b/src/sk_scene_intro.c index 48272e3..c26716e 100644 --- a/src/sk_scene_intro.c +++ b/src/sk_scene_intro.c @@ -65,6 +65,8 @@ void sk_scene_intro_draw(sk_state *s) { RAYWHITE); break; case 2: + phase = 0; + time = 0; s->curr_scene.kind = SK_SCENE_KIND_MAIN_MENU; s->curr_scene.update = sk_scene_main_menu_update; s->curr_scene.draw = sk_scene_main_menu_draw; diff --git a/src/sk_state.c b/src/sk_state.c index 8b88083..0fdec69 100644 --- a/src/sk_state.c +++ b/src/sk_state.c @@ -77,6 +77,8 @@ void sk_state_destroy_offline(sk_state *s) { UnloadImage(s->win_icon); sk_config_destroy(&s->config); sk_renderer_destroy(); + *s = (sk_state) {0}; + s = 0; } sk_state sk_state_create_online(u8 lobby_id) {