mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-23 17:32:02 +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 {
|
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() {
|
||||||
|
@ -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(
|
||||||
|
@ -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 {
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user