Browse Source

display remaining sleep time for next frame in window

master
Georg Hopp 6 years ago
parent
commit
1cfd791833
Signed by: ghopp GPG Key ID: 4C5D226768784538
  1. 1
      fractional/src/easel.rs
  2. 12
      fractional/src/main.rs
  3. 15
      fractional/src/xcb.rs

1
fractional/src/easel.rs

@ -35,6 +35,7 @@ pub trait Canvas {
fn clear(&mut self); fn clear(&mut self);
fn draw(&mut self, c :&dyn Drawable, ofs :Coordinate, color :u32); fn draw(&mut self, c :&dyn Drawable, ofs :Coordinate, color :u32);
fn put_text(&self, ofs :Coordinate, s :&str);
fn show(&self); fn show(&self);
} }

12
fractional/src/main.rs

@ -344,12 +344,12 @@ fn main() {
// use floating point values. // use floating point values.
// https://rechneronline.de/pi/tetrahedron.php // https://rechneronline.de/pi/tetrahedron.php
// yi = a / 12 * √6 // yi = a / 12 * √6
// yc = a / 4 * √6
// zi = √3 / 6 * a
// zc = √3 / 3 * a
let yi = 60.0 / 12.0 * 6.0.sqrt().unwrap(); let yi = 60.0 / 12.0 * 6.0.sqrt().unwrap();
// yc = a / 4 * √6
let yc = 60.0 / 4.0 * 6.0.sqrt().unwrap(); let yc = 60.0 / 4.0 * 6.0.sqrt().unwrap();
// zi = √3 / 6 * a
let zi = 3.0.sqrt().unwrap() / 6.0 * 60.0; let zi = 3.0.sqrt().unwrap() / 6.0 * 60.0;
// zc = √3 / 3 * a
let zc = 3.0.sqrt().unwrap() / 3.0 * 60.0; let zc = 3.0.sqrt().unwrap() / 3.0 * 60.0;
let i = Vector( 0.0, yc, 0.0); let i = Vector( 0.0, yc, 0.0);
@ -471,7 +471,6 @@ fn main() {
canvas.draw( &co, Coordinate(0,0), 0x0000FF); canvas.draw( &co, Coordinate(0,0), 0x0000FF);
canvas.draw( &cl, Coordinate(0,0), 0x0000FF); canvas.draw( &cl, Coordinate(0,0), 0x0000FF);
canvas.draw( &cr, Coordinate(0,0), 0x0000FF); canvas.draw( &cr, Coordinate(0,0), 0x0000FF);
canvas.show();
let passed = Instant::now() - last; let passed = Instant::now() - last;
let f = (passed.as_nanos() / step.as_nanos()) as u32; let f = (passed.as_nanos() / step.as_nanos()) as u32;
@ -481,7 +480,10 @@ fn main() {
} }
last = last + step*(f + 1); last = last + step*(f + 1);
println!("Sleep for: {:?}", last - Instant::now());
canvas.put_text( Coordinate(10, 15)
, &format!( "sleep: {:?}"
, last - Instant::now() ));
canvas.show();
thread::sleep(last - Instant::now()); thread::sleep(last - Instant::now());
} }
}); });

15
fractional/src/xcb.rs

@ -74,10 +74,10 @@ impl XcbEasel {
, screen.root(), 0, 0, width, width, 0 , screen.root(), 0, 0, width, width, 0
, xcb::WINDOW_CLASS_INPUT_OUTPUT as u16 , xcb::WINDOW_CLASS_INPUT_OUTPUT as u16
, screen.root_visual() , screen.root_visual()
, &[(xcb::CW_BACK_PIXEL, screen.white_pixel())] );
, &[(xcb::CW_BACK_PIXEL, screen.black_pixel())] );
xcb::create_gc( &conn, gc, screen.root() xcb::create_gc( &conn, gc, screen.root()
, &[ (xcb::GC_FOREGROUND, screen.black_pixel())
, &[ (xcb::GC_FOREGROUND, screen.white_pixel())
, (xcb::GC_GRAPHICS_EXPOSURES, 0) ] ); , (xcb::GC_GRAPHICS_EXPOSURES, 0) ] );
let (shmid, shm) = getshm((width * height) as usize); let (shmid, shm) = getshm((width * height) as usize);
@ -120,8 +120,8 @@ fn getshm<'a>(size :usize) -> (i32, &'a mut [u32]) {
unsafe { unsafe {
let id = libc::shmget( libc::IPC_PRIVATE let id = libc::shmget( libc::IPC_PRIVATE
, size * 4
, libc::IPC_CREAT | 0o744 );
, size * 4
, libc::IPC_CREAT | 0o744 );
let ptr = libc::shmat(id, ptr::null(), 0); let ptr = libc::shmat(id, ptr::null(), 0);
(id as i32, from_raw_parts_mut(ptr as *mut u32, size)) (id as i32, from_raw_parts_mut(ptr as *mut u32, size))
} }
@ -233,6 +233,13 @@ impl<'a> Canvas for XcbCanvas<'a> {
} }
} }
fn put_text(&self, ofs :Coordinate, s :&str) {
let Coordinate(xofs, yofs) = ofs;
xcb::xproto::image_text_8( &self.conn, self.pixmap, self.gc
, xofs as i16, yofs as i16, s );
self.conn.flush();
}
fn show(&self) { fn show(&self) {
xcb::copy_area( &self.conn, self.pixmap, self.window, self.gc xcb::copy_area( &self.conn, self.pixmap, self.window, self.gc
, 0, 0, 0, 0 , 0, 0, 0, 0

Loading…
Cancel
Save