-
Notifications
You must be signed in to change notification settings - Fork 11
/
image.go
54 lines (45 loc) · 1.11 KB
/
image.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package strife
import (
"fmt"
img "github.com/veandco/go-sdl2/img"
"github.com/veandco/go-sdl2/sdl"
)
// Image wraps a SDL texture _and_ SDL surface
type Image struct {
*sdl.Texture
*sdl.Surface
Width, Height int
}
// LoadImage will load the image at the given path. It will
// return the loaded image, and any errors encountered.
func LoadImage(path string) (*Image, error) {
surface, err := img.Load(path)
if err != nil {
return nil, fmt.Errorf("Failed to load image '%s'\n", path)
}
if RenderInstance == nil {
return nil, fmt.Errorf("Render context has not been initialized yet.")
}
texture, err := RenderInstance.CreateTextureFromSurface(surface)
if err != nil {
surface.Free()
return nil, fmt.Errorf("Failed to load '%s' into memory\n", path)
}
image := &Image{
texture,
surface,
int(surface.W),
int(surface.H),
}
return image, nil
}
// GetSurface returns a pointer to the SDL_Surface object
func (i *Image) GetSurface() *sdl.Surface {
return i.Surface
}
// Destroy must be invoked when finished with the
// resource.
func (i *Image) Destroy() {
i.Texture.Destroy()
i.Surface.Free()
}