diff --git a/_examples/atlas/go.mod b/_examples/atlas/go.mod index 63e8eb0..1ff4cf0 100644 --- a/_examples/atlas/go.mod +++ b/_examples/atlas/go.mod @@ -4,6 +4,6 @@ go 1.24.0 replace github.com/firefly-zero/firefly-go => ../../ -require github.com/firefly-zero/firefly-go v0.12.0 +require github.com/firefly-zero/firefly-go v0.13.0 require github.com/orsinium-labs/tinymath v1.2.0 // indirect diff --git a/_examples/audio_file/go.mod b/_examples/audio_file/go.mod index 85af38f..c1e7f10 100644 --- a/_examples/audio_file/go.mod +++ b/_examples/audio_file/go.mod @@ -4,6 +4,6 @@ go 1.24.0 replace github.com/firefly-zero/firefly-go => ../../ -require github.com/firefly-zero/firefly-go v0.12.0 +require github.com/firefly-zero/firefly-go v0.13.0 require github.com/orsinium-labs/tinymath v1.2.0 // indirect diff --git a/_examples/audio_sine/go.mod b/_examples/audio_sine/go.mod index 325203d..d7eb73d 100644 --- a/_examples/audio_sine/go.mod +++ b/_examples/audio_sine/go.mod @@ -4,6 +4,6 @@ go 1.24.0 replace github.com/firefly-zero/firefly-go => ../../ -require github.com/firefly-zero/firefly-go v0.12.0 +require github.com/firefly-zero/firefly-go v0.13.0 require github.com/orsinium-labs/tinymath v1.2.0 // indirect diff --git a/_examples/canvas/go.mod b/_examples/canvas/go.mod index c700d3f..5f75d3f 100644 --- a/_examples/canvas/go.mod +++ b/_examples/canvas/go.mod @@ -4,6 +4,6 @@ go 1.24.0 replace github.com/firefly-zero/firefly-go => ../../ -require github.com/firefly-zero/firefly-go v0.12.0 +require github.com/firefly-zero/firefly-go v0.13.0 require github.com/orsinium-labs/tinymath v1.2.0 // indirect diff --git a/_examples/debug/go.mod b/_examples/debug/go.mod index 1239b39..867fbbb 100644 --- a/_examples/debug/go.mod +++ b/_examples/debug/go.mod @@ -4,6 +4,6 @@ go 1.24.0 replace github.com/firefly-zero/firefly-go => ../../ -require github.com/firefly-zero/firefly-go v0.12.0 +require github.com/firefly-zero/firefly-go v0.13.0 require github.com/orsinium-labs/tinymath v1.2.0 // indirect diff --git a/_examples/image/go.mod b/_examples/image/go.mod index 0ca1b5b..5e4ddeb 100644 --- a/_examples/image/go.mod +++ b/_examples/image/go.mod @@ -4,6 +4,6 @@ go 1.24.0 replace github.com/firefly-zero/firefly-go => ../../ -require github.com/firefly-zero/firefly-go v0.12.0 +require github.com/firefly-zero/firefly-go v0.13.0 require github.com/orsinium-labs/tinymath v1.2.0 // indirect diff --git a/_examples/qr/go.mod b/_examples/qr/go.mod index 4a07508..6b8c92b 100644 --- a/_examples/qr/go.mod +++ b/_examples/qr/go.mod @@ -4,6 +4,6 @@ go 1.24.0 replace github.com/firefly-zero/firefly-go => ../../ -require github.com/firefly-zero/firefly-go v0.12.0 +require github.com/firefly-zero/firefly-go v0.13.0 require github.com/orsinium-labs/tinymath v1.2.0 // indirect diff --git a/_examples/scores/go.mod b/_examples/scores/go.mod index d9b3b10..db85f8a 100644 --- a/_examples/scores/go.mod +++ b/_examples/scores/go.mod @@ -4,6 +4,6 @@ go 1.24.0 replace github.com/firefly-zero/firefly-go => ../../ -require github.com/firefly-zero/firefly-go v0.12.0 +require github.com/firefly-zero/firefly-go v0.13.0 require github.com/orsinium-labs/tinymath v1.2.0 // indirect diff --git a/_examples/sprite/go.mod b/_examples/sprite/go.mod index 42dbfe6..09f6923 100644 --- a/_examples/sprite/go.mod +++ b/_examples/sprite/go.mod @@ -4,6 +4,6 @@ go 1.24.0 replace github.com/firefly-zero/firefly-go => ../../ -require github.com/firefly-zero/firefly-go v0.12.0 +require github.com/firefly-zero/firefly-go v0.13.0 require github.com/orsinium-labs/tinymath v1.2.0 // indirect diff --git a/_examples/stash/go.mod b/_examples/stash/go.mod index 4bd2371..9304a34 100644 --- a/_examples/stash/go.mod +++ b/_examples/stash/go.mod @@ -4,6 +4,6 @@ go 1.24.0 replace github.com/firefly-zero/firefly-go => ../../ -require github.com/firefly-zero/firefly-go v0.12.0 +require github.com/firefly-zero/firefly-go v0.13.0 require github.com/orsinium-labs/tinymath v1.2.0 // indirect diff --git a/_examples/text/go.mod b/_examples/text/go.mod index d9b3b10..db85f8a 100644 --- a/_examples/text/go.mod +++ b/_examples/text/go.mod @@ -4,6 +4,6 @@ go 1.24.0 replace github.com/firefly-zero/firefly-go => ../../ -require github.com/firefly-zero/firefly-go v0.12.0 +require github.com/firefly-zero/firefly-go v0.13.0 require github.com/orsinium-labs/tinymath v1.2.0 // indirect diff --git a/_examples/time/firefly.toml b/_examples/time/firefly.toml new file mode 100644 index 0000000..614d597 --- /dev/null +++ b/_examples/time/firefly.toml @@ -0,0 +1,7 @@ +author_id = "demo" +app_id = "go-time" +author_name = "Demo" +app_name = "Time Demo (Go)" + +[files] +font = { path = "eg_6x10.fff", url = "https://fonts.fireflyzero.com/fonts/ascii/eg_6x10.fff" } diff --git a/_examples/time/go.mod b/_examples/time/go.mod new file mode 100644 index 0000000..db85f8a --- /dev/null +++ b/_examples/time/go.mod @@ -0,0 +1,9 @@ +module demotext + +go 1.24.0 + +replace github.com/firefly-zero/firefly-go => ../../ + +require github.com/firefly-zero/firefly-go v0.13.0 + +require github.com/orsinium-labs/tinymath v1.2.0 // indirect diff --git a/_examples/time/go.sum b/_examples/time/go.sum new file mode 100644 index 0000000..e8c4120 --- /dev/null +++ b/_examples/time/go.sum @@ -0,0 +1,2 @@ +github.com/orsinium-labs/tinymath v1.2.0 h1:mxbQzDQz00XQvPqPmIRCgXvYMyPCgCng1kYWfvjXr6M= +github.com/orsinium-labs/tinymath v1.2.0/go.mod h1:WPXX6ei3KSXG7JfA03a+ekCYaY9SWN4I+JRl2p6ck+A= diff --git a/_examples/time/time.go b/_examples/time/time.go new file mode 100644 index 0000000..e9a5078 --- /dev/null +++ b/_examples/time/time.go @@ -0,0 +1,22 @@ +package main + +import "github.com/firefly-zero/firefly-go/firefly" + +func init() { + firefly.Boot = boot + firefly.Render = render +} + +var font firefly.Font + +func boot() { + font = firefly.LoadFile("font", nil).Font() +} + +func render() { + firefly.ClearScreen(firefly.ColorWhite) + firefly.DrawText( + firefly.GetTime().String(), + font, firefly.Point{X: 10, Y: 60}, firefly.ColorDarkBlue, + ) +} diff --git a/_examples/touchpad/go.mod b/_examples/touchpad/go.mod index 0449ba5..3677904 100644 --- a/_examples/touchpad/go.mod +++ b/_examples/touchpad/go.mod @@ -4,6 +4,6 @@ go 1.24.0 replace github.com/firefly-zero/firefly-go => ../../ -require github.com/firefly-zero/firefly-go v0.12.0 +require github.com/firefly-zero/firefly-go v0.13.0 require github.com/orsinium-labs/tinymath v1.2.0 // indirect diff --git a/_examples/triangle/go.mod b/_examples/triangle/go.mod index 4788892..69bbb9f 100644 --- a/_examples/triangle/go.mod +++ b/_examples/triangle/go.mod @@ -4,6 +4,6 @@ go 1.24.0 replace github.com/firefly-zero/firefly-go => ../../ -require github.com/firefly-zero/firefly-go v0.12.0 +require github.com/firefly-zero/firefly-go v0.13.0 require github.com/orsinium-labs/tinymath v1.2.0 // indirect diff --git a/firefly/bindings.go b/firefly/bindings.go index 2cc43c9..a163f80 100644 --- a/firefly/bindings.go +++ b/firefly/bindings.go @@ -161,6 +161,9 @@ func setSeed(seed uint32) //go:wasmimport misc get_random func getRandom() uint32 +//go:wasmimport misc get_time +func getTime() uint64 + //go:wasmimport misc get_name func getName(index uint32, ptr unsafe.Pointer) uint32 diff --git a/firefly/misc.go b/firefly/misc.go index 26de17b..7f6c6e0 100644 --- a/firefly/misc.go +++ b/firefly/misc.go @@ -1,6 +1,9 @@ package firefly -import "unsafe" +import ( + "time" + "unsafe" +) type Language uint16 @@ -201,6 +204,11 @@ func GetRandom() uint32 { return getRandom() } +// Get the time passed since the app was started. +func GetTime() time.Duration { + return time.Duration(getTime()) * time.Microsecond +} + // Get human-readable name of the given peer. func GetName(p Peer) string { buf := [16]byte{} diff --git a/firefly/net.go b/firefly/net.go index 6fa8aec..cfab90f 100644 --- a/firefly/net.go +++ b/firefly/net.go @@ -63,7 +63,9 @@ var Combined = Peer{0xFF} // The list of peers online. // // Can be obtained using [GetPeers]. -type Peers uint32 +type Peers struct { + p uint32 +} // Stash is a serialized binary state of the app that you want to persist // between app runs and to be available in multiplayer. @@ -108,13 +110,13 @@ func (peers Peers) Iter() iter.Seq[Peer] { // Check if the given [Peer] is online. func (peers Peers) Contains(peer Peer) bool { - return peers>>peer.raw&1 != 0 + return peers.p>>peer.raw&1 != 0 } // Get how many peers are online. func (peers Peers) Len() int { // Should be converted by TinyGo in a single wasm instruction. - return bits.OnesCount32(uint32(peers)) + return bits.OnesCount32(peers.p) } // Get the peer corresponding to the local device. @@ -129,7 +131,7 @@ func GetMe() Me { // It can be used to detect if multiplayer is active: // if there is more than 1 peer, you're playing with friends. func GetPeers() Peers { - return Peers(getPeers()) + return Peers{getPeers()} } // Save the given [Stash].