refactor shape example
-use fltk::{app, frame::*, image::*, utils, window::*};
+use fltk::*;
fn main() {
- let mut shape: Vec<u8> = vec![0u8; 400 * 400 * 4];
- for (iter, pixel) in shape.chunks_exact_mut(4).enumerate() {
- let x = (iter % 400 as usize) as i16;
- let y = (iter / 400 as usize) as i16;
- let d = {
- let xd = x as i32 - 200 as i32;
- let yd = y as i32 - 200 as i32;
- ((xd.pow(2) + yd.pow(2)) as f64).sqrt().powi(2)
- };
- let inside_the_circle = d < (200 as f64).powi(2);
-
- let rgba = if inside_the_circle {
- [0x00, 0x00, 0x00, 0xff] // non-transparent
- } else {
- [0x00, 0x00, 0x00, 0x00] // transparent
- };
-
- pixel.copy_from_slice(&rgba);
- }
- let shape = RgbImage::new(&shape, 400, 400, ColorDepth::Rgba8).unwrap();
+ let shape = prep_surface();
let mut pattern: Vec<u8> = vec![0u8; 500 * 500 * 3];
for (iter, pixel) in pattern.chunks_exact_mut(3).enumerate() {
@@ -29,11 +10,11 @@ fn main() {
let (red, green, blue) = utils::hex2rgb((x ^ y) as u32);
pixel.copy_from_slice(&[red, green, blue]);
}
- let pattern = RgbImage::new(&pattern, 500, 500, ColorDepth::Rgb8).unwrap();
+ let pattern = image::RgbImage::new(&pattern, 500, 500, ColorDepth::Rgb8).unwrap();
let app = app::App::default();
- let mut wind = Window::default().with_size(400, 400);
- let mut frm = Frame::default().size_of_parent();
+ let mut wind = window::Window::default().with_size(400, 400);
+ let mut frm = frame::Frame::default().size_of_parent();
wind.end();
wind.show();
wind.set_shape(Some(shape));
@@ -41,3 +22,15 @@ fn main() {
app.run().unwrap();
}
+
+fn prep_surface() -> image::RgbImage {
+ let surf = surface::ImageSurface::new(400, 400, false);
+ surface::ImageSurface::push_current(&surf);
+ draw::set_draw_color(Color::Black);
+ draw::draw_rectf(-1, -1, 402, 402);
+ draw::set_draw_color(Color::White);
+ draw::draw_pie(0, 0, 400, 400, 0., 360.);
+ let img = surf.image().unwrap();
+ surface::ImageSurface::pop_current();
+ img
+}
\ No newline at end of file
