@@ -11,6 +11,7 @@ use std::process;
1111use libbreenix:: graphics;
1212use libbreenix:: graphics:: { FlushRect , VirglRect } ;
1313use libbreenix:: time;
14+ use libbreenix:: types;
1415
1516use libgfx:: color:: Color ;
1617use libgfx:: font;
@@ -123,10 +124,29 @@ impl FpsCounter {
123124
124125const NUM_RECTS : usize = 6 ;
125126
127+ /// Window buffer dimensions — a nice floating window size for the compositor.
128+ const WIN_W : u32 = 400 ;
129+ const WIN_H : u32 = 300 ;
130+
126131fn main ( ) {
127132 let boot_id = clock_monotonic_ns ( ) ;
128133 println ! ( "Bounce demo starting (for Gus!) [boot_id={:016x}]" , boot_id) ;
129134
135+ // Try window-buffer mode first: BWM composites us as a floating window
136+ match graphics:: create_window ( WIN_W , WIN_H ) {
137+ Ok ( win) => {
138+ let _ = graphics:: register_window ( win. id , b"Bounce" ) ;
139+ println ! ( "[bounce] Window mode: id={} {}x{} @ {:p} [boot_id={:016x}]" ,
140+ win. id, WIN_W , WIN_H , win. pixels, boot_id) ;
141+ let mut rects = make_rects ( ) ;
142+ run_window_loop ( & win, & mut rects) ;
143+ return ;
144+ }
145+ Err ( e) => {
146+ println ! ( "[bounce] create_window failed: {} — falling back to VirGL direct" , e) ;
147+ }
148+ }
149+
130150 let info = match graphics:: fbinfo ( ) {
131151 Ok ( info) => info,
132152 Err ( _e) => { println ! ( "Error: Could not get framebuffer info" ) ; process:: exit ( 1 ) ; }
@@ -135,15 +155,7 @@ fn main() {
135155 let height = info. height as i32 ;
136156 let width = info. left_pane_width ( ) as i32 ;
137157
138- let mut rects = [
139- AnimRect :: new ( 50 , 40 , 900 , 700 , 120 , 80 , Color :: rgb ( 255 , 50 , 50 ) ) , // Red
140- AnimRect :: new ( 200 , 150 , -800 , 600 , 90 , 110 , Color :: rgb ( 50 , 255 , 50 ) ) , // Green
141- AnimRect :: new ( 100 , 300 , 750 , -850 , 140 , 60 , Color :: rgb ( 50 , 50 , 255 ) ) , // Blue
142- AnimRect :: new ( 300 , 100 , -700 , -750 , 70 , 130 , Color :: rgb ( 255 , 255 , 50 ) ) , // Yellow
143- AnimRect :: new ( 150 , 400 , 850 , 500 , 100 , 100 , Color :: rgb ( 255 , 50 , 255 ) ) , // Magenta
144- AnimRect :: new ( 350 , 250 , -650 , 800 , 110 , 70 , Color :: rgb ( 50 , 255 , 255 ) ) , // Cyan
145- ] ;
146-
158+ let mut rects = make_rects ( ) ;
147159 let bg_packed = graphics:: rgb ( 15 , 15 , 30 ) ;
148160
149161 // Try VirGL GPU rendering first
@@ -160,6 +172,55 @@ fn main() {
160172 }
161173}
162174
175+ fn make_rects ( ) -> [ AnimRect ; NUM_RECTS ] {
176+ [
177+ AnimRect :: new ( 50 , 40 , 900 , 700 , 120 , 80 , Color :: rgb ( 255 , 50 , 50 ) ) , // Red
178+ AnimRect :: new ( 200 , 150 , -800 , 600 , 90 , 110 , Color :: rgb ( 50 , 255 , 50 ) ) , // Green
179+ AnimRect :: new ( 100 , 300 , 750 , -850 , 140 , 60 , Color :: rgb ( 50 , 50 , 255 ) ) , // Blue
180+ AnimRect :: new ( 300 , 100 , -700 , -750 , 70 , 130 , Color :: rgb ( 255 , 255 , 50 ) ) , // Yellow
181+ AnimRect :: new ( 150 , 400 , 850 , 500 , 100 , 100 , Color :: rgb ( 255 , 50 , 255 ) ) , // Magenta
182+ AnimRect :: new ( 350 , 250 , -650 , 800 , 110 , 70 , Color :: rgb ( 50 , 255 , 255 ) ) , // Cyan
183+ ]
184+ }
185+
186+ /// Render bouncing rects into a window buffer. BWM composites this as a floating window.
187+ fn run_window_loop ( win : & graphics:: WindowBuffer , rects : & mut [ AnimRect ; NUM_RECTS ] ) {
188+ let width = win. width as i32 ;
189+ let height = win. height as i32 ;
190+ let bpp = 4usize ;
191+ let stride = width as usize * bpp;
192+ let bg = Color :: rgb ( 15 , 15 , 30 ) ;
193+
194+ let mut fb = unsafe {
195+ FrameBuf :: from_raw (
196+ win. pixels as * mut u8 ,
197+ width as usize ,
198+ height as usize ,
199+ stride,
200+ bpp,
201+ true , // BGRA
202+ )
203+ } ;
204+
205+ let mut fps = FpsCounter :: new ( ) ;
206+ const SUBSTEPS : i32 = 8 ;
207+
208+ loop {
209+ for _ in 0 ..SUBSTEPS {
210+ for rect in rects. iter_mut ( ) { rect. step ( SUBSTEPS ) ; }
211+ for rect in rects. iter_mut ( ) { rect. bounce_walls ( width, height) ; }
212+ }
213+
214+ fb. clear ( bg) ;
215+ for rect in rects. iter ( ) { rect. draw ( & mut fb) ; }
216+ fps. tick ( ) ;
217+ fps. draw ( & mut fb) ;
218+
219+ // Throttle to ~60 FPS — BWM reads our buffer each frame anyway
220+ let _ = time:: nanosleep ( & libbreenix:: types:: Timespec { tv_sec : 0 , tv_nsec : 16_000_000 } ) ;
221+ }
222+ }
223+
163224fn build_virgl_rects ( rects : & [ AnimRect ; NUM_RECTS ] ) -> [ VirglRect ; NUM_RECTS ] {
164225 let mut vr = [ VirglRect :: default ( ) ; NUM_RECTS ] ;
165226 for ( i, rect) in rects. iter ( ) . enumerate ( ) {
0 commit comments