Switch from GLFW to Glutin

This commit is contained in:
Scetch 2015-10-01 15:07:27 -04:00 committed by Thinkofdeath
parent 82c3235de1
commit 670f1eb8e7
3 changed files with 61 additions and 67 deletions

View File

@ -5,7 +5,7 @@ authors = [ "Thinkofdeath <thinkofdeath@spigotmc.org>" ]
build = "build.rs" build = "build.rs"
[dependencies] [dependencies]
glfw = "0.1.0" glutin = "0.3.7"
byteorder = "0.3.13" byteorder = "0.3.13"
hyper = "0.6.13" hyper = "0.6.13"
serde = "0.6.0" serde = "0.6.0"
@ -25,13 +25,3 @@ version = "0"
[dependencies.steven_openssl] [dependencies.steven_openssl]
path = "./openssl" path = "./openssl"
version = "0" 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 }

View File

@ -13,7 +13,7 @@
// limitations under the License. // limitations under the License.
extern crate steven_gl as gl; extern crate steven_gl as gl;
extern crate glfw; use glutin;
use std::ops::BitOr; use std::ops::BitOr;
use std::ffi; use std::ffi;
@ -22,7 +22,7 @@ use std::ptr;
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
/// Inits the gl library. This should be called once a context is ready. /// 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)); gl::load_with(|s| window.get_proc_address(s));
} }
@ -319,7 +319,7 @@ impl Shader {
let len = self.get_parameter(INFO_LOG_LENGTH); let len = self.get_parameter(INFO_LOG_LENGTH);
let mut data = Vec::<u8>::with_capacity(len as usize); let mut data = Vec::<u8>::with_capacity(len as usize);
unsafe { unsafe {
data.set_len(len as usize); data.set_len(len as usize);
gl::GetShaderInfoLog(self.0, len, ptr::null_mut(), data.as_mut_ptr() as *mut i8); 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) { fn drop(&mut self) {
unsafe { gl::DeleteVertexArrays(1, &self.0); } unsafe { gl::DeleteVertexArrays(1, &self.0); }
self.0 = 0; self.0 = 0;
@ -455,7 +455,7 @@ pub const WRITE_ONLY: Access = gl::WRITE_ONLY;
pub struct Buffer(u32); pub struct Buffer(u32);
impl Buffer { impl Buffer {
/// Allocates a new Buffer. /// Allocates a new Buffer.
pub fn new() -> Buffer { pub fn new() -> Buffer {
let mut b = Buffer(0); let mut b = Buffer(0);
unsafe { gl::GenBuffers(1, &mut b.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. /// 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 /// This will allow it to be the source of operations that act on a buffer
/// (Data, Map etc). /// (Data, Map etc).
pub fn bind(&self, target: BufferTarget) { pub fn bind(&self, target: BufferTarget) {
unsafe { gl::BindBuffer(target, self.0); } unsafe { gl::BindBuffer(target, self.0); }
} }

View File

@ -25,7 +25,7 @@ pub mod screen;
#[macro_use] #[macro_use]
pub mod console; pub mod console;
extern crate glfw; extern crate glutin;
extern crate image; extern crate image;
extern crate time; extern crate time;
extern crate byteorder; extern crate byteorder;
@ -40,7 +40,6 @@ extern crate log;
use std::sync::{Arc, RwLock, Mutex}; use std::sync::{Arc, RwLock, Mutex};
use std::marker::PhantomData; use std::marker::PhantomData;
use glfw::{Action, Context, Key};
const CL_BRAND: console::CVar<String> = console::CVar { const CL_BRAND: console::CVar<String> = console::CVar {
ty: PhantomData, ty: PhantomData,
@ -48,7 +47,7 @@ const CL_BRAND: console::CVar<String> = console::CVar {
description: "cl_brand has the value of the clients current 'brand'. \ description: "cl_brand has the value of the clients current 'brand'. \
e.g. \"Steven\" or \"Vanilla\"", e.g. \"Steven\" or \"Vanilla\"",
mutable: false, mutable: false,
serializable: false, serializable: false,
default: &|| "steven".to_owned(), default: &|| "steven".to_owned(),
}; };
@ -57,6 +56,8 @@ pub struct Game {
screen_sys: screen::ScreenSystem, screen_sys: screen::ScreenSystem,
resource_manager: Arc<RwLock<resources::Manager>>, resource_manager: Arc<RwLock<resources::Manager>>,
console: Arc<Mutex<console::Console>>, console: Arc<Mutex<console::Console>>,
should_close: bool,
mouse_pos: (i32, i32),
} }
fn main() { fn main() {
@ -67,7 +68,9 @@ fn main() {
con.load_config(); con.load_config();
con.save_config(); con.save_config();
} }
let proxy = console::ConsoleProxy::new(con.clone()); let proxy = console::ConsoleProxy::new(con.clone());
log::set_logger(|max_log_level| { log::set_logger(|max_log_level| {
max_log_level.set(log::LogLevelFilter::Trace); max_log_level.set(log::LogLevelFilter::Trace);
Box::new(proxy) Box::new(proxy)
@ -78,27 +81,22 @@ fn main() {
let resource_manager = Arc::new(RwLock::new(resources::Manager::new())); let resource_manager = Arc::new(RwLock::new(resources::Manager::new()));
{ resource_manager.write().unwrap().tick(); } { 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)); unsafe {
glfw.window_hint(glfw::WindowHint::OpenGlProfile(glfw::OpenGlProfileHint::Core)); window.make_current().ok().expect("Could not set current context.");
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");
gl::init(&mut window); 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 renderer = render::Renderer::new(resource_manager.clone());
let mut ui_container = ui::Container::new(); let mut ui_container = ui::Container::new();
@ -113,56 +111,62 @@ fn main() {
screen_sys: screen_sys, screen_sys: screen_sys,
resource_manager: resource_manager, resource_manager: resource_manager,
console: con, console: con,
should_close: false,
mouse_pos: (0, 0),
}; };
while !window.should_close() { while !game.should_close {
{ game.resource_manager.write().unwrap().tick(); } { game.resource_manager.write().unwrap().tick(); }
let now = time::now(); let now = time::now();
let diff = now - last_frame; let diff = now - last_frame;
last_frame = now; last_frame = now;
let delta = (diff.num_nanoseconds().unwrap() as f64) / frame_time; 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); 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); 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); 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(); let _ = window.swap_buffers();
glfw.poll_events();
for (_, event) in glfw::flush_messages(&events) { for event in window.poll_events() {
handle_window_event(&mut window, &mut game, &mut ui_container, event); 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 { match event {
glfw::WindowEvent::Key(Key::GraveAccent, _, Action::Press, _) => { glutin::Event::Closed => game.should_close = true,
game.console.lock().unwrap().toggle();
} glutin::Event::MouseMoved((x, y)) => {
glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) => { game.mouse_pos = (x, y);
window.set_should_close(true) 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); 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)
}
_ => {}
} }
} }