Skip to content

Commit

Permalink
Remove Quack from Cargo.toml
Browse files Browse the repository at this point in the history
[WIP] Fix #215 - Remove quack library usage

Not currently working, but progress has been made:
* cargo update
* flate (deprecated) -> flate2
* os::MemoryMap -> mmap
* rustc-serialize 0.2.8 -> 0.3.x
* range(a, b) -> a..b
* old_io -> io
* Other misc changes.

Add Mapper associated type.

Latest Changes.

Revert some trait changes.

This removes some of the changes I made while trying to understand what was going on. In its current form, the code is certianly *not* correct, but it's more correct than it was (BlockStates<R> vs BlockStates for example).

Fix misc errors
Thanks @eddyb!

Fix AsRawFd, WindowSettings builder pattern.

Fix window use.  Fix pub/mut in several places. Add NbtReaderError type for NbtReader to simplify errorr handling.

Fix all warnings and errors

Make changes as suggested by @eddyb

Linux build works now.

Fix Windows build.

Changes to the retep998 branch of mmap and adds libc from crates.io to deal with rust-lang/rust#24450.

Change Frame -> Output

Remove assets zip (oops)

Remove superfluous map.
  • Loading branch information
Matt Ickstadt authored and mattico committed May 13, 2015
1 parent bb70aa9 commit fd8afbe
Show file tree
Hide file tree
Showing 10 changed files with 419 additions and 308 deletions.
365 changes: 211 additions & 154 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ name = "hematite"
path = "src/main.rs"

[dependencies]
byteorder = "0.3"
flate2 = "0.2"
libc = "*"
rustc-serialize = "0.3"
time = "*"
rustc-serialize = "0.2.8"

[dependencies.mmap]
git = "https://github.com/retep998/rust-mmap"

[dependencies.sdl2]
git = "https://github.com/AngryLawyer/rust-sdl2"
Expand Down Expand Up @@ -60,5 +66,3 @@ git = "https://github.com/gfx-rs/gfx_device_gl"
[dependencies.shader_version]
git = "https://github.com/PistonDevelopers/shader_version"

[dependencies.quack]
git = "https://github.com/PistonDevelopers/quack"
8 changes: 4 additions & 4 deletions src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ use array::*;
use shader::Buffer;
use gfx;

#[derive(Copy)]
#[derive(Copy, Clone)]
pub struct BlockState {
pub value: u16
}

pub const EMPTY_BLOCK: BlockState = BlockState { value: 0 };

#[derive(Copy)]
#[derive(Copy, Clone)]
pub struct BiomeId {
pub value: u8
}

#[derive(Copy)]
#[derive(Copy, Clone)]
pub struct LightLevel {
pub value: u8
}
Expand Down Expand Up @@ -87,7 +87,7 @@ impl<R: gfx::Resources> ChunkManager<R> {
)
);
let central = columns[1][1].unwrap();
for y in range(0, central.chunks.len()) {
for y in 0..central.chunks.len() {
let chunks = [-1, 0, 1].map(|dy| {
let y = y as i32 + dy;
columns.map(
Expand Down
79 changes: 41 additions & 38 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
#![feature(box_syntax, collections, core, custom_attribute, old_io,
old_path, os, plugin, rustc_private, std_misc)]
#![feature(box_syntax, collections, convert, core,
custom_attribute, plugin, slice_patterns)]
#![plugin(gfx_macros)]

extern crate byteorder;
extern crate camera_controllers;
extern crate event;
extern crate flate;
extern crate flate2;
extern crate fps_counter;
extern crate gfx;
extern crate gfx_device_gl;
extern crate gfx_voxel;
extern crate image;
extern crate input;
extern crate quack;
extern crate libc;
extern crate mmap;
extern crate sdl2;
extern crate sdl2_window;
extern crate shader_version;
extern crate time;
extern crate vecmath;
extern crate window;

extern crate "rustc-serialize" as serialize;
extern crate rustc_serialize as serialize;

// Reexport modules from gfx_voxel while stuff is moving
// from Hematite to the library.
Expand All @@ -29,16 +31,17 @@ use std::cell::RefCell;
use std::cmp::max;
use std::f32::consts::PI;
use std::f32::INFINITY;
use std::old_io::fs::File;
use std::num::Float;
use std::fs::File;
use std::path::Path;
use std::rc::Rc;

use array::*;
use event::{ Event, Events, MaxFps, Ups };
use quack::{Get, Set};
use event::{ Event, Events };
use flate2::read::GzDecoder;
use sdl2_window::Sdl2Window;
use shader::Renderer;
use vecmath::{ vec3_add, vec3_scale, vec3_normalized };
use window::{ CaptureCursor, Size, WindowSettings };
use window::{ Size, Window, AdvancedWindow, WindowSettings };

use minecraft::biome::Biomes;
use minecraft::block_state::BlockStates;
Expand All @@ -49,7 +52,7 @@ pub mod shader;
pub mod minecraft {
pub use self::data_1_8_pre2 as data;

mod data_1_8_pre2;
pub mod data_1_8_pre2;
pub mod biome;
pub mod block_state;
pub mod model;
Expand All @@ -62,10 +65,8 @@ fn main() {
let world = args.nth(1).expect("Usage: ./hematite <path/to/world>");
let world = Path::new(&world);

let level_gzip = File::open(&world.join("level.dat"))
.read_to_end().unwrap();
let level = minecraft::nbt::Nbt::from_gzip(level_gzip.as_slice())
.unwrap();
let level_reader = GzDecoder::new(File::open(world.join("level.dat")).unwrap()).unwrap();
let level = minecraft::nbt::Nbt::from_reader(level_reader).unwrap();
println!("{:?}", level);
let player_pos: [f32; 3] = Array::from_iter(
level["Data"]["Player"]["Pos"]
Expand All @@ -86,40 +87,42 @@ fn main() {

let loading_title = format!(
"Hematite loading... - {}",
world.filename_display()
world.file_name().unwrap().to_str().unwrap()
);
let window = Sdl2Window::new(
shader_version::OpenGL::_3_3,
WindowSettings {
title: loading_title,
size: [854, 480],
fullscreen: false,
exit_on_esc: true,
samples: 0,
}
WindowSettings::new(
loading_title,
Size { width: 854, height: 480 })
.fullscreen(false)
.exit_on_esc(true)
.samples(0)
.vsync(false)
);
let mut device = gfx_device_gl::GlDevice::new(|s| unsafe {

let (device, mut factory) = gfx_device_gl::create(|s| unsafe {
std::mem::transmute(sdl2::video::gl_get_proc_address(s))
});
let Size([w, h]) = window.get();
let frame = gfx::Frame::new(w as u16, h as u16);

let Size { width: w, height: h } = window.size();
let frame = factory.make_fake_output(w as u16, h as u16);

let assets = Path::new("./assets");

// Load biomes.
let biomes = Biomes::load(&assets);

// Load block state definitions and models.
let block_states = BlockStates::load(&assets, &mut device);
let block_states = BlockStates::load(&assets, &mut factory);

let mut renderer = Renderer::new(device, frame, block_states.texture().handle.clone());
let mut renderer = Renderer::new(device, factory, frame, block_states.texture.handle());

let mut chunk_manager = chunk::ChunkManager::new();

println!("Started loading chunks...");
let [cx_base, cz_base] = player_chunk.map(|x| max(0, (x & 0x1f) - 8) as u8);
for cz in range(cz_base, cz_base + 16) {
for cx in range(cx_base, cx_base + 16) {
for cz in cz_base..cz_base + 16 {
for cx in cx_base..cx_base + 16 {
match region.get_chunk_column(cx, cz) {
Some(column) => {
let [cx, cz] = [
Expand All @@ -139,7 +142,7 @@ fn main() {
near_clip: 0.1,
far_clip: 1000.0,
aspect_ratio: {
let Size([w, h]) = window.get();
let Size { width: w, height: h } = window.size();
(w as f32) / (h as f32)
}
}.projection();
Expand Down Expand Up @@ -171,10 +174,10 @@ fn main() {
println!("Press C to capture mouse");

let mut staging_buffer = vec![];
let ref window = RefCell::new(window);
for e in Events::new(window)
.set(Ups(120))
.set(MaxFps(10_000)) {
let ref window = Rc::new(RefCell::new(window));
for e in window.clone().events()
.ups(120)
.max_fps(10_000) {
use input::Button::Keyboard;
use input::Input::{ Move, Press };
use input::keyboard::Key;
Expand Down Expand Up @@ -255,9 +258,9 @@ fn main() {
num_total_chunks,
(end_time - start_time) as f64 / 1e6,
(frame_end_time - end_time) as f64 / 1e6,
fps, world.filename_display()
fps, world.file_name().unwrap().to_str().unwrap()
);
window.borrow_mut().window.set_title(title.as_slice()).unwrap();
window.borrow_mut().set_title(title);
}
Event::Update(_) => {
// HACK(eddyb) find the closest chunk to the player.
Expand Down Expand Up @@ -302,7 +305,7 @@ fn main() {
if capture_cursor { "off" } else { "on" });
capture_cursor = !capture_cursor;

window.set(CaptureCursor(capture_cursor));
window.borrow_mut().set_capture_cursor(capture_cursor);
}
Event::Input(Move(MouseRelative(_, _))) => {
if !capture_cursor {
Expand Down
5 changes: 3 additions & 2 deletions src/minecraft/biome.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::ops::Index;
use std::path::Path;

use chunk::BiomeId;
use minecraft::data;
use gfx_voxel::texture::ColorMap;

#[derive(Copy)]
#[derive(Copy, Clone)]
pub struct Biome {
pub name: &'static str,
pub temperature: f32,
Expand Down Expand Up @@ -43,7 +44,7 @@ impl Biomes {
impl Index<BiomeId> for Biomes {
type Output = Biome;

fn index<'a>(&'a self, id: &BiomeId) -> &'a Biome {
fn index<'a>(&'a self, id: BiomeId) -> &'a Biome {
self.biomes[id.value as usize].as_ref().unwrap()
}
}
50 changes: 25 additions & 25 deletions src/minecraft/block_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use std::borrow::Cow;
use std::cmp::max;
use std::collections::hash_map::Entry::{Occupied, Vacant};
use std::collections::HashMap;
use std::old_io::fs::File;
use std::num::Float;
use std::num::UnsignedInt;
use std::num::wrapping::{WrappingOps, Wrapping};
use std::fs::File;
use std::path::Path;
use std::num::Wrapping;

use array::*;
use chunk::{BiomeId, BlockState, Chunk};
Expand All @@ -22,9 +21,9 @@ use vecmath::vec3_add;

use self::PolymorphDecision::*;

pub struct BlockStates<D: gfx::Device> {
models: Vec<ModelAndBehavior>,
texture: Texture<D::Resources>,
pub struct BlockStates<R: gfx::Resources> {
pub models: Vec<ModelAndBehavior>,
pub texture: Texture<R>,
}

#[derive(PartialEq, Eq, Clone, Copy)]
Expand Down Expand Up @@ -112,10 +111,11 @@ impl ModelAndBehavior {
}
}

impl<R: gfx::Resources, D: gfx::Device<Resources=R> + gfx::Factory<R>> BlockStates<D> {
pub fn load(
assets: &Path, d: &mut D
) -> BlockStates<D> {
impl<R: gfx::Resources> BlockStates<R> {

pub fn load<F: gfx::Factory<R>>(
assets: &Path, f: &mut F
) -> BlockStates<R> {
let mut last_id = BLOCK_STATES.last().map_or(0, |state| state.0);
let mut states = Vec::<Description>::with_capacity(BLOCK_STATES.len().next_power_of_two());
let mut extras = vec![];
Expand All @@ -139,7 +139,7 @@ impl<R: gfx::Resources, D: gfx::Device<Resources=R> + gfx::Factory<R>> BlockStat
// Note: excluding paeonia itself, which works as-is.
let num_plants = lower.count();

for j in range(i - 1 - num_plants, i - 1) {
for j in i - 1 - num_plants..i - 1 {
last_id += 1;
let (_, lower_name, _) = BLOCK_STATES[j];
extras.push(Description {
Expand Down Expand Up @@ -174,7 +174,7 @@ impl<R: gfx::Resources, D: gfx::Device<Resources=R> + gfx::Factory<R>> BlockStat
}

let variant = if variant.ends_with(",shape=outer_right") {
Cow::Owned(format!("{}=straight", variant.slice_to(variant.len() - 12)))
Cow::Owned(format!("{}=straight", &variant[..variant.len() - 12]))
} else {
Cow::Borrowed(variant)
};
Expand All @@ -189,13 +189,13 @@ impl<R: gfx::Resources, D: gfx::Device<Resources=R> + gfx::Factory<R>> BlockStat
}
states.extend(extras.into_iter());

BlockStates::load_with_states(assets, d, states)
BlockStates::load_with_states(assets, f, states)
}

fn load_with_states(
assets: &Path, d: &mut D,
fn load_with_states<F: gfx::Factory<R>>(
assets: &Path, f: &mut F,
states: Vec<Description>
) -> BlockStates<D> {
) -> BlockStates<R> {
struct Variant {
model: String,
rotate_x: OrthoRotation,
Expand All @@ -214,7 +214,7 @@ impl<R: gfx::Resources, D: gfx::Device<Resources=R> + gfx::Factory<R>> BlockStat
Occupied(entry) => entry.into_mut(),
Vacant(entry) => entry.insert({
let name = state.name;
let path = assets.join(Path::new(format!("minecraft/blockstates/{}.json", name).as_slice()));
let path = assets.join(Path::new(format!("minecraft/blockstates/{}.json", name).as_str()));
match json::Json::from_reader(&mut File::open(&path).unwrap()).unwrap() {
json::Json::Object(mut json) => match json.remove("variants").unwrap() {
json::Json::Object(variants) => variants.into_iter().map(|(k, v)| {
Expand Down Expand Up @@ -269,7 +269,7 @@ impl<R: gfx::Resources, D: gfx::Device<Resources=R> + gfx::Factory<R>> BlockStat
Cow::Owned(ref variant) => variants.get(variant),
Cow::Borrowed(variant) => variants.get(variant)
}.unwrap();
let mut model = Model::load(variant.model.as_slice(), assets,
let mut model = Model::load(&variant.model, assets,
&mut atlas, &mut partial_model_cache);

let rotate_faces = |m: &mut Model, ix: usize, iy: usize, rot_mat: [i32; 4]| {
Expand Down Expand Up @@ -352,7 +352,7 @@ impl<R: gfx::Resources, D: gfx::Device<Resources=R> + gfx::Factory<R>> BlockStat
drop(partial_model_cache);
drop(block_state_cache);

let texture = atlas.complete(d);
let texture = atlas.complete(f);
let (width, height) = texture.get_size();
let u_unit = 1.0 / (width as f32);
let v_unit = 1.0 / (height as f32);
Expand Down Expand Up @@ -381,7 +381,7 @@ impl<R: gfx::Resources, D: gfx::Device<Resources=R> + gfx::Factory<R>> BlockStat
}
}

pub fn texture<'a>(&'a self) -> &'a Texture<D::Resources> {
pub fn texture<'a>(&'a self) -> &'a Texture<R> {
&self.texture
}

Expand All @@ -395,14 +395,14 @@ impl<R: gfx::Resources, D: gfx::Device<Resources=R> + gfx::Factory<R>> BlockStat
}
}

pub fn fill_buffer<R: gfx::Resources, D: gfx::Device<Resources=R> + gfx::Factory<R>>(block_states: &BlockStates<D>,
pub fn fill_buffer<R: gfx::Resources>(block_states: &BlockStates<R>,
biomes: &Biomes, buffer: &mut Vec<Vertex>,
coords: [i32; 3], chunks: [[[&Chunk; 3]; 3]; 3],
column_biomes: [[Option<&[[BiomeId; 16]; 16]>; 3]; 3]) {
let chunk_xyz = coords.map(|x| x as f32 * 16.0);
for y in range(0, 16) {
for z in range(0, 16) {
for x in range(0, 16) {
for y in 0..16_usize {
for z in 0..16_usize {
for x in 0..16_usize {
let at = |dir: [i32; 3]| {
let [dx, dy, dz] = dir.map(|x| x as usize);
let [x, y, z] = [x.wrapping_add(dx), y.wrapping_add(dy), z.wrapping_add(dz)].map(|x| x.wrapping_add(16));
Expand Down
Loading

0 comments on commit fd8afbe

Please sign in to comment.