From e7470c2d8fe5616e02cc1f72eca776d938d19a95 Mon Sep 17 00:00:00 2001 From: gallant Date: Fri, 21 Apr 2023 10:29:44 -0500 Subject: [PATCH] karx pls review, i believe I am doing the vec3 right --- build.rs | 10 +- examples/example_1.rs | 5 + src/lib.rs | 13 +- src/math/mod.rs | 2 + src/{ => math}/vec2.rs | 0 src/math/vec3.rs | 385 +++++++++++++++++++++++++++++++++++++++++ src/space/mesh.rs | 37 ++++ src/space/mod.rs | 1 + 8 files changed, 441 insertions(+), 12 deletions(-) create mode 100644 src/math/mod.rs rename src/{ => math}/vec2.rs (100%) create mode 100644 src/math/vec3.rs create mode 100644 src/space/mesh.rs create mode 100644 src/space/mod.rs diff --git a/build.rs b/build.rs index 9304eb9..85d4e6b 100644 --- a/build.rs +++ b/build.rs @@ -76,9 +76,9 @@ fn main() -> Result<(), Box> { let mut new_file_path = manifest_dir.clone(); if let Some(file_name) = file_name_result { let file_name = file_name.to_str().unwrap(); - if Path::new(file_name) + if Path::new(file_name) .extension() - .map_or(false, |ext| ext.eq_ignore_ascii_case("dll")) + .map_or(false, |ext| ext.eq_ignore_ascii_case("dll")) { new_file_path.push(file_name); std::fs::copy(&entry_path, new_file_path.as_path()) @@ -94,11 +94,11 @@ fn main() -> Result<(), Box> { /// # Panics /// panics if no response or response lacks "content-disposition" header /// # Errors -/// errors if +/// errors if /// A: unable to get `TlsConnector` -/// B: A File is unable to be create +/// B: A File is unable to be create /// C: or if the reader is unable to be copied into the writer -pub fn download_files(path: &Path,url: &str) -> Result> { +pub fn download_files(path: &Path, url: &str) -> Result> { let agent = AgentBuilder::new() .tls_connector(Arc::new(native_tls::TlsConnector::new()?)) .build(); diff --git a/examples/example_1.rs b/examples/example_1.rs index 3acc8fd..03fbddf 100644 --- a/examples/example_1.rs +++ b/examples/example_1.rs @@ -22,6 +22,11 @@ fn main() { #[cfg(feature = "audio")] cat_box::play("output.mp3", 120); game.run(|ctx| { + let (_, _, _) = ctx.inner(); + + //let win = b.window_mut(); + /* let instance_schtuff = win.vulkan_instance_extensions().unwrap(); */ + if game.step() >= 1 { i = (i + 1) % 255; ctx.set_background_colour(i, 64, 255); diff --git a/src/lib.rs b/src/lib.rs index c468968..50642ab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -92,8 +92,9 @@ )] #![cfg_attr(docsrs, feature(doc_cfg))] +pub mod math; pub mod physics; -pub mod vec2; +pub mod space; #[cfg(feature = "audio")] use rodio::{self, source::Source, Decoder, OutputStream}; @@ -115,8 +116,8 @@ use std::{ slice::IterMut, time::Instant, }; -use vec2::Vec2Int; +use math::vec2::Vec2Int; #[doc(no_inline)] pub use sdl2::{self, event::Event, keyboard::Scancode, pixels::Color}; @@ -748,7 +749,6 @@ impl Game { ///``` pub fn step(&self) -> u128 { self.time.get().elapsed().as_millis() - } ///Resets in-game timer @@ -769,11 +769,10 @@ impl Game { let sdl_context = sdl2::init()?; let video_subsystem = sdl_context.video()?; - let mut window_build = video_subsystem - .window(&self.title, self.width, self.height); + let mut window_build = video_subsystem.window(&self.title, self.width, self.height); //init window - let window = if cfg!(feature = "opengl"){ + let window = if cfg!(feature = "opengl") { window_build.opengl().build()? } else if cfg!(feature = "vulkan") { window_build.vulkan().build()? @@ -817,7 +816,7 @@ impl Game { /// Plays an audio file given the path of file and plays it for y seconds /// ```no_run /// # use cat_box::play; -/// play("/path/to/song.mp3", 15); +/// play("/path/to/song.mp71", 15); /// ``` pub fn play + Send + 'static>( path: P, diff --git a/src/math/mod.rs b/src/math/mod.rs new file mode 100644 index 0000000..84df29d --- /dev/null +++ b/src/math/mod.rs @@ -0,0 +1,2 @@ +pub mod vec2; +pub mod vec3; diff --git a/src/vec2.rs b/src/math/vec2.rs similarity index 100% rename from src/vec2.rs rename to src/math/vec2.rs diff --git a/src/math/vec3.rs b/src/math/vec3.rs new file mode 100644 index 0000000..edf5b5a --- /dev/null +++ b/src/math/vec3.rs @@ -0,0 +1,385 @@ +use std::{ + fmt::Debug, + ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}, +}; + +// Vec3 +/// A set of 3 [`f32`]s representing a location or direction in the 3d plane. +#[derive(Clone, Copy, Default, PartialEq)] +pub struct Vec3 { + /// The x component of the vector. + pub x: f32, + /// The y component of the vector. + pub y: f32, + /// The z component of the vector + pub z: f32, +} + +impl Debug for Vec3 { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_tuple("Vec3") + .field(&self.x) + .field(&self.y) + .field(&self.z) + .finish() + } +} + +impl Vec3 { + /// Creates a new `Vec3` with the given x- and y-values. + /// + /// It is often simpler, and preferred, to just write `(x, y).into()`. + #[must_use] + pub const fn new(x: f32, y: f32, z: f32) -> Vec3 { + Self { x, y, z } + } + + /// Gets the squared magnitude of the vector. + /// + /// Useful for comparisons as it is faster to calculate than `magnitude`. + #[must_use] + pub fn sq_magnitude(self) -> f32 { + self.x * self.x + self.y * self.y + self.z * self.z + } + + /// Gets the magnitude of the vector. + #[must_use] + pub fn magnitude(self) -> f32 { + self.sq_magnitude().sqrt() + } + + /// Gets the squared distance from this vector to `rhs`. + /// + /// Useful for comparisons as it is faster to calculate than `dist`. + #[must_use] + pub fn sq_dist(self, rhs: Self) -> f32 { + (self - rhs).sq_magnitude() + } + + /// Gets the distance from this vector to `rhs`. + #[must_use] + pub fn dist(self, rhs: Self) -> f32 { + (self - rhs).magnitude() + } + + /// Normalizes the vector, making its magnitude `1`. + #[must_use] + pub fn normalized(self) -> Self { + self / self.magnitude() + } + + /// Rounds the vector to a [`Vec3Int`]. + /// + /// This uses `as i32` under the hood, and as such comes with all the same unfortunate edge cases. Beware. + #[must_use] + pub fn rounded(self) -> Vec3Int { + #[allow(clippy::cast_possible_truncation)] + Vec3Int { + x: self.x as i32, + y: self.y as i32, + z: self.z as i32, + } + } +} + +impl From<(i32, i32, i32)> for Vec3 { + fn from(v: (i32, i32, i32)) -> Self { + Vec3Int::from(v).to_f32() + } +} + +impl From<(f32, f32, f32)> for Vec3 { + fn from(v: (f32, f32, f32)) -> Self { + Self { + x: v.0, + y: v.1, + z: v.2, + } + } +} + +impl From for (f32, f32, f32) { + fn from(v: Vec3) -> Self { + (v.x, v.y, v.z) + } +} + +impl PartialEq<(i32, i32, i32)> for Vec3 { + fn eq(&self, other: &(i32, i32, i32)) -> bool { + self == &Self::from(*other) + } +} + +impl PartialEq<(f32, f32, f32)> for Vec3 { + fn eq(&self, other: &(f32, f32, f32)) -> bool { + self == &Self::from(*other) + } +} + +// ...and related op impls +impl Neg for Vec3 { + type Output = Self; + + fn neg(self) -> Self::Output { + self * -1.0 + } +} + +impl Add for Vec3 { + type Output = Self; + + fn add(self, rhs: Self) -> Self::Output { + Self { + x: self.x + rhs.x, + y: self.y + rhs.y, + z: self.z + rhs.z, + } + } +} + +impl AddAssign for Vec3 +where + Vec3: Add, +{ + fn add_assign(&mut self, rhs: T) { + *self = *self + rhs; + } +} + +impl Sub for Vec3 +where + Vec3: Add, +{ + type Output = Self; + + fn sub(self, rhs: T) -> Self::Output { + -(-self + rhs) + } +} + +impl SubAssign for Vec3 +where + Vec3: Sub, +{ + fn sub_assign(&mut self, rhs: T) { + *self = *self - rhs; + } +} + +impl Mul for Vec3 { + type Output = Self; + + fn mul(self, rhs: f32) -> Self::Output { + Self { + x: self.x * rhs, + y: self.y * rhs, + z: self.z * rhs, + } + } +} + +impl Div for Vec3 { + type Output = Self; + + fn div(self, rhs: f32) -> Self::Output { + Self { + x: self.x / rhs, + y: self.y / rhs, + z: self.z / rhs, + } + } +} + +impl MulAssign for Vec3 { + fn mul_assign(&mut self, rhs: f32) { + *self = *self * rhs; + } +} + +impl DivAssign for Vec3 { + fn div_assign(&mut self, rhs: f32) { + *self = *self / rhs; + } +} + +// Vec3Int +/// A set of 2 [`i32`]s representing a location or direction in the 2d plane. +#[derive(Clone, Copy, Default, PartialEq, Eq, Hash)] +pub struct Vec3Int { + /// The x component of the vector. + pub x: i32, + /// The y component of the vector. + pub y: i32, + + pub z: i32, +} + +impl Debug for Vec3Int { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_tuple("Vec3Int") + .field(&self.x) + .field(&self.y) + .field(&self.z) + .finish() + } +} + +impl Vec3Int { + /// Creates a new `Vec3` with the given x- and y-values. + /// + /// It is often simpler, and preferred, to just write `(x, y).into()`. + #[must_use] + pub const fn new(x: i32, y: i32, z: i32) -> Vec3Int { + Self { x, y, z } + } + + /// Gets the squared magnitude of the vector. + /// + /// Useful for comparisons as it is faster to calculate than `magnitude`. + #[must_use] + pub fn sq_magnitude(self) -> i32 { + self.x * self.x + self.y * self.y + self.z * self.z + } + + /// Gets the magnitude of the vector. + #[must_use] + pub fn magnitude(self) -> f32 { + #[allow(clippy::cast_precision_loss)] + (self.sq_magnitude() as f32).sqrt() + } + + /// Gets the squared distance from this vector to `rhs`. + /// + /// Useful for comparisons as it is faster to calculate than `dist`. + #[must_use] + pub fn sq_dist(self, rhs: Self) -> i32 { + (self - rhs).sq_magnitude() + } + + /// Gets the distance from this vector to `rhs`. + #[must_use] + pub fn dist(self, rhs: Self) -> f32 { + (self - rhs).magnitude() + } + + /// Casts this vector to a [`Vec3`]. + /// + /// This uses `as f32` under the hood, and as such comes with all the same unfortunate edge cases. Beware. + #[must_use] + pub fn to_f32(self) -> Vec3 { + #[allow(clippy::cast_precision_loss)] + Vec3 { + x: self.x as f32, + y: self.y as f32, + z: self.z as f32, + } + } +} + +impl From<(i32, i32, i32)> for Vec3Int { + fn from(v: (i32, i32, i32)) -> Self { + Self { + x: v.0, + y: v.1, + z: v.2, + } + } +} + +impl From for (i32, i32, i32) { + fn from(v: Vec3Int) -> Self { + (v.x, v.y, v.z) + } +} + +impl PartialEq<(i32, i32, i32)> for Vec3Int { + fn eq(&self, other: &(i32, i32, i32)) -> bool { + self == &Self::from(*other) + } +} + +// ...and related op impls +impl Neg for Vec3Int { + type Output = Self; + + fn neg(self) -> Self::Output { + self * -1 + } +} + +impl Add for Vec3Int { + type Output = Self; + + fn add(self, rhs: Self) -> Self::Output { + Self { + x: self.x + rhs.x, + y: self.y + rhs.y, + z: self.z + rhs.z, + } + } +} + +impl AddAssign for Vec3Int +where + Vec3Int: Add, +{ + fn add_assign(&mut self, rhs: T) { + *self = *self + rhs; + } +} + +impl Sub for Vec3Int +where + Vec3Int: Add, +{ + type Output = Self; + + fn sub(self, rhs: T) -> Self::Output { + -(-self + rhs) + } +} + +impl SubAssign for Vec3Int +where + Vec3Int: Sub, +{ + fn sub_assign(&mut self, rhs: T) { + *self = *self - rhs; + } +} + +impl Mul for Vec3Int { + type Output = Self; + + fn mul(self, rhs: i32) -> Self::Output { + Self { + x: self.x * rhs, + y: self.y * rhs, + z: self.z * rhs, + } + } +} + +impl Div for Vec3Int { + type Output = Self; + + fn div(self, rhs: i32) -> Self::Output { + Self { + x: self.x / rhs, + y: self.y / rhs, + z: self.z / rhs, + } + } +} + +impl MulAssign for Vec3Int { + fn mul_assign(&mut self, rhs: i32) { + *self = *self * rhs; + } +} + +impl DivAssign for Vec3Int { + fn div_assign(&mut self, rhs: i32) { + *self = *self / rhs; + } +} diff --git a/src/space/mesh.rs b/src/space/mesh.rs new file mode 100644 index 0000000..fef7cb6 --- /dev/null +++ b/src/space/mesh.rs @@ -0,0 +1,37 @@ +use crate::math::{vec3::Vec3, vec2::Vec2}; + +//credit to Djuk1c, abstracting your code into a game engine bcuz i can +#[derive(Default, Clone, Copy, Debug)] +pub struct Vertex { + pub pos: Vec3, + pub normal: Vec3, + pub texture: Vec2, + pub color: u32, + pub lit: f32, +} +impl Vertex { + pub fn new(pos: Vec3, normal: Vec3, texture: Vec2, color: u32, lit: f32) -> Self { + Self { pos, normal, texture, color, lit } + } +} + +#[derive(Clone, Debug)] +pub struct Triangle { + pub v: [Vertex; 3], +} +impl Triangle { + pub fn new( + p1: Vertex, + p2: Vertex, + p3: Vertex, + ) -> Self { + Self { + v: [p1, p2, p3] + } + } +} + +pub struct Mesh { + pub triangles: Vec, +} + diff --git a/src/space/mod.rs b/src/space/mod.rs new file mode 100644 index 0000000..cb3e16e --- /dev/null +++ b/src/space/mod.rs @@ -0,0 +1 @@ +pub mod mesh;