Initial Setup

This commit is contained in:
2023-10-25 20:08:46 -04:00
commit 2d7df265d0
8 changed files with 179 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
/target
/Cargo.lock
# Ignore IntelliJ
.idea/

20
Cargo.toml Normal file
View File

@@ -0,0 +1,20 @@
[package]
name = "nytegearui"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "nytegearui"
path = "src/lib.rs"
[[bin]]
name = "demo"
path = "src/main.rs"
[dependencies]
winit = "0.29"
glutin = "0.31.0"
gl = "0.14"
raw-gl-context = { git = "https://github.com/Aargonian/raw-gl-context.git", branch = "master" }

2
src/backends/mod.rs Normal file
View File

@@ -0,0 +1,2 @@
mod opengl;
pub use opengl::*;

View File

@@ -0,0 +1,2 @@
mod opengl_renderer;
pub use opengl_renderer::*;

View File

@@ -0,0 +1,39 @@
use crate::window::Renderer;
use raw_gl_context::{GlConfig, GlContext};
use winit::window::Window as WinitWindow;
use winit::raw_window_handle::HasRawWindowHandle;
pub struct OpenGLRenderer {
context: GlContext,
}
impl OpenGLRenderer
{
pub fn try_create(window: &WinitWindow) -> Option<OpenGLRenderer>
{
let context = unsafe { GlContext::create(window, GlConfig::default()).unwrap() };
unsafe {
context.make_current();
}
Some(OpenGLRenderer {
context,
})
}
}
impl Renderer for OpenGLRenderer
{
fn draw_pixel(&mut self, x: u32, y: u32, value: u32) {
todo!()
}
fn draw_rect(&mut self, x: u32, y: u32, width: u32, height: u32, value: u32) {
todo!()
}
fn fill_rect(&mut self, x: u32, y: u32, width: u32, height: u32, value: u32) {
todo!()
}
}

2
src/lib.rs Normal file
View File

@@ -0,0 +1,2 @@
pub mod window;
pub mod backends;

50
src/main.rs Normal file
View File

@@ -0,0 +1,50 @@
use raw_gl_context::{GlConfig, GlContext};
use winit::event_loop::EventLoop;
use winit::window::WindowBuilder;
use winit::event::{Event, WindowEvent};
fn main() {
let event_loop = EventLoop::new().unwrap();
let window = WindowBuilder::new().build(&event_loop).unwrap();
let context = unsafe { GlContext::create(&window, GlConfig::default()).unwrap() };
unsafe {
context.make_current();
}
gl::load_with(|symbol| context.get_proc_address(symbol) as *const _);
event_loop.run(move |event, window_target| {
match event {
Event::WindowEvent {
event: winit::event::WindowEvent::CloseRequested,
..
} => {
window_target.exit();
}
Event::WindowEvent {
event: WindowEvent::RedrawRequested,
..
} => {
unsafe {
context.make_current();
}
unsafe {
gl::ClearColor(0.2, 0.1, 0.5, 1.0);
gl::Clear(gl::COLOR_BUFFER_BIT);
}
context.swap_buffers();
unsafe {
context.make_not_current();
}
}
_ => {}
}
}).expect("TODO: panic message");
}

59
src/window.rs Normal file
View File

@@ -0,0 +1,59 @@
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{Window as WinitWindow, WindowBuilder},
};
use crate::backends::OpenGLRenderer;
pub trait Renderer
{
fn draw_pixel(&mut self, x: u32, y: u32, value: u32);
fn draw_rect(&mut self, x: u32, y: u32, width: u32, height: u32, value: u32);
fn fill_rect(&mut self, x: u32, y: u32, width: u32, height: u32, value: u32);
}
pub trait Widget
{
fn draw(&self, renderer: &mut dyn Renderer);
}
pub struct Window
{
window: WinitWindow,
event_loop: EventLoop<()>,
renderer: Box<dyn Renderer>,
children: Vec<Box<dyn Widget>>,
}
impl Window
{
pub fn new() -> Self {
// Construct the EventLoop for Winit
let event_loop = EventLoop::new().unwrap();
event_loop.set_control_flow(ControlFlow::Wait);
let window = WindowBuilder::new().build(&event_loop).unwrap();
// Determine the available rendering backend
if let Some(renderer) = Self::resolve_rendering_backend(&window) {
Window {
window,
event_loop,
renderer,
children: vec![],
}
} else {
panic!("FUCK")
}
}
fn resolve_rendering_backend(window: &WinitWindow) -> Option<Box<dyn Renderer>>
{
//TODO: Expand this to allow for Metal and DirectX Renderers
// First check if OpenGL is available
OpenGLRenderer::try_create(window).map(|renderer| Box::new(renderer) as Box<dyn Renderer>)
}
}