From 670f1eb8e7acae2ea5b8917334bc626973ace2bd Mon Sep 17 00:00:00 2001 From: Scetch Date: Thu, 1 Oct 2015 15:07:27 -0400 Subject: [PATCH] Switch from GLFW to Glutin --- Cargo.toml | 12 +----- src/gl/mod.rs | 12 +++--- src/main.rs | 104 ++++++++++++++++++++++++++------------------------ 3 files changed, 61 insertions(+), 67 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index dbc9146..f329edc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ authors = [ "Thinkofdeath " ] build = "build.rs" [dependencies] -glfw = "0.1.0" +glutin = "0.3.7" byteorder = "0.3.13" hyper = "0.6.13" serde = "0.6.0" @@ -25,13 +25,3 @@ version = "0" [dependencies.steven_openssl] path = "./openssl" version = "0" - -[target.x86_64-pc-windows-gnu.dependencies] -glfw = { version = "0.1.0", default-features = false } - -[target.x86_64-pc-windows-msvc.dependencies] -glfw = { version = "0.1.0", default-features = false } - -[target.i686-pc-windows-gnu.dependencies] -glfw = { version = "0.1.0", default-features = false } - diff --git a/src/gl/mod.rs b/src/gl/mod.rs index 9653816..15d1adf 100644 --- a/src/gl/mod.rs +++ b/src/gl/mod.rs @@ -13,7 +13,7 @@ // limitations under the License. extern crate steven_gl as gl; -extern crate glfw; +use glutin; use std::ops::BitOr; use std::ffi; @@ -22,7 +22,7 @@ use std::ptr; use std::ops::{Deref, DerefMut}; /// Inits the gl library. This should be called once a context is ready. -pub fn init(window: &mut glfw::Window) { +pub fn init(window: &mut glutin::Window) { gl::load_with(|s| window.get_proc_address(s)); } @@ -319,7 +319,7 @@ impl Shader { let len = self.get_parameter(INFO_LOG_LENGTH); let mut data = Vec::::with_capacity(len as usize); - unsafe { + unsafe { data.set_len(len as usize); gl::GetShaderInfoLog(self.0, len, ptr::null_mut(), data.as_mut_ptr() as *mut i8); } @@ -416,7 +416,7 @@ impl VertexArray { } } -impl Drop for VertexArray { +impl Drop for VertexArray { fn drop(&mut self) { unsafe { gl::DeleteVertexArrays(1, &self.0); } self.0 = 0; @@ -455,7 +455,7 @@ pub const WRITE_ONLY: Access = gl::WRITE_ONLY; pub struct Buffer(u32); impl Buffer { - /// Allocates a new Buffer. + /// Allocates a new Buffer. pub fn new() -> Buffer { let mut b = Buffer(0); unsafe { gl::GenBuffers(1, &mut b.0); } @@ -465,7 +465,7 @@ impl Buffer { /// Makes the buffer the currently active one for the given target. /// This will allow it to be the source of operations that act on a buffer /// (Data, Map etc). - pub fn bind(&self, target: BufferTarget) { + pub fn bind(&self, target: BufferTarget) { unsafe { gl::BindBuffer(target, self.0); } } diff --git a/src/main.rs b/src/main.rs index 7fd0251..7d0d94a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,7 +25,7 @@ pub mod screen; #[macro_use] pub mod console; -extern crate glfw; +extern crate glutin; extern crate image; extern crate time; extern crate byteorder; @@ -40,7 +40,6 @@ extern crate log; use std::sync::{Arc, RwLock, Mutex}; use std::marker::PhantomData; -use glfw::{Action, Context, Key}; const CL_BRAND: console::CVar = console::CVar { ty: PhantomData, @@ -48,7 +47,7 @@ const CL_BRAND: console::CVar = console::CVar { description: "cl_brand has the value of the clients current 'brand'. \ e.g. \"Steven\" or \"Vanilla\"", mutable: false, - serializable: false, + serializable: false, default: &|| "steven".to_owned(), }; @@ -57,6 +56,8 @@ pub struct Game { screen_sys: screen::ScreenSystem, resource_manager: Arc>, console: Arc>, + should_close: bool, + mouse_pos: (i32, i32), } fn main() { @@ -67,7 +68,9 @@ fn main() { con.load_config(); con.save_config(); } + let proxy = console::ConsoleProxy::new(con.clone()); + log::set_logger(|max_log_level| { max_log_level.set(log::LogLevelFilter::Trace); Box::new(proxy) @@ -78,27 +81,22 @@ fn main() { let resource_manager = Arc::new(RwLock::new(resources::Manager::new())); { resource_manager.write().unwrap().tick(); } - let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap(); + let mut window = glutin::WindowBuilder::new() + .with_title("Steven".to_string()) + .with_dimensions(854, 480) + .with_gl(glutin::GlRequest::Specific(glutin::Api::OpenGl, (3, 2))) + .with_gl_profile(glutin::GlProfile::Core) + .with_depth_buffer(24) + .with_stencil_buffer(0) + .with_vsync() + .build().ok().expect("Could not create Glutin window."); - glfw.window_hint(glfw::WindowHint::ContextVersion(3, 2)); - glfw.window_hint(glfw::WindowHint::OpenGlProfile(glfw::OpenGlProfileHint::Core)); - glfw.window_hint(glfw::WindowHint::OpenGlForwardCompat(true)); - glfw.window_hint(glfw::WindowHint::DepthBits(32)); - glfw.window_hint(glfw::WindowHint::StencilBits(0)); - - let (mut window, events) = glfw.create_window(854, 480, "Steven", glfw::WindowMode::Windowed) - .expect("Failed to create GLFW window"); + unsafe { + window.make_current().ok().expect("Could not set current context."); + } gl::init(&mut window); - window.set_key_polling(true); - window.set_char_polling(true); - window.set_scroll_polling(true); - window.set_mouse_button_polling(true); - window.set_cursor_pos_polling(true); - window.make_current(); - glfw.set_swap_interval(1); - let renderer = render::Renderer::new(resource_manager.clone()); let mut ui_container = ui::Container::new(); @@ -113,56 +111,62 @@ fn main() { screen_sys: screen_sys, resource_manager: resource_manager, console: con, + should_close: false, + mouse_pos: (0, 0), }; - while !window.should_close() { + while !game.should_close { { game.resource_manager.write().unwrap().tick(); } + let now = time::now(); let diff = now - last_frame; last_frame = now; let delta = (diff.num_nanoseconds().unwrap() as f64) / frame_time; + let (width, height) = window.get_inner_size_pixels().unwrap(); game.screen_sys.tick(delta, &mut game.renderer, &mut ui_container); - - let (width, height) = window.get_framebuffer_size(); game.console.lock().unwrap().tick(&mut ui_container, &mut game.renderer, delta, width as f64); ui_container.tick(&mut game.renderer, delta, width as f64, height as f64); - game.renderer.tick(delta, width as u32, height as u32); + game.renderer.tick(delta, width, height); - window.swap_buffers(); - glfw.poll_events(); - for (_, event) in glfw::flush_messages(&events) { - handle_window_event(&mut window, &mut game, &mut ui_container, event); + let _ = window.swap_buffers(); + + for event in window.poll_events() { + handle_window_event(&window, &mut game, &mut ui_container, event) } } } -fn handle_window_event(window: &mut glfw::Window, game: &mut Game, ui_container: &mut ui::Container, event: glfw::WindowEvent) { +fn handle_window_event(window: &glutin::Window, game: &mut Game, ui_container: &mut ui::Container, event: glutin::Event) { match event { - glfw::WindowEvent::Key(Key::GraveAccent, _, Action::Press, _) => { - game.console.lock().unwrap().toggle(); - } - glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) => { - window.set_should_close(true) + glutin::Event::Closed => game.should_close = true, + + glutin::Event::MouseMoved((x, y)) => { + game.mouse_pos = (x, y); + let (width, height) = window.get_inner_size_pixels().unwrap(); + + ui_container.hover_at(game, x as f64, y as f64, width as f64, height as f64); }, - glfw::WindowEvent::Key(key, _, Action::Press, _) => { + glutin::Event::MouseInput(glutin::ElementState::Released, glutin::MouseButton::Left) => { + let (x, y) = game.mouse_pos; + let (width, height) = window.get_inner_size_pixels().unwrap(); + + ui_container.click_at(game, x as f64, y as f64, width as f64, height as f64); + }, + + glutin::Event::MouseWheel(glutin::MouseScrollDelta::PixelDelta(x, y)) => { + game.screen_sys.on_scroll(x as f64, y as f64); + }, + + glutin::Event::KeyboardInput(glutin::ElementState::Pressed, 41 /* ` GRAVE */, _) => { + game.console.lock().unwrap().toggle(); + }, + + glutin::Event::KeyboardInput(glutin::ElementState::Pressed, key, _) => { println!("Key: {:?}", key); }, - glfw::WindowEvent::Scroll(x, y) => { - game.screen_sys.on_scroll(x, y); - }, - glfw::WindowEvent::MouseButton(glfw::MouseButton::Button1, Action::Release, _) => { - let (width, height) = window.get_size(); - let (xpos, ypos) = window.get_cursor_pos(); - let (fw, fh) = window.get_framebuffer_size(); - ui_container.click_at(game, xpos*((fw as f64)/(width as f64)), ypos*((fh as f64)/(height as f64)), fw as f64, fh as f64) - }, - glfw::WindowEvent::CursorPos(xpos, ypos) => { - let (width, height) = window.get_size(); - let (fw, fh) = window.get_framebuffer_size(); - ui_container.hover_at(game, xpos*((fw as f64)/(width as f64)), ypos*((fh as f64)/(height as f64)), fw as f64, fh as f64) - } - _ => {} + + _ => () } }