Define textureError

This commit is contained in:
Hajime Hoshi 2013-06-23 16:06:32 +09:00
parent e0b36010f7
commit 81a7d4c4fd
5 changed files with 22 additions and 9 deletions

View File

@ -38,7 +38,9 @@ func (game *Rotating) Init(tf graphics.TextureFactory) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
game.ebitenTexture = tf.NewTextureFromImage(img) if game.ebitenTexture, err = tf.NewTextureFromImage(img); err != nil {
panic(err)
}
} }
func (game *Rotating) Update() { func (game *Rotating) Update() {

View File

@ -86,7 +86,9 @@ func (game *Sprites) Init(tf graphics.TextureFactory) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
game.ebitenTexture = tf.NewTextureFromImage(img) if game.ebitenTexture, err = tf.NewTextureFromImage(img); err != nil {
panic(err)
}
game.sprites = []*Sprite{} game.sprites = []*Sprite{}
for i := 0; i < 1000; i++ { for i := 0; i < 1000; i++ {
sprite := newSprite( sprite := newSprite(

View File

@ -40,7 +40,7 @@ type GraphicsContext interface {
type TextureFactory interface { type TextureFactory interface {
NewTexture(width, height int) Texture NewTexture(width, height int) Texture
NewTextureFromImage(img image.Image) Texture NewTextureFromImage(img image.Image) (Texture, error)
} }
type Texture struct { type Texture struct {

View File

@ -355,13 +355,16 @@ func (context *GraphicsContext) NewTexture(width, height int) graphics.Texture {
} }
} }
func (context *GraphicsContext) NewTextureFromImage(img image.Image) graphics.Texture { func (context *GraphicsContext) NewTextureFromImage(img image.Image) (graphics.Texture, error) {
texture := newTextureFromImage(img) texture, err := newTextureFromImage(img)
if err != nil {
return graphics.Texture{}, err
}
id := graphics.TextureID(texture.id) id := graphics.TextureID(texture.id)
context.textures[id] = texture context.textures[id] = texture
return graphics.Texture{ return graphics.Texture{
ID: id, ID: id,
Width: texture.width, Width: texture.width,
Height: texture.height, Height: texture.height,
} }, nil
} }

View File

@ -72,11 +72,17 @@ func createTexture(width, height int, pixels []uint8) *Texture {
return texture return texture
} }
type textureError string
func (err textureError) Error() string {
return "Texture Error: " + string(err)
}
func newTexture(width, height int) *Texture { func newTexture(width, height int) *Texture {
return createTexture(width, height, nil) return createTexture(width, height, nil)
} }
func newTextureFromImage(img image.Image) *Texture { func newTextureFromImage(img image.Image) (*Texture, error) {
var pix []uint8 var pix []uint8
switch img.(type) { switch img.(type) {
case *image.RGBA: case *image.RGBA:
@ -84,8 +90,8 @@ func newTextureFromImage(img image.Image) *Texture {
case *image.NRGBA: case *image.NRGBA:
pix = img.(*image.NRGBA).Pix pix = img.(*image.NRGBA).Pix
default: default:
panic("image should be RGBA or NRGBA") return nil, textureError("image format must be RGBA or NRGBA")
} }
size := img.Bounds().Size() size := img.Bounds().Size()
return createTexture(size.X, size.Y, pix) return createTexture(size.X, size.Y, pix), nil
} }