diff options
Diffstat (limited to 'engine/src/texture.rs')
-rw-r--r-- | engine/src/texture.rs | 58 |
1 files changed, 57 insertions, 1 deletions
diff --git a/engine/src/texture.rs b/engine/src/texture.rs index f32bc0a..f644b58 100644 --- a/engine/src/texture.rs +++ b/engine/src/texture.rs @@ -1,8 +1,9 @@ use std::path::Path; use image::io::Reader as ImageReader; -use image::{DynamicImage, ImageError}; +use image::{DynamicImage, EncodableLayout, ImageError, Rgb, RgbImage}; +use crate::color::Color; use crate::opengl::texture::{PixelDataFormat, Texture as InnerTexture}; use crate::vector::Vec2; @@ -67,6 +68,39 @@ impl Texture Ok(me) } + #[must_use] + pub fn new_from_color(dimensions: &Vec2<u32>, color: &Color<u8>) -> Self + { + let image = RgbImage::from_pixel( + dimensions.x, + dimensions.y, + Rgb([color.red, color.green, color.blue]), + ); + + let inner = InnerTexture::new(); + + inner.bind(|texture_curr_bound| { + InnerTexture::generate( + &texture_curr_bound, + dimensions, + image.as_bytes(), + PixelDataFormat::Rgb, + ); + }); + + let me = Self { + inner, + pixel_data_format: PixelDataFormat::Rgb, + dimensions: dimensions.clone(), + }; + + me.set_wrap(Wrapping::Repeat); + me.set_magnifying_filter(Filtering::Linear); + me.set_minifying_filter(Filtering::Nearest); + + me + } + pub fn set_wrap(&self, wrapping: Wrapping) { self.inner.bind(|texture_curr_bound| { @@ -126,3 +160,25 @@ pub enum Error #[error("Unsupported image data kind")] UnsupportedImageDataKind, } + +/// Texture ID. +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct Id +{ + id: u32, +} + +impl Id +{ + /// Returns a new texture ID. + #[must_use] + pub fn new(id: u32) -> Self + { + Self { id } + } + + pub(crate) fn into_inner(self) -> u32 + { + self.id + } +} |