Verified Commit 592d53af authored by insert's avatar insert

Add delta time, redraw pref, and mesh references.

parent 2dc217b3
use super::ct::{ContextTracker, ContextWrapper};
use super::viewport::Viewport;
use super::window::{Window, WindowOptions};
use super::window::{Window, WindowOptions, RedrawPreference};
use glutin::event_loop::{ControlFlow, EventLoop};
use glutin::window::WindowId;
use glutin::PossiblyCurrent;
use hashbrown::HashMap;
use std::time::Instant;
use std::sync::{Arc, Mutex};
pub struct ByolOptions {
......@@ -21,47 +22,37 @@ impl ByolOptions {
}
}
/*pub struct SdlOptions {
gl_context_profile: sdl2::video::GLProfile,
gl_major_version: u8,
gl_minor_version: u8,
}
impl SdlOptions {
pub fn new() -> Self {
SdlOptions {
gl_context_profile: sdl2::video::GLProfile::Core,
gl_major_version: 3,
gl_minor_version: 3,
}
}
pub fn set_profile(&mut self, profile: sdl2::video::GLProfile) -> &mut Self {
self.gl_context_profile = profile;
self
}
pub fn set_gl_version(&mut self, major: u8, minor: u8) -> &mut Self {
self.gl_major_version = major;
self.gl_major_version = minor;
self
}
}*/
pub use glutin::event::{ WindowEvent, DeviceEvent, VirtualKeyCode, ElementState };
pub enum Event<'a> {
Draw {
window_id: WindowId,
ctx: &'a mut ContextWrapper<PossiblyCurrent>,
delta: f64
},
CloseRequested {
window_id: WindowId,
},
WindowEvent {
window_id: WindowId,
event: glutin::event::WindowEvent<'a>
},
DeviceEvent{
device_id: glutin::event::DeviceId,
event: glutin::event::DeviceEvent
},
Cleanup,
}
pub struct WindowReference {
ctx_id: usize,
viewport: Arc<Mutex<Viewport>>,
redraw: RedrawPreference,
last_frame: Instant
}
pub struct Byol {
......@@ -75,20 +66,6 @@ impl Byol {
pub fn new<O: Into<Option<ByolOptions>>>(options: O) -> Result<Byol, String> {
let options = options.into().unwrap_or(ByolOptions::new());
/*let sdl = sdl2::init()?;
let video = sdl.video()?;
let event_pump = sdl.event_pump()?;
let gl_attributes = video.gl_attr();
gl_attributes.set_context_profile(options.gl_context_profile);
gl_attributes.set_context_version(options.gl_major_version, options.gl_minor_version);
Ok(Sdl {
sdl,
video,
event_pump,
})*/
let event_loop = EventLoop::new();
let context_tracker = ContextTracker::default();
let window_context = HashMap::new();
......@@ -121,52 +98,73 @@ impl Byol {
window: WindowId,
ctx_id: usize,
viewport: Arc<Mutex<Viewport>>,
redraw: RedrawPreference,
) {
let last_frame = Instant::now();
self.window_context
.insert(window, WindowReference { ctx_id, viewport });
.insert(window, WindowReference { ctx_id, viewport, redraw, last_frame });
}
pub fn run<F: FnMut(Event, &mut dyn FnMut()) + 'static>(self, mut func: F) {
let options = self.options;
let win_ctx = self.window_context;
let mut win_ctx = self.window_context;
let mut ctx_tracker = self.context_tracker;
self.event_loop.run(move |event, _, control_flow| {
let mut close = || {
*control_flow = ControlFlow::Exit;
};
//println!("{:?}", event);
match event {
glutin::event::Event::LoopDestroyed => {
func(Event::Cleanup, &mut close);
return;
}
glutin::event::Event::WindowEvent {
event, window_id, ..
} => match event {
glutin::event::WindowEvent::Resized(size) => {
let window_ref = win_ctx.get(&window_id).unwrap().clone();
window_ref
.viewport
.lock()
.unwrap()
.update_size(size.width as i32, size.height as i32);
}
glutin::event::WindowEvent::CloseRequested => {
func(Event::CloseRequested { window_id }, &mut close);
event, window_id
} => {
match event {
glutin::event::WindowEvent::Resized(size) => {
let window_ref = win_ctx.get(&window_id).unwrap().clone();
window_ref
.viewport
.lock()
.unwrap()
.update_size(size.width as i32, size.height as i32);
}
glutin::event::WindowEvent::CloseRequested => {
func(Event::CloseRequested { window_id }, &mut close);
}
_ => (),
}
_ => (),
},
func(Event::WindowEvent { window_id, event }, &mut close);
}
glutin::event::Event::DeviceEvent { event, device_id } => {
func(Event::DeviceEvent { device_id, event }, &mut close);
}
glutin::event::Event::RedrawRequested(window_id) => {
let window_ref = win_ctx.get(&window_id).unwrap().clone();
let mut window_ref = win_ctx.get_mut(&window_id).unwrap();
let ctx = ctx_tracker.get_current(window_ref.ctx_id).unwrap();
let delta = window_ref.last_frame.elapsed().as_secs_f64();
window_ref.last_frame = Instant::now();
window_ref.viewport.lock().unwrap().enable();
func(Event::Draw { window_id, ctx }, &mut close);
func(Event::Draw { window_id, ctx, delta }, &mut close);
if options.swap_after_draw {
ctx.windowed().swap_buffers().unwrap();
}
match window_ref.redraw {
RedrawPreference::None => return,
RedrawPreference::Unlimited |
RedrawPreference::Vsync => {
*control_flow = ControlFlow::Poll;
}
}
ctx.windowed().window().request_redraw();
}
_ => (),
}
......
......@@ -9,6 +9,13 @@ use glutin::{self};
use std::sync::{Arc, Mutex};
pub enum RedrawPreference {
None,
Unlimited,
Vsync
// consider adding FPS cap
}
pub struct WindowOptions<'a> {
pub title: &'a str,
pub size: Size,
......@@ -17,6 +24,7 @@ pub struct WindowOptions<'a> {
pub fullscreen: Option<Fullscreen>,
pub decorations: bool,
pub transparent: bool,
pub redraw: RedrawPreference,
}
impl<'a> WindowOptions<'a> {
......@@ -32,6 +40,7 @@ impl<'a> WindowOptions<'a> {
fullscreen: None,
decorations: true,
transparent: false,
redraw: RedrawPreference::Unlimited
}
}
......@@ -69,6 +78,16 @@ impl<'a> WindowOptions<'a> {
self.decorations = decorations;
self
}
pub fn transparent(&mut self, transparent: bool) -> &mut Self {
self.transparent = transparent;
self
}
pub fn redraw(&mut self, redraw: RedrawPreference) -> &mut Self {
self.redraw = redraw;
self
}
}
pub struct Window {
......@@ -93,6 +112,7 @@ impl Window {
.with_transparent(options.transparent);
let ctx = ContextBuilder::new()
.with_vsync(if let RedrawPreference::Vsync = options.redraw { true } else { false })
.build_windowed(wb, &byol.el())
.unwrap();
......@@ -120,7 +140,7 @@ impl Window {
viewport.enable();
let viewport = Arc::new(Mutex::new(viewport));
byol.register_window(id, ctx_id, viewport.clone());
byol.register_window(id, ctx_id, viewport.clone(), options.redraw);
Ok(Window {
id,
......
......@@ -3,11 +3,14 @@ use super::{MeshData, Render};
use std::convert::TryInto;
use std::rc::Rc;
pub struct Mesh {
vbo: Vec<u32>,
vao: u32,
indices: gl::types::GLsizei,
indexed: bool,
references: Rc<()>,
}
/// Bind vertex attribute.
......@@ -47,7 +50,8 @@ impl Mesh {
vbo,
vao,
indices: 0,
indexed: false
indexed: false,
references: Rc::new(())
}
}
......@@ -137,3 +141,20 @@ impl Mesh {
}
}
}
impl Drop for Mesh {
fn drop(&mut self) {
if Rc::strong_count(&self.references) == 1 {
unsafe {
println!("Dropping mesh [VAO: {}]!", self.vao);
// ! FIXME; drop VAO + VBO
}
}
}
}
impl Default for Mesh {
fn default() -> Self {
Mesh::new()
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment