diff --git a/examples/simple.rs b/examples/simple.rs index 6c1b8f0d09489df01af713e31c2710514e24f79a..a0398ea5a2356b1d3c6ff37124304d1d42dbf681 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -70,6 +70,16 @@ fn main() { println!("Testing colors: they are NOT the same!") } + //Draw a transparent rectangle over window content + window.rect(250, 200, 80, 80, Color::rgba(100,100,100,100)); + + //Draw an opaque rectangle replacing window + window.rect_opaque(300, 220, 80, 80, Color::rgb(100,100,100)); + + //Draw a hole in the window replacing also alpha channel !! + window.rect_(300, 100, 80, 80, Color::rgba(10,10,10,255), true); + + window.sync(); 'events: loop { diff --git a/src/renderer.rs b/src/renderer.rs index 6251bdb24e07d89b7442085dd81d2822a2c088f8..d2b39d8364e47ee72c5ae4ce1869c4096bc92bc2 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -47,8 +47,18 @@ pub trait Renderer { /// Flip the buffer fn sync(&mut self) -> bool; - /// Draw a pixel + /// Draw a pixel (compositing) fn pixel(&mut self, x: i32, y: i32, color: Color) { + self.pixel_(x, y, color, false); + } + + /// Draw a pixel (replacing) + fn pixel_opaque(&mut self, x: i32, y: i32, color: Color) { + self.pixel_(x, y, color, true); + } + + //Draw a pixel replacing or compositing depending on 'replace' boolean + fn pixel_(&mut self, x: i32, y: i32, color: Color, replace: bool) { let w = self.width(); let h = self.height(); let data = self.data_mut(); @@ -57,11 +67,11 @@ pub trait Renderer { let new = color.data; let alpha = (new >> 24) & 0xFF; - if alpha > 0 { + //if alpha > 0 { let old = unsafe{ &mut data[y as usize * w as usize + x as usize].data}; - if alpha >= 255 { + if alpha >= 255 || replace { *old = new; - } else { + } else if alpha >0 { let n_r = (((new >> 16) & 0xFF) * alpha) >> 8; let n_g = (((new >> 8) & 0xFF) * alpha) >> 8; let n_b = ((new & 0xFF) * alpha) >> 8; @@ -74,7 +84,7 @@ pub trait Renderer { *old = ((o_a << 24) | (o_r << 16) | (o_g << 8) | o_b) + ((alpha << 24) | (n_r << 16) | (n_g << 8) | n_b); } - } + //} } } @@ -265,8 +275,17 @@ pub trait Renderer { self.set(Color::rgb(0,0,0)); } - /// Draw rectangle + /// Draw rectangle (compositing) fn rect(&mut self, x: i32, y: i32, w: u32, h: u32, color: Color) { + self.rect_(x, y, w, h, color, false); + } + + /// Draw rectangle (replacing) + fn rect_opaque(&mut self, x: i32, y: i32, w: u32, h: u32, color: Color) { + self.rect_(x, y, w, h, color, true); + } + + fn rect_(&mut self, x: i32, y: i32, w: u32, h: u32, color: Color, replace: bool) { let self_w = self.width(); let self_h = self.height(); @@ -277,8 +296,8 @@ pub trait Renderer { let len = cmp::max(start_x, cmp::min(self_w as i32, x + w as i32)) - start_x; let alpha = (color.data >> 24) & 0xFF; - if alpha > 0 { - if alpha >= 255 { + //if alpha > 0 { + if alpha >= 255 || replace { let data = self.data_mut(); for y in start_y..end_y { unsafe { @@ -288,11 +307,11 @@ pub trait Renderer { } else { for y in start_y..end_y { for x in start_x..start_x + len { - self.pixel(x, y, color); + self.pixel_(x, y, color, false); } } } - } + //} } /// Display an image