From 943412986c5e2e5281010c398921beb4af31af97 Mon Sep 17 00:00:00 2001 From: Able Date: Fri, 25 Mar 2022 19:47:55 -0500 Subject: [PATCH] initial commit --- .gitignore | 1 + Cargo.lock | 154 ++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 13 ++++ src/main.rs | 104 ++++++++++++++++++++++++++++++++ src/platform.rs | 55 +++++++++++++++++ 5 files changed, 327 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs create mode 100644 src/platform.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..db786fc --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,154 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "bgfx-rs" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99d70c350be673dbbe40eb048e6542e1f20b3f2a04462a248d078b76dd2281fd" +dependencies = [ + "bgfx-sys", + "bitflags", + "cfixed-string", +] + +[[package]] +name = "bgfx-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2947cb050bee39d214c166ba8931b0ee3b5fb4d44ac0aebb3b05637d421ef" +dependencies = [ + "cc", +] + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "cc" +version = "1.0.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" +dependencies = [ + "jobserver", +] + +[[package]] +name = "cfixed-string" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9234271d550b2b3898521602c86dbea82bd97ae31c22fbc5969474a4934e0ac" + +[[package]] +name = "cmake" +version = "0.1.48" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8ad8cef104ac57b68b89df3208164d228503abbdce70f6880ffa3d970e7443a" +dependencies = [ + "cc", +] + +[[package]] +name = "cty" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35" + +[[package]] +name = "glfw" +version = "0.44.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "103c29fa9d30c5371187895a10948d4f54b4111450328b4e439133d88243c5e3" +dependencies = [ + "bitflags", + "glfw-sys", + "objc", + "raw-window-handle", + "winapi", +] + +[[package]] +name = "glfw-sys" +version = "3.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b0cbb12e2dfac494316e91f6bf7cf070ccd9c61501a02f4af14d71222c9f67f" +dependencies = [ + "cmake", +] + +[[package]] +name = "jobserver" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af25a77299a7f711a01975c35a6a424eb6862092cc2d6c72c4ed6cbc56dfc1fa" +dependencies = [ + "libc", +] + +[[package]] +name = "libc" +version = "0.2.121" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "efaa7b300f3b5fe8eb6bf21ce3895e1751d9665086af2d64b42f19701015ff4f" + +[[package]] +name = "malloc_buf" +version = "0.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62bb907fe88d54d8d9ce32a3cceab4218ed2f6b7d35617cafe9adf84e43919cb" +dependencies = [ + "libc", +] + +[[package]] +name = "objc" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "915b1b472bc21c53464d6c8461c9d3af805ba1ef837e1cac254428f4a77177b1" +dependencies = [ + "malloc_buf", +] + +[[package]] +name = "raw-window-handle" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fba75eee94a9d5273a68c9e1e105d9cffe1ef700532325788389e5a83e2522b7" +dependencies = [ + "cty", +] + +[[package]] +name = "vox" +version = "0.1.0" +dependencies = [ + "bgfx-rs", + "glfw", + "raw-window-handle", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..391df7b --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "vox" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] + +bgfx-rs = "0.7.0" + +glfw = "0.44.0" +raw-window-handle = "0.4.2" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..f297e43 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,104 @@ +pub mod platform; + +pub struct Position3D { + pub x: f64, + pub y: f64, + pub z: f64, +} + +pub struct VoxelGrid { + pub name: String, + pub position: Position3D, +} +use bgfx::*; +use bgfx_rs::bgfx; +use glfw::{Action, Key, Window}; +use platform::{get_platform_data, get_render_type}; + +const WIDTH: usize = 1280; +const HEIGHT: usize = 720; + +fn main() { + let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap(); + glfw.window_hint(glfw::WindowHint::ClientApi(glfw::ClientApiHint::NoApi)); + + let (mut window, events) = glfw + .create_window( + WIDTH as _, + HEIGHT as _, + "Vox - ESC to close", + glfw::WindowMode::Windowed, + ) + .expect("Failed to create GLFW window."); + + window.set_key_polling(true); + + let init = init(&window); + if !bgfx::init(&init) { + panic!("failed to init bgfx"); + } + + // bgfx::set_debug(DebugFlags::TEXT.bits()); + bgfx::set_view_clear( + 0, + ClearFlags::COLOR.bits() | ClearFlags::DEPTH.bits(), + SetViewClearArgs { + rgba: 0x00000000, + ..Default::default() + }, + ); + + let mut old_size = (0, 0); + + while !window.should_close() { + glfw.poll_events(); + for (_, event) in glfw::flush_messages(&events) { + if let glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) = event { + window.set_should_close(true) + } + } + + let size = window.get_framebuffer_size(); + + if old_size != size { + bgfx::reset(size.0 as _, size.1 as _, ResetArgs::default()); + old_size = size; + } + + bgfx::set_view_rect(0, 0, 0, size.0 as _, size.1 as _); + bgfx::touch(0); + + /* + bgfx::dbg_text_clear(DbgTextClearArgs::default()); + + bgfx::dbg_text(0, 1, 0x0f, "Color can be changed with ANSI \x1b[9;me\x1b[10;ms\x1b[11;mc\x1b[12;ma\x1b[13;mp\x1b[14;me\x1b[0m code too."); + bgfx::dbg_text(80, 1, 0x0f, "\x1b[;0m \x1b[;1m \x1b[; 2m \x1b[; 3m \x1b[; 4m \x1b[; 5m \x1b[; 6m \x1b[; 7m \x1b[0m"); + bgfx::dbg_text(80, 2, 0x0f, "\x1b[;8m \x1b[;9m \x1b[;10m \x1b[;11m \x1b[;12m \x1b[;13m \x1b[;14m \x1b[;15m \x1b[0m"); + bgfx::dbg_text( + 0, + 4, + 0x3f, + "Description: Initialization and debug text with bgfx-rs Rust API.", + ); + */ + + bgfx::frame(false); + } + shutdown_handler(); +} +pub fn shutdown_handler() { + bgfx::shutdown(); + + println!("shutdown"); +} + +pub fn init(window: &Window) -> Init { + let mut init = Init::new(); + + init.type_r = get_render_type(); + init.resolution.width = WIDTH as u32; + init.resolution.height = HEIGHT as u32; + init.resolution.reset = ResetFlags::VSYNC.bits(); + init.platform_data = get_platform_data(&window); + init +} diff --git a/src/platform.rs b/src/platform.rs new file mode 100644 index 0000000..6d1249b --- /dev/null +++ b/src/platform.rs @@ -0,0 +1,55 @@ +use bgfx_rs::static_lib::PlatformData; +use bgfx_rs::static_lib::RendererType; +use glfw::Window; +use raw_window_handle::{HasRawWindowHandle, RawWindowHandle}; + +pub fn get_platform_data(window: &Window) -> PlatformData { + let mut pd = PlatformData::new(); + + match window.raw_window_handle() { + #[cfg(any( + target_os = "linux", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "netbsd", + target_os = "openbsd" + ))] + RawWindowHandle::Xlib(data) => { + pd.nwh = data.window as *mut _; + pd.ndt = data.display as *mut _; + } + #[cfg(any( + target_os = "linux", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "netbsd", + target_os = "openbsd" + ))] + RawWindowHandle::Wayland(data) => { + pd.ndt = data.surface; // same as window, on wayland there ins't a concept of windows + pd.nwh = data.display; + } + + #[cfg(target_os = "macos")] + RawWindowHandle::MacOS(data) => { + pd.nwh = data.ns_window; + } + #[cfg(target_os = "windows")] + RawWindowHandle::Windows(data) => { + pd.nwh = data.hwnd; + } + _ => panic!("Unsupported Window Manager"), + } + + return pd; +} + +#[cfg(target_os = "linux")] +pub fn get_render_type() -> RendererType { + RendererType::OpenGL +} + +#[cfg(not(target_os = "linux"))] +pub fn get_render_type() -> RendererType { + RendererType::Count +}