Skip to content

Commit

Permalink
Adding sprite loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Allen Ray committed Aug 14, 2020
1 parent 7ab6c44 commit 1a5aea8
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions sprite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package pixelutils

import (
"image"
"os"

"github.com/faiface/pixel"
)

// Helper to load a sprite from file and make a Pixel.Sprite from it
func LoadSprite(path string) (sprite *pixel.Sprite, err error) {
var data *pixel.PictureData
if data, err = LoadPictureData(path); err != nil {
return nil, err
}

return pixel.NewSprite(data, data.Bounds()), nil
}

// Helper to load a sprite from a file and make Pixel.PictureData from it
func LoadPictureData(path string) (data *pixel.PictureData, err error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()

img, _, err := image.Decode(file)
if err != nil {
return nil, err
}

return pixel.PictureDataFromImage(img), nil
}

0 comments on commit 1a5aea8

Please sign in to comment.