]> git.nega.tv - josh/narcissus/commitdiff
Move image stuff into its own crate
authorJoshua Simmons <josh@nega.tv>
Sun, 20 Nov 2022 16:35:01 +0000 (17:35 +0100)
committerJoshua Simmons <josh@nega.tv>
Sun, 20 Nov 2022 16:35:01 +0000 (17:35 +0100)
Cargo.lock
Cargo.toml
narcissus-core/src/lib.rs
narcissus-image/Cargo.toml [new file with mode: 0644]
narcissus-image/src/lib.rs [moved from narcissus-core/src/texture.rs with 92% similarity]
narcissus/Cargo.toml
narcissus/src/main.rs

index 30b7efb8e53b8f45b874228ed380a4e5d6daccdc..a6c4f682dfcb4ef6500a5a11ee5b34aaf566fae6 100644 (file)
@@ -37,6 +37,7 @@ dependencies = [
  "narcissus-app",
  "narcissus-core",
  "narcissus-gpu",
+ "narcissus-image",
  "narcissus-maths",
 ]
 
@@ -65,6 +66,13 @@ dependencies = [
  "vulkan-sys",
 ]
 
+[[package]]
+name = "narcissus-image"
+version = "0.1.0"
+dependencies = [
+ "stb_image-sys",
+]
+
 [[package]]
 name = "narcissus-maths"
 version = "0.1.0"
index 0f8c72e312a996d355a66402cb388950aa6389a6..bddc787eaed1ac3c3ea25f9b3492782f7e9378c1 100644 (file)
@@ -9,6 +9,7 @@ members = [
     "narcissus-app",
     "narcissus-core",
     "narcissus-gpu",
+    "narcissus-image",
     "narcissus-maths",
     "narcissus-world",
 ]
index 83653ac76e22e66159dc9fed60511062f7a41609..cffbf95da77b4f034bf0e31d7d39bacba8b7b112 100644 (file)
@@ -10,7 +10,6 @@ pub mod rand;
 pub mod raw_window;
 mod ref_count;
 pub mod slice;
-mod texture;
 mod uuid;
 mod virtual_mem;
 mod virtual_vec;
@@ -22,7 +21,6 @@ pub use fixed_vec::FixedVec;
 pub use mutex::Mutex;
 pub use pool::{Handle, Pool};
 pub use ref_count::{Arc, Rc};
-pub use texture::Texture;
 pub use uuid::Uuid;
 pub use virtual_mem::{virtual_commit, virtual_free, virtual_reserve};
 pub use virtual_vec::{VirtualDeque, VirtualVec};
diff --git a/narcissus-image/Cargo.toml b/narcissus-image/Cargo.toml
new file mode 100644 (file)
index 0000000..f1435d4
--- /dev/null
@@ -0,0 +1,9 @@
+[package]
+name = "narcissus-image"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+stb_image-sys = { path = "../ffi/stb_image-sys" }
\ No newline at end of file
similarity index 92%
rename from narcissus-core/src/texture.rs
rename to narcissus-image/src/lib.rs
index 49fa4ecc17a892fdf3e8d496a3440c137c56e3a7..9df77beecea8f117ddc22e043c25d8fbc8e24187 100644 (file)
@@ -13,7 +13,7 @@ impl std::fmt::Display for LoadError {
 
 impl std::error::Error for LoadError {}
 
-pub struct Texture {
+pub struct Image {
     width: usize,
     height: usize,
     components: usize,
@@ -21,8 +21,8 @@ pub struct Texture {
     buffer: NonNull<u8>,
 }
 
-impl Texture {
-    pub fn from_buffer(buffer: &[u8]) -> Result<Texture, LoadError> {
+impl Image {
+    pub fn from_buffer(buffer: &[u8]) -> Result<Image, LoadError> {
         let mut x = 0;
         let mut y = 0;
         let mut components = 0;
@@ -47,7 +47,7 @@ impl Texture {
         let components = components as usize;
         let len = x * y * components;
 
-        Ok(Texture {
+        Ok(Image {
             width: x,
             height: y,
             components,
@@ -95,9 +95,17 @@ impl Texture {
     }
 }
 
-impl Drop for Texture {
+impl Drop for Image {
     fn drop(&mut self) {
         // Safety: Always allocated by `stbi_load_xxx` functions.
         unsafe { stbi_image_free(self.buffer.as_ptr() as *mut _) }
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn it_works() {}
+}
index 8605f27e554cc6ad0d46efdb3101c13fe9332a08..2332c3ab7a6e499efe5d9bbeca3a270cdefc6616 100644 (file)
@@ -8,5 +8,6 @@ edition = "2021"
 [dependencies]
 narcissus-core = { path = "../narcissus-core" }
 narcissus-maths = { path = "../narcissus-maths" }
+narcissus-image = { path = "../narcissus-image" }
 narcissus-app = { path = "../narcissus-app" }
 narcissus-gpu = { path = "../narcissus-gpu" }
\ No newline at end of file
index 4905ee844547bd8a9bedc2df74bf8f4c1aca0b54..c433417ab8e97d49d1eb393d5635d336b20bc86d 100644 (file)
@@ -1,7 +1,7 @@
 use std::{path::Path, time::Instant};
 
 use narcissus_app::{create_app, Event, Key, WindowDesc};
-use narcissus_core::{cstr, default, obj, rand::Pcg64, Texture};
+use narcissus_core::{cstr, default, obj, rand::Pcg64};
 use narcissus_gpu::{
     create_device, Access, Bind, BindGroupLayoutDesc, BindGroupLayoutEntryDesc, BindingType,
     Buffer, BufferDesc, BufferImageCopy, BufferUsageFlags, ClearValue, CompareOp, CullingMode,
@@ -11,6 +11,7 @@ use narcissus_gpu::{
     SamplerAddressMode, SamplerDesc, SamplerFilter, Scissor, ShaderDesc, ShaderStageFlags, StoreOp,
     ThreadToken, Topology, TypedBind, Viewport,
 };
+use narcissus_image as image;
 use narcissus_maths::{
     sin_cos_pi_f32, vec2, vec3, vec4, Affine3, Deg, HalfTurn, Mat3, Mat4, Point3, Vec2, Vec3,
 };
@@ -113,11 +114,11 @@ fn load_obj<P: AsRef<Path>>(path: P) -> (Vec<Vertex>, Vec<u16>) {
     (vertices, indices)
 }
 
-fn load_texture<P: AsRef<Path>>(path: P) -> Texture {
+fn load_image<P: AsRef<Path>>(path: P) -> image::Image {
     let start = std::time::Instant::now();
     let path = path.as_ref();
     let texture =
-        Texture::from_buffer(std::fs::read(path).expect("failed to read file").as_slice())
+        image::Image::from_buffer(std::fs::read(path).expect("failed to read file").as_slice())
             .expect("failed to load image");
     println!(
         "loading image {path:?} took {:?}",
@@ -365,7 +366,7 @@ pub fn main() {
         stencil_front: default(),
     });
 
-    let blåhaj_image = load_texture("narcissus/data/blåhaj.png");
+    let blåhaj_image = load_image("narcissus/data/blåhaj.png");
     let (blåhaj_vertices, blåhaj_indices) = load_obj("narcissus/data/blåhaj.obj");
 
     let blåhaj_vertex_buffer = create_buffer_with_data(