parralellllll start

This commit is contained in:
WanderingPenwing 2024-03-08 08:11:28 +01:00
parent 8988bbd180
commit f8ec903a2c

View file

@ -8,81 +8,78 @@ mod bot;
const MAX_FPS: f32 = 30.0; const MAX_FPS: f32 = 30.0;
fn main() { fn main() {
println!("hello there"); println!("hello there");
// Create a separate Tokio runtime for the bot let handle = thread::spawn(|| {
let bot_runtime = Runtime::new().unwrap(); println!("general kenobi");
let bot_handle = bot_runtime.handle().clone(); let mut rt = Runtime::new().unwrap();
rt.block_on(bot::start_discord_bot());
// Spawn the bot task onto the separate runtime
bot_handle.spawn(async move {
bot::start_discord_bot().await;
}); });
// Run the GUI on the main thread // Run the GUI on the main thread
gui(); gui();
} }
fn gui() { fn gui() {
let icon_data = load_icon().unwrap_or_default(); let icon_data = load_icon().unwrap_or_default();
let options = eframe::NativeOptions { let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default() viewport: egui::ViewportBuilder::default()
.with_inner_size([1200.0, 800.0]) .with_inner_size([1200.0, 800.0])
.with_icon(Arc::new(icon_data)), .with_icon(Arc::new(icon_data)),
..Default::default() ..Default::default()
}; };
let _ = eframe::run_native("Jiji", options, Box::new(move |_cc| Box::from(Jiji::new()))); let _ = eframe::run_native("Jiji", options, Box::new(move |_cc| Box::from(Jiji::new())));
} }
struct Jiji { struct Jiji {
next_frame: time::Instant, next_frame: time::Instant,
} }
impl Jiji { impl Jiji {
fn new() -> Self { fn new() -> Self {
Self { Self {
next_frame: time::Instant::now(), next_frame: time::Instant::now(),
} }
} }
} }
impl eframe::App for Jiji { impl eframe::App for Jiji {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
thread::sleep(time::Duration::from_secs_f32( thread::sleep(time::Duration::from_secs_f32(
((1.0 / MAX_FPS) - self.next_frame.elapsed().as_secs_f32()).max(0.0), ((1.0 / MAX_FPS) - self.next_frame.elapsed().as_secs_f32()).max(0.0),
)); ));
self.next_frame = time::Instant::now(); self.next_frame = time::Instant::now();
self.draw_feed(ctx); self.draw_feed(ctx);
} }
fn on_exit(&mut self, _gl: std::option::Option<&eframe::glow::Context>) { fn on_exit(&mut self, _gl: std::option::Option<&eframe::glow::Context>) {
//self.runtime.shutdown_background(); //self.runtime.shutdown_background();
} }
} }
impl Jiji { impl Jiji {
pub fn draw_feed(&mut self, ctx: &egui::Context) { pub fn draw_feed(&mut self, ctx: &egui::Context) {
egui::CentralPanel::default().show(ctx, |ui| { egui::CentralPanel::default().show(ctx, |ui| {
ui.label("Hello there"); ui.label("Hello there");
}); });
} }
} }
pub fn load_icon() -> Result<egui::IconData, Box<dyn Error>> { pub fn load_icon() -> Result<egui::IconData, Box<dyn Error>> {
let (icon_rgba, icon_width, icon_height) = { let (icon_rgba, icon_width, icon_height) = {
let icon = include_bytes!("../assets/icon.png"); let icon = include_bytes!("../assets/icon.png");
let image = image::load_from_memory(icon)?; let image = image::load_from_memory(icon)?;
let rgba = image.clone().into_rgba8().to_vec(); let rgba = image.clone().into_rgba8().to_vec();
let (width, height) = image.dimensions(); let (width, height) = image.dimensions();
(rgba, width, height) (rgba, width, height)
}; };
Ok(egui::IconData { Ok(egui::IconData {
rgba: icon_rgba, rgba: icon_rgba,
width: icon_width, width: icon_width,
height: icon_height, height: icon_height,
}) })
} }