summaryrefslogtreecommitdiff
path: root/src/graphics/image.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/graphics/image.rs')
-rw-r--r--src/graphics/image.rs51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/graphics/image.rs b/src/graphics/image.rs
new file mode 100644
index 0000000..26833c0
--- /dev/null
+++ b/src/graphics/image.rs
@@ -0,0 +1,51 @@
+// TODO: このファイルは好きな設計ではないので破壊する。
+
+use super::texture::TextureWrapper;
+use png::{BitDepth, ColorType, Decoder};
+use std::io::Cursor;
+use wgpu::{Device, Queue, TextureFormat, TextureUsages};
+
+#[derive(Clone, Copy)]
+pub enum ImageId {
+ Image,
+}
+
+static IMAGE_DATAS: [&[u8]; 1] = [include_bytes!(concat!(
+ env!("CARGO_MANIFEST_DIR"),
+ "/assets/image.png"
+))];
+
+pub fn create_textures(device: &Device, queue: &Queue) -> Vec<TextureWrapper> {
+ IMAGE_DATAS
+ .iter()
+ .map(|data| create_texture(device, queue, data))
+ .collect()
+}
+
+fn create_texture(device: &Device, queue: &Queue, data: &[u8]) -> TextureWrapper {
+ let mut reader = Decoder::new(Cursor::new(data))
+ .read_info()
+ .expect("fatal: failed to load image");
+ let mut buf = vec![
+ 0x00;
+ reader
+ .output_buffer_size()
+ .expect("fatal: image is too large")
+ ];
+ let info = reader
+ .next_frame(&mut buf)
+ .expect("fatal: failed to decode image");
+ if info.color_type != ColorType::Rgba || info.bit_depth != BitDepth::Eight {
+ panic!("fatal: image is not 8-bit RGBA");
+ }
+
+ let texture = TextureWrapper::new(
+ device,
+ info.width,
+ info.height,
+ TextureFormat::Rgba8UnormSrgb,
+ TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST,
+ );
+ texture.write(queue, &buf[..info.buffer_size()]);
+ texture
+}