Commit 1cfd791833cb9908fe13fac2aaa27b236aa1e92f
1 parent
d8cd5ed9
display remaining sleep time for next frame in window
Showing
3 changed files
with
19 additions
and
9 deletions
| @@ -35,6 +35,7 @@ pub trait Canvas { | @@ -35,6 +35,7 @@ pub trait Canvas { | ||
| 35 | 35 | ||
| 36 | fn clear(&mut self); | 36 | fn clear(&mut self); |
| 37 | fn draw(&mut self, c :&dyn Drawable, ofs :Coordinate, color :u32); | 37 | fn draw(&mut self, c :&dyn Drawable, ofs :Coordinate, color :u32); |
| 38 | + fn put_text(&self, ofs :Coordinate, s :&str); | ||
| 38 | fn show(&self); | 39 | fn show(&self); |
| 39 | } | 40 | } |
| 40 | 41 |
| @@ -344,12 +344,12 @@ fn main() { | @@ -344,12 +344,12 @@ fn main() { | ||
| 344 | // use floating point values. | 344 | // use floating point values. |
| 345 | // https://rechneronline.de/pi/tetrahedron.php | 345 | // https://rechneronline.de/pi/tetrahedron.php |
| 346 | // yi = a / 12 * √6 | 346 | // yi = a / 12 * √6 |
| 347 | - // yc = a / 4 * √6 | ||
| 348 | - // zi = √3 / 6 * a | ||
| 349 | - // zc = √3 / 3 * a | ||
| 350 | let yi = 60.0 / 12.0 * 6.0.sqrt().unwrap(); | 347 | let yi = 60.0 / 12.0 * 6.0.sqrt().unwrap(); |
| 348 | + // yc = a / 4 * √6 | ||
| 351 | let yc = 60.0 / 4.0 * 6.0.sqrt().unwrap(); | 349 | let yc = 60.0 / 4.0 * 6.0.sqrt().unwrap(); |
| 350 | + // zi = √3 / 6 * a | ||
| 352 | let zi = 3.0.sqrt().unwrap() / 6.0 * 60.0; | 351 | let zi = 3.0.sqrt().unwrap() / 6.0 * 60.0; |
| 352 | + // zc = √3 / 3 * a | ||
| 353 | let zc = 3.0.sqrt().unwrap() / 3.0 * 60.0; | 353 | let zc = 3.0.sqrt().unwrap() / 3.0 * 60.0; |
| 354 | 354 | ||
| 355 | let i = Vector( 0.0, yc, 0.0); | 355 | let i = Vector( 0.0, yc, 0.0); |
| @@ -471,7 +471,6 @@ fn main() { | @@ -471,7 +471,6 @@ fn main() { | ||
| 471 | canvas.draw( &co, Coordinate(0,0), 0x0000FF); | 471 | canvas.draw( &co, Coordinate(0,0), 0x0000FF); |
| 472 | canvas.draw( &cl, Coordinate(0,0), 0x0000FF); | 472 | canvas.draw( &cl, Coordinate(0,0), 0x0000FF); |
| 473 | canvas.draw( &cr, Coordinate(0,0), 0x0000FF); | 473 | canvas.draw( &cr, Coordinate(0,0), 0x0000FF); |
| 474 | - canvas.show(); | ||
| 475 | 474 | ||
| 476 | let passed = Instant::now() - last; | 475 | let passed = Instant::now() - last; |
| 477 | let f = (passed.as_nanos() / step.as_nanos()) as u32; | 476 | let f = (passed.as_nanos() / step.as_nanos()) as u32; |
| @@ -481,7 +480,10 @@ fn main() { | @@ -481,7 +480,10 @@ fn main() { | ||
| 481 | } | 480 | } |
| 482 | 481 | ||
| 483 | last = last + step*(f + 1); | 482 | last = last + step*(f + 1); |
| 484 | - println!("Sleep for: {:?}", last - Instant::now()); | 483 | + canvas.put_text( Coordinate(10, 15) |
| 484 | + , &format!( "sleep: {:?}" | ||
| 485 | + , last - Instant::now() )); | ||
| 486 | + canvas.show(); | ||
| 485 | thread::sleep(last - Instant::now()); | 487 | thread::sleep(last - Instant::now()); |
| 486 | } | 488 | } |
| 487 | }); | 489 | }); |
| @@ -74,10 +74,10 @@ impl XcbEasel { | @@ -74,10 +74,10 @@ impl XcbEasel { | ||
| 74 | , screen.root(), 0, 0, width, width, 0 | 74 | , screen.root(), 0, 0, width, width, 0 |
| 75 | , xcb::WINDOW_CLASS_INPUT_OUTPUT as u16 | 75 | , xcb::WINDOW_CLASS_INPUT_OUTPUT as u16 |
| 76 | , screen.root_visual() | 76 | , screen.root_visual() |
| 77 | - , &[(xcb::CW_BACK_PIXEL, screen.white_pixel())] ); | 77 | + , &[(xcb::CW_BACK_PIXEL, screen.black_pixel())] ); |
| 78 | 78 | ||
| 79 | xcb::create_gc( &conn, gc, screen.root() | 79 | xcb::create_gc( &conn, gc, screen.root() |
| 80 | - , &[ (xcb::GC_FOREGROUND, screen.black_pixel()) | 80 | + , &[ (xcb::GC_FOREGROUND, screen.white_pixel()) |
| 81 | , (xcb::GC_GRAPHICS_EXPOSURES, 0) ] ); | 81 | , (xcb::GC_GRAPHICS_EXPOSURES, 0) ] ); |
| 82 | 82 | ||
| 83 | let (shmid, shm) = getshm((width * height) as usize); | 83 | let (shmid, shm) = getshm((width * height) as usize); |
| @@ -120,8 +120,8 @@ fn getshm<'a>(size :usize) -> (i32, &'a mut [u32]) { | @@ -120,8 +120,8 @@ fn getshm<'a>(size :usize) -> (i32, &'a mut [u32]) { | ||
| 120 | 120 | ||
| 121 | unsafe { | 121 | unsafe { |
| 122 | let id = libc::shmget( libc::IPC_PRIVATE | 122 | let id = libc::shmget( libc::IPC_PRIVATE |
| 123 | - , size * 4 | ||
| 124 | - , libc::IPC_CREAT | 0o744 ); | 123 | + , size * 4 |
| 124 | + , libc::IPC_CREAT | 0o744 ); | ||
| 125 | let ptr = libc::shmat(id, ptr::null(), 0); | 125 | let ptr = libc::shmat(id, ptr::null(), 0); |
| 126 | (id as i32, from_raw_parts_mut(ptr as *mut u32, size)) | 126 | (id as i32, from_raw_parts_mut(ptr as *mut u32, size)) |
| 127 | } | 127 | } |
| @@ -233,6 +233,13 @@ impl<'a> Canvas for XcbCanvas<'a> { | @@ -233,6 +233,13 @@ impl<'a> Canvas for XcbCanvas<'a> { | ||
| 233 | } | 233 | } |
| 234 | } | 234 | } |
| 235 | 235 | ||
| 236 | + fn put_text(&self, ofs :Coordinate, s :&str) { | ||
| 237 | + let Coordinate(xofs, yofs) = ofs; | ||
| 238 | + xcb::xproto::image_text_8( &self.conn, self.pixmap, self.gc | ||
| 239 | + , xofs as i16, yofs as i16, s ); | ||
| 240 | + self.conn.flush(); | ||
| 241 | + } | ||
| 242 | + | ||
| 236 | fn show(&self) { | 243 | fn show(&self) { |
| 237 | xcb::copy_area( &self.conn, self.pixmap, self.window, self.gc | 244 | xcb::copy_area( &self.conn, self.pixmap, self.window, self.gc |
| 238 | , 0, 0, 0, 0 | 245 | , 0, 0, 0, 0 |
Please
register
or
login
to post a comment