Pass delay to Loop

This commit is contained in:
Dimitri Sokolyuk
2023-01-15 22:29:50 +01:00
parent 2d544291d8
commit 4b25db9da8
3 changed files with 17 additions and 22 deletions

View File

@ -16,8 +16,7 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
defer f.Close() defer f.Close()
lastFrame := time.Now() for f.Loop(time.Second / 60) {
for f.Loop() {
// If escape is pressed - exit // If escape is pressed - exit
if f.Key(27) { if f.Key(27) {
break break
@ -28,11 +27,5 @@ func main() {
f.Set(j, i, fenster.RGB(rand.Uint32())) 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()
} }
} }

View File

@ -29,7 +29,6 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
defer f.Close() defer f.Close()
lastFrame := time.Now()
img, err := openImage("testdata/Testbild.png") img, err := openImage("testdata/Testbild.png")
if err != nil { if err != nil {
@ -40,7 +39,7 @@ func main() {
pimg := image.NewPaletted(img.Bounds(), palette.Plan9) pimg := image.NewPaletted(img.Bounds(), palette.Plan9)
draw.Draw(pimg, pimg.Bounds(), img, image.Point{}, draw.Src) 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 escape is pressed - exit
if f.Key(27) { if f.Key(27) {
break break
@ -49,12 +48,5 @@ func main() {
// rotate palette // rotate palette
pimg.Palette = append(pimg.Palette[1:], pimg.Palette[0]) pimg.Palette = append(pimg.Palette[1:], pimg.Palette[0])
draw.Draw(f, f.Bounds(), pimg, image.Point{}, draw.Src) 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()
} }
} }

View File

@ -13,6 +13,7 @@ import (
"image/color" "image/color"
"image/draw" "image/draw"
"runtime" "runtime"
"time"
"unsafe" "unsafe"
) )
@ -22,7 +23,7 @@ func init() {
} }
type Fenster interface { type Fenster interface {
Loop() bool Loop(d time.Duration) bool
Close() Close()
Key(byte) bool Key(byte) bool
Mod() Mods Mod() Mods
@ -56,8 +57,9 @@ func rgbModel(c color.Color) color.Color {
type Mods int type Mods int
type fenster struct { type fenster struct {
f C.struct_fenster f C.struct_fenster
buf []uint32 buf []uint32
lastFrame time.Time
} }
func (f *fenster) ColorModel() color.Model { return RGBModel } 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.height = C.int(h)
f.f.buf = (*C.uint32_t)(C.malloc(C.size_t(w * h * 4))) 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.buf = unsafe.Slice((*uint32)(f.f.buf), w*h)
f.lastFrame = time.Now()
res := C.fenster_open(&f.f) res := C.fenster_open(&f.f)
if res != 0 { if res != 0 {
return nil, fmt.Errorf("failed to open window: %d", res) return nil, fmt.Errorf("failed to open window: %d", res)
} }
return f, nil 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) Key(code byte) bool { return f.f.keys[code] != 0 }
func (f *fenster) Mod() Mods { return Mods(f.f.mod) } func (f *fenster) Mod() Mods { return Mods(f.f.mod) }