ebiten: Remove the error returning value from NewImage

Updates #1380
This commit is contained in:
Hajime Hoshi 2020-10-06 00:33:05 +09:00
parent 54da0d9763
commit c6053bcf14
29 changed files with 153 additions and 243 deletions

View File

@ -23,11 +23,10 @@ import (
)
var (
emptyImage *ebiten.Image
emptyImage = ebiten.NewImage(1, 1)
)
func init() {
emptyImage, _ = ebiten.NewImage(1, 1)
emptyImage.Fill(color.White)
}

View File

@ -69,7 +69,7 @@ func (g *Game) Update() error {
func (g *Game) Draw(screen *ebiten.Image) {
if g.boardImage == nil {
w, h := g.board.Size()
g.boardImage, _ = ebiten.NewImage(w, h)
g.boardImage = ebiten.NewImage(w, h)
}
screen.Fill(backgroundColor)
g.board.Draw(g.boardImage)

View File

@ -351,11 +351,10 @@ const (
)
var (
tileImage *ebiten.Image
tileImage = ebiten.NewImage(tileSize, tileSize)
)
func init() {
tileImage, _ = ebiten.NewImage(tileSize, tileSize)
tileImage.Fill(color.White)
}

View File

@ -42,8 +42,8 @@ var (
gophersImage *ebiten.Image
repeatedGophersImage *ebiten.Image
groundImage *ebiten.Image
perspectiveGroundImage *ebiten.Image
groundImage = ebiten.NewImage(screenWidth*2, screenHeight*2/3+50)
perspectiveGroundImage = ebiten.NewImage(screenWidth*2, screenHeight)
fogImage *ebiten.Image
)
@ -63,12 +63,9 @@ func init() {
}
gophersImage, _ = ebiten.NewImageFromImage(img)
groundImage, _ = ebiten.NewImage(screenWidth*2, screenHeight*2/3+50)
perspectiveGroundImage, _ = ebiten.NewImage(screenWidth*2, screenHeight)
const repeat = 5
w, h := gophersImage.Size()
repeatedGophersImage, _ = ebiten.NewImage(w*repeat, h*repeat)
repeatedGophersImage = ebiten.NewImage(w*repeat, h*repeat)
for j := 0; j < repeat; j++ {
for i := 0; i < repeat; i++ {
op := &ebiten.DrawImageOptions{}

View File

@ -32,8 +32,8 @@ import (
var (
imageGameBG *ebiten.Image
imageWindows *ebiten.Image
imageGameover *ebiten.Image
imageWindows = ebiten.NewImage(ScreenWidth, ScreenHeight)
imageGameover = ebiten.NewImage(ScreenWidth, ScreenHeight)
)
func fieldWindowPosition() (x, y int) {
@ -78,9 +78,6 @@ func init() {
}
imageGameBG, _ = ebiten.NewImageFromImage(img)
// Windows
imageWindows, _ = ebiten.NewImage(ScreenWidth, ScreenHeight)
// Windows: Field
x, y := fieldWindowPosition()
drawWindow(imageWindows, x, y, fieldWidth, fieldHeight)
@ -104,7 +101,6 @@ func init() {
drawTextBox(imageWindows, "LINES", x, y, textBoxWidth())
// Gameover
imageGameover, _ = ebiten.NewImage(ScreenWidth, ScreenHeight)
imageGameover.Fill(color.NRGBA{0x00, 0x00, 0x00, 0x80})
y = (ScreenHeight - blockHeight) / 2
drawTextWithShadowCenter(imageGameover, "GAME OVER\n\nPRESS SPACE", 0, y, 1, color.White, ScreenWidth)

View File

@ -19,15 +19,10 @@ import (
)
var (
transitionFrom *ebiten.Image
transitionTo *ebiten.Image
transitionFrom = ebiten.NewImage(ScreenWidth, ScreenHeight)
transitionTo = ebiten.NewImage(ScreenWidth, ScreenHeight)
)
func init() {
transitionFrom, _ = ebiten.NewImage(ScreenWidth, ScreenHeight)
transitionTo, _ = ebiten.NewImage(ScreenWidth, ScreenHeight)
}
type Scene interface {
Update(state *GameState) error
Draw(screen *ebiten.Image)

View File

@ -261,7 +261,7 @@ func main() {
},
camera: Camera{ViewPort: f64.Vec2{screenWidth, screenHeight}},
}
g.world, _ = ebiten.NewImage(worldWidth, worldHeight)
g.world = ebiten.NewImage(worldWidth, worldHeight)
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Tiles (Ebiten Demo)")

View File

@ -33,11 +33,10 @@ const (
)
var (
dot *ebiten.Image
dot = ebiten.NewImage(1, 1)
)
func init() {
dot, _ = ebiten.NewImage(1, 1)
dot.Fill(color.White)
}

View File

@ -30,7 +30,7 @@ const (
)
var (
offscreen *ebiten.Image
offscreen = ebiten.NewImage(screenWidth, screenHeight)
offscreenPix []byte
palette [maxIt]byte
)
@ -69,7 +69,6 @@ func updateOffscreen(centerX, centerY, size float64) {
}
func init() {
offscreen, _ = ebiten.NewImage(screenWidth, screenHeight)
offscreenPix = make([]byte, screenWidth*screenHeight*4)
for i := range palette {
palette[i] = byte(math.Sqrt(float64(i)/float64(len(palette))) * 0x80)

View File

@ -36,7 +36,7 @@ const (
var (
bgImage *ebiten.Image
fgImage *ebiten.Image
maskedFgImage *ebiten.Image
maskedFgImage = ebiten.NewImage(screenWidth, screenHeight)
spotLightImage *ebiten.Image
)
@ -62,8 +62,6 @@ func init() {
}
fgImage, _ = ebiten.NewImageFromImage(img)
maskedFgImage, _ = ebiten.NewImage(screenWidth, screenHeight)
// Initialize the spot light image.
const r = 64
alphas := image.Point{r * 2, r * 2}

View File

@ -81,7 +81,7 @@ func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
func main() {
w, h := gophersImage.Size()
gophersRenderTarget, _ = ebiten.NewImage(w/mosaicRatio, h/mosaicRatio)
gophersRenderTarget = ebiten.NewImage(w/mosaicRatio, h/mosaicRatio)
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Mosaic (Ebiten Demo)")
if err := ebiten.RunGame(&Game{}); err != nil {

View File

@ -34,7 +34,7 @@ const (
var (
brushImage *ebiten.Image
canvasImage *ebiten.Image
canvasImage = ebiten.NewImage(screenWidth, screenHeight)
)
func init() {
@ -55,7 +55,6 @@ func init() {
Rect: image.Rect(0, 0, 4, 4),
})
canvasImage, _ = ebiten.NewImage(screenWidth, screenHeight)
canvasImage.Fill(color.White)
}

View File

@ -144,12 +144,10 @@ func playNote(freq float64) {
}
var (
pianoImage *ebiten.Image
pianoImage = ebiten.NewImage(screenWidth, screenHeight)
)
func init() {
pianoImage, _ = ebiten.NewImage(screenWidth, screenHeight)
const (
keyWidth = 24
y = 48

View File

@ -33,7 +33,7 @@ const (
)
var (
emptyImage, _ = ebiten.NewImage(16, 16)
emptyImage = ebiten.NewImage(16, 16)
)
func init() {

View File

@ -41,8 +41,8 @@ const (
var (
bgImage *ebiten.Image
shadowImage *ebiten.Image
triangleImage *ebiten.Image
shadowImage = ebiten.NewImage(screenWidth, screenHeight)
triangleImage = ebiten.NewImage(screenWidth, screenHeight)
)
func init() {
@ -60,8 +60,6 @@ func init() {
log.Fatal(err)
}
bgImage, _ = ebiten.NewImageFromImage(img)
shadowImage, _ = ebiten.NewImage(screenWidth, screenHeight)
triangleImage, _ = ebiten.NewImage(screenWidth, screenHeight)
triangleImage.Fill(color.White)
}

View File

@ -36,11 +36,7 @@ func init() {
rand.Seed(time.Now().UnixNano())
}
var offscreen *ebiten.Image
func init() {
offscreen, _ = ebiten.NewImage(screenWidth, screenHeight)
}
var offscreen = ebiten.NewImage(screenWidth, screenHeight)
type Game struct {
}

View File

@ -32,7 +32,7 @@ const (
)
var (
emptyImage, _ = ebiten.NewImage(16, 16)
emptyImage = ebiten.NewImage(16, 16)
)
func init() {

View File

@ -57,7 +57,7 @@ func init() {
origEbitenImage, _ := ebiten.NewImageFromImage(img)
w, h := origEbitenImage.Size()
ebitenImage, _ = ebiten.NewImage(w, h)
ebitenImage = ebiten.NewImage(w, h)
op := &ebiten.DrawImageOptions{}
op.ColorM.Scale(1, 1, 1, 0.5)

View File

@ -59,7 +59,7 @@ func init() {
origEbitenImage, _ := ebiten.NewImageFromImage(img)
w, h := origEbitenImage.Size()
ebitenImage, _ = ebiten.NewImage(w, h)
ebitenImage = ebiten.NewImage(w, h)
op := &ebiten.DrawImageOptions{}
op.ColorM.Scale(1, 1, 1, 0.5)

View File

@ -79,7 +79,7 @@ var (
}
selectedPalette = 0
colorCycle = 0
canvas *ebiten.Image
canvas = ebiten.NewImage(width, height)
auto *automaton
// blocker is an arbitrary color used to prevent the
// squirals from leaving the canvas.
@ -279,8 +279,6 @@ func setpix(xy vec2, col color.Color) {
func init() {
rand.Seed(time.Now().UnixNano())
c, _ := ebiten.NewImage(width, height)
canvas = c
canvas.Fill(background)
auto = &automaton{}

View File

@ -358,7 +358,7 @@ func (t *TextBox) Draw(dst *ebiten.Image) {
}
if t.contentBuf == nil {
w, h := t.viewSize()
t.contentBuf, _ = ebiten.NewImage(w, h)
t.contentBuf = ebiten.NewImage(w, h)
}
t.contentBuf.Clear()

View File

@ -25,10 +25,9 @@ import (
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
)
var pointerImage *ebiten.Image
var pointerImage = ebiten.NewImage(4, 4)
func init() {
pointerImage, _ = ebiten.NewImage(4, 4)
pointerImage.Fill(color.RGBA{0xff, 0, 0, 0xff})
}

View File

@ -717,13 +717,7 @@ func (i *Image) ReplacePixels(pixels []byte) {
// NewImage returns an empty image.
//
// If width or height is less than 1 or more than device-dependent maximum size, NewImage panics.
//
// Error returned by NewImage is always nil as of 1.5.0.
func NewImage(width, height int) (*Image, error) {
return newImage(width, height), nil
}
func newImage(width, height int) *Image {
func NewImage(width, height int) *Image {
i := &Image{
mipmap: mipmap.New(width, height),
bounds: image.Rect(0, 0, width, height),

View File

@ -123,36 +123,19 @@ func TestImageComposition(t *testing.T) {
w, h := img1.Bounds().Size().X, img1.Bounds().Size().Y
img2, err := NewImage(w, h)
if err != nil {
t.Fatal(err)
return
}
img3, err := NewImage(w, h)
if err != nil {
t.Fatal(err)
return
}
img2 := NewImage(w, h)
img3 := NewImage(w, h)
img2.Fill(img2Color)
img3.Fill(img3Color)
img_12_3, err := NewImage(w, h)
if err != nil {
t.Fatal(err)
return
}
img_12_3 := NewImage(w, h)
img2.DrawImage(img1, nil)
img3.DrawImage(img2, nil)
img_12_3.DrawImage(img3, nil)
img2.Fill(img2Color)
img3.Fill(img3Color)
img_1_23, err := NewImage(w, h)
if err != nil {
t.Fatal(err)
return
}
img_1_23 := NewImage(w, h)
img3.DrawImage(img2, nil)
img3.DrawImage(img1, nil)
img_1_23.DrawImage(img3, nil)
@ -197,11 +180,7 @@ func TestImageScale(t *testing.T) {
return
}
w, h := img0.Size()
img1, err := NewImage(w*scale, h*scale)
if err != nil {
t.Fatal(err)
return
}
img1 := NewImage(w*scale, h*scale)
op := &DrawImageOptions{}
op.GeoM.Scale(float64(scale), float64(scale))
@ -226,11 +205,7 @@ func TestImage90DegreeRotate(t *testing.T) {
return
}
w, h := img0.Size()
img1, err := NewImage(h, w)
if err != nil {
t.Fatal(err)
return
}
img1 := NewImage(h, w)
op := &DrawImageOptions{}
op.GeoM.Rotate(math.Pi / 2)
op.GeoM.Translate(float64(h), 0)
@ -254,11 +229,7 @@ func TestImageDotByDotInversion(t *testing.T) {
return
}
w, h := img0.Size()
img1, err := NewImage(w, h)
if err != nil {
t.Fatal(err)
return
}
img1 := NewImage(w, h)
op := &DrawImageOptions{}
op.GeoM.Rotate(math.Pi)
op.GeoM.Translate(float64(w), float64(h))
@ -290,11 +261,7 @@ func TestImageReplacePixels(t *testing.T) {
draw.Draw(img, img.Bounds(), origImg, image.ZP, draw.Src)
size := img.Bounds().Size()
img0, err := NewImage(size.X, size.Y)
if err != nil {
t.Fatal(err)
return
}
img0 := NewImage(size.X, size.Y)
img0.ReplacePixels(img.Pix)
for j := 0; j < img0.Bounds().Size().Y; j++ {
@ -334,21 +301,13 @@ func TestImageReplacePixelsNil(t *testing.T) {
}
}()
img, err := NewImage(16, 16)
if err != nil {
t.Fatal(err)
return
}
img := NewImage(16, 16)
img.Fill(color.White)
img.ReplacePixels(nil)
}
func TestImageDispose(t *testing.T) {
img, err := NewImage(16, 16)
if err != nil {
t.Fatal(err)
return
}
img := NewImage(16, 16)
img.Fill(color.White)
img.Dispose()
@ -376,11 +335,7 @@ func TestImageCompositeModeLighter(t *testing.T) {
}
w, h := img0.Size()
img1, err := NewImage(w, h)
if err != nil {
t.Fatal(err)
return
}
img1 := NewImage(w, h)
img1.Fill(color.RGBA{0x01, 0x02, 0x03, 0x04})
op := &DrawImageOptions{}
op.CompositeMode = CompositeModeLighter
@ -453,11 +408,7 @@ func (c *mutableRGBA) RGBA() (r, g, b, a uint32) {
func TestImageFill(t *testing.T) {
w, h := 10, 10
img, err := NewImage(w, h)
if err != nil {
t.Fatal(err)
return
}
img := NewImage(w, h)
clr := &mutableRGBA{0x80, 0x80, 0x80, 0x80}
img.Fill(clr)
clr.r = 0
@ -475,11 +426,7 @@ func TestImageFill(t *testing.T) {
// Issue #740
func TestImageClear(t *testing.T) {
const w, h = 128, 256
img, err := NewImage(w, h)
if err != nil {
t.Fatal(err)
return
}
img := NewImage(w, h)
img.Fill(color.White)
for j := 0; j < h; j++ {
for i := 0; i < w; i++ {
@ -519,7 +466,7 @@ func TestImageEdge(t *testing.T) {
img1Width = 32
img1Height = 32
)
img0, _ := NewImage(img0Width, img0Height)
img0 := NewImage(img0Width, img0Height)
pixels := make([]uint8, 4*img0Width*img0Height)
for j := 0; j < img0Height; j++ {
for i := 0; i < img0Width; i++ {
@ -531,7 +478,7 @@ func TestImageEdge(t *testing.T) {
}
}
img0.ReplacePixels(pixels)
img1, _ := NewImage(img1Width, img1Height)
img1 := NewImage(img1Width, img1Height)
red := color.RGBA{0xff, 0, 0, 0xff}
transparent := color.RGBA{0, 0, 0, 0}
@ -651,8 +598,8 @@ func TestImageTooManyFill(t *testing.T) {
return uint8((17*index + 0x40) % 256)
}
src, _ := NewImage(1, 1)
dst, _ := NewImage(width, 1)
src := NewImage(1, 1)
dst := NewImage(width, 1)
for i := 0; i < width; i++ {
c := indexToColor(i)
src.Fill(color.RGBA{c, c, c, 0xff})
@ -672,8 +619,8 @@ func TestImageTooManyFill(t *testing.T) {
}
func BenchmarkDrawImage(b *testing.B) {
img0, _ := NewImage(16, 16)
img1, _ := NewImage(16, 16)
img0 := NewImage(16, 16)
img1 := NewImage(16, 16)
op := &DrawImageOptions{}
for i := 0; i < b.N; i++ {
img0.DrawImage(img1, op)
@ -681,7 +628,7 @@ func BenchmarkDrawImage(b *testing.B) {
}
func TestImageLinearGradiation(t *testing.T) {
img0, _ := NewImage(2, 2)
img0 := NewImage(2, 2)
img0.ReplacePixels([]byte{
0xff, 0x00, 0x00, 0xff,
0x00, 0xff, 0x00, 0xff,
@ -690,7 +637,7 @@ func TestImageLinearGradiation(t *testing.T) {
})
const w, h = 32, 32
img1, _ := NewImage(w, h)
img1 := NewImage(w, h)
op := &DrawImageOptions{}
op.GeoM.Scale(w, h)
op.GeoM.Translate(-w/4, -h/4)
@ -708,8 +655,8 @@ func TestImageLinearGradiation(t *testing.T) {
}
func TestImageOutside(t *testing.T) {
src, _ := NewImage(5, 10) // internal texture size is 8x16.
dst, _ := NewImage(4, 4)
src := NewImage(5, 10) // internal texture size is 8x16.
dst := NewImage(4, 4)
src.Fill(color.RGBA{0xff, 0, 0, 0xff})
cases := []struct {
@ -750,9 +697,9 @@ func TestImageOutside(t *testing.T) {
}
func TestImageOutsideUpperLeft(t *testing.T) {
src, _ := NewImage(4, 4)
dst1, _ := NewImage(16, 16)
dst2, _ := NewImage(16, 16)
src := NewImage(4, 4)
dst1 := NewImage(16, 16)
dst2 := NewImage(16, 16)
src.Fill(color.RGBA{0xff, 0, 0, 0xff})
op := &DrawImageOptions{}
@ -779,7 +726,7 @@ func TestImageSize(t *testing.T) {
w = 17
h = 31
)
img, _ := NewImage(w, h)
img := NewImage(w, h)
gotW, gotH := img.Size()
if gotW != w {
t.Errorf("got: %d, want: %d", gotW, w)
@ -790,8 +737,8 @@ func TestImageSize(t *testing.T) {
}
func TestImageSize1(t *testing.T) {
src, _ := NewImage(1, 1)
dst, _ := NewImage(1, 1)
src := NewImage(1, 1)
dst := NewImage(1, 1)
src.Fill(color.White)
dst.DrawImage(src, nil)
got := src.At(0, 0).(color.RGBA)
@ -803,8 +750,8 @@ func TestImageSize1(t *testing.T) {
// TODO: Enable this test again. This test fails after #1217 is fixed.
func Skip_TestImageSize4096(t *testing.T) {
src, _ := NewImage(4096, 4096)
dst, _ := NewImage(4096, 4096)
src := NewImage(4096, 4096)
dst := NewImage(4096, 4096)
pix := make([]byte, 4096*4096*4)
for i := 0; i < 4096; i++ {
j := 4095
@ -849,7 +796,7 @@ func TestImageCopy(t *testing.T) {
}
}()
img0, _ := NewImage(256, 256)
img0 := NewImage(256, 256)
img1 := *img0
img1.Fill(color.Transparent)
}
@ -862,10 +809,10 @@ func TestImageStretch(t *testing.T) {
const w = 16
dst, _ := NewImage(w, 4096)
dst := NewImage(w, 4096)
loop:
for h := 1; h <= 32; h++ {
src, _ := NewImage(w, h)
src := NewImage(w, h)
pix := make([]byte, 4*w*h)
for i := 0; i < w*h; i++ {
@ -912,9 +859,9 @@ func TestImageSprites(t *testing.T) {
height = 512
)
src, _ := NewImage(4, 4)
src := NewImage(4, 4)
src.Fill(color.RGBA{0xff, 0xff, 0xff, 0xff})
dst, _ := NewImage(width, height)
dst := NewImage(width, height)
for j := 0; j < height/4; j++ {
for i := 0; i < width/4; i++ {
op := &DrawImageOptions{}
@ -943,26 +890,26 @@ func Disabled_TestImageMipmap(t *testing.T) {
}
w, h := src.Size()
l1, _ := NewImage(w/2, h/2)
l1 := NewImage(w/2, h/2)
op := &DrawImageOptions{}
op.GeoM.Scale(1/2.0, 1/2.0)
op.Filter = FilterLinear
l1.DrawImage(src, op)
l1w, l1h := l1.Size()
l2, _ := NewImage(l1w/2, l1h/2)
l2 := NewImage(l1w/2, l1h/2)
op = &DrawImageOptions{}
op.GeoM.Scale(1/2.0, 1/2.0)
op.Filter = FilterLinear
l2.DrawImage(l1, op)
gotDst, _ := NewImage(w, h)
gotDst := NewImage(w, h)
op = &DrawImageOptions{}
op.GeoM.Scale(1/5.0, 1/5.0)
op.Filter = FilterLinear
gotDst.DrawImage(src, op)
wantDst, _ := NewImage(w, h)
wantDst := NewImage(w, h)
op = &DrawImageOptions{}
op.GeoM.Scale(4.0/5.0, 4.0/5.0)
op.Filter = FilterLinear
@ -988,27 +935,27 @@ func Disabled_TestImageMipmapNegativeDet(t *testing.T) {
}
w, h := src.Size()
l1, _ := NewImage(w/2, h/2)
l1 := NewImage(w/2, h/2)
op := &DrawImageOptions{}
op.GeoM.Scale(1/2.0, 1/2.0)
op.Filter = FilterLinear
l1.DrawImage(src, op)
l1w, l1h := l1.Size()
l2, _ := NewImage(l1w/2, l1h/2)
l2 := NewImage(l1w/2, l1h/2)
op = &DrawImageOptions{}
op.GeoM.Scale(1/2.0, 1/2.0)
op.Filter = FilterLinear
l2.DrawImage(l1, op)
gotDst, _ := NewImage(w, h)
gotDst := NewImage(w, h)
op = &DrawImageOptions{}
op.GeoM.Scale(-1/5.0, -1/5.0)
op.GeoM.Translate(float64(w), float64(h))
op.Filter = FilterLinear
gotDst.DrawImage(src, op)
wantDst, _ := NewImage(w, h)
wantDst := NewImage(w, h)
op = &DrawImageOptions{}
op.GeoM.Scale(-4.0/5.0, -4.0/5.0)
op.GeoM.Translate(float64(w), float64(h))
@ -1036,8 +983,8 @@ func Disabled_TestImageMipmapNegativeDet(t *testing.T) {
// Issue #710
func TestImageMipmapColor(t *testing.T) {
img0, _ := NewImage(256, 256)
img1, _ := NewImage(128, 128)
img0 := NewImage(256, 256)
img1 := NewImage(128, 128)
img1.Fill(color.White)
for i := 0; i < 8; i++ {
@ -1066,9 +1013,9 @@ func TestImageMipmapColor(t *testing.T) {
// Issue #725
func TestImageMiamapAndDrawTriangle(t *testing.T) {
img0, _ := NewImage(32, 32)
img1, _ := NewImage(128, 128)
img2, _ := NewImage(128, 128)
img0 := NewImage(32, 32)
img1 := NewImage(128, 128)
img2 := NewImage(128, 128)
// Fill img1 red and create img1's mipmap
img1.Fill(color.RGBA{0xff, 0, 0, 0xff})
@ -1142,7 +1089,7 @@ func TestImageMiamapAndDrawTriangle(t *testing.T) {
}
func TestImageSubImageAt(t *testing.T) {
img, _ := NewImage(16, 16)
img := NewImage(16, 16)
img.Fill(color.RGBA{0xff, 0, 0, 0xff})
got := img.SubImage(image.Rect(1, 1, 16, 16)).At(0, 0).(color.RGBA)
@ -1159,7 +1106,7 @@ func TestImageSubImageAt(t *testing.T) {
}
func TestImageSubImageSize(t *testing.T) {
img, _ := NewImage(16, 16)
img := NewImage(16, 16)
img.Fill(color.RGBA{0xff, 0, 0, 0xff})
got, _ := img.SubImage(image.Rect(1, 1, 16, 16)).(*Image).Size()
@ -1171,8 +1118,8 @@ func TestImageSubImageSize(t *testing.T) {
func TestImageDrawImmediately(t *testing.T) {
const w, h = 16, 16
img0, _ := NewImage(w, h)
img1, _ := NewImage(w, h)
img0 := NewImage(w, h)
img1 := NewImage(w, h)
// Do not manipulate img0 here.
img0.Fill(color.RGBA{0xff, 0, 0, 0xff})
@ -1203,8 +1150,8 @@ func TestImageDrawImmediately(t *testing.T) {
func TestImageLinearFilterGlitch(t *testing.T) {
const w, h = 200, 12
const scale = 1.2
src, _ := NewImage(w, h)
dst, _ := NewImage(int(math.Floor(w*scale)), h)
src := NewImage(w, h)
dst := NewImage(int(math.Floor(w*scale)), h)
pix := make([]byte, 4*w*h)
for j := 0; j < h; j++ {
@ -1252,8 +1199,8 @@ func TestImageLinearFilterGlitch(t *testing.T) {
// Issue #1212
func TestImageLinearFilterGlitch2(t *testing.T) {
const w, h = 100, 100
src, _ := NewImage(w, h)
dst, _ := NewImage(w, h)
src := NewImage(w, h)
dst := NewImage(w, h)
idx := 0
pix := make([]byte, 4*w*h)
@ -1297,8 +1244,8 @@ func TestImageLinearFilterGlitch2(t *testing.T) {
func TestImageAddressRepeat(t *testing.T) {
const w, h = 16, 16
src, _ := NewImage(w, h)
dst, _ := NewImage(w, h)
src := NewImage(w, h)
dst := NewImage(w, h)
pix := make([]byte, 4*w*h)
for j := 0; j < h; j++ {
for i := 0; i < w; i++ {
@ -1378,7 +1325,7 @@ func TestImageAddressRepeat(t *testing.T) {
func TestImageReplacePixelsAfterClear(t *testing.T) {
const w, h = 256, 256
img, _ := NewImage(w, h)
img := NewImage(w, h)
img.ReplacePixels(make([]byte, 4*w*h))
// Clear used to call DrawImage to clear the image, which was the cause of crash. It is because after
// DrawImage is called, ReplacePixels for a region is forbidden.
@ -1396,7 +1343,7 @@ func TestImageSet(t *testing.T) {
}
const w, h = 16, 16
img, _ := NewImage(w, h)
img := NewImage(w, h)
colors := map[Pt]color.RGBA{
{1, 2}: {3, 4, 5, 6},
{7, 8}: {9, 10, 11, 12},
@ -1428,8 +1375,8 @@ func TestImageSetAndDraw(t *testing.T) {
}
const w, h = 16, 16
src, _ := NewImage(w, h)
dst, _ := NewImage(w, h)
src := NewImage(w, h)
dst := NewImage(w, h)
colors := map[Pt]color.RGBA{
{1, 2}: {3, 4, 5, 6},
{7, 8}: {9, 10, 11, 12},
@ -1487,10 +1434,10 @@ func TestImageSetAndDraw(t *testing.T) {
func TestImageAlphaOnBlack(t *testing.T) {
const w, h = 16, 16
src0, _ := NewImage(w, h)
src1, _ := NewImage(w, h)
dst0, _ := NewImage(w, h)
dst1, _ := NewImage(w, h)
src0 := NewImage(w, h)
src1 := NewImage(w, h)
dst0 := NewImage(w, h)
dst1 := NewImage(w, h)
pix0 := make([]byte, 4*w*h)
for j := 0; j < h; j++ {
@ -1552,8 +1499,8 @@ func TestImageAlphaOnBlack(t *testing.T) {
func TestImageDrawTrianglesWithSubImage(t *testing.T) {
const w, h = 16, 16
src, _ := NewImage(w, h)
dst, _ := NewImage(w, h)
src := NewImage(w, h)
dst := NewImage(w, h)
pix := make([]byte, 4*w*h)
for j := 0; j < h; j++ {
@ -1636,7 +1583,7 @@ func TestImageDrawTrianglesWithSubImage(t *testing.T) {
// Issue #823
func TestImageAtAfterDisposingSubImage(t *testing.T) {
img, _ := NewImage(16, 16)
img := NewImage(16, 16)
img.Set(0, 0, color.White)
img.SubImage(image.Rect(0, 0, 16, 16))
runtime.GC()
@ -1656,7 +1603,7 @@ func TestImageAtAfterDisposingSubImage(t *testing.T) {
}
func TestImageSubImageSubImage(t *testing.T) {
img, _ := NewImage(16, 16)
img := NewImage(16, 16)
img.Fill(color.White)
sub0 := img.SubImage(image.Rect(0, 0, 12, 12)).(*Image)
sub1 := sub0.SubImage(image.Rect(4, 4, 16, 16)).(*Image)
@ -1693,8 +1640,8 @@ func TestImageSubImageSubImage(t *testing.T) {
// Issue #839
func TestImageTooSmallMipmap(t *testing.T) {
const w, h = 16, 16
src, _ := NewImage(w, h)
dst, _ := NewImage(w, h)
src := NewImage(w, h)
dst := NewImage(w, h)
src.Fill(color.White)
op := &DrawImageOptions{}
@ -1710,8 +1657,8 @@ func TestImageTooSmallMipmap(t *testing.T) {
func TestImageZeroSizedMipmap(t *testing.T) {
const w, h = 16, 16
src, _ := NewImage(w, h)
dst, _ := NewImage(w, h)
src := NewImage(w, h)
dst := NewImage(w, h)
op := &DrawImageOptions{}
op.Filter = FilterLinear
@ -1725,8 +1672,8 @@ func TestImageFillingAndEdges(t *testing.T) {
dstw, dsth = 256, 16
)
src, _ := NewImage(srcw, srch)
dst, _ := NewImage(dstw, dsth)
src := NewImage(srcw, srch)
dst := NewImage(dstw, dsth)
src.Fill(color.White)
dst.Fill(color.Black)
@ -1752,8 +1699,8 @@ func TestImageFillingAndEdges(t *testing.T) {
func TestImageDrawTrianglesAndMutateArgs(t *testing.T) {
const w, h = 16, 16
dst, _ := NewImage(w, h)
src, _ := NewImage(w, h)
dst := NewImage(w, h)
src := NewImage(w, h)
clr := color.RGBA{0xff, 0, 0, 0xff}
src.Fill(clr)
@ -1817,7 +1764,7 @@ func TestImageDrawTrianglesAndMutateArgs(t *testing.T) {
}
func TestImageReplacePixelsOnSubImage(t *testing.T) {
dst, _ := NewImage(17, 31)
dst := NewImage(17, 31)
dst.Fill(color.RGBA{0xff, 0, 0, 0xff})
pix0 := make([]byte, 4*5*3)
@ -1868,9 +1815,9 @@ func TestImageReplacePixelsOnSubImage(t *testing.T) {
func TestImageDrawTrianglesWithColorM(t *testing.T) {
const w, h = 16, 16
dst0, _ := NewImage(w, h)
dst1, _ := NewImage(w, h)
src, _ := NewImage(w, h)
dst0 := NewImage(w, h)
dst1 := NewImage(w, h)
src := NewImage(w, h)
src.Fill(color.White)
vs0 := []Vertex{
@ -1977,7 +1924,7 @@ func TestImageDrawTrianglesWithColorM(t *testing.T) {
// Issue #1137
func TestImageDrawOver(t *testing.T) {
dst, _ := NewImage(320, 240)
dst := NewImage(320, 240)
src := image.NewUniform(color.RGBA{0xff, 0, 0, 0xff})
// This must not cause infinite-loop.
draw.Draw(dst, dst.Bounds(), src, image.ZP, draw.Over)
@ -1999,8 +1946,8 @@ func TestImageDrawDisposedImage(t *testing.T) {
}
}()
dst, _ := NewImage(16, 16)
src, _ := NewImage(16, 16)
dst := NewImage(16, 16)
src := NewImage(16, 16)
src.Dispose()
dst.DrawImage(src, nil)
}
@ -2012,8 +1959,8 @@ func TestImageDrawTrianglesDisposedImage(t *testing.T) {
}
}()
dst, _ := NewImage(16, 16)
src, _ := NewImage(16, 16)
dst := NewImage(16, 16)
src := NewImage(16, 16)
src.Dispose()
vs := make([]Vertex, 4)
is := []uint16{0, 1, 2, 1, 2, 3}
@ -2022,7 +1969,7 @@ func TestImageDrawTrianglesDisposedImage(t *testing.T) {
// #1137
func BenchmarkImageDrawOver(b *testing.B) {
dst, _ := NewImage(16, 16)
dst := NewImage(16, 16)
src := image.NewUniform(color.Black)
for n := 0; n < b.N; n++ {
draw.Draw(dst, dst.Bounds(), src, image.ZP, draw.Over)
@ -2037,7 +1984,7 @@ func TestImageFloatTranslate(t *testing.T) {
s := s
t.Run(fmt.Sprintf("scale%d", s), func(t *testing.T) {
check := func(src *Image) {
dst, _ := NewImage(w*(s+1), h*(s+1))
dst := NewImage(w*(s+1), h*(s+1))
dst.Fill(color.RGBA{0xff, 0, 0, 0xff})
op := &DrawImageOptions{}
@ -2061,7 +2008,7 @@ func TestImageFloatTranslate(t *testing.T) {
}
t.Run("image", func(t *testing.T) {
src, _ := NewImage(w, h)
src := NewImage(w, h)
pix := make([]byte, 4*w*h)
for j := 0; j < h; j++ {
for i := 0; i < w; i++ {
@ -2074,7 +2021,7 @@ func TestImageFloatTranslate(t *testing.T) {
})
t.Run("subimage", func(t *testing.T) {
src, _ := NewImage(w*s, h*s)
src := NewImage(w*s, h*s)
pix := make([]byte, 4*(w*s)*(h*s))
for j := 0; j < h*s; j++ {
for i := 0; i < w*s; i++ {
@ -2092,8 +2039,8 @@ func TestImageFloatTranslate(t *testing.T) {
// Issue #1213
func TestImageColorMCopy(t *testing.T) {
const w, h = 16, 16
dst, _ := NewImage(w, h)
src, _ := NewImage(w, h)
dst := NewImage(w, h)
src := NewImage(w, h)
for k := 0; k < 256; k++ {
op := &DrawImageOptions{}
@ -2116,8 +2063,8 @@ func TestImageColorMCopy(t *testing.T) {
// TODO: Do we have to guarantee this behavior? See #1222
func TestImageReplacePixelsAndModifyPixels(t *testing.T) {
const w, h = 16, 16
dst, _ := NewImage(w, h)
src, _ := NewImage(w, h)
dst := NewImage(w, h)
src := NewImage(w, h)
pix := make([]byte, 4*w*h)
for j := 0; j < h; j++ {
@ -2159,8 +2106,8 @@ func TestImageReplacePixelsAndModifyPixels(t *testing.T) {
func TestImageCompositeModeMultiply(t *testing.T) {
const w, h = 16, 16
dst, _ := NewImage(w, h)
src, _ := NewImage(w, h)
dst := NewImage(w, h)
src := NewImage(w, h)
dst.Fill(color.RGBA{0x10, 0x20, 0x30, 0x40})
src.Fill(color.RGBA{0x50, 0x60, 0x70, 0x80})
@ -2188,8 +2135,8 @@ func TestImageCompositeModeMultiply(t *testing.T) {
// Issue #1269
func TestImageZeroTriangle(t *testing.T) {
const w, h = 16, 16
dst, _ := NewImage(w, h)
src, _ := NewImage(1, 1)
dst := NewImage(w, h)
src := NewImage(1, 1)
vs := []Vertex{}
is := []uint16{}

View File

@ -87,7 +87,7 @@ type testResult struct {
var testSetBeforeMainResult = func() testResult {
clr := color.RGBA{1, 2, 3, 4}
img, _ := ebiten.NewImage(16, 16)
img := ebiten.NewImage(16, 16)
img.Set(0, 0, clr)
ch := make(chan color.RGBA, 1)
@ -114,8 +114,8 @@ func TestSetBeforeMain(t *testing.T) {
var testDrawImageBeforeMainResult = func() testResult {
const w, h = 16, 16
src, _ := ebiten.NewImage(w, h)
dst, _ := ebiten.NewImage(w, h)
src := ebiten.NewImage(w, h)
dst := ebiten.NewImage(w, h)
src.Set(0, 0, color.White)
dst.DrawImage(src, nil)
@ -143,8 +143,8 @@ func TestDrawImageBeforeMain(t *testing.T) {
var testDrawTrianglesBeforeMainResult = func() testResult {
const w, h = 16, 16
src, _ := ebiten.NewImage(w, h)
dst, _ := ebiten.NewImage(w, h)
src := ebiten.NewImage(w, h)
dst := ebiten.NewImage(w, h)
src.Set(0, 0, color.White)
vs := []ebiten.Vertex{
{
@ -204,7 +204,7 @@ func TestDrawTrianglesBeforeMain(t *testing.T) {
var testSetAndFillBeforeMainResult = func() testResult {
clr := color.RGBA{1, 2, 3, 4}
img, _ := ebiten.NewImage(16, 16)
img := ebiten.NewImage(16, 16)
img.Set(0, 0, clr)
img.Fill(color.RGBA{5, 6, 7, 8})
img.Set(1, 0, clr)
@ -233,7 +233,7 @@ func TestSetAndFillBeforeMain(t *testing.T) {
var testSetAndReplacePixelsBeforeMainResult = func() testResult {
clr := color.RGBA{1, 2, 3, 4}
img, _ := ebiten.NewImage(16, 16)
img := ebiten.NewImage(16, 16)
img.Set(0, 0, clr)
pix := make([]byte, 4*16*16)
for i := 0; i < len(pix)/4; i++ {
@ -268,7 +268,7 @@ func TestSetAndReplacePixelsBeforeMain(t *testing.T) {
}
var testReplacePixelsAndModifyBeforeMainResult = func() testResult {
img, _ := ebiten.NewImage(16, 16)
img := ebiten.NewImage(16, 16)
pix := make([]byte, 4*16*16)
for i := 0; i < len(pix)/4; i++ {
pix[4*i] = 1

View File

@ -25,7 +25,7 @@ import (
func TestShaderFill(t *testing.T) {
const w, h = 16, 16
dst, _ := NewImage(w, h)
dst := NewImage(w, h)
s, err := NewShader([]byte(`package main
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
@ -55,7 +55,7 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
func TestShaderFillWithDrawImage(t *testing.T) {
const w, h = 16, 16
dst, _ := NewImage(w, h)
dst := NewImage(w, h)
s, err := NewShader([]byte(`package main
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
@ -66,7 +66,7 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
t.Fatal(err)
}
src, _ := NewImage(w/2, h/2)
src := NewImage(w/2, h/2)
op := &DrawRectShaderOptions{}
op.Images[0] = src
dst.DrawRectShader(w/2, h/2, s, op)
@ -88,7 +88,7 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
func TestShaderFillWithDrawTriangles(t *testing.T) {
const w, h = 16, 16
dst, _ := NewImage(w, h)
dst := NewImage(w, h)
s, err := NewShader([]byte(`package main
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
@ -99,7 +99,7 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
t.Fatal(err)
}
src, _ := NewImage(w/2, h/2)
src := NewImage(w/2, h/2)
op := &DrawTrianglesShaderOptions{}
op.Images[0] = src
@ -163,7 +163,7 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
func TestShaderFunction(t *testing.T) {
const w, h = 16, 16
dst, _ := NewImage(w, h)
dst := NewImage(w, h)
s, err := NewShader([]byte(`package main
func clr(red float) (float, float, float, float) {
@ -523,7 +523,7 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
func TestShaderUninitializedUniformVariables(t *testing.T) {
const w, h = 16, 16
dst, _ := NewImage(w, h)
dst := NewImage(w, h)
s, err := NewShader([]byte(`package main
var U vec4
@ -766,7 +766,7 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
func TestShaderMatrix(t *testing.T) {
const w, h = 16, 16
dst, _ := NewImage(w, h)
dst := NewImage(w, h)
s, err := NewShader([]byte(`package main
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
@ -786,7 +786,7 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
t.Fatal(err)
}
src, _ := NewImage(w, h)
src := NewImage(w, h)
op := &DrawRectShaderOptions{}
op.Images[0] = src
dst.DrawRectShader(w, h, s, op)
@ -817,7 +817,7 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
t.Fatal(err)
}
src0, _ := NewImage(w, h)
src0 := NewImage(w, h)
pix0 := make([]byte, 4*w*h)
for j := 0; j < h; j++ {
for i := 0; i < w; i++ {
@ -832,7 +832,7 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
src0.ReplacePixels(pix0)
src0 = src0.SubImage(image.Rect(2, 3, 10, 11)).(*Image)
src1, _ := NewImage(w, h)
src1 := NewImage(w, h)
pix1 := make([]byte, 4*w*h)
for j := 0; j < h; j++ {
for i := 0; i < w; i++ {
@ -863,7 +863,7 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
}
t.Run("DrawRectShader", func(t *testing.T) {
dst, _ := NewImage(w, h)
dst := NewImage(w, h)
op := &DrawRectShaderOptions{}
op.Images[0] = src0
op.Images[1] = src1
@ -872,7 +872,7 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
})
t.Run("DrawTrianglesShader", func(t *testing.T) {
dst, _ := NewImage(w, h)
dst := NewImage(w, h)
vs := []Vertex{
{
DstX: 0,

View File

@ -34,7 +34,7 @@ func TestMain(m *testing.M) {
func TestTextColor(t *testing.T) {
clr := color.RGBA{0x80, 0x80, 0x80, 0x80}
img, _ := ebiten.NewImage(30, 30)
img := ebiten.NewImage(30, 30)
Draw(img, "Hello", bitmapfont.Face, 12, 12, clr)
w, h := img.Size()
@ -119,7 +119,7 @@ func (f *testFace) Metrics() font.Metrics {
func TestTextOverlap(t *testing.T) {
f := &testFace{}
dst, _ := ebiten.NewImage(testFaceSize*2, testFaceSize)
dst := ebiten.NewImage(testFaceSize*2, testFaceSize)
// With testFace, 'b' is rendered at the previous position as 0xff.
// 'a' is rendered at the current position as 0x80.

View File

@ -92,7 +92,7 @@ func (c *uiContext) updateOffscreen() {
}
}
if c.offscreen == nil {
c.offscreen = newImage(sw, sh)
c.offscreen = NewImage(sw, sh)
c.offscreen.mipmap.SetVolatile(IsScreenClearedEveryFrame())
}

View File

@ -25,10 +25,9 @@ import (
"github.com/hajimehoshi/ebiten/v2/vector/internal/triangulate"
)
var emptyImage *ebiten.Image
var emptyImage = ebiten.NewImage(1, 1)
func init() {
emptyImage, _ = ebiten.NewImage(1, 1)
emptyImage.Fill(color.White)
}