stevenarella/src/main.rs

192 lines
5.9 KiB
Rust
Raw Normal View History

2016-03-16 14:25:35 -04:00
// Copyright 2016 Matthew Collins
2015-09-17 11:21:56 -04:00
//
// 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-28 18:37:14 -04:00
#[macro_use]
pub mod console;
2015-09-17 11:04:25 -04:00
2015-10-01 15:07:27 -04:00
extern crate glutin;
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;
2016-03-16 13:33:06 -04:00
extern crate cgmath;
2015-09-29 17:33:24 -04:00
#[macro_use]
extern crate log;
2015-09-07 16:11:00 -04:00
2015-09-29 17:33:24 -04:00
use std::sync::{Arc, RwLock, Mutex};
2015-09-28 18:37:14 -04:00
use std::marker::PhantomData;
2015-09-07 16:11:00 -04:00
2015-09-28 18:37:14 -04:00
const CL_BRAND: console::CVar<String> = console::CVar {
ty: PhantomData,
name: "cl_brand",
2015-10-07 14:36:59 -04:00
description: "cl_brand has the value of the clients current 'brand'. e.g. \"Steven\" or \
\"Vanilla\"",
2015-09-28 18:37:14 -04:00
mutable: false,
2015-10-01 15:07:27 -04:00
serializable: false,
2016-03-16 15:11:50 -04:00
default: &|| "Steven".to_owned(),
2015-09-28 18:37:14 -04:00
};
pub struct Game {
renderer: render::Renderer,
screen_sys: screen::ScreenSystem,
resource_manager: Arc<RwLock<resources::Manager>>,
2015-09-29 17:33:24 -04:00
console: Arc<Mutex<console::Console>>,
2015-10-01 15:07:27 -04:00
should_close: bool,
mouse_pos: (i32, i32),
2015-09-28 18:37:14 -04:00
}
2015-09-07 16:11:00 -04:00
fn main() {
2015-09-29 17:33:24 -04:00
let con = Arc::new(Mutex::new(console::Console::new()));
2015-09-29 18:24:58 -04:00
{
let mut con = con.lock().unwrap();
con.register(CL_BRAND);
con.load_config();
con.save_config();
}
2015-10-01 15:07:27 -04:00
2015-09-29 17:33:24 -04:00
let proxy = console::ConsoleProxy::new(con.clone());
2015-10-01 15:07:27 -04:00
2015-09-29 17:33:24 -04:00
log::set_logger(|max_log_level| {
max_log_level.set(log::LogLevelFilter::Trace);
Box::new(proxy)
2015-10-07 14:36:59 -04:00
})
.unwrap();
2015-09-29 17:33:24 -04:00
info!("Starting steven");
2015-09-28 18:37:14 -04:00
2015-09-17 11:04:25 -04:00
let resource_manager = Arc::new(RwLock::new(resources::Manager::new()));
2015-10-07 14:36:59 -04:00
{
resource_manager.write().unwrap().tick();
}
2015-09-17 11:04:25 -04:00
2015-10-01 15:07:27 -04:00
let mut window = glutin::WindowBuilder::new()
2015-10-07 14:36:59 -04:00
.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.");
2015-10-01 15:07:27 -04:00
unsafe {
2016-03-16 14:22:03 -04:00
window.make_current().expect("Could not set current context.");
2015-10-01 15:07:27 -04:00
}
2015-09-07 16:11:00 -04:00
gl::init(&mut window);
2015-09-28 18:37:14 -04:00
let 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-28 18:37:14 -04:00
let mut game = Game {
renderer: renderer,
screen_sys: screen_sys,
resource_manager: resource_manager,
console: con,
2015-10-01 15:07:27 -04:00
should_close: false,
mouse_pos: (0, 0),
2015-09-28 18:37:14 -04:00
};
2015-10-01 15:07:27 -04:00
while !game.should_close {
2015-10-07 14:36:59 -04:00
{
game.resource_manager.write().unwrap().tick();
}
2015-10-01 15:07:27 -04:00
2015-09-17 11:04:25 -04:00
let now = time::now();
let diff = now - last_frame;
last_frame = now;
let delta = (diff.num_nanoseconds().unwrap() as f64) / frame_time;
2015-10-01 15:07:27 -04:00
let (width, height) = window.get_inner_size_pixels().unwrap();
2015-09-17 11:04:25 -04:00
2015-09-28 18:37:14 -04:00
game.screen_sys.tick(delta, &mut game.renderer, &mut ui_container);
2015-10-07 14:36:59 -04:00
game.console
.lock()
.unwrap()
.tick(&mut ui_container, &mut game.renderer, delta, width as f64);
2015-09-28 18:37:14 -04:00
ui_container.tick(&mut game.renderer, delta, width as f64, height as f64);
2015-10-01 15:07:27 -04:00
game.renderer.tick(delta, width, height);
let _ = window.swap_buffers();
2015-09-07 16:11:00 -04:00
2015-10-01 15:07:27 -04:00
for event in window.poll_events() {
handle_window_event(&window, &mut game, &mut ui_container, event)
2015-09-07 16:11:00 -04:00
}
}
}
2015-10-07 14:36:59 -04:00
fn handle_window_event(window: &glutin::Window,
game: &mut Game,
ui_container: &mut ui::Container,
event: glutin::Event) {
2015-10-06 19:10:59 -04:00
use glutin::{Event, VirtualKeyCode};
2015-09-07 16:11:00 -04:00
match event {
2015-10-06 19:10:59 -04:00
Event::Closed => game.should_close = true,
2015-10-01 15:07:27 -04:00
2015-10-06 19:10:59 -04:00
Event::MouseMoved((x, y)) => {
2015-10-01 15:07:27 -04:00
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);
2015-10-07 14:36:59 -04:00
}
2015-09-29 17:33:24 -04:00
2015-10-06 19:10:59 -04:00
Event::MouseInput(glutin::ElementState::Released, glutin::MouseButton::Left) => {
2015-10-01 15:07:27 -04:00
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);
2015-10-07 14:36:59 -04:00
}
2015-10-01 15:07:27 -04:00
2015-10-06 19:10:59 -04:00
Event::MouseWheel(delta) => {
2015-10-01 16:50:37 -04:00
let (x, y) = match delta {
glutin::MouseScrollDelta::LineDelta(x, y) => (x, y),
2015-10-07 14:36:59 -04:00
glutin::MouseScrollDelta::PixelDelta(x, y) => (x, y),
2015-10-01 16:50:37 -04:00
};
2015-10-01 15:07:27 -04:00
game.screen_sys.on_scroll(x as f64, y as f64);
2015-10-07 14:36:59 -04:00
}
2015-10-01 15:07:27 -04:00
2016-03-16 14:22:03 -04:00
Event::KeyboardInput(glutin::ElementState::Pressed, 41 /* ` GRAVE */, _)
| Event::KeyboardInput(glutin::ElementState::Pressed, _, Some(VirtualKeyCode::Grave)) => {
2015-10-06 19:10:59 -04:00
game.console.lock().unwrap().toggle();
2015-10-07 14:36:59 -04:00
}
2015-10-06 19:10:59 -04:00
Event::KeyboardInput(glutin::ElementState::Pressed, key, virt) => {
println!("Key: {:?} {:?}", key, virt);
2015-10-07 14:36:59 -04:00
}
2015-10-01 15:07:27 -04:00
2015-10-07 14:36:59 -04:00
_ => (),
2015-09-07 16:11:00 -04:00
}
}