Skip to content

Commit e7b6541

Browse files
committed
Added README.md
1 parent 5cdaae1 commit e7b6541

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
Overview
2+
==
3+
As the popularity of the library [minifb](https://crates.io/crates/minifb) shows, it is useful to put a 2D buffer/image
4+
on a window in a platform-independent way. Minifb's approach to doing window management itself, however, is problematic
5+
code duplication. We already have very high quality libraries for this in the Rust ecosystem
6+
(such as [winit](https://crates.io/crates/winit)), and minifb's implementation of window management is not ideal. For
7+
example, it occasionally segfaults on some platforms and is missing key features such as the ability to set a window
8+
icon. While it would be possible to add these features to minifb, it makes more sense to instead use the standard
9+
window handling systems.
10+
11+
Softbuffer integrates with the [raw-window-handle](https://crates.io/crates/raw-window-handle) crate
12+
to allow writing to a window in a cross-platform way while using the very high quality dedicated window management
13+
libraries that are available in the Rust ecosystem.
14+
15+
What about [pixels](https://crates.io/crates/pixels)? Pixels accomplishes a very similar goal to softbuffer,
16+
however there are two key differences. Pixels provides some capacity for GPU-accelerated post-processing of what is
17+
displayed, while Softbuffer does not. Due to not having this post-processing, Softbuffer does not rely on the GPU or
18+
hardware accelerated graphcis stack in any way, and is thus more portable to installations that do not have access to
19+
hardware acceleration (e.g. VMs, older computers, computers with misconfigured drivers). Softbuffer should be used over
20+
pixels when its GPU-accelerated post-processing effects are not needed.
21+
22+
23+
License & Credits
24+
==
25+
26+
This library is dual-licensed under MIT or Apache-2.0, just like minifb and rust. Significant portions of code were taken
27+
from the minifb library to do platform-specific work.
28+
29+
Platform support:
30+
==
31+
Some, but not all, platforms supported in [raw-window-handle]() are supported by Softbuffer. Pull requests are welcome
32+
to add new platforms!
33+
34+
For now, the priority for new platforms is:
35+
1) to have at least one platform on each OS working (e.g. one of Win32 or WinRT, or one of Xlib, Xcb, and Wayland) and
36+
2) for that one platform on each OS to be the one that winit uses.
37+
38+
(PRs will be accepted for any platform, even if it does not follow the above priority.)
39+
40+
✅: Present | ❌: Absent
41+
- AndroidNdk ❌
42+
- AppKit ❌
43+
- Orbital ❌
44+
- UiKit ❌
45+
- Wayland ❌
46+
- Web ❌
47+
- Win32 ✅
48+
- WinRt ❌
49+
- Xcb ❌
50+
- Xlib ✅
51+
52+
Example
53+
==
54+
```no_run
55+
use winit::event::{Event, WindowEvent};
56+
use winit::event_loop::{ControlFlow, EventLoop};
57+
use winit::window::WindowBuilder;
58+
use softbuffer::GraphicsContext;
59+
60+
fn main() {
61+
let event_loop = EventLoop::new();
62+
let window = WindowBuilder::new().build(&event_loop).unwrap();
63+
let mut graphics_context = unsafe { GraphicsContext::new(window) };
64+
65+
event_loop.run(move |event, _, control_flow| {
66+
*control_flow = ControlFlow::Wait;
67+
68+
match event {
69+
Event::RedrawRequested(window_id) if window_id == graphics_context.window().id() => {
70+
let (width, height) = {
71+
let size = graphics_context.window().inner_size();
72+
(size.width, size.height)
73+
};
74+
let buffer = (0..((width*height) as usize)).map(|index|{
75+
let y = index / (width as usize);
76+
let x = index % (width as usize);
77+
let red = x % 255;
78+
let green = y % 255;
79+
let blue = (x*y) % 255;
80+
81+
let color = blue | (green << 8) | (red << 16);
82+
83+
color as u32
84+
}).collect::<Vec<_>>();
85+
86+
graphics_context.set_buffer(&buffer, width as u16, height as u16);
87+
}
88+
Event::WindowEvent {
89+
event: WindowEvent::CloseRequested,
90+
window_id
91+
} if window_id == graphics_context.window().id() => {
92+
*control_flow = ControlFlow::Exit;
93+
},
94+
_ => {}
95+
}
96+
});
97+
}
98+
```

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![doc = include_str!("../README.md")]
2+
13
#[cfg(target_os = "linux")]
24
mod x11;
35
#[cfg(target_os = "windows")]

0 commit comments

Comments
 (0)