stevenarella/src/main.rs

122 lines
4.2 KiB
Rust
Raw Normal View History

2015-09-17 11:21:56 -04:00
// Copyright 2015 Matthew Collins
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2015-09-17 11:04:25 -04:00
pub mod protocol;
pub mod format;
pub mod nbt;
pub mod item;
pub mod gl;
pub mod types;
pub mod resources;
pub mod render;
2015-09-18 17:02:08 -04:00
pub mod ui;
2015-09-23 15:16:25 -04:00
pub mod screen;
2015-09-17 11:04:25 -04:00
2015-09-07 16:11:00 -04:00
extern crate glfw;
2015-09-17 11:04:25 -04:00
extern crate image;
extern crate time;
extern crate byteorder;
extern crate serde_json;
extern crate steven_openssl as openssl;
extern crate hyper;
extern crate flate2;
2015-09-18 17:02:08 -04:00
extern crate rand;
2015-09-25 09:00:49 -04:00
extern crate rustc_serialize;
2015-09-07 16:11:00 -04:00
2015-09-17 11:04:25 -04:00
use std::sync::{Arc, RwLock};
2015-09-07 16:11:00 -04:00
use glfw::{Action, Context, Key};
fn main() {
2015-09-17 11:04:25 -04:00
let resource_manager = Arc::new(RwLock::new(resources::Manager::new()));
{ resource_manager.write().unwrap().tick(); }
2015-09-07 16:11:00 -04:00
let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap();
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");
gl::init(&mut window);
window.set_key_polling(true);
2015-09-23 15:16:25 -04:00
window.set_scroll_polling(true);
2015-09-25 09:00:49 -04:00
window.set_mouse_button_polling(true);
window.set_cursor_pos_polling(true);
2015-09-07 16:11:00 -04:00
window.make_current();
glfw.set_swap_interval(1);
2015-09-17 11:04:25 -04:00
let mut renderer = render::Renderer::new(resource_manager.clone());
2015-09-18 17:02:08 -04:00
let mut ui_container = ui::Container::new();
2015-09-17 11:04:25 -04:00
let mut last_frame = time::now();
let frame_time = (time::Duration::seconds(1).num_nanoseconds().unwrap() as f64) / 60.0;
2015-09-23 15:16:25 -04:00
let mut screen_sys = screen::ScreenSystem::new();
2015-09-27 14:38:58 -04:00
screen_sys.add_screen(Box::new(screen::Login::new()));
2015-09-19 14:08:28 -04:00
2015-09-07 16:11:00 -04:00
while !window.should_close() {
2015-09-17 11:04:25 -04:00
{ 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;
2015-09-23 15:16:25 -04:00
screen_sys.tick(delta, &mut renderer, &mut ui_container);
2015-09-21 16:11:30 -04:00
2015-09-17 11:04:25 -04:00
let (width, height) = window.get_framebuffer_size();
2015-09-18 17:02:08 -04:00
ui_container.tick(&mut renderer, delta, width as f64, height as f64);
2015-09-17 11:04:25 -04:00
renderer.tick(delta, width as u32, height as u32);
2015-09-07 16:11:00 -04:00
window.swap_buffers();
glfw.poll_events();
for (_, event) in glfw::flush_messages(&events) {
2015-09-25 09:00:49 -04:00
handle_window_event(&mut window, &mut renderer, &mut screen_sys, &mut ui_container, event);
2015-09-07 16:11:00 -04:00
}
}
}
2015-09-25 09:00:49 -04:00
fn handle_window_event(
window: &mut glfw::Window,
renderer: &mut render::Renderer,
screen_sys: &mut screen::ScreenSystem,
ui_container: &mut ui::Container,
event: glfw::WindowEvent
) {
2015-09-07 16:11:00 -04:00
match event {
glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) => {
window.set_should_close(true)
2015-09-23 15:16:25 -04:00
},
glfw::WindowEvent::Scroll(x, y) => {
screen_sys.on_scroll(x, y);
},
2015-09-25 09:00:49 -04:00
glfw::WindowEvent::MouseButton(glfw::MouseButton::Button1, Action::Press, _) => {
let (width, height) = window.get_size();
let (xpos, ypos) = window.get_cursor_pos();
let (fw, fh) = window.get_framebuffer_size();
2015-09-25 09:48:35 -04:00
ui_container.click_at(screen_sys, renderer, xpos*((fw as f64)/(width as f64)), ypos*((fh as f64)/(height as f64)), fw as f64, fh as f64)
2015-09-25 09:00:49 -04:00
},
glfw::WindowEvent::CursorPos(xpos, ypos) => {
let (width, height) = window.get_size();
let (fw, fh) = window.get_framebuffer_size();
2015-09-25 09:48:35 -04:00
ui_container.hover_at(screen_sys, renderer, xpos*((fw as f64)/(width as f64)), ypos*((fh as f64)/(height as f64)), fw as f64, fh as f64)
2015-09-25 09:00:49 -04:00
}
2015-09-07 16:11:00 -04:00
_ => {}
}
}