Switch from glutin to sdl2

This commit is contained in:
Thinkofname 2016-03-25 13:15:35 +00:00
parent 3738c5a0c0
commit 9f04c09a8f
7 changed files with 120 additions and 373 deletions

319
Cargo.lock generated
View File

@ -2,17 +2,17 @@
name = "steven"
version = "0.0.1"
dependencies = [
"byteorder 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"byteorder 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"cgmath 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"collision 0.5.1 (git+https://github.com/csherratt/collision-rs?rev=f80825e)",
"flate2 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)",
"glutin 0.4.9 (git+https://github.com/tomaka/glutin?rev=8ae2df68e)",
"hyper 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"image 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)",
"sdl2 0.16.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"steven_gl 0.0.1",
@ -22,11 +22,6 @@ dependencies = [
"zip 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "android_glue"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "bitflags"
version = "0.3.3"
@ -44,7 +39,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "byteorder"
version = "0.5.0"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
@ -61,16 +56,7 @@ name = "bzip2-sys"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"gcc 0.3.25 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "cgl"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"gleam 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"gcc 0.3.26 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -84,17 +70,6 @@ dependencies = [
"rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "cocoa"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"core-graphics 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"objc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "collision"
version = "0.5.1"
@ -120,38 +95,6 @@ dependencies = [
"url 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "core-foundation"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"core-foundation-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "core-foundation-sys"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "core-graphics"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"core-foundation 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "crossbeam"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "deque"
version = "0.3.1"
@ -160,31 +103,6 @@ dependencies = [
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "dlib"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libloading 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "dwmapi-sys"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "dylib"
version = "0.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "enum_primitive"
version = "0.1.0"
@ -212,19 +130,9 @@ dependencies = [
"miniz-sys 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "fs2"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "gcc"
version = "0.3.25"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
@ -254,58 +162,11 @@ dependencies = [
"xml-rs 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "gl_generator"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"khronos_api 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"xml-rs 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "gleam"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"gl_generator 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"khronos_api 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "glob"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "glutin"
version = "0.4.9"
source = "git+https://github.com/tomaka/glutin?rev=8ae2df68e#8ae2df68e19ca71453af5e11de9ca807ef7adcb4"
dependencies = [
"android_glue 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"cgl 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"cocoa 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"core-foundation 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"core-graphics 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"dwmapi-sys 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"gdi32-sys 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"gl_generator 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"kernel32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"objc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"osmesa-sys 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
"shared_library 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"shell32-sys 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"user32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"wayland-client 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)",
"wayland-kbd 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"wayland-window 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
"x11-dl 2.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "hpack"
version = "0.2.0"
@ -399,16 +260,6 @@ name = "libc"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libloading"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "libressl-pnacl-sys"
version = "2.1.6"
@ -430,30 +281,11 @@ name = "lzw"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "malloc_buf"
version = "0.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "matches"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "memmap"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"fs2 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"kernel32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "mime"
version = "0.2.0"
@ -467,7 +299,7 @@ name = "miniz-sys"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"gcc 0.3.25 (registry+https://github.com/rust-lang/crates.io-index)",
"gcc 0.3.26 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -498,21 +330,13 @@ dependencies = [
"libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "objc"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"malloc_buf 0.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "openssl"
version = "0.7.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"gcc 0.3.25 (registry+https://github.com/rust-lang/crates.io-index)",
"gcc 0.3.26 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"openssl-sys 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)",
@ -536,20 +360,11 @@ name = "openssl-sys-extras"
version = "0.7.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"gcc 0.3.25 (registry+https://github.com/rust-lang/crates.io-index)",
"gcc 0.3.26 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"openssl-sys 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "osmesa-sys"
version = "0.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"shared_library 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "pkg-config"
version = "0.3.8"
@ -611,6 +426,27 @@ dependencies = [
"semver 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "sdl2"
version = "0.16.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"num 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
"sdl2-sys 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "sdl2-sys"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "semver"
version = "0.1.20"
@ -630,24 +466,6 @@ dependencies = [
"serde 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "shared_library"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "shell32-sys"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "solicit"
version = "0.4.4"
@ -686,17 +504,6 @@ dependencies = [
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "tempfile"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "time"
version = "0.1.34"
@ -768,57 +575,6 @@ dependencies = [
"rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "wayland-client"
version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"crossbeam 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"wayland-scanner 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)",
"wayland-sys 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "wayland-kbd"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"dlib 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
"memmap 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"wayland-client 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "wayland-scanner"
version = "0.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"xml-rs 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "wayland-sys"
version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"dlib 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "wayland-window"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"byteorder 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"tempfile 1.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"wayland-client 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "winapi"
version = "0.2.6"
@ -829,15 +585,6 @@ name = "winapi-build"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "x11-dl"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"dylib 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "xml-rs"
version = "0.2.2"
@ -846,14 +593,6 @@ dependencies = [
"bitflags 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "xml-rs"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "zip"
version = "0.1.16"

View File

@ -4,7 +4,7 @@ version = "0.0.1"
authors = [ "Thinkofdeath <thinkofdeath@spigotmc.org>" ]
[dependencies]
glutin = {git = "https://github.com/tomaka/glutin", rev = "8ae2df68e"}
sdl2 = "0.16.1"
byteorder = "0.5.0"
hyper = "0.8.0"
serde = "0.7.0"

View File

@ -13,7 +13,7 @@
// limitations under the License.
extern crate steven_gl as gl;
use glutin;
use sdl2;
use std::ops::BitOr;
use std::ffi;
@ -22,8 +22,8 @@ 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 glutin::Window) {
gl::load_with(|s| window.get_proc_address(s) as *const _);
pub fn init(vid: & sdl2::VideoSubsystem) {
gl::load_with(|s| vid.gl_get_proc_address(s) as *const _);
}
/// Dsed to specify how the vertices will be handled

View File

@ -15,7 +15,7 @@
#![recursion_limit="200"]
#![feature(const_fn)]
extern crate glutin;
extern crate sdl2;
extern crate image;
extern crate time;
extern crate byteorder;
@ -75,7 +75,6 @@ pub struct Game {
resource_manager: Arc<RwLock<resources::Manager>>,
console: Arc<Mutex<console::Console>>,
should_close: bool,
mouse_pos: (i32, i32),
server: server::Server,
focused: bool,
@ -159,22 +158,27 @@ fn main() {
resource_manager.write().unwrap().tick();
}
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()
.expect("Could not create Glutin window.");
let sdl = sdl2::init().unwrap();
let sdl_video = sdl.video().unwrap();
unsafe {
window.make_current().expect("Could not set current context.");
}
sdl_video.gl_set_swap_interval(1);
gl::init(&mut window);
let window = sdl2::video::WindowBuilder::new(&sdl_video, "Steven", 854, 480)
.opengl()
.resizable()
.build()
.expect("Could not create sdl window.");
let gl_attr = sdl_video.gl_attr();
gl_attr.set_stencil_size(0);
gl_attr.set_depth_size(24);
gl_attr.set_context_major_version(3);
gl_attr.set_context_minor_version(2);
gl_attr.set_context_profile(sdl2::video::GLProfile::Core);
let gl_context = window.gl_create_context().unwrap();
window.gl_make_current(&gl_context).expect("Could not set current context.");
gl::init(&sdl_video);
let renderer = render::Renderer::new(resource_manager.clone());
let mut ui_container = ui::Container::new();
@ -194,11 +198,11 @@ fn main() {
resource_manager: resource_manager.clone(),
console: con,
should_close: false,
mouse_pos: (0, 0),
chunk_builder: chunk_builder::ChunkBuilder::new(resource_manager, textures),
connect_reply: None,
};
let mut events = sdl.event_pump().unwrap();
while !game.should_close {
{
game.resource_manager.write().unwrap().tick();
@ -208,7 +212,7 @@ fn main() {
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();
let (width, height) = window.size();
game.tick(delta);
game.server.tick(&mut game.renderer, delta);
@ -225,31 +229,36 @@ fn main() {
ui_container.tick(&mut game.renderer, delta, width as f64, height as f64);
game.renderer.tick(&mut game.server.world, delta, width, height);
let _ = window.swap_buffers();
window.gl_swap_window();
for event in window.poll_events() {
for event in events.poll_iter() {
handle_window_event(&window, &mut game, &mut ui_container, event)
}
}
}
fn handle_window_event(window: &glutin::Window,
fn handle_window_event(window: &sdl2::video::Window,
game: &mut Game,
ui_container: &mut ui::Container,
event: glutin::Event) {
use glutin::{Event, VirtualKeyCode};
event: sdl2::event::Event) {
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::mouse::Mouse;
use std::f64::consts::PI;
match event {
Event::Closed => game.should_close = true,
Event::MouseMoved((x, y)) => {
game.mouse_pos = (x, y);
let (width, height) = window.get_inner_size_pixels().unwrap();
let mouse = window.subsystem().sdl().mouse();
match event {
Event::Quit{..} => game.should_close = true,
Event::MouseMotion{x, y, xrel, yrel, ..} => {
let (width, height) = window.size();
if game.focused {
window.set_cursor_state(glutin::CursorState::Hide).unwrap();
window.set_cursor_position((width/2) as i32, (height/2) as i32).unwrap();
if !mouse.relative_mouse_mode() {
mouse.set_relative_mouse_mode(true);
}
let s = 2000.0 + 0.01;
let (rx, ry) = ((x-(width/2) as i32) as f64 / s, (y-(height/2) as i32) as f64 / s);
let (rx, ry) = (xrel as f64 / s, yrel as f64 / s);
game.server.yaw -= rx;
game.server.pitch -= ry;
if game.server.pitch < (PI/2.0) + 0.01 {
@ -262,49 +271,50 @@ fn handle_window_event(window: &glutin::Window,
ui_container.hover_at(game, x as f64, y as f64, width as f64, height as f64);
}
}
Event::MouseInput(glutin::ElementState::Released, glutin::MouseButton::Left) => {
let (x, y) = game.mouse_pos;
let (width, height) = window.get_inner_size_pixels().unwrap();
Event::MouseButtonUp{mouse_btn: Mouse::Left, x, y, ..} => {
let (width, height) = window.size();
if game.server.is_connected() && !game.focused {
game.focused = true;
window.set_cursor_state(glutin::CursorState::Hide).unwrap();
window.set_cursor_position((width/2) as i32, (height/2) as i32).unwrap();
mouse.set_relative_mouse_mode(true);
mouse.warp_mouse_in_window(&window, (width/2) as i32, (height/2) as i32);
return;
}
if !game.focused {
ui_container.click_at(game, x as f64, y as f64, width as f64, height as f64);
}
}
Event::MouseWheel(delta, _) => {
let (x, y) = match delta {
glutin::MouseScrollDelta::LineDelta(x, y) => (x, y),
glutin::MouseScrollDelta::PixelDelta(x, y) => (x, y),
};
Event::MouseWheel{x, y, ..} => {
game.screen_sys.on_scroll(x as f64, y as f64);
}
Event::KeyboardInput(glutin::ElementState::Released, _, Some(VirtualKeyCode::Escape)) => {
Event::KeyUp{keycode: Some(Keycode::Escape), ..} => {
if game.focused {
window.set_cursor_state(glutin::CursorState::Normal).unwrap();
mouse.set_relative_mouse_mode(false);
game.focused = false;
}
}
Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(VirtualKeyCode::Grave)) => {
Event::KeyDown{keycode: Some(Keycode::Backquote), ..} => {
game.console.lock().unwrap().toggle();
}
Event::KeyboardInput(state, key, virt) => {
Event::KeyDown{keycode: Some(key), ..} => {
if game.focused {
if let Some(virt) = virt {
game.server.key_press(state == glutin::ElementState::Pressed, virt);
}
game.server.key_press(true, key);
} else {
ui_container.key_press(game, virt, key, state == glutin::ElementState::Pressed);
ui_container.key_press(game, key, true);
}
}
Event::ReceivedCharacter(c) => {
Event::KeyUp{keycode: Some(key), ..} => {
if game.focused {
game.server.key_press(false, key);
} else {
ui_container.key_press(game, key, false);
}
}
Event::TextInput{text, ..} => {
if !game.focused {
ui_container.key_type(game, c);
for c in text.chars() {
ui_container.key_type(game, c);
}
}
}
_ => (),

View File

@ -26,7 +26,7 @@ use console;
use render;
use auth;
use cgmath::{self, Vector};
use glutin::VirtualKeyCode;
use sdl2::keyboard::Keycode;
pub struct Server {
conn: Option<protocol::Conn>,
@ -41,7 +41,7 @@ pub struct Server {
pub yaw: f64,
pub pitch: f64,
pressed_keys: HashMap<VirtualKeyCode, bool>,
pressed_keys: HashMap<Keycode, bool>,
tick_timer: f64,
}
@ -224,16 +224,16 @@ impl Server {
if self.world.is_chunk_loaded((self.position.x as i32) >> 4, (self.position.z as i32) >> 4) {
let mut speed = 4.317 / 60.0;
if self.is_key_pressed(VirtualKeyCode::LShift) {
if self.is_key_pressed(Keycode::LShift) {
speed = 5.612 / 60.0;
}
// TODO: only do this for flying
speed *= 2.5;
if self.is_key_pressed(VirtualKeyCode::Space) {
if self.is_key_pressed(Keycode::Space) {
self.position.y += speed * delta;
}
if self.is_key_pressed(VirtualKeyCode::LControl) {
if self.is_key_pressed(Keycode::LCtrl) {
self.position.y -= speed * delta;
}
self.position.x += forward * yaw.cos() * delta * speed;
@ -256,23 +256,23 @@ impl Server {
use std::f64::consts::PI;
let mut forward = 0.0f64;
let mut yaw = self.yaw - (PI/2.0);
if self.is_key_pressed(VirtualKeyCode::W) || self.is_key_pressed(VirtualKeyCode::S) {
if self.is_key_pressed(Keycode::W) || self.is_key_pressed(Keycode::S) {
forward = 1.0;
if self.is_key_pressed(VirtualKeyCode::S) {
if self.is_key_pressed(Keycode::S) {
yaw += PI;
}
}
let mut change = 0.0;
if self.is_key_pressed(VirtualKeyCode::A) {
if self.is_key_pressed(Keycode::A) {
change = (PI / 2.0) / (forward.abs() + 1.0);
}
if self.is_key_pressed(VirtualKeyCode::D) {
if self.is_key_pressed(Keycode::D) {
change = -(PI / 2.0) / (forward.abs() + 1.0);
}
if self.is_key_pressed(VirtualKeyCode::A) || self.is_key_pressed(VirtualKeyCode::D) {
if self.is_key_pressed(Keycode::A) || self.is_key_pressed(Keycode::D) {
forward = 1.0;
}
if self.is_key_pressed(VirtualKeyCode::S) {
if self.is_key_pressed(Keycode::S) {
yaw -= change;
} else {
yaw += change;
@ -295,11 +295,11 @@ impl Server {
self.write_packet(packet);
}
pub fn key_press(&mut self, down: bool, key: VirtualKeyCode) {
pub fn key_press(&mut self, down: bool, key: Keycode) {
self.pressed_keys.insert(key, down);
}
fn is_key_pressed(&self, key: VirtualKeyCode) -> bool {
fn is_key_pressed(&self, key: Keycode) -> bool {
self.pressed_keys.get(&key).map(|v| *v).unwrap_or(false)
}

View File

@ -20,7 +20,7 @@ use std::rc::Rc;
use rand;
use render;
use format;
use glutin::VirtualKeyCode;
use sdl2::keyboard::Keycode;
const SCALED_WIDTH: f64 = 854.0;
const SCALED_HEIGHT: f64 = 480.0;
@ -68,10 +68,10 @@ impl Element {
}
}
fn key_press(&mut self, game: &mut ::Game, key: Option<VirtualKeyCode>, raw: u8, down: bool) -> Vec<Rc<ClickFunc>> {
fn key_press(&mut self, game: &mut ::Game, key: Keycode, down: bool) -> Vec<Rc<ClickFunc>> {
match *self {
$(
Element::$name(ref mut val) => val.key_press(game, key, raw, down),
Element::$name(ref mut val) => val.key_press(game, key, down),
)+
_ => unimplemented!(),
}
@ -489,8 +489,8 @@ impl Container {
}
}
pub fn key_press(&mut self, game: &mut ::Game, key: Option<VirtualKeyCode>, raw: u8, down: bool) {
if key == Some(VirtualKeyCode::Tab) {
pub fn key_press(&mut self, game: &mut ::Game, key: Keycode, down: bool) {
if key == Keycode::Tab {
if !down {
self.cycle_focus();
}
@ -499,7 +499,7 @@ impl Container {
let mut callbacks = None;
for (_, e) in &mut self.elements {
if e.is_focused() {
callbacks = Some(e.key_press(game, key, raw, down));
callbacks = Some(e.key_press(game, key, down));
break;
}
}
@ -607,7 +607,7 @@ pub trait UIElement {
fn unwrap_ref(&Element) -> &Self;
fn unwrap_ref_mut(&mut Element) -> &mut Self;
fn key_press(&mut self, _game: &mut ::Game, _key: Option<VirtualKeyCode>, _raw: u8, _down: bool) -> Vec<Rc<ClickFunc>> {
fn key_press(&mut self, _game: &mut ::Game, _key: Keycode, _down: bool) -> Vec<Rc<ClickFunc>> {
vec![]
}

View File

@ -149,13 +149,11 @@ impl TextBox {
impl UIElement for TextBox {
fn key_press(&mut self, _game: &mut ::Game, key: Option<VirtualKeyCode>, _raw: u8, down: bool) -> Vec<Rc<ClickFunc>> {
if let Some(key) = key {
match (key, down) {
(VirtualKeyCode::Back, false) => {self.input.pop();},
(VirtualKeyCode::Return, false) => return self.submit_funcs.clone(),
_ => {},
}
fn key_press(&mut self, _game: &mut ::Game, key: Keycode, down: bool) -> Vec<Rc<ClickFunc>> {
match (key, down) {
(Keycode::Backspace, false) => {self.input.pop();},
(Keycode::Return, false) => return self.submit_funcs.clone(),
_ => {},
}
vec![]
}