Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ noise = "0.7.0"
rand = "0.8.4"
parking_lot = "0.11.2"
rayon = "1.5.1"
rand_xoshiro = "0.6.0"
rand_xoshiro = "0.6.0"
548 changes: 364 additions & 184 deletions src/camera.rs

Large diffs are not rendered by default.

130 changes: 87 additions & 43 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,57 @@ extern crate glium;
mod camera;
use glium::{glutin, Surface};
extern crate stopwatch;
pub mod world;
pub mod player;
pub mod skybox;
pub mod skybox;
pub mod world;
use glutin::dpi::{LogicalPosition, Position};

extern crate rand_xoshiro;
use std::time::{SystemTime, UNIX_EPOCH};
use rand::{SeedableRng};
use rand::Rng;
use rand::SeedableRng;
use std::time::{SystemTime, UNIX_EPOCH};

//$Env:RUST_BACKTRACE=1
fn main() {
//Settings
const WINDOW_WIDTH: u32 = 1280; // For windowed only
const WINDOW_HEIGHT: u32 = 720; // For windowed only
const SQUARE_CHUNK_WIDTH: usize = 16; //Values can be: 4,6,10,16,22,28
const CHUNKS_LAYERS_FROM_PLAYER: usize = 53; //Odd numbers ONLYYY

const SQUARE_CHUNK_WIDTH: usize = 16; //Values can be: 4,6,10,16,22,28
const CHUNKS_LAYERS_FROM_PLAYER: usize = 53; //Odd numbers ONLYYY
const PLAYER_HEIGHT: f32 = 1.5;


let start = SystemTime::now();
let since_the_epoch = start
.duration_since(UNIX_EPOCH)
.expect("Time went backwards");

let mut rng = rand_xoshiro::SplitMix64::seed_from_u64((since_the_epoch.as_millis()) as u64);
// const WORLD_GEN_SEED: u32 = 60; //Any number
let WORLD_GEN_SEED: u32 = rng.gen_range(1..999999999);
let world_gen_seed: u32 = rng.gen_range(1..999999999);

const MID_HEIGHT: u8 = 24; //The terrain variation part
const SKY_HEIGHT: u8 = 4; //Works as a buffer for the mid heigt needs to be at least 20 percent of mid size
const UNDERGROUND_HEIGHT: u8 = 0;
const MID_HEIGHT: u8 = 24; //The terrain variation part
const SKY_HEIGHT: u8 = 4; //Works as a buffer for the mid heigt needs to be at least 20 percent of mid size
const UNDERGROUND_HEIGHT: u8 = 0;

const TIME_BETWEEN_FRAMES: u64 = 20;

let event_loop = glutin::event_loop::EventLoop::new();
let wb = glutin::window::WindowBuilder::new()
.with_title(format!("Minecraft RS"))
.with_inner_size(glutin::dpi::LogicalSize::new(WINDOW_WIDTH, WINDOW_HEIGHT));
.with_title(format!("Minecraft RS"))
.with_inner_size(glutin::dpi::LogicalSize::new(WINDOW_WIDTH, WINDOW_HEIGHT));
let cb = glutin::ContextBuilder::new();
let display = glium::Display::new(wb, cb, &event_loop).unwrap();


let monitor_handle = display.gl_window().window().available_monitors().next().unwrap();
let monitor_handle = display
.gl_window()
.window()
.available_monitors()
.next()
.unwrap();
let fs = glium::glutin::window::Fullscreen::Borderless(Some(monitor_handle));
display.gl_window().window().set_fullscreen(Some(fs));
display.gl_window().window().set_cursor_grab(true);
display.gl_window().window().set_cursor_grab(true).unwrap();
display.gl_window().window().set_cursor_visible(false);

let vertex_shader_block = r#"
Expand Down Expand Up @@ -93,30 +96,37 @@ fn main() {
}
"#;

let program_block = glium::Program::from_source(&display, vertex_shader_block, fragment_shader_block, None).unwrap();

let program_block =
glium::Program::from_source(&display, vertex_shader_block, fragment_shader_block, None)
.unwrap();

let camera_pos = [0.0, 0.0, 0.0];

let mut world = world::World::new(
&display,
camera_pos,
&SQUARE_CHUNK_WIDTH,
&CHUNKS_LAYERS_FROM_PLAYER,
&WORLD_GEN_SEED,
camera_pos,
&SQUARE_CHUNK_WIDTH,
&CHUNKS_LAYERS_FROM_PLAYER,
&world_gen_seed,
&MID_HEIGHT,
&UNDERGROUND_HEIGHT,
&SKY_HEIGHT,
);
let mut camera = camera::CameraState::new(&mut world, PLAYER_HEIGHT, camera_pos, WINDOW_WIDTH, WINDOW_HEIGHT);
let mut camera = camera::CameraState::new(
&mut world,
PLAYER_HEIGHT,
camera_pos,
WINDOW_WIDTH,
WINDOW_HEIGHT,
);
let skybox: skybox::Skybox = skybox::Skybox::new(&display);

let mut time_increment: f32 = 0.0;
let model = [
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0f32]
[0.0, 0.0, 0.0, 1.0f32],
];

let mut stopwatch = stopwatch::Stopwatch::new();
Expand All @@ -127,7 +137,7 @@ fn main() {
stopwatch.reset();
stopwatch.start();
match event {
glutin::event::Event::WindowEvent {event, .. } =>{
glutin::event::Event::WindowEvent { event, .. } => {
camera.process_input(&event, &mut world);

match event {
Expand All @@ -140,10 +150,10 @@ fn main() {
glutin::event::VirtualKeyCode::Escape => {
*control_flow = glutin::event_loop::ControlFlow::Exit;
return;
},
}
_ => (),
};
},
}
glutin::event::WindowEvent::CloseRequested => {
*control_flow = glutin::event_loop::ControlFlow::Exit;
return;
Expand All @@ -154,47 +164,81 @@ fn main() {
}
_ => (),
}
},
}
glutin::event::Event::NewEvents(cause) => match cause {
glutin::event::StartCause::ResumeTimeReached { .. } => (),
glutin::event::StartCause::Init => (),
_ => return,
},
glutin::event::Event::MainEventsCleared => {
let position = Position::Logical(LogicalPosition::new(camera.window_width as f64 / 2.0, camera.window_height as f64 / 2.0));
display.gl_window().window().set_cursor_position(position);
draw_frame(&display, &mut camera, &skybox, &mut world, &program_block, TIME_BETWEEN_FRAMES, &stopwatch, &mut time_increment, model);
},
let position = Position::Logical(LogicalPosition::new(
camera.window_width as f64 / 2.0,
camera.window_height as f64 / 2.0,
));
display
.gl_window()
.window()
.set_cursor_position(position)
.unwrap();
draw_frame(
&display,
&mut camera,
&skybox,
&mut world,
&program_block,
TIME_BETWEEN_FRAMES,
&stopwatch,
&mut time_increment,
model,
);
}
_ => return,
}
});
}

fn draw_frame(display: &glium::Display, camera: &mut camera::CameraState, skybox: &skybox::Skybox, world: &mut world::World, program_block: &glium::Program, frame_time: u64, stopwatch: &stopwatch::Stopwatch, time_increment: &mut f32, model: [[f32;4]; 4], ){
fn draw_frame(
display: &glium::Display,
camera: &mut camera::CameraState,
skybox: &skybox::Skybox,
world: &mut world::World,
program_block: &glium::Program,
frame_time: u64,
stopwatch: &stopwatch::Stopwatch,
time_increment: &mut f32,
model: [[f32; 4]; 4],
) {
camera.delta_time = time_increment.clone() - camera.last_frame;
camera.last_frame = time_increment.clone();

camera.update(world);
let view = camera.get_view();
let projection = camera.get_projection();

let mut target = display.draw();
target.clear_color_and_depth((0.0, 0.0, 1.0, 1.0), 1.0);

world.draw(&camera.camera_pos, view, projection, &mut target, &display, &program_block, model);

world.draw(
&camera.camera_pos,
view,
projection,
&mut target,
&display,
&program_block,
model,
);
skybox.draw(&mut target, &display, view, projection);

target.finish().unwrap();

*time_increment += 0.02;

world.render_loop();
loop{
loop {
if (stopwatch.elapsed_ms() as u64) < frame_time {
world.render_loop();
}else{

} else {
break;
}
}
}
}
Loading