Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions _examples/atlas/atlas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import "github.com/firefly-zero/firefly-go/firefly"

func init() {
firefly.Boot = boot
firefly.Render = render
}

var (
atlas = firefly.NewAtlas(16, 16)

knight = atlas.Sprite(0, 0)
wall = atlas.Sprite(2, 0)
cat = atlas.Sprite(3, 0)
key = atlas.Sprite(4, 0)
door = atlas.Sprite(0, 1)
)

var frame = 0

func boot() {
atlas.Load("atlas")
}

func render() {
firefly.ClearScreen(firefly.ColorBlack)

w := 16
h := 16

// top wall
wall.Draw(firefly.P(2*w, 2*h))
wall.Draw(firefly.P(3*w, 2*h))
wall.Draw(firefly.P(4*w, 2*h))
wall.Draw(firefly.P(5*w, 2*h))
wall.Draw(firefly.P(6*w, 2*h))

// bottom wall
wall.Draw(firefly.P(2*w, 6*h))
wall.Draw(firefly.P(3*w, 6*h))
door.Draw(firefly.P(4*w, 6*h))
wall.Draw(firefly.P(5*w, 6*h))
wall.Draw(firefly.P(6*w, 6*h))

// left wall
wall.Draw(firefly.P(2*w, 2*h))
wall.Draw(firefly.P(2*w, 3*h))
wall.Draw(firefly.P(2*w, 4*h))
wall.Draw(firefly.P(2*w, 5*h))
wall.Draw(firefly.P(2*w, 6*h))

// right wall
wall.Draw(firefly.P(6*w, 2*h))
wall.Draw(firefly.P(6*w, 3*h))
wall.Draw(firefly.P(6*w, 4*h))
wall.Draw(firefly.P(6*w, 5*h))
wall.Draw(firefly.P(6*w, 6*h))

// characters and items
knight.Draw(firefly.P(3*w, 4*h))
cat.Draw(firefly.P(5*w, 4*h))
key.Draw(firefly.P(4*w, 5*h))
}
Binary file added _examples/atlas/atlas.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions _examples/atlas/firefly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
author_id = "demo"
app_id = "go-atlas"
author_name = "Demo"
app_name = "Atlas Demo (Go)"

[files]
atlas = { path = "atlas.png" }
9 changes: 9 additions & 0 deletions _examples/atlas/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module atlas

go 1.24.0

replace github.com/firefly-zero/firefly-go => ../../

require github.com/firefly-zero/firefly-go v0.10.0

require github.com/orsinium-labs/tinymath v1.1.0 // indirect
4 changes: 4 additions & 0 deletions _examples/atlas/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/orsinium-labs/tinymath v1.0.0 h1:Uzp3GmjWIBxMObx4MQi9ACDu4Q8WKjSRakB1OMo9Bu0=
github.com/orsinium-labs/tinymath v1.0.0/go.mod h1:WPXX6ei3KSXG7JfA03a+ekCYaY9SWN4I+JRl2p6ck+A=
github.com/orsinium-labs/tinymath v1.1.0 h1:KomdsyLHB7vE3f1nRAJF2dyf1m/gnM2HxfTeV1vS5UA=
github.com/orsinium-labs/tinymath v1.1.0/go.mod h1:WPXX6ei3KSXG7JfA03a+ekCYaY9SWN4I+JRl2p6ck+A=
18 changes: 13 additions & 5 deletions firefly/callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,45 @@ var (
)

//go:export boot
func boot() { //nolint
func boot() {
if Boot != nil {
Boot()
}
}

//go:export update
func update() { //nolint
func update() {
if Update != nil {
Update()
}
}

//go:export render
func render() { //nolint
func render() {
if Render != nil {
Render()
}
}

//go:export before_exit
func beforeExit() { //nolint
func beforeExit() {
if BeforeExit != nil {
BeforeExit()
}
}

//go:export cheat
func cheat(c, v int32) int32 { //nolint
func cheat(c, v int32) int32 {
if Cheat != nil {
return int32(Cheat(int(c), int(v)))
}
return 0
}

var (
_ = boot
_ = update
_ = render
_ = beforeExit
_ = cheat
)
40 changes: 40 additions & 0 deletions firefly/graphics.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,46 @@ func (c Canvas) Image() Image {
return Image(c)
}

// Helper for working with spritesheets.
//
// Constructed by [NewAtlas].
type Atlas struct {
img Image
spriteSize Size
}

// Create a new [Atlas] with the given sprite dimensions.
func NewAtlas(spriteW, spriteH int) Atlas {
return Atlas{spriteSize: S(spriteW, spriteH)}
}

// Set the underlying spritesheet image for the atlas.
//
// Should be called before any [Sprite.Draw].
// The best is to call it once from [Boot].
func (a *Atlas) Load(path string) {
a.img = LoadImage(path, nil)
}

// Create a reference to a sprite within the atlas.
func (a *Atlas) Sprite(row, col int) Sprite {
pos := P(row*a.spriteSize.W, col*a.spriteSize.H)
return Sprite{atlas: a, pos: pos}
}

type Sprite struct {
atlas *Atlas
pos Point
}

// Render the sprite at the given position.
//
// Make sure to call [Atlas.Load] first.
func (s Sprite) Draw(p Point) {
sub := s.atlas.img.Sub(s.pos, s.atlas.spriteSize)
sub.Draw(p)
}

// Fill the whole frame with the given color.
func ClearScreen(c Color) {
clearScreen(int32(c))
Expand Down