Skip to content

Commit b89520f

Browse files
committed
Rework documentation for GraphicsContext::set_buffer
This includes a more clear description of the pixel format expected.
1 parent a800ca4 commit b89520f

File tree

1 file changed

+36
-15
lines changed

1 file changed

+36
-15
lines changed

src/lib.rs

+36-15
Original file line numberDiff line numberDiff line change
@@ -160,28 +160,49 @@ impl GraphicsContext {
160160
}
161161

162162
/// Shows the given buffer with the given width and height on the window corresponding to this
163-
/// graphics context. Panics if buffer.len() ≠ width*height. If the size of the buffer does
164-
/// not match the size of the window, the buffer is drawn in the upper-left corner of the window.
163+
/// graphics context.
164+
///
165165
/// It is recommended in most production use cases to have the buffer fill the entire window.
166-
/// Use your windowing library to find the size of the window.
166+
/// Use your windowing library to determine the size of the window.
167+
///
168+
/// # Panics
169+
///
170+
/// Panics if `buffer.len() ≠ width*height`. If the size of the buffer does not match the size of the
171+
/// window.
167172
///
168-
/// The format of the buffer is as follows. There is one u32 in the buffer for each pixel in
169-
/// the area to draw. The first entry is the upper-left most pixel. The second is one to the right
170-
/// etc. (Row-major top to bottom left to right one u32 per pixel). Within each u32 the highest
171-
/// order 8 bits are to be set to 0. The next highest order 8 bits are the red channel, then the
172-
/// green channel, and then the blue channel in the lowest-order 8 bits. See the examples for
173-
/// one way to build this format using bitwise operations.
173+
/// # Pixel format
174174
///
175-
/// --------
175+
/// The buffer layout is in a row-major layout. The buffer is drawn in the upper-left corner of
176+
/// the window. The x axis grows to the right and the y axis grows downwards.
176177
///
177-
/// Pixel format (u32):
178+
/// Each pixel is represented in memory as a [`u32`]. Starting with the most significant byte
179+
/// going to the least significant byte, the byte is set to `0`. Then there is a byte for the red,
180+
/// green and blue channel.
178181
///
182+
/// ```text
179183
/// 00000000RRRRRRRRGGGGGGGGBBBBBBBB
184+
/// ^ ^
185+
/// MSB LSB
186+
/// ```
187+
///
188+
/// | Bit | Channel |
189+
/// |------|---------------|
190+
/// | 0 | Bit is `0` |
191+
/// | R | Red channel |
192+
/// | G | Green channel |
193+
/// | B | Blue channel |
194+
///
195+
/// The function shown below may be used to create a color from a 24-bit RGB color using bitwise\
196+
/// operations:
180197
///
181-
/// 0: Bit is 0
182-
/// R: Red channel
183-
/// G: Green channel
184-
/// B: Blue channel
198+
/// ```
199+
/// fn pixel(red: u8, green: u8, blue: u8) -> u32 {
200+
/// let color = blue as u32
201+
/// | ((green as u32) << 8)
202+
/// | ((red as u32) << 16);
203+
/// color
204+
/// }
205+
/// ```
185206
#[inline]
186207
pub fn set_buffer(&mut self, buffer: &[u32], width: u16, height: u16) {
187208
if (width as usize) * (height as usize) != buffer.len() {

0 commit comments

Comments
 (0)