Verified Commit cd8378a3 authored by insert's avatar insert

Work on options.

parent 734dd7be
......@@ -2,5 +2,5 @@
out vec4 FragColor;
void main() {
FragColor = vec4(1.0, 0.0, 0.0, 0.0);
FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
use byol;
use byol::native::Event;
use byol::render::mesh::MeshData;
use byol::render::shader::{Program, Shader, ShaderType};
use byol::native::Event;
fn main() {
let mut byol = byol::native::Byol::new().unwrap();
let mut byol = byol::native::Byol::new(None).unwrap();
let win = byol.create_window(None).unwrap();
let shader = Program::from_shaders(&vec![
......@@ -16,7 +16,7 @@ fn main() {
let mesh = MeshData::new()
.vertex(vec![-0.5, -0.5, 0.0, 0.5, -0.5, 0.0, 0.0, 0.5, 0.0])
.create();
let win2 = byol.create_window(None).unwrap();
let shader2 = Program::from_shaders(&vec![
......@@ -29,34 +29,15 @@ fn main() {
.vertex(vec![-0.5, 0.5, 0.0, 0.5, 0.5, 0.0, 0.0, -0.5, 0.0])
.create();
byol.run(move |event| {
match event {
Event::Draw{ window_id, .. } => {
if &window_id == win.id() {
shader.enable();
mesh.draw();
} else if &window_id == win2.id() {
shader2.enable();
mesh2.draw();
}
byol.run(move |event| match event {
Event::Draw { window_id, .. } => {
if &window_id == win.id() {
shader.enable();
mesh.draw();
} else if &window_id == win2.id() {
shader2.enable();
mesh2.draw();
}
}
});
/*'main: loop {
while let Some(event) = sdl.take_event() {
match event {
Event::Quit { .. } => break 'main,
Event::Window { win_event, .. } => win.on_event(win_event),
_ => {}
}
}
win.clear();
shader.enable();
mesh.draw();
win.swap();
}*/
}
use super::ct::{ContextTracker, ContextWrapper};
use super::window::{Window, WindowOptions};
use super::ct::ContextTracker;
use hashbrown::HashMap;
use glutin::PossiblyCurrent;
use glutin::event::WindowEvent;
use glutin::event_loop::EventLoop;
use glutin::event::{WindowEvent};
use glutin::window::WindowId;
use hashbrown::HashMap;
pub struct ByolOptions {
swap_after_draw: bool
swap_after_draw: bool,
}
impl ByolOptions {
pub fn new() -> Self {
ByolOptions {
swap_after_draw: true,
}
}
}
/*pub struct SdlOptions {
......@@ -37,32 +46,25 @@ impl SdlOptions {
}
}*/
use glutin::{PossiblyCurrent};
use super::ct::ContextWrapper;
/*pub struct DrawEvent<'a> {
pub window_id: WindowId,
pub ctx: &'a mut ContextWrapper<PossiblyCurrent>
}*/
pub enum Event<'a> {
Draw {
window_id: WindowId,
ctx: &'a mut ContextWrapper<PossiblyCurrent>
}
ctx: &'a mut ContextWrapper<PossiblyCurrent>,
},
}
pub struct Byol {
options: ByolOptions,
event_loop: EventLoop<()>,
context_tracker: ContextTracker,
window_context: HashMap<WindowId, usize>
window_context: HashMap<WindowId, usize>,
}
impl Byol {
pub fn new() -> Result<Byol, String> {
/*let options = options.into().unwrap_or(SdlOptions::new());
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 sdl = sdl2::init()?;
let video = sdl.video()?;
let event_pump = sdl.event_pump()?;
......@@ -81,9 +83,10 @@ impl Byol {
let window_context = HashMap::new();
Ok(Byol {
options,
event_loop,
context_tracker,
window_context
window_context,
})
}
......@@ -107,14 +110,15 @@ impl Byol {
}
pub fn run<F: FnMut(Event) + 'static>(self, mut func: F) {
let options = self.options;
let win_ctx = self.window_context;
let mut ctx_tracker = self.context_tracker;
self.event_loop.run(move |event, _, control_flow| {
self.event_loop.run(move |event, _, _control_flow| {
println!("{:?}", event);
match event {
glutin::event::Event::LoopDestroyed => return,
glutin::event::Event::WindowEvent { event, window_id } => match event {
WindowEvent::Resized(physical_size) => {
glutin::event::Event::WindowEvent { event, .. } => match event { //window_id
WindowEvent::Resized(_physical_size) => {
//
}
WindowEvent::CloseRequested => {
......@@ -124,23 +128,16 @@ impl Byol {
},
glutin::event::Event::RedrawRequested(window_id) => {
let ctx_id = win_ctx.get(&window_id).unwrap().clone();
let mut ctx = ctx_tracker.get_current(ctx_id).unwrap();
let ctx = ctx_tracker.get_current(ctx_id).unwrap();
func(Event::Draw { window_id, ctx });
ctx.windowed().swap_buffers().unwrap();
if options.swap_after_draw {
ctx.windowed().swap_buffers().unwrap();
}
}
_ => (),
}
});
}
/*pub fn take_event(&mut self) -> Option<Event> {
self.event_pump.poll_event()
}*/
}
/*impl Drop for Sdl {
fn drop(&mut self) {
self.video.gl_unload_library();
}
}*/
use glutin::{self, PossiblyCurrent};
pub use self::context_tracker::{ContextCurrentWrapper, ContextId, ContextTracker, ContextWrapper};
mod context_tracker {
......
pub mod ct;
pub mod byol;
pub mod ct;
pub mod viewport;
pub mod window;
......
use super::Byol;
use super::viewport::Viewport;
use super::ct::{ContextCurrentWrapper, ContextWrapper};
use super::viewport::Viewport;
use super::Byol;
use glutin::window::{WindowBuilder};
use glutin::window::{WindowBuilder, WindowId, Fullscreen};
use glutin::ContextBuilder;
use glutin::dpi::{Size, LogicalSize, PhysicalSize};
use glutin::{self};
use glutin::window::WindowId;
use gl;
pub struct WindowOptions<'a> {
pub title: &'a str,
pub width: u32,
pub height: u32,
pub size: Size,
pub clear_color: [f32; 4],
pub resizable: bool,
pub fullscreen: bool,
pub borderless: bool,
pub fullscreen: Option<Fullscreen>,
pub decorations: bool,
pub transparent: bool,
}
impl<'a> WindowOptions<'a> {
pub fn new() -> Self {
WindowOptions {
title: "byol",
width: 1280,
height: 720,
size: Size::Logical(LogicalSize { width: 1280.0, height: 720.0 }),
clear_color: [0.4, 0.2, 0.6, 1.0],
resizable: true,
fullscreen: false,
borderless: false,
fullscreen: None,
decorations: false,
transparent: false,
}
}
......@@ -37,9 +37,13 @@ impl<'a> WindowOptions<'a> {
self
}
pub fn size(&mut self, width: u32, height: u32) -> &mut Self {
self.width = width;
self.height = height;
pub fn size(&mut self, size: Size) -> &mut Self {
self.size = size;
self
}
pub fn window_size(&mut self, width: f64, height: f64) -> &mut Self {
self.size = Size::Logical(LogicalSize { width, height });
self
}
......@@ -48,8 +52,8 @@ impl<'a> WindowOptions<'a> {
self
}
pub fn fullscreen(&mut self, fullscreen: bool) -> &mut Self {
self.fullscreen = fullscreen;
pub fn fullscreen(&mut self, fullscreen: Fullscreen) -> &mut Self {
self.fullscreen = Some(fullscreen);
self
}
......@@ -58,18 +62,15 @@ impl<'a> WindowOptions<'a> {
self
}
pub fn borderless(&mut self, borderless: bool) -> &mut Self {
self.borderless = borderless;
pub fn decorations(&mut self, decorations: bool) -> &mut Self {
self.decorations = decorations;
self
}
}
pub struct Window {
id: WindowId,
ctx_id: usize,
//ctx: ContextWrapper<PossiblyCurrent, glutin::window::Window>,
//instance: sdl2::video::Window,
//_context: sdl2::video::GLContext,
_ctx_id: usize,
viewport: Viewport,
}
......@@ -80,43 +81,18 @@ impl Window {
) -> Result<Window, String> {
let options = options.into().unwrap_or(WindowOptions::new());
/*let mut builder = sdl
.video
.window(options.title, options.width, options.height);
builder.opengl();
if options.resizable {
builder.resizable();
}
if options.fullscreen {
builder.fullscreen();
}
if options.borderless {
builder.borderless();
}
let instance = match builder.build() {
Ok(i) => i,
Err(err) => {
return Err(match err {
WindowBuildError::HeightOverflows(height) => {
format!("Height overflow. {}", height)
}
WindowBuildError::WidthOverflows(width) => format!("Width overflow. {}", width),
WindowBuildError::InvalidTitle(_) => format!("Invalid title."),
WindowBuildError::SdlError(err) => err,
});
}
};
let _context = instance.gl_create_context()?;
gl::load_with(|s| sdl.video.gl_get_proc_address(s) as *const std::os::raw::c_void);*/
let wb = WindowBuilder::new().with_title(options.title);
let ctx = ContextBuilder::new().build_windowed(wb, &byol.el()).unwrap();
let wb = WindowBuilder::new()
.with_title(options.title)
.with_inner_size(options.size)
.with_resizable(options.resizable)
.with_fullscreen(options.fullscreen)
.with_decorations(options.decorations)
.with_transparent(options.transparent);
let ctx = ContextBuilder::new()
.build_windowed(wb, &byol.el())
.unwrap();
let ctx = unsafe { ctx.make_current().unwrap() };
let id = ctx.window().id();
......@@ -138,14 +114,13 @@ impl Window {
);
}
let viewport = Viewport::new(1280, 720);
let physical_size: PhysicalSize<i32> = options.size.to_physical(1.0);
let viewport = Viewport::new(physical_size.width, physical_size.height);
viewport.enable();
Ok(Window {
id,
ctx_id,
//instance,
//_context,
_ctx_id: ctx_id,
viewport,
})
}
......@@ -154,37 +129,12 @@ impl Window {
&self.id
}
/*pub fn on_event(&mut self, ev: sdl2::event::WindowEvent) {
match ev {
sdl2::event::WindowEvent::Resized(w, h) => {
self.viewport.update_size(w, h);
self.viewport.enable();
}
_ => {}
}
}*/
/*pub fn enable(self) -> Result<(), String> {
unsafe {
if let Ok(ctx) = self.ctx.make_current() {
Ok(())
} else {
Err("Failed to switch context.".to_string())
}
}
}*/
pub fn clear(&self) {
unsafe {
gl::Clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT);
}
}
//pub fn swap(&self) -> Result<(), ContextError> {
//self.ctx.swap_buffers()
//self.instance.gl_swap_window();
//}
pub fn enable_viewport(&self) {
self.viewport.enable();
}
......@@ -192,4 +142,12 @@ impl Window {
pub fn get_aspect_ratio(&self) -> f32 {
self.viewport.aspect()
}
pub fn width(&self) -> i32 {
self.viewport.w
}
pub fn height(&self) -> i32 {
self.viewport.h
}
}
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