stuff oder so

This commit is contained in:
LunarAkai 2025-08-03 23:27:32 +02:00
commit 0eeb4b2563
8 changed files with 286 additions and 18 deletions

View file

@ -1,10 +1,10 @@
//! Base functionality for a Moonhare Game Engine Project
use std::{cell::{RefCell, RefMut}, rc::Rc, sync::Arc};
use std::rc::Rc;
use moonhare_graphics::glium::{backend::Context, glutin::api::egl::context, winit::event_loop};
use moonhare_graphics::{color::Color, glium::{backend::Context, glutin::api::egl::context}};
use moonhare_log::*;
use moonhare_window::{glfw::PWindow, platforms::glfw_window::GLFWWindow};
use moonhare_window::{platforms::glfw_window::GLFWWindow};
pub mod basic;
/// Only one Game may exist per project
@ -13,7 +13,7 @@ pub struct Game {
pub is_running: bool,
pub name: String,
pub context: moonhare_window::WindowRenderContext,
pub glfw_window: Option<moonhare_window::platforms::glfw_window::GLFWWindow>
pub glfw_window: Option<moonhare_window::platforms::glfw_window::GLFWWindow>,
}
impl Default for Game {
@ -27,6 +27,16 @@ impl Default for Game {
}
}
pub struct GraphicsHandler {
pub context: Option<Rc<Context>>
}
impl Default for GraphicsHandler {
fn default() -> Self {
Self { context: None }
}
}
impl Game {
pub fn new() -> Self {
Game::default()
@ -34,15 +44,15 @@ impl Game {
pub fn run(self) {
info("Running Game...");
let mut glfw_window_unwrapped = self.glfw_window;
let mut context: std::rc::Rc<moonhare_graphics::glium::backend::Context>;
let glfw_window_unwrapped = self.glfw_window;
let mut graphics_handler: GraphicsHandler = GraphicsHandler { ..Default::default() };
let context: std::rc::Rc<moonhare_graphics::glium::backend::Context>;
context = moonhare_graphics::build_context(glfw_window_unwrapped.clone().unwrap().glfw_window);
graphics_handler.context = Some(context.clone());
let mut value = glfw_window_unwrapped;
while self.is_running {
// can't move glfwwindow cause i can't implement clone, or idk
handle_window_event(value.as_mut().unwrap());
render(context.clone());
@ -61,7 +71,7 @@ fn default_game_name() -> String {
}
/// Deals with GLFW Window Events (in `monhare_window`)
fn handle_window_event(mut glfw_window: &GLFWWindow) {
fn handle_window_event(glfw_window: &GLFWWindow) {
glfw_window.glfw_window.borrow_mut().glfw.poll_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);
@ -69,8 +79,6 @@ fn handle_window_event(mut glfw_window: &GLFWWindow) {
}
fn render(context: Rc<Context>) {
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();
let target = moonhare_graphics::glium::Frame::new(context.clone(), context.get_framebuffer_dimensions());
moonhare_graphics::draw_background_color(Color::color_from_rgb(255, 255, 255), target);
}

View file

@ -7,3 +7,5 @@ authors.workspace = true
[dependencies]
moonhare_window = { path = "../moonhare_window" }
glium = "0.36"
state = "0.6"
lazy_static = "*"

View file

@ -0,0 +1,29 @@
pub struct Color {
pub red: f32,
pub green: f32,
pub blue: f32,
pub alpha: f32
}
impl Color {
/// Color Values from 0 - 255
/// For example: 255, 255, 255 -> White
pub fn color_from_rgb(red: i32, green: i32, blue: i32) -> Self {
// 255 -> 1.0
// 0 -> 0.0
let r: f32 = red as f32/ 255.0;
let g: f32 = green as f32 / 255.0;
let b: f32 = blue as f32/ 255.0;
Self { red: r, green: g, blue: b, alpha: 1.0 }
}
pub fn color_from_rgba(red: i32, green: i32, blue: i32, alpha: f32) -> Self {
// 255 -> 1.0
// 0 -> 0.0
let r: f32 = red as f32 / 255.0;
let g: f32 = green as f32 / 255.0;
let b: f32 = blue as f32 / 255.0;
let a: f32 = alpha;
Self { red: r, green: g, blue: b, alpha: a }
}
}

View file

@ -1,16 +1,21 @@
//! Crate for providing an abstraction layer over different graphics APIs
use std::{cell::RefCell, rc::Rc};
use std::{cell::{Cell, RefCell}, rc::Rc, sync::{Arc, Mutex, RwLock}};
use glium::{backend::Context, Surface};
use moonhare_window::glfw::{PWindow, Window};
use glium::{backend::{Context, Facade}, Frame, Surface};
use lazy_static::lazy_static;
use moonhare_window::glfw::PWindow;
pub mod shader;
pub mod backend;
pub mod vertices;
pub mod color;
pub use glium;
use state::InitCell;
use crate::color::Color;
pub fn build_context(window: Rc<RefCell<PWindow>>) -> Rc<Context>{
let gl_window = window;
let gl_window: Rc<RefCell<PWindow>> = window;
// now building the context
let context = unsafe {
@ -27,6 +32,10 @@ pub fn build_context(window: Rc<RefCell<PWindow>>) -> Rc<Context>{
glium::backend::Context::new(backend, true, Default::default())
}
.unwrap();
context
}
pub fn draw_background_color(color: Color, mut target: Frame) {
Surface::clear_color(&mut target, color.red, color.green, color.blue, color.alpha);
target.finish().unwrap()
}

View file

@ -0,0 +1 @@
pub mod vertex;

View file

@ -0,0 +1,8 @@
use glium::implement_vertex;
#[derive(Clone, Copy)]
pub struct Vertex {
pub(crate) position: [f32; 2],
pub(crate) color: [f32; 3],
}
implement_vertex!(Vertex, position, color);

View file

@ -7,6 +7,7 @@ use crate::{window_config, MoonhareWindow};
#[derive(Debug)]
pub struct GLFWWindow {
// Todo: learn more about rust smart pointers so i actually understand whats going on here, but hey it works for now
pub glfw_window: Rc<RefCell<PWindow>>,
pub events: Rc<RefCell<GlfwReceiver<(f64, WindowEvent)>>>,
pub glfw: Glfw,