damn, rust borrow checker got tamed >:3

This commit is contained in:
LunarAkai 2025-08-03 20:53:44 +02:00
commit 78b5348b92
2 changed files with 18 additions and 12 deletions

View file

@ -34,16 +34,16 @@ impl Game {
pub fn run(self) { pub fn run(self) {
info("Running Game..."); info("Running Game...");
let mut glfw_window_unwrapped: moonhare_window::platforms::glfw_window::GLFWWindow = self.glfw_window.unwrap(); let mut glfw_window_unwrapped = self.glfw_window;
let mut context: std::rc::Rc<moonhare_graphics::glium::backend::Context>; let mut context: std::rc::Rc<moonhare_graphics::glium::backend::Context>;
context = moonhare_graphics::build_context(Rc::new(RefCell::new(glfw_window_unwrapped.glfw_window))); context = moonhare_graphics::build_context(glfw_window_unwrapped.clone().unwrap().glfw_window);
let value = glfw_window_unwrapped; let mut value = glfw_window_unwrapped;
while self.is_running { while self.is_running {
// can't move glfwwindow cause i can't implement clone, or idk // can't move glfwwindow cause i can't implement clone, or idk
handle_window_event(value); handle_window_event(value.as_mut().unwrap());
render(context.clone()); render(context.clone());
// update(); // update();
@ -61,9 +61,9 @@ fn default_game_name() -> String {
} }
/// Deals with GLFW Window Events (in `monhare_window`) /// Deals with GLFW Window Events (in `monhare_window`)
fn handle_window_event(mut glfw_window: GLFWWindow) { fn handle_window_event(mut glfw_window: &GLFWWindow) {
glfw_window.glfw_window.glfw.poll_events(); glfw_window.glfw_window.borrow_mut().glfw.poll_events();
for (_, event) in moonhare_window::glfw::flush_messages(&glfw_window.events) { for (_, event) in moonhare_window::glfw::flush_messages(&glfw_window.events.borrow()) {
moonhare_window::platforms::glfw_window::GLFWWindow::handle_window_event(&glfw_window, event); moonhare_window::platforms::glfw_window::GLFWWindow::handle_window_event(&glfw_window, event);
} }
} }

View file

@ -1,4 +1,4 @@
use std::sync::Arc; use std::{cell::RefCell, rc::Rc, sync::Arc};
use glfw::{Context, Glfw, GlfwReceiver, PWindow, Window, WindowEvent}; use glfw::{Context, Glfw, GlfwReceiver, PWindow, Window, WindowEvent};
use moonhare_event::{event::Event, events::window_events::window_close_event::WindowCloseEvent}; use moonhare_event::{event::Event, events::window_events::window_close_event::WindowCloseEvent};
@ -7,14 +7,20 @@ use crate::{window_config, MoonhareWindow};
#[derive(Debug)] #[derive(Debug)]
pub struct GLFWWindow { pub struct GLFWWindow {
pub glfw_window: PWindow, pub glfw_window: Rc<RefCell<PWindow>>,
pub events: GlfwReceiver<(f64, WindowEvent)>, pub events: Rc<RefCell<GlfwReceiver<(f64, WindowEvent)>>>,
pub glfw: Glfw, pub glfw: Glfw,
pub is_running: bool, pub is_running: bool,
} }
const APP_ID: &str = "de.lunarakai.moonhare_engine"; const APP_ID: &str = "de.lunarakai.moonhare_engine";
impl Clone for GLFWWindow {
fn clone(&self) -> Self {
Self { glfw_window: self.glfw_window.clone(), events: self.events.clone(), glfw: self.glfw.clone(), is_running: self.is_running.clone() }
}
}
impl GLFWWindow { impl GLFWWindow {
fn new() -> Self { fn new() -> Self {
let mut glfw = glfw::init(glfw::fail_on_errors).unwrap(); let mut glfw = glfw::init(glfw::fail_on_errors).unwrap();
@ -30,8 +36,8 @@ impl GLFWWindow {
window.make_current(); window.make_current();
Self { Self {
glfw_window: window.try_into().unwrap(), glfw_window: Rc::new(RefCell::new(window)),
events: events, events: Rc::new(RefCell::new(events)),
glfw: glfw, glfw: glfw,
is_running: true is_running: true
} }