func loadImage(name string) *sdl.Surface { image := sdl.Load(name) if image == nil { panic(sdl.GetError()) } return image }
// Load a file as sprite set. elemWidth/elemHeight are the size of each // element in the sprite set func NewSpriteSet(file string, elemWidth uint, elemHeight uint) *SpriteSet { surface := sdl.Load(file) if surface == nil { log.Fatal("[SpriteSet] NewSpriteSet(): unable to load resource") } nbElemWidth := int(surface.W) / int(elemWidth) nbElemHeight := int(surface.H) / int(elemHeight) nbElem := nbElemWidth * nbElemHeight elemList := make([]Vector2d, nbElem) var x, y int = 0, 0 for i := 0; i < nbElem; i++ { elemList[i] = Vector2d{x * int(elemWidth), y * int(elemHeight)} x = (x + 1) % nbElemWidth if x == 0 { y++ } } return &SpriteSet{surface, elemWidth, elemHeight, elemList} }
func (this *SpriteLoader) loadAnims() { for _, anim := range this.data.Anims { if anim.Name == "" { log.Fatal("[SpriteLoader] gspr: 'anim' must contain", " a 'name'") } if anim.Frequency == 0 { log.Fatal("[SpriteLoader] gspr: 'anim' must contain", " a 'frequency'") } newAnim := NewAnim(anim.Name, anim.Frequency) for _, image := range anim.Images { if image.Path == "" && image.Tileset == "" { log.Fatal("[SpriteLoader] gspr: 'anim>image' must contain", " a 'path' or a 'tileset'") } if image.Path != "" && image.Tileset != "" { log.Fatal("[SpriteLoader] gspr: 'anim>image' must not contain", " either 'path' or 'tileset'") } if image.Tileset != "" { newAnim.AddSprite(image.Tileset, image.Sprite_num) } else { sprite := sdl.Load(image.Path) imageName := strconv.Itoa(int(this.singleImageCount)) this.mapSprite[imageName] = NewSpriteSet( image.Path, uint(sprite.W), uint(sprite.H), ) sprite.Free() newAnim.AddSprite(imageName, 0) this.singleImageCount++ } } this.animMap[anim.Name] = newAnim } }