-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Allen Ray
committed
Aug 14, 2020
1 parent
7ab6c44
commit 1a5aea8
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |