From 4b25db9da8a8942e06064725cdcb25535293cf77 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 15 Jan 2023 22:29:50 +0100 Subject: [PATCH] Pass delay to Loop --- examples/example.go | 9 +-------- examples/testimage.go | 10 +--------- fenster.go | 20 +++++++++++++++----- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/examples/example.go b/examples/example.go index 8c42c61..784f54a 100644 --- a/examples/example.go +++ b/examples/example.go @@ -16,8 +16,7 @@ func main() { log.Fatal(err) } defer f.Close() - lastFrame := time.Now() - for f.Loop() { + for f.Loop(time.Second / 60) { // If escape is pressed - exit if f.Key(27) { break @@ -28,11 +27,5 @@ func main() { f.Set(j, i, fenster.RGB(rand.Uint32())) } } - // Wait for FPS rate - sleep := 16*time.Millisecond - time.Since(lastFrame) - if sleep > 0 { - time.Sleep(sleep) - } - lastFrame = time.Now() } } diff --git a/examples/testimage.go b/examples/testimage.go index 760181b..046b52b 100644 --- a/examples/testimage.go +++ b/examples/testimage.go @@ -29,7 +29,6 @@ func main() { log.Fatal(err) } defer f.Close() - lastFrame := time.Now() img, err := openImage("testdata/Testbild.png") if err != nil { @@ -40,7 +39,7 @@ func main() { pimg := image.NewPaletted(img.Bounds(), palette.Plan9) draw.Draw(pimg, pimg.Bounds(), img, image.Point{}, draw.Src) - for f.Loop() { + for f.Loop(time.Second / 24) { // If escape is pressed - exit if f.Key(27) { break @@ -49,12 +48,5 @@ func main() { // rotate palette pimg.Palette = append(pimg.Palette[1:], pimg.Palette[0]) draw.Draw(f, f.Bounds(), pimg, image.Point{}, draw.Src) - - // Wait for FPS rate - sleep := 16*time.Millisecond - time.Since(lastFrame) - if sleep > 0 { - time.Sleep(sleep) - } - lastFrame = time.Now() } } diff --git a/fenster.go b/fenster.go index 45f58b6..c81c95d 100644 --- a/fenster.go +++ b/fenster.go @@ -13,6 +13,7 @@ import ( "image/color" "image/draw" "runtime" + "time" "unsafe" ) @@ -22,7 +23,7 @@ func init() { } type Fenster interface { - Loop() bool + Loop(d time.Duration) bool Close() Key(byte) bool Mod() Mods @@ -56,8 +57,9 @@ func rgbModel(c color.Color) color.Color { type Mods int type fenster struct { - f C.struct_fenster - buf []uint32 + f C.struct_fenster + buf []uint32 + lastFrame time.Time } func (f *fenster) ColorModel() color.Model { return RGBModel } @@ -72,14 +74,22 @@ func New(w, h int, title string) (Fenster, error) { f.f.height = C.int(h) f.f.buf = (*C.uint32_t)(C.malloc(C.size_t(w * h * 4))) f.buf = unsafe.Slice((*uint32)(f.f.buf), w*h) + f.lastFrame = time.Now() res := C.fenster_open(&f.f) if res != 0 { return nil, fmt.Errorf("failed to open window: %d", res) } return f, nil } -func (f *fenster) Loop() bool { return C.fenster_loop(&f.f) == 0 } -func (f *fenster) Close() { C.fenster_close(&f.f) } + +func (f *fenster) Close() { C.fenster_close(&f.f) } +func (f *fenster) Loop(d time.Duration) bool { + if sleep := d - time.Since(f.lastFrame); sleep > 0 { + time.Sleep(sleep) + } + f.lastFrame = time.Now() + return C.fenster_loop(&f.f) == 0 +} func (f *fenster) Key(code byte) bool { return f.f.keys[code] != 0 } func (f *fenster) Mod() Mods { return Mods(f.f.mod) }