Compare commits

...

2 Commits
master ... dev

Author SHA1 Message Date
a014b3e144 Fix convertion from Color to Pixel 2024-03-10 13:54:42 +01:00
f0ab184ea8 Implement create_texture() 2024-03-02 21:53:00 +01:00
3 changed files with 14 additions and 8 deletions

View File

@ -94,10 +94,15 @@ impl Canvas {
unimplemented!(); unimplemented!();
} }
pub fn create_texture(&mut self, _size: Size, _background: Option<Pixel>) pub fn create_texture(&mut self, size: Size, background: Option<Pixel>)
-> Rc<RefCell<Texture>> -> Result<TextureHandle, &'static str>
{ {
unimplemented!(); let buffer = vec!(background.unwrap_or_else(|| Pixel::from(Color::NONE));
(size.w * size.h).try_into().unwrap());
let texture = Texture::create(&self.renderer, &Vec::from(bytemuck::cast_slice(&buffer)),
size);
Ok(TextureHandle::from_texture(texture))
} }
pub fn create_texture_from_file(&mut self, pub fn create_texture_from_file(&mut self,

View File

@ -17,6 +17,7 @@ pub struct Texture {
impl Texture { impl Texture {
//TODO: take buffer's ownership
pub fn create(renderer: &WgpuRenderer, pub fn create(renderer: &WgpuRenderer,
buffer: &[u8], buffer: &[u8],
size: Size) size: Size)

View File

@ -175,10 +175,10 @@ impl Color {
//that the values are within [0.0, 1.0], meaning we won't ever overflow an u8 //that the values are within [0.0, 1.0], meaning we won't ever overflow an u8
unsafe { unsafe {
Pixel { Pixel {
r: self.r.to_int_unchecked::<u8>() * 255, r: (self.r * 255.0).to_int_unchecked::<u8>(),
g: self.g.to_int_unchecked::<u8>() * 255, g: (self.g * 255.0).to_int_unchecked::<u8>(),
b: self.b.to_int_unchecked::<u8>() * 255, b: (self.b * 255.0).to_int_unchecked::<u8>(),
a: self.a.to_int_unchecked::<u8>() * 255, a: (self.a * 255.0).to_int_unchecked::<u8>(),
} }
} }
} }
@ -193,7 +193,7 @@ impl Color {
} }
pub fn alpha_blend(&self, other: &Self) -> Self { pub fn alpha_blend(&self, other: &Self) -> Self {
//apply alpha compisting's "over" operator, see //apply alpha compositing's "over" operator, see
//[https://en.wikipedia.org/wiki/Alpha_compositing] for more detail //[https://en.wikipedia.org/wiki/Alpha_compositing] for more detail
let a = self.a + other.a * (1.0 - self.a); let a = self.a + other.a * (1.0 - self.a);
Self { Self {