16-Bit-Truecolor (RGB565) statt 8-Bit-Palette

Framebuffer ist jetzt &mut [u16], jeder Wert ein RGB565-Pixel. Der
Shader dekodiert die Bitfelder direkt aus einer R16Uint-Textur; die
Palette (Textur, API, Presets, Tooling) entfaellt. Neu: pbio::rgb565()
zum Packen von 8-Bit-Kanaelen.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-05 15:04:05 +02:00
parent 2d81471820
commit c5a4d0026b
6 changed files with 60 additions and 128 deletions
+13 -11
View File
@@ -3,11 +3,11 @@
//! Ausführen: cargo run --example starter --release
//!
//! Zeigt: Game-Loop-Struktur, non-blocking Event-Polling, Tastatur (Esc beendet),
//! Maus, direktes Schreiben auf den indizierten Framebuffer.
//! Maus, direktes Schreiben auf den RGB565-Framebuffer.
use std::time::Duration;
use pbio::{Event, Key, MouseButton, Platform, PlatformConfig};
use pbio::{rgb565, Event, Key, MouseButton, Platform, PlatformConfig};
fn main() {
let mut p = Platform::new(PlatformConfig {
@@ -29,8 +29,8 @@ fn main() {
p.poll_events(Some(Duration::from_millis(0)));
// poll_event() gibt &mut self nach jedem Call wieder frei → Aktionen
// auf p (request_close, set_mouse_capture, palette_mut …) sind direkt
// im Loop erlaubt. drain_events() existiert weiter für den Fall, dass
// auf p (request_close, set_mouse_capture …) sind direkt im Loop
// erlaubt. drain_events() existiert weiter für den Fall, dass
// nur gelesen wird.
while let Some(ev) = p.poll_event() {
match ev {
@@ -44,24 +44,26 @@ fn main() {
let (w, h) = p.framebuffer_size();
let fb = p.framebuffer_mut();
// Diagonal wanderndes Gradientband. Jeder Pixel ist ein Palettenindex
// 0..=255 — Default-Palette ist RGB332, daher die sichtbaren Bänder.
let t = (frame / 256) as u8;
// Diagonal wanderndes Farbverlauf-Band, direkt als RGB565-Truecolor.
let t = (frame / 2) as u8;
for y in 0..h {
for x in 0..w {
fb[(y * w + x) as usize] = (x as u8).wrapping_add(y as u8).wrapping_add(t);
let r = (x as u8).wrapping_add(t);
let g = (y as u8).wrapping_add(t);
let b = (x as u8).wrapping_add(y as u8);
fb[(y * w + x) as usize] = rgb565(r, g, b);
}
}
// Cursor: 3x3-Quadrat in Palettenindex 0xE0 (rot), beim Klick 0xFC (gelb).
// Cursor: 3x3-Quadrat, rot; beim Klick gelb.
let (mx, my) = (mouse.0 as i32, mouse.1 as i32);
let cursor_ix = if paint { 0xFC } else { 0xE0 };
let cursor_px = if paint { rgb565(255, 255, 0) } else { rgb565(255, 0, 0) };
for dy in -1..=1 {
for dx in -1..=1 {
let x = mx + dx;
let y = my + dy;
if (0..w as i32).contains(&x) && (0..h as i32).contains(&y) {
fb[(y as u32 * w + x as u32) as usize] = cursor_ix;
fb[(y as u32 * w + x as u32) as usize] = cursor_px;
}
}
}