Skip to content

Commit

Permalink
launcher: run game client/server in separate thread
Browse files Browse the repository at this point in the history
Also, played with some conditional-based rendering for the end user to
have a better experience.
  • Loading branch information
iWas-Coder committed Apr 25, 2024
1 parent da204ae commit 59b8c53
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
2 changes: 1 addition & 1 deletion include/sk_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@

#include <sk_defines.h>

#define SK_CLIENT_NAME "sparky-client"
#define SK_CLIENT_NAME "sparky::client"

u8 sk_client_run(const char *ip);
2 changes: 1 addition & 1 deletion include/sk_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#include <sk_defines.h>

#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
Expand Down
53 changes: 42 additions & 11 deletions src/sk_launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AtomicBool>,
is_server_running: Arc<AtomicBool>
}

extern "C" {
Expand All @@ -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))
}
}
}
Expand All @@ -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 {
Expand All @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions src/sk_scene_intro.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/sk_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 59b8c53

Please sign in to comment.