ebiten: Remove the error returning value from NewImageFromImage

Updates #1380
This commit is contained in:
Hajime Hoshi 2020-10-06 01:03:30 +09:00
parent c6053bcf14
commit 1b816eb249
42 changed files with 58 additions and 79 deletions

View File

@ -22,15 +22,10 @@ import (
)
var (
debugPrintTextImage *ebiten.Image
debugPrintTextImage = ebiten.NewImageFromImage(assets.CreateTextImage())
debugPrintTextSubImages = map[rune]*ebiten.Image{}
)
func init() {
img := assets.CreateTextImage()
debugPrintTextImage, _ = ebiten.NewImageFromImage(img)
}
// DebugPrint draws the string str on the image on left top corner.
//
// The available runes are in U+0000 to U+00FF, which is C0 Controls and Basic Latin and C1 Controls and Latin-1 Supplement.

View File

@ -45,9 +45,6 @@ func NewImageFromFile(path string) (*ebiten.Image, image.Image, error) {
if err != nil {
return nil, nil, err
}
img2, err := ebiten.NewImageFromImage(img)
if err != nil {
return nil, nil, err
}
img2 := ebiten.NewImageFromImage(img)
return img2, img, err
}

View File

@ -37,6 +37,6 @@ func NewImageFromURL(url string) (*ebiten.Image, error) {
return nil, err
}
eimg, _ := ebiten.NewImageFromImage(img)
eimg := ebiten.NewImageFromImage(img)
return eimg, nil
}

View File

@ -82,7 +82,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
ebitenImage, _ = ebiten.NewImageFromImage(img)
ebitenImage = ebiten.NewImageFromImage(img)
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Additive Blending (Ebiten Demo)")

View File

@ -48,7 +48,7 @@ func init() {
if err != nil {
log.Fatal(err)
}
ebitenImage, _ = ebiten.NewImageFromImage(img)
ebitenImage = ebiten.NewImageFromImage(img)
}
func drawRect(screen *ebiten.Image, img *ebiten.Image, x, y, width, height float32, address ebiten.Address, msg string) {

View File

@ -61,7 +61,7 @@ func init() {
if err != nil {
log.Fatal(err)
}
gophersImage, _ = ebiten.NewImageFromImage(img)
gophersImage = ebiten.NewImageFromImage(img)
const repeat = 5
w, h := gophersImage.Size()
@ -89,7 +89,7 @@ func init() {
fogRGBA.SetRGBA(i, j, clr)
}
}
fogImage, _ = ebiten.NewImageFromImage(fogRGBA)
fogImage = ebiten.NewImageFromImage(fogRGBA)
}
// player represents the current airship's position.

View File

@ -91,7 +91,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
ebitenImage, _ = ebiten.NewImageFromImage(img)
ebitenImage = ebiten.NewImageFromImage(img)
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Alpha Blending (Ebiten Demo)")

View File

@ -77,7 +77,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
runnerImage, _ = ebiten.NewImageFromImage(img)
runnerImage = ebiten.NewImageFromImage(img)
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Animation (Ebiten Demo)")

View File

@ -132,7 +132,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
img, _ = ebiten.NewImageFromImage(rawimg)
img = ebiten.NewImageFromImage(rawimg)
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Audio Panning Loop (Ebiten Demo)")

View File

@ -76,7 +76,7 @@ func init() {
if err != nil {
panic(err)
}
imageGameBG, _ = ebiten.NewImageFromImage(img)
imageGameBG = ebiten.NewImageFromImage(img)
// Windows: Field
x, y := fieldWindowPosition()

View File

@ -30,7 +30,7 @@ func init() {
if err != nil {
panic(err)
}
imageBlocks, _ = ebiten.NewImageFromImage(img)
imageBlocks = ebiten.NewImageFromImage(img)
}

View File

@ -32,7 +32,7 @@ func init() {
if err != nil {
panic(err)
}
imageBackground, _ = ebiten.NewImageFromImage(img)
imageBackground = ebiten.NewImageFromImage(img)
}
type TitleScene struct {

View File

@ -82,7 +82,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
gophersImage, _ = ebiten.NewImageFromImage(img)
gophersImage = ebiten.NewImageFromImage(img)
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Blur (Ebiten Demo)")

View File

@ -65,7 +65,7 @@ func init() {
if err != nil {
log.Fatal(err)
}
tilesImage, _ = ebiten.NewImageFromImage(img)
tilesImage = ebiten.NewImageFromImage(img)
}
type Camera struct {

View File

@ -203,7 +203,7 @@ func init() {
if err != nil {
log.Fatal(err)
}
ebitenImage, _ = ebiten.NewImageFromImage(img)
ebitenImage = ebiten.NewImageFromImage(img)
}
func NewGame() *Game {

View File

@ -79,7 +79,7 @@ func main() {
log.Fatal(err)
}
ebitenImage, _ = ebiten.NewImageFromImage(img)
ebitenImage = ebiten.NewImageFromImage(img)
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Filter (Ebiten Demo)")

View File

@ -82,13 +82,13 @@ func init() {
if err != nil {
log.Fatal(err)
}
gopherImage, _ = ebiten.NewImageFromImage(img)
gopherImage = ebiten.NewImageFromImage(img)
img, _, err = image.Decode(bytes.NewReader(resources.Tiles_png))
if err != nil {
log.Fatal(err)
}
tilesImage, _ = ebiten.NewImageFromImage(img)
tilesImage = ebiten.NewImageFromImage(img)
}
func init() {

View File

@ -103,7 +103,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
ebitenImage, _ = ebiten.NewImageFromImage(img)
ebitenImage = ebiten.NewImageFromImage(img)
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Flood fill with solid colors (Ebiten Demo)")

View File

@ -55,7 +55,7 @@ func init() {
if err != nil {
log.Fatal(err)
}
gophersImage, _ = ebiten.NewImageFromImage(img)
gophersImage = ebiten.NewImageFromImage(img)
}
func initFont() {

View File

@ -149,7 +149,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
gophersImage, _ = ebiten.NewImageFromImage(img)
gophersImage = ebiten.NewImageFromImage(img)
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("HSV (Ebiten Demo)")

View File

@ -75,7 +75,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
gophersImage, _ = ebiten.NewImageFromImage(img)
gophersImage = ebiten.NewImageFromImage(img)
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Hue (Ebiten Demo)")

View File

@ -51,7 +51,7 @@ func init() {
if err != nil {
log.Fatal(err)
}
bgImage, _ = ebiten.NewImageFromImage(img)
bgImage = ebiten.NewImageFromImage(img)
}
type viewport struct {

View File

@ -42,7 +42,7 @@ func init() {
log.Fatal(err)
}
keyboardImage, _ = ebiten.NewImageFromImage(img)
keyboardImage = ebiten.NewImageFromImage(img)
}
type Game struct {

View File

@ -55,19 +55,19 @@ func init() {
if err != nil {
log.Fatal(err)
}
gopher1, _ = ebiten.NewImageFromImage(img1)
gopher1 = ebiten.NewImageFromImage(img1)
img2, _, err := image.Decode(bytes.NewReader(rmascot.Out02_png))
if err != nil {
log.Fatal(err)
}
gopher2, _ = ebiten.NewImageFromImage(img2)
gopher2 = ebiten.NewImageFromImage(img2)
img3, _, err := image.Decode(bytes.NewReader(rmascot.Out03_png))
if err != nil {
log.Fatal(err)
}
gopher3, _ = ebiten.NewImageFromImage(img3)
gopher3 = ebiten.NewImageFromImage(img3)
}
func init() {

View File

@ -54,13 +54,13 @@ func init() {
if err != nil {
log.Fatal(err)
}
bgImage, _ = ebiten.NewImageFromImage(img)
bgImage = ebiten.NewImageFromImage(img)
img, _, err = image.Decode(bytes.NewReader(images.FiveYears_jpg))
if err != nil {
log.Fatal(err)
}
fgImage, _ = ebiten.NewImageFromImage(img)
fgImage = ebiten.NewImageFromImage(img)
// Initialize the spot light image.
const r = 64
@ -75,7 +75,7 @@ func init() {
a.SetAlpha(i, j, color.Alpha{b})
}
}
spotLightImage, _ = ebiten.NewImageFromImage(a)
spotLightImage = ebiten.NewImageFromImage(a)
}
type Game struct {

View File

@ -111,7 +111,7 @@ func main() {
log.Fatal(err)
}
gophersImage, _ = ebiten.NewImageFromImage(img)
gophersImage = ebiten.NewImageFromImage(img)
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Minify (Ebiten Demo)")

View File

@ -52,7 +52,7 @@ func init() {
if err != nil {
log.Fatal(err)
}
gophersImage, _ = ebiten.NewImageFromImage(img)
gophersImage = ebiten.NewImageFromImage(img)
}
type Game struct {

View File

@ -49,7 +49,7 @@ func init() {
a1, a2, a2, a1,
a0, a1, a1, a0,
}
brushImage, _ = ebiten.NewImageFromImage(&image.Alpha{
brushImage = ebiten.NewImageFromImage(&image.Alpha{
Pix: pixels,
Stride: 4,
Rect: image.Rect(0, 0, 4, 4),

View File

@ -58,7 +58,7 @@ func init() {
if err != nil {
log.Fatal(err)
}
smokeImage, _ = ebiten.NewImageFromImage(img)
smokeImage = ebiten.NewImageFromImage(img)
}
type sprite struct {

View File

@ -82,7 +82,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
gophersImage, _ = ebiten.NewImageFromImage(img)
gophersImage = ebiten.NewImageFromImage(img)
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Perspective (Ebiten Demo)")

View File

@ -47,25 +47,25 @@ func init() {
if err != nil {
panic(err)
}
rightSprite, _ = ebiten.NewImageFromImage(img)
rightSprite = ebiten.NewImageFromImage(img)
img, _, err = image.Decode(bytes.NewReader(rplatformer.Left_png))
if err != nil {
panic(err)
}
leftSprite, _ = ebiten.NewImageFromImage(img)
leftSprite = ebiten.NewImageFromImage(img)
img, _, err = image.Decode(bytes.NewReader(rplatformer.MainChar_png))
if err != nil {
panic(err)
}
idleSprite, _ = ebiten.NewImageFromImage(img)
idleSprite = ebiten.NewImageFromImage(img)
img, _, err = image.Decode(bytes.NewReader(rplatformer.Background_png))
if err != nil {
panic(err)
}
backgroundImage, _ = ebiten.NewImageFromImage(img)
backgroundImage = ebiten.NewImageFromImage(img)
}
const (

View File

@ -59,7 +59,7 @@ func init() {
if err != nil {
log.Fatal(err)
}
bgImage, _ = ebiten.NewImageFromImage(img)
bgImage = ebiten.NewImageFromImage(img)
triangleImage.Fill(color.White)
}

View File

@ -82,7 +82,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
gophersImage, _ = ebiten.NewImageFromImage(img)
gophersImage = ebiten.NewImageFromImage(img)
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Rotate (Ebiten Demo)")

View File

@ -54,7 +54,7 @@ func init() {
if err != nil {
log.Fatal(err)
}
gopherImage, _ = ebiten.NewImageFromImage(img)
gopherImage = ebiten.NewImageFromImage(img)
}
func init() {
@ -62,7 +62,7 @@ func init() {
if err != nil {
log.Fatal(err)
}
gopherBgImage, _ = ebiten.NewImageFromImage(img)
gopherBgImage = ebiten.NewImageFromImage(img)
}
func init() {
@ -70,7 +70,7 @@ func init() {
if err != nil {
log.Fatal(err)
}
normalImage, _ = ebiten.NewImageFromImage(img)
normalImage = ebiten.NewImageFromImage(img)
}
func init() {
@ -78,7 +78,7 @@ func init() {
if err != nil {
log.Fatal(err)
}
noiseImage, _ = ebiten.NewImageFromImage(img)
noiseImage = ebiten.NewImageFromImage(img)
}
var shaderSrcs = [][]byte{

View File

@ -54,7 +54,7 @@ func init() {
if err != nil {
log.Fatal(err)
}
origEbitenImage, _ := ebiten.NewImageFromImage(img)
origEbitenImage := ebiten.NewImageFromImage(img)
w, h := origEbitenImage.Size()
ebitenImage = ebiten.NewImage(w, h)

View File

@ -56,7 +56,7 @@ func init() {
if err != nil {
log.Fatal(err)
}
origEbitenImage, _ := ebiten.NewImageFromImage(img)
origEbitenImage := ebiten.NewImageFromImage(img)
w, h := origEbitenImage.Size()
ebitenImage = ebiten.NewImage(w, h)

View File

@ -56,7 +56,7 @@ func init() {
if err != nil {
log.Fatal(err)
}
tilesImage, _ = ebiten.NewImageFromImage(img)
tilesImage = ebiten.NewImageFromImage(img)
}
type Game struct {

View File

@ -58,7 +58,7 @@ func init() {
if err != nil {
log.Fatal(err)
}
uiImage, _ = ebiten.NewImageFromImage(img)
uiImage = ebiten.NewImageFromImage(img)
tt, err := opentype.Parse(goregular.TTF)
if err != nil {

View File

@ -349,7 +349,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
gophersImage, _ = ebiten.NewImageFromImage(img)
gophersImage = ebiten.NewImageFromImage(img)
ebiten.SetWindowIcon([]image.Image{createRandomIconImage()})

View File

@ -31,8 +31,6 @@ var panicOnErrorAtImageAt bool
// Image represents a rectangle set of pixels.
// The pixel format is alpha-premultiplied RGBA.
// Image implements image.Image and draw.Image.
//
// Functions of Image never returns error as of 1.5.0, and error values are always nil.
type Image struct {
// addr holds self to check copying.
// See strings.Builder for similar examples.
@ -729,9 +727,7 @@ func NewImage(width, height int) *Image {
// NewImageFromImage creates a new image with the given image (source).
//
// If source's width or height is less than 1 or more than device-dependent maximum size, NewImageFromImage panics.
//
// Error returned by NewImageFromImage is always nil as of 1.5.0.
func NewImageFromImage(source image.Image) (*Image, error) {
func NewImageFromImage(source image.Image) *Image {
size := source.Bounds().Size()
width, height := size.X, size.Y
@ -743,7 +739,7 @@ func NewImageFromImage(source image.Image) (*Image, error) {
i.addr = i
i.ReplacePixels(imageToBytes(source))
return i, nil
return i
}
func newScreenFramebufferImage(width, height int) *Image {

View File

@ -59,10 +59,7 @@ func openEbitenImage() (*Image, image.Image, error) {
return nil, nil, err
}
eimg, err := NewImageFromImage(img)
if err != nil {
return nil, nil, err
}
eimg := NewImageFromImage(img)
return eimg, img, nil
}
@ -248,7 +245,7 @@ func TestImageDotByDotInversion(t *testing.T) {
func TestImageReplacePixels(t *testing.T) {
// Create a dummy image so that the shared texture is used and origImg's position is shfited.
dummyImg, _ := NewImageFromImage(image.NewRGBA(image.Rect(0, 0, 16, 16)))
dummyImg := NewImageFromImage(image.NewRGBA(image.Rect(0, 0, 16, 16)))
defer dummyImg.Dispose()
_, origImg, err := openEbitenImage()
@ -361,9 +358,7 @@ func TestNewImageFromEbitenImage(t *testing.T) {
t.Fatal(err)
return
}
if _, err := NewImageFromImage(img); err != nil {
t.Errorf("NewImageFromImage returns error: %v", err)
}
_ = NewImageFromImage(img)
}
func TestNewImageFromSubImage(t *testing.T) {
@ -374,11 +369,7 @@ func TestNewImageFromSubImage(t *testing.T) {
}
w, h := img.Bounds().Dx(), img.Bounds().Dy()
subImg := img.(*image.NRGBA).SubImage(image.Rect(1, 1, w-1, h-1))
eimg, err := NewImageFromImage(subImg)
if err != nil {
t.Fatal(err)
return
}
eimg := NewImageFromImage(subImg)
sw, sh := subImg.Bounds().Dx(), subImg.Bounds().Dy()
w2, h2 := eimg.Size()
if w2 != sw {

View File

@ -154,7 +154,7 @@ func getGlyphImages(face font.Face, runes []rune) []*ebiten.Image {
d.Dot = fixed.Point26_6{X: x, Y: y}
d.DrawString(string(r))
img, _ := ebiten.NewImageFromImage(rgba)
img := ebiten.NewImageFromImage(rgba)
if _, ok := glyphImageCache[face][r]; !ok {
glyphImageCache[face][r] = &glyphImageCacheEntry{
image: img,