Added function to easily access loaded textures.

This commit is contained in:
Marvin Blum
2016-05-06 17:38:32 +02:00
parent 51d61ba605
commit 11cf7a9251
3 changed files with 31 additions and 6 deletions

View File

@@ -4,3 +4,4 @@
* more logging
* limit FPS
* fullscreen
* simple access to default resources like GetTex()

View File

@@ -11,24 +11,25 @@ const (
type Game struct{}
func (g *Game) Setup() {
res, err := goga.LoadRes(gopher_path)
// load texture
_, err := goga.LoadRes(gopher_path)
if err != nil {
panic(err)
}
tex, ok := res.(*goga.Tex)
// create sprite
tex, err := goga.GetTex("gopher.png")
if !ok {
panic("Resource is not a texture")
if err != nil {
panic(err)
}
sprite := goga.NewSprite(tex)
goga.AddActor(sprite)
}
func (g *Game) Update(delta float64) {
}
func (g *Game) Update(delta float64) {}
func main() {
game := Game{}

23
res_util.go Normal file
View File

@@ -0,0 +1,23 @@
package goga
import (
"errors"
)
// Finds and returns a Tex resource.
// If not found or when the resource is of wrong type, an error will be returned.
func GetTex(name string) (*Tex, error) {
res := GetResByName(name)
if res == nil {
return nil, errors.New("Resource not found")
}
tex, ok := res.(*Tex)
if !ok {
return nil, errors.New("Resource was not of type *Tex")
}
return tex, nil
}