diff --git a/Cargo.lock b/Cargo.lock index 7e5016c..e104295 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1058,6 +1058,7 @@ version = "0.1.0" name = "moonhare_game" version = "0.1.0" dependencies = [ + "moonhare_graphics", "moonhare_log", "moonhare_window", ] diff --git a/crates/moonhare_game/Cargo.toml b/crates/moonhare_game/Cargo.toml index efd0f1d..2800e98 100644 --- a/crates/moonhare_game/Cargo.toml +++ b/crates/moonhare_game/Cargo.toml @@ -5,5 +5,6 @@ version.workspace = true authors.workspace = true [dependencies] +moonhare_graphics = { path = "../moonhare_graphics" } moonhare_log = { path = "../moonhare_log" } -moonhare_window = { path = "../moonhare_window" } \ No newline at end of file +moonhare_window = { path = "../moonhare_window" } diff --git a/crates/moonhare_game/src/lib.rs b/crates/moonhare_game/src/lib.rs index d08681b..96e3e15 100644 --- a/crates/moonhare_game/src/lib.rs +++ b/crates/moonhare_game/src/lib.rs @@ -1,10 +1,10 @@ //! Base functionality for a Moonhare Game Engine Project -use std::sync::Arc; +use std::rc::Rc; +use moonhare_graphics::glium::{backend::Context, glutin::api::egl::context}; use moonhare_log::*; -use moonhare_window::{glfw::{self, PWindow}, platforms::glfw_window::GLFWWindow, Window, WindowRenderContext}; - +use moonhare_window::{glfw::PWindow, platforms::glfw_window::GLFWWindow}; pub mod basic; /// Only one Game may exist per project @@ -12,8 +12,8 @@ pub mod basic; pub struct Game { pub is_running: bool, pub name: String, - pub context: WindowRenderContext, - pub glfw_window: Option + pub context: moonhare_window::WindowRenderContext, + pub glfw_window: Option } impl Default for Game { @@ -21,7 +21,7 @@ impl Default for Game { Self { is_running: true, name: default_game_name(), - context: WindowRenderContext::OPENGLGLFW, + context: moonhare_window::WindowRenderContext::OPENGLGLFW, glfw_window: None, } } @@ -34,18 +34,28 @@ impl Game { pub fn run(self) { info("Running Game..."); - let mut glfw_window_unwrapped: GLFWWindow = self.glfw_window.unwrap(); + let mut glfw_window_unwrapped: moonhare_window::platforms::glfw_window::GLFWWindow = self.glfw_window.unwrap(); + let mut context: std::rc::Rc; + + context = moonhare_graphics::build_context(glfw_window_unwrapped.glfw_window); + + - while self.is_running { - handle_window_event(&mut glfw_window_unwrapped); + while self.is_running { + let _ = move |mut window: GLFWWindow| { + handle_window_event(&mut window); + }; + // update(); // render(); + render(context.clone()); + } } pub fn add_window(&mut self) { moonhare_log::info(format!("Adding window to {:?}", self)); - self.glfw_window =Some(Window::create(self.context).into()); + self.glfw_window =Some(moonhare_window::Window::create(self.context).into()); } } @@ -54,9 +64,15 @@ fn default_game_name() -> String { } /// Deals with GLFW Window Events (in `monhare_window`) -fn handle_window_event(glfw_window: &mut GLFWWindow) { +fn handle_window_event(glfw_window: &mut moonhare_window::platforms::glfw_window::GLFWWindow) { glfw_window.glfw_window.glfw.poll_events(); - for (_, event) in glfw::flush_messages(&glfw_window.events) { - GLFWWindow::handle_window_event(&mut glfw_window.glfw_window, event); + for (_, event) in moonhare_window::glfw::flush_messages(&glfw_window.events) { + moonhare_window::platforms::glfw_window::GLFWWindow::handle_window_event(&mut glfw_window.glfw_window, event); } } + +fn render(context: Rc) { + let mut target = moonhare_graphics::glium::Frame::new(context.clone(), context.get_framebuffer_dimensions()); + moonhare_graphics::glium::Surface::clear_color(&mut target, 0.0, 0.0, 1.0, 1.0); + target.finish().unwrap(); +} diff --git a/crates/moonhare_graphics/src/backend.rs b/crates/moonhare_graphics/src/backend.rs index e9196ef..175564b 100644 --- a/crates/moonhare_graphics/src/backend.rs +++ b/crates/moonhare_graphics/src/backend.rs @@ -6,8 +6,8 @@ use moonhare_window::glfw::Context; // adopted from the glium repo -> examples -> manual_creation.rs #[derive(Clone)] -struct Backend { - gl_window: Rc>, +pub struct Backend { + pub gl_window: Rc>, } unsafe impl glium::backend::Backend for Backend { diff --git a/crates/moonhare_graphics/src/lib.rs b/crates/moonhare_graphics/src/lib.rs index b331c2f..3a60e6c 100644 --- a/crates/moonhare_graphics/src/lib.rs +++ b/crates/moonhare_graphics/src/lib.rs @@ -1,3 +1,32 @@ //! Crate for providing an abstraction layer over different graphics APIs + +use std::{cell::RefCell, rc::Rc}; + +use glium::{backend::Context, Surface}; +use moonhare_window::glfw::{PWindow, Window}; pub mod shader; -pub mod backend; \ No newline at end of file +pub mod backend; +pub use glium; + + +pub fn build_context(window: PWindow) -> Rc{ + let gl_window = Rc::new(RefCell::new(window)); + // now building the context + + let context = unsafe { + // The first parameter is our backend. + // + // The second parameter tells glium whether or not it should regularly call `is_current` + // on the backend to make sure that the OpenGL context is still the current one. + // + // It is recommended to pass `true`, but you can pass `false` if you are sure that no + // other OpenGL context will be made current in this thread. + let backend = backend::Backend { + gl_window: gl_window, + }; + glium::backend::Context::new(backend, true, Default::default()) + } + .unwrap(); + + context +} diff --git a/crates/moonhare_window/src/platforms/glfw_window.rs b/crates/moonhare_window/src/platforms/glfw_window.rs index 0148139..f8aba21 100644 --- a/crates/moonhare_window/src/platforms/glfw_window.rs +++ b/crates/moonhare_window/src/platforms/glfw_window.rs @@ -1,6 +1,6 @@ use std::sync::Arc; -use glfw::{Context, Glfw, GlfwReceiver, PWindow, WindowEvent}; +use glfw::{Context, Glfw, GlfwReceiver, PWindow, Window, WindowEvent}; use moonhare_event::{event::Event, events::window_events::window_close_event::WindowCloseEvent}; use crate::{window_config, MoonhareWindow}; @@ -30,7 +30,7 @@ impl GLFWWindow { window.make_current(); Self { - glfw_window: window, + glfw_window: window.try_into().unwrap(), events: events, glfw: glfw, is_running: true