This commit is contained in:
LunarAkai 2025-08-12 16:36:47 +02:00
commit e2d5748733
41 changed files with 291 additions and 268 deletions

View file

@ -1,9 +1,12 @@
use std::{cell::RefCell, ffi::{c_void, CString}, rc::Rc};
use std::{
cell::RefCell,
ffi::{CString, c_void},
rc::Rc,
};
use glium::SwapBuffersError;
use moonhare_window::glfw::Context;
// adopted from the glium repo -> examples -> manual_creation.rs
#[derive(Clone)]
pub struct Backend {
@ -11,34 +14,36 @@ pub struct Backend {
}
unsafe impl glium::backend::Backend for Backend {
fn swap_buffers(&self) -> Result<(), SwapBuffersError> {
self.gl_window.borrow_mut().swap_buffers();
Ok(())
}
fn swap_buffers(&self) -> Result<(), SwapBuffersError> {
self.gl_window.borrow_mut().swap_buffers();
Ok(())
}
unsafe fn get_proc_address(&self, symbol: &str) -> *const c_void {
let symbol = CString::new(symbol).unwrap();
self.gl_window
.borrow_mut()
.get_proc_address(&symbol.to_str().unwrap()) as *const _
}
unsafe fn get_proc_address(&self, symbol: &str) -> *const c_void {
let symbol = CString::new(symbol).unwrap();
self.gl_window
.borrow_mut()
.get_proc_address(&symbol.to_str().unwrap()) as *const _
}
fn get_framebuffer_dimensions(&self) -> (u32, u32) {
let window = &self.gl_window.borrow();
(window.get_size().0 as u32, window.get_size().1 as u32)
}
fn get_framebuffer_dimensions(&self) -> (u32, u32) {
let window = &self.gl_window.borrow();
(window.get_size().0 as u32, window.get_size().1 as u32)
}
fn resize(&self, new_size: (u32, u32)) {
let _ = &self.gl_window.borrow_mut().set_size(new_size.0 as i32, new_size.1 as i32);
}
fn resize(&self, new_size: (u32, u32)) {
let _ = &self
.gl_window
.borrow_mut()
.set_size(new_size.0 as i32, new_size.1 as i32);
}
fn is_current(&self) -> bool {
self.gl_window.borrow().is_current()
}
fn is_current(&self) -> bool {
self.gl_window.borrow().is_current()
}
unsafe fn make_current(&self) {
let mut gl_window = self.gl_window.borrow_mut();
gl_window.make_current();
}
}
unsafe fn make_current(&self) {
let mut gl_window = self.gl_window.borrow_mut();
gl_window.make_current();
}
}

View file

@ -2,28 +2,38 @@ pub struct Color {
pub red: f32,
pub green: f32,
pub blue: f32,
pub alpha: f32
pub alpha: f32,
}
impl Color {
/// Color Values from 0 - 255
/// 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 }
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 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 }
Self {
red: r,
green: g,
blue: b,
alpha: a,
}
}
}
}

View file

@ -1,22 +1,29 @@
//! Crate for providing an abstraction layer over different graphics APIs
use std::{cell::{Cell, RefCell}, rc::Rc, sync::{Arc, Mutex, RwLock}};
use std::{
cell::{Cell, RefCell},
rc::Rc,
sync::{Arc, Mutex, RwLock},
};
use glium::{backend::{Context, Facade}, Frame, Surface};
use glium::{
Frame, Surface,
backend::{Context, Facade},
};
use lazy_static::lazy_static;
use moonhare_window::glfw::PWindow;
pub mod shader;
pub mod backend;
pub mod vertices;
pub mod color;
pub mod shader;
pub mod vertices;
pub use glium;
use state::InitCell;
use crate::color::Color;
pub fn build_context(window: Rc<RefCell<PWindow>>) -> Rc<Context>{
pub fn build_context(window: Rc<RefCell<PWindow>>) -> Rc<Context> {
let gl_window: Rc<RefCell<PWindow>> = window;
// now building the context
// now building the context
let context = unsafe {
// The first parameter is our backend.
@ -38,4 +45,4 @@ pub fn build_context(window: Rc<RefCell<PWindow>>) -> Rc<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

@ -1,4 +1,4 @@
pub enum ShaderType {
Vertex,
Fragment,
}
}

View file

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