softbuffer icon indicating copy to clipboard operation
softbuffer copied to clipboard

README.md Example not working (outdated?)

Open BerryArray opened this issue 1 year ago • 2 comments

The example on the README.md file (Here) seems to contain structs(?) like softbuffer::Context and softbuffer::Surface which aren't in the documentation. (nor seem to exist at all?)

There's an extremely similar example used in the docs.rs softbuffer home-page (Here) which does compile.

What's strange is that I can't seem to find any mention of softbuffer::Context and softbuffer::Surface in any of the older versions' documentation.

BerryArray avatar Mar 04 '23 14:03 BerryArray

The API was updated in https://github.com/rust-windowing/softbuffer/pull/64. The example in the README should work on the git master of softbuffer, but not the latest release.

ids1024 avatar Mar 04 '23 17:03 ids1024

Allow me to save you some time.

let mut graphics_context = unsafe { GraphicsContext::new(&window, &window) }.unwrap();

Event::RedrawRequested(window_id) => {
  if window_id == window.id() {
    let (width, height) = {
      let size = window.inner_size();
      (size.width, size.height)
    };
    let buffer = (0..((width * height) as usize))
      .map(|index| {
        let y = index / (width as usize);
        let x = index % (width as usize);
        let red = x % 255;
        let green = y % 255;
        let blue = (x * y) % 255;
        let color = blue | (green << 8) | (red << 16);
        color as u32
        })
        .collect::<Vec<_>>();

      graphics_context.set_buffer(&buffer, width as u16, height as u16);
  }
}

Alternatively, modify the entry in Cargo.toml and use what is was in the README.

softbuffer = { git = "https://github.com/rust-windowing/softbuffer.git" rev = "e5d546" }

ghost avatar Mar 31 '23 19:03 ghost

As of now, README.md contains the correctly released code.

notgull avatar Jun 22 '24 16:06 notgull