yaldabaranth 0.0.0

A roguelike. (Coming soon!)
use crate::engine::game::GameConfig;
use raylib::prelude::*;
use std::sync::mpsc::{self, Receiver, Sender};
use std::thread::{self, JoinHandle};

pub enum DisplayMessage {
    Continue,
}

pub enum DisplayResponse {
    Shutdown,
}

pub struct Display {
    to_thread: Sender<DisplayMessage>,
    from_thread: Receiver<DisplayResponse>,
    handle: Option<JoinHandle<()>>,
}

impl Display {
    pub fn new(config: GameConfig) -> Self {
        let (to_thread, from_logic) = mpsc::channel();
        let (to_logic, from_thread) = mpsc::channel();
        let handle = Some(thread::spawn(move || {
            let (rlh, rlt) = config.init_raylib();
            let mut worker = DisplayWorker {
                rlh,
                rlt,
                from_logic,
                to_logic,
            };
            worker.run();
        }));
        Self {
            to_thread,
            from_thread,
            handle,
        }
    }
    pub fn poll_events(&self) -> bool {
        let mut should_exit = false;
        while let Ok(response) = self.from_thread.try_recv() {
            match response {
                DisplayResponse::Shutdown => {
                    should_exit = true;
                }
            }
        }
        should_exit
    }
}

struct DisplayWorker {
    rlh: RaylibHandle,
    rlt: RaylibThread,
    from_logic: Receiver<DisplayMessage>,
    to_logic: Sender<DisplayResponse>,
}

impl DisplayWorker {
    fn run(&mut self) {
        let display_text = String::from("Initial State");
        while !self.rlh.window_should_close() {
            while let Ok(msg) = self.from_logic.try_recv() {
                match msg {
                    DisplayMessage::Continue => {}
                }
            }
            let mut d = self.rlh.begin_drawing(&self.rlt);
            d.clear_background(Color::RAYWHITE);
            d.draw_text(&display_text, 12, 12, 20, Color::BLACK);
        }
        self.to_logic.send(DisplayResponse::Shutdown).unwrap();
    }
}