mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
Define textureError
This commit is contained in:
parent
e0b36010f7
commit
81a7d4c4fd
@ -38,7 +38,9 @@ func (game *Rotating) Init(tf graphics.TextureFactory) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
game.ebitenTexture = tf.NewTextureFromImage(img)
|
||||
if game.ebitenTexture, err = tf.NewTextureFromImage(img); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (game *Rotating) Update() {
|
||||
|
@ -86,7 +86,9 @@ func (game *Sprites) Init(tf graphics.TextureFactory) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
game.ebitenTexture = tf.NewTextureFromImage(img)
|
||||
if game.ebitenTexture, err = tf.NewTextureFromImage(img); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
game.sprites = []*Sprite{}
|
||||
for i := 0; i < 1000; i++ {
|
||||
sprite := newSprite(
|
||||
|
@ -40,7 +40,7 @@ type GraphicsContext interface {
|
||||
|
||||
type TextureFactory interface {
|
||||
NewTexture(width, height int) Texture
|
||||
NewTextureFromImage(img image.Image) Texture
|
||||
NewTextureFromImage(img image.Image) (Texture, error)
|
||||
}
|
||||
|
||||
type Texture struct {
|
||||
|
@ -355,13 +355,16 @@ func (context *GraphicsContext) NewTexture(width, height int) graphics.Texture {
|
||||
}
|
||||
}
|
||||
|
||||
func (context *GraphicsContext) NewTextureFromImage(img image.Image) graphics.Texture {
|
||||
texture := newTextureFromImage(img)
|
||||
func (context *GraphicsContext) NewTextureFromImage(img image.Image) (graphics.Texture, error) {
|
||||
texture, err := newTextureFromImage(img)
|
||||
if err != nil {
|
||||
return graphics.Texture{}, err
|
||||
}
|
||||
id := graphics.TextureID(texture.id)
|
||||
context.textures[id] = texture
|
||||
return graphics.Texture{
|
||||
ID: id,
|
||||
Width: texture.width,
|
||||
Height: texture.height,
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
@ -72,11 +72,17 @@ func createTexture(width, height int, pixels []uint8) *Texture {
|
||||
return texture
|
||||
}
|
||||
|
||||
type textureError string
|
||||
|
||||
func (err textureError) Error() string {
|
||||
return "Texture Error: " + string(err)
|
||||
}
|
||||
|
||||
func newTexture(width, height int) *Texture {
|
||||
return createTexture(width, height, nil)
|
||||
}
|
||||
|
||||
func newTextureFromImage(img image.Image) *Texture {
|
||||
func newTextureFromImage(img image.Image) (*Texture, error) {
|
||||
var pix []uint8
|
||||
switch img.(type) {
|
||||
case *image.RGBA:
|
||||
@ -84,8 +90,8 @@ func newTextureFromImage(img image.Image) *Texture {
|
||||
case *image.NRGBA:
|
||||
pix = img.(*image.NRGBA).Pix
|
||||
default:
|
||||
panic("image should be RGBA or NRGBA")
|
||||
return nil, textureError("image format must be RGBA or NRGBA")
|
||||
}
|
||||
size := img.Bounds().Size()
|
||||
return createTexture(size.X, size.Y, pix)
|
||||
return createTexture(size.X, size.Y, pix), nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user