Initial Setup
This commit is contained in:
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
/target
|
||||
/Cargo.lock
|
||||
|
||||
# Ignore IntelliJ
|
||||
.idea/
|
||||
20
Cargo.toml
Normal file
20
Cargo.toml
Normal 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
2
src/backends/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
mod opengl;
|
||||
pub use opengl::*;
|
||||
2
src/backends/opengl/mod.rs
Normal file
2
src/backends/opengl/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
mod opengl_renderer;
|
||||
pub use opengl_renderer::*;
|
||||
39
src/backends/opengl/opengl_renderer.rs
Normal file
39
src/backends/opengl/opengl_renderer.rs
Normal 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
2
src/lib.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod window;
|
||||
pub mod backends;
|
||||
50
src/main.rs
Normal file
50
src/main.rs
Normal 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
59
src/window.rs
Normal 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>)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user