sakuga

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2021 License: MIT Imports: 10 Imported by: 0

README

sakuga

Sprite animation library for Ebiten.

GoDoc

Example

source code

Steps to build an animation

Followings are the steps to make animation from a image data.

  • Create a Texture specifying the rectangle and the frame width & height for animation frames (i.e. tex := sakuga.NewTexture(image, image.Rect(0,0,64,32), 32, 32))
  • Create a sequence of Frames by calling Texture.Frames() ( i.e. frames := tex.Frames("1-4") )
  • Create an Animation from the Frames ( i.e. anime := sakuga.Anime(frames) )
  • Set attributes of the Animation ( i.e. anime.SetPosition(100, 100) or anime.SetDuration(time.Millisecond * 500))

Done!

Then you can call Animation.Update() and Animation.Draw() methods to display the animation.

It also provides on-the-fly texture packing ability (sakuga.Packer) to merge separated image files into a single texture at the program startup time if you want. Example is here.

Example

type Game struct {
	prevUpdateTime time.Time
	animes         []*sakuga.Animation
}

func NewGame() *Game {
	g := &Game{
		prevUpdateTime: time.Now(),
	}
	g.setupAnimations()

	return g
}

// initialize animations
func (g *Game) setupAnimations() {
	// create *ebiten.Image
	img := ebiten.NewImageFromImage(bytes2Image(&images.SAMURAI))

	// create *sakuga.Texture from *ebiten.Image
	// the second parameter is the area to use for the animation
	// the third parameter is the frame width & height to split the texture into the sequence of animation frames
	tex := sakuga.NewTexture(img, image.Rect(0, 0, 288, 480), 48, 48)

	// Initialize Animation

	// The parameter of Texture.Frames() is the indices of the frames
	attackAnime := sakuga.Anime(tex.Frames("4-6"))
	attackAnime.SetPosition(screenWidth/2+24, screenHeight/2)
	attackAnime.SetScale(2.0, 2.0)
	g.animes = append(g.animes, attackAnime)

	// Another animation
	walkAnime := sakuga.Anime(tex.Frames(2, 3))
	walkAnime.SetPosition(screenWidth/2-24, screenHeight/2)
	g.animes = append(g.animes, walkAnime)
}

func (g *Game) Draw(screen *ebiten.Image) {
	screen.Clear()
  
	// draw animations
	for _, anime := range g.animes {
		anime.Draw(screen)
	}
}

func (g *Game) Update() error {
	elapsedTime := time.Now().Sub(g.prevUpdateTime)

	// update animations
	for _, anime := range g.animes {
		anime.Update(elapsedTime)
	}

	g.prevUpdateTime = time.Now()
	return nil
}

source code

Output

Frame Index

You need to specify the frame indices to build an Animation from a Texture (calling Texture.Frames()).

The index number starts at the top left of the split frame and increases by 1 from left to right. When it reaches the most right frame, the number starts from the one row down and it goes the same.

For example, in the following image, the number on the each cell is the frame index number (staring from 1).

Documentation

Index

Constants

View Source
const (
	Playing = iota
	Paused
	Finished
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Animation

type Animation struct {
	// contains filtered or unexported fields
}

AnimeOptions represents an anime

func Anime

func Anime(frames AnimeFrames) *Animation

Anime make an animation (shorthand)

func NewAnimation

func NewAnimation(frames AnimeFrames) *Animation

NewAnimation craetes anime new Animation

func (*Animation) Angle

func (anime *Animation) Angle() float64

Angle returns angle

func (*Animation) Clone

func (anime *Animation) Clone() *Animation

Clone creates anime cloned anime

func (*Animation) Draw

func (anime *Animation) Draw(screen *ebiten.Image)

Draw draws the anime

func (*Animation) FlipH

func (anime *Animation) FlipH() bool

FlipH returns flipH value

func (*Animation) FlipV

func (anime *Animation) FlipV() bool

FlipV returns flipV value

func (*Animation) Height

func (anime *Animation) Height() float64

Height returns height

func (*Animation) Index

func (anime *Animation) Index() int

Index returns the current index of the anime

func (*Animation) IsFinished

func (anime *Animation) IsFinished() bool

IsFinished returns if the anime is finished

func (*Animation) IsPlaying

func (anime *Animation) IsPlaying() bool

IsFinished returns if the anime is playing

func (*Animation) Length

func (anime *Animation) Length() int

Size returns frame length

func (*Animation) Pause

func (anime *Animation) Pause()

Pause set the status paused

func (*Animation) PlayFromStart

func (anime *Animation) PlayFromStart()

PlayFromStart start the anime from the start frame

func (*Animation) Position

func (anime *Animation) Position() (float64, float64)

Position returns x, y

func (*Animation) Random

func (anime *Animation) Random()

Random randomize current frame

func (*Animation) Resume

func (anime *Animation) Resume()

Resume set the status playing

func (*Animation) ScaleX

func (anime *Animation) ScaleX() float64

ScaleX returns scale

func (*Animation) ScaleY

func (anime *Animation) ScaleY() float64

ScaleY returns scale

func (*Animation) SetAngle

func (anime *Animation) SetAngle(angle float64)

SetAngle sets the current angle of the anime

func (*Animation) SetDuration

func (anime *Animation) SetDuration(dur time.Duration)

SetDuration sets the duration of each frames

func (*Animation) SetFlipH

func (anime *Animation) SetFlipH(flipH bool)

SetFlipH sets the current value of flipH

func (*Animation) SetFlipV

func (anime *Animation) SetFlipV(flipV bool)

SetFlipV sets the current value of flipV

func (*Animation) SetFrames

func (anime *Animation) SetFrames(frames AnimeFrames)

SetFrames sets the frame of the anime

func (*Animation) SetIndex

func (anime *Animation) SetIndex(index int)

SetIndex sets the current index of the anime frame

func (*Animation) SetLooping

func (anime *Animation) SetLooping(looping bool)

SetLooping sets looping flag

func (*Animation) SetOrigin

func (anime *Animation) SetOrigin(x, y float64)

SetOrigin sets the origin of theAnimation

func (*Animation) SetPosition

func (anime *Animation) SetPosition(x, y float64)

SetPosition sets the current index of the anime

func (*Animation) SetScale

func (anime *Animation) SetScale(scaleX, scaleY float64)

SetScale sets the current scale of the anime

func (*Animation) SetX

func (anime *Animation) SetX(x float64)

SetX sets x

func (*Animation) SetY

func (anime *Animation) SetY(y float64)

SetY sets y

func (*Animation) Size

func (anime *Animation) Size() (float64, float64)

Size returns width, height

func (*Animation) Update

func (anime *Animation) Update(elapsedTime time.Duration)

Update updates the anime

func (*Animation) Width

func (anime *Animation) Width() float64

Width returns width

func (*Animation) X

func (anime *Animation) X() float64

X returns x

func (*Animation) Y

func (anime *Animation) Y() float64

y returns y

type AnimeFrames

type AnimeFrames []*Frame

type Frame

type Frame struct {
	// contains filtered or unexported fields
}

func (*Frame) Size

func (f *Frame) Size() (int, int)

type Image

type Image struct {
	Image *ebiten.Image
}

type Origin

type Origin struct {
	X, Y float64
}

type Packer

type Packer struct {
	// contains filtered or unexported fields
}

Packer packs multiple images into a single image dynamically for performance reasons

func (*Packer) Add

func (packer *Packer) Add(id TextureId, rawImage *[]byte, rect image.Rectangle, w, h int)

Add adds rawImage for packing

func (*Packer) Pack

func (packer *Packer) Pack(w, h int)

Pack packs images into a TextureAtlas

func (*Packer) PackedImage

func (packer *Packer) PackedImage() *ebiten.Image

PackedImage returns the underlying packedImage

func (*Packer) Texture

func (packer *Packer) Texture(id TextureId) *Texture

Texture returns a texture of the specified id

type Rectangle

type Rectangle struct {
	W, H, X, Y int
}

Rectangle represents a rectangle area

func Rect

func Rect(w, h int) Rectangle

Rect returns a Rectangle object

type SimplePacker

type SimplePacker struct{}

SimplePacker packs rectagles with a simple algorithm

func (SimplePacker) Pack

func (packer SimplePacker) Pack(R []*Rectangle, maxWidth, maxHeight int)

Pack computes rectangle locations without overlapping

type Status

type Status int

type SubImage

type SubImage struct {
	Image *ebiten.Image
	Rect  image.Rectangle
}

func SubImg

func SubImg(i *ebiten.Image, x0, y0, x1, y1 int) *SubImage

type Texture

type Texture struct {
	// contains filtered or unexported fields
}

Texture represents a texture

func NewTexture

func NewTexture(img *ebiten.Image, rect image.Rectangle, w, h int) *Texture

NewTexture creates a new texture

func (*Texture) Frames

func (tex *Texture) Frames(args ...frameArg) []*Frame

Frames returns the []Frames that is specified by the args Ex1: Frames(1, 3) -> returns 1 and 3 frames. parameter indicates the frame indices starting left-top to right-bottom Ex2: Frames("1-6") -> equivalent to Frames(1, 2, 3, 4, 5, 6) Ex3: Frames() -> equivalent to all frames

func (*Texture) Image

func (tex *Texture) Image() *ebiten.Image

Image returns the underlying image of the texture

type TextureId

type TextureId interface{}

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL