Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
cohaereo committed Nov 4, 2022
0 parents commit 09e8669
Show file tree
Hide file tree
Showing 49 changed files with 5,568 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
[package]
name = "minecrab"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1"
bytemuck = { version = "1.12", features = [ "derive" ] }
byteorder = "1.4"
flate2 = "1.0"
fnv = "1.0.7"
font-awesome = "0.2"
futures = "0.3"
glam = "0.21"
hecs = "0.9"
hex = "0.4"
image = "0.24"
imgui = "0.8"
imgui-wgpu = "0.20"
imgui-winit-support = { version = "0.8", features = ["winit-26"] }
intmap = "2.0.0"
log = "0.4"
nibble_vec = "0.1.0"
pretty_env_logger = "0.4"
profiling = { version = "1.0.7", features = ["profile-with-tracy"] }
raw-cpuid = "10.6"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio = { version = "1.21.2", features = ["full"] }
tracy-client = "0.14.1"
wgpu = { version = "0.13", features = ["spirv"] }
winit = "0.26"

[build-dependencies]
rustc_version = "0.4.0"
shaderc = "0.8"
version = "3.0.0"

[profile.dev.package.miniz_oxide]
opt-level = 3

# [profile.release]
# opt-level = 3
# lto = true
# overflow-checks = false
# strip = "debuginfo"

Binary file added DroidSans.ttf
Binary file not shown.
Binary file added FontAwesomeSolid.ttf
Binary file not shown.
Binary file added atlas.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added atlas_debug.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added atlas_ids.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added block_debug.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
88 changes: 88 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use std::{
fs::File,
io::{Read, Write},
path::Path,
};

fn resolve_include(
requested_file: &str,
_rtype: shaderc::IncludeType,
_source_file: &str,
_depth: usize,
) -> shaderc::IncludeCallbackResult {
let p = Path::new("src/shaders/").join(requested_file);
if let Ok(mut f) = File::open(p) {
let mut inc_source = String::new();
f.read_to_string(&mut inc_source).unwrap();
Ok(shaderc::ResolvedInclude {
resolved_name: "src/shaders/".to_string() + requested_file,
content: inc_source,
})
} else {
// TODO: Maybe give the reason?
Err("Could not open file".to_string())
}
}

fn main() -> std::io::Result<()> {
let compiler = shaderc::Compiler::new().unwrap();
let mut options = shaderc::CompileOptions::new().unwrap();
options.set_source_language(shaderc::SourceLanguage::HLSL);
options.set_include_callback(resolve_include);

macro_rules! compile_shader {
($path:expr) => {
println!(concat!(concat!("cargo:rerun-if-changed=", $path), ".hlsl"));
let mut source = String::new();
let mut f = File::open(concat!($path, ".hlsl"))?;
f.read_to_string(&mut source)?;
let spv_vs = compiler
.compile_into_spirv(
&source,
shaderc::ShaderKind::Vertex,
concat!($path, ".hlsl"),
"vs_main",
Some(&options),
)
.expect("VS compilation failed");
let spv_fs = compiler
.compile_into_spirv(
&source,
shaderc::ShaderKind::Fragment,
concat!($path, ".hlsl"),
"fs_main",
Some(&options),
)
.expect("FS compilation failed");

println!("Successfully compiled shader {}", $path);

File::create(concat!($path, ".vs.spv"))?.write_all(spv_vs.as_binary_u8())?;
File::create(concat!($path, ".fs.spv"))?.write_all(spv_fs.as_binary_u8())?;
};
}

compile_shader!("src/shaders/chunk");
compile_shader!("src/shaders/debug_lines");
compile_shader!("src/shaders/debug_cube");
compile_shader!("src/shaders/post/fxaa");

let path = Path::new(&std::env::var("OUT_DIR").unwrap()).join("build_info.rs");
let mut output_file = File::create(&path).expect("Failed to create `build_info.rs`");

output_file
.write_fmt(format_args!(
"pub const RUSTC_VERSION: &'static str = \"{}\";\n",
rustc_version::version().unwrap().to_string()
))
.expect("Failed to write RUST_VERSION constant");

output_file
.write_fmt(format_args!(
"pub const CRATE_VERSION: &'static str = \"{}\";\n",
version::version!()
))
.expect("Failed to write RUST_VERSION constant");

Ok(())
}
3 changes: 3 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cargo build --release && \
./verify_shaders.sh && \
RUST_LOG=info,wgpu_hal=warn,wgpu_core=warn,winit=warn ./target/release/minecrab
39 changes: 39 additions & 0 deletions src/ecs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::{fmt::Debug, num::NonZeroU32};

use glam::Vec3;
use hecs::{Entity, World};

// In units (aka blocks) per second
pub const VELOCITY_UNIT: f32 = (1. / 8000.) * 20.;
// pub const VELOCITY_UNIT: f32 = (1. / 8000.);

pub struct Position(pub Vec3);
pub struct Velocity(pub Vec3);

pub fn update_velocity(world: &mut World, delta: f32) {
for (e, (pos, vel)) in world.query::<(&mut Position, &Velocity)>().iter() {
// FIXME: We're not moving in Y, because the lack of block collision makes mobs phase through the ground
let temp_vel = Vec3::new(vel.0.x, 0., vel.0.z);
pos.0 += temp_vel * delta;
}
}

pub fn get_or_insert(world: &mut World, eid: i32) -> Entity {
// println!("{:?}", NonZeroU32::new(eid as u32));
let ent = Entity::from_bits(eid as u64 | (1 << 32)).unwrap();
if world.get::<(&Position)>(ent).is_ok() {
ent
} else {
world.spawn_at(ent, (Position(Vec3::default()), Velocity(Vec3::default())));
ent
}
}

impl Debug for Position {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!(
"<{:.2},{:.2},{:.2}>",
self.0.x, self.0.y, self.0.z
))
}
}
24 changes: 24 additions & 0 deletions src/fixed_point.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
pub trait FixedPoint {
fn to_f64(&self) -> f64;
fn from_f64(v: f64) -> Self;
}

impl FixedPoint for i32 {
fn to_f64(&self) -> f64 {
(*self as f64) / 32.0
}

fn from_f64(v: f64) -> Self {
(v * 32.0) as Self
}
}

impl FixedPoint for i8 {
fn to_f64(&self) -> f64 {
(*self as f64) / 32.0
}

fn from_f64(v: f64) -> Self {
(v * 32.0) as Self
}
}
Loading

0 comments on commit 09e8669

Please sign in to comment.