Skip to content
Permalink
Browse files

graphics: Experimental: (*image.Image).SubImage

Fixes #722
  • Loading branch information...
hajimehoshi committed Oct 23, 2018
1 parent 41877bb commit 710f56531f09e5444463ff8b5949824658245a42
@@ -56,7 +56,6 @@ func drawDebugText(rt *ebiten.Image, str string, ox, oy int, shadow bool) {
x := 0
y := 0
w, _ := debugPrintTextImage.Size()
var r image.Rectangle
for _, c := range str {
const (
cw = assets.CharWidth
@@ -70,15 +69,10 @@ func drawDebugText(rt *ebiten.Image, str string, ox, oy int, shadow bool) {
n := w / cw
sx := (int(c) % n) * cw
sy := (int(c) / n) * ch
r.Min.X = sx
r.Min.Y = sy
r.Max.X = sx + cw
r.Max.Y = sy + ch
op.SourceRect = &r
op.GeoM.Reset()
op.GeoM.Translate(float64(x), float64(y))
op.GeoM.Translate(float64(ox+1), float64(oy))
_ = rt.DrawImage(debugPrintTextImage, op)
_ = rt.DrawImage(debugPrintTextImage.SubImage(image.Rect(sx, sy, sx+cw, sy+ch)).(*ebiten.Image), op)
x += cw
}
}
@@ -218,9 +218,7 @@ func drawGroundImage(screen *ebiten.Image, ground *ebiten.Image) {
op.GeoM.Scale(1/z, 8) // 8 is an arbitrary number not to make empty lines.
op.GeoM.Translate(float64(pw)/2, float64(j)/z)

src := image.Rect(0, j, gw, j+1)
op.SourceRect = &src
perspectiveGroundImage.DrawImage(ground, op)
perspectiveGroundImage.DrawImage(ground.SubImage(image.Rect(0, j, gw, j+1)).(*ebiten.Image), op)
}

perspectiveGroundImage.DrawImage(fogImage, nil)
@@ -54,9 +54,7 @@ func update(screen *ebiten.Image) error {
op.GeoM.Translate(screenWidth/2, screenHeight/2)
i := (count / 5) % frameNum
sx, sy := frameOX+i*frameWidth, frameOY
r := image.Rect(sx, sy, sx+frameWidth, sy+frameHeight)
op.SourceRect = &r
screen.DrawImage(runnerImage, op)
screen.DrawImage(runnerImage.SubImage(image.Rect(sx, sy, sx+frameWidth, sy+frameHeight)).(*ebiten.Image), op)
return nil
}

@@ -175,9 +175,7 @@ func drawBlock(r *ebiten.Image, block BlockType, x, y int, clr ebiten.ColorM) {
op.GeoM.Translate(float64(x), float64(y))

srcX := (int(block) - 1) * blockWidth
src := image.Rect(srcX, 0, srcX+blockWidth, blockHeight)
op.SourceRect = &src
r.DrawImage(imageBlocks, op)
r.DrawImage(imageBlocks.SubImage(image.Rect(srcX, 0, srcX+blockWidth, blockHeight)).(*ebiten.Image), op)
}

func (p *Piece) InitialPosition() (int, int) {
@@ -345,9 +345,7 @@ func (g *Game) drawTiles(screen *ebiten.Image) {
op.GeoM.Reset()
op.GeoM.Translate(float64(i*tileSize-floorMod(g.cameraX, tileSize)),
float64((ny-1)*tileSize-floorMod(g.cameraY, tileSize)))
r := image.Rect(0, 0, tileSize, tileSize)
op.SourceRect = &r
screen.DrawImage(tilesImage, op)
screen.DrawImage(tilesImage.SubImage(image.Rect(0, 0, tileSize, tileSize)).(*ebiten.Image), op)

// pipe
if tileY, ok := g.pipeAt(floorDiv(g.cameraX, tileSize) + i); ok {
@@ -357,27 +355,25 @@ func (g *Game) drawTiles(screen *ebiten.Image) {
op.GeoM.Translate(float64(i*tileSize-floorMod(g.cameraX, tileSize)),
float64(j*tileSize-floorMod(g.cameraY, tileSize)))
op.GeoM.Translate(0, tileSize)
var r image.Rectangle
if j == tileY-1 {
r := image.Rect(pipeTileSrcX, pipeTileSrcY, pipeTileSrcX+tileSize*2, pipeTileSrcY+tileSize)
op.SourceRect = &r
r = image.Rect(pipeTileSrcX, pipeTileSrcY, pipeTileSrcX+tileSize*2, pipeTileSrcY+tileSize)
} else {
r := image.Rect(pipeTileSrcX, pipeTileSrcY+tileSize, pipeTileSrcX+tileSize*2, pipeTileSrcY+tileSize*2)
op.SourceRect = &r
r = image.Rect(pipeTileSrcX, pipeTileSrcY+tileSize, pipeTileSrcX+tileSize*2, pipeTileSrcY+tileSize*2)
}
screen.DrawImage(tilesImage, op)
screen.DrawImage(tilesImage.SubImage(r).(*ebiten.Image), op)
}
for j := tileY + pipeGapY; j < screenHeight/tileSize-1; j++ {
op.GeoM.Reset()
op.GeoM.Translate(float64(i*tileSize-floorMod(g.cameraX, tileSize)),
float64(j*tileSize-floorMod(g.cameraY, tileSize)))
var r image.Rectangle
if j == tileY+pipeGapY {
r := image.Rect(pipeTileSrcX, pipeTileSrcY, pipeTileSrcX+pipeWidth, pipeTileSrcY+tileSize)
op.SourceRect = &r
r = image.Rect(pipeTileSrcX, pipeTileSrcY, pipeTileSrcX+pipeWidth, pipeTileSrcY+tileSize)
} else {
r := image.Rect(pipeTileSrcX, pipeTileSrcY+tileSize, pipeTileSrcX+pipeWidth, pipeTileSrcY+tileSize+tileSize)
op.SourceRect = &r
r = image.Rect(pipeTileSrcX, pipeTileSrcY+tileSize, pipeTileSrcX+pipeWidth, pipeTileSrcY+tileSize+tileSize)
}
screen.DrawImage(tilesImage, op)
screen.DrawImage(tilesImage.SubImage(r).(*ebiten.Image), op)
}
}
}
@@ -78,8 +78,7 @@ func update(screen *ebiten.Image) error {
}
op.GeoM.Translate(float64(r.Min.X), float64(r.Min.Y))
op.GeoM.Translate(offsetX, offsetY)
op.SourceRect = &r
screen.DrawImage(keyboardImage, op)
screen.DrawImage(keyboardImage.SubImage(r).(*ebiten.Image), op)
}

keyStrs := []string{}
@@ -69,6 +69,7 @@ Press C to clip the images.
Scale: %0.2f`, s)
ebitenutil.DebugPrint(screen, msg)

clippedGophersImage := gophersImage.SubImage(image.Rect(10, 10, 100, 100)).(*ebiten.Image)
for i, f := range []ebiten.Filter{ebiten.FilterNearest, ebiten.FilterLinear} {
w, h := gophersImage.Size()

@@ -82,10 +83,10 @@ Scale: %0.2f`, s)
op.GeoM.Translate(32+float64(i*w)*s+float64(i*4), 64)
op.Filter = f
if clip {
r := image.Rect(10, 10, 100, 100)
op.SourceRect = &r
screen.DrawImage(clippedGophersImage, op)
} else {
screen.DrawImage(gophersImage, op)
}
screen.DrawImage(gophersImage, op)
}

return nil
@@ -58,9 +58,7 @@ func update(screen *ebiten.Image) error {
// Move the image's center to the screen's center.
op.GeoM.Translate(screenWidth/2, screenHeight/2)

r := image.Rect(0, i, w, i+1)
op.SourceRect = &r
screen.DrawImage(gophersImage, op)
screen.DrawImage(gophersImage.SubImage(image.Rect(0, i, w, i+1)).(*ebiten.Image), op)
}
return nil
}
@@ -119,9 +119,7 @@ func update(screen *ebiten.Image) error {

sx := (t % tileXNum) * tileSize
sy := (t / tileXNum) * tileSize
r := image.Rect(sx, sy, sx+tileSize, sy+tileSize)
op.SourceRect = &r
screen.DrawImage(tilesImage, op)
screen.DrawImage(tilesImage.SubImage(image.Rect(sx, sy, sx+tileSize, sy+tileSize)).(*ebiten.Image), op)
}
}

@@ -154,9 +154,7 @@ func drawNinePatches(dst *ebiten.Image, dstRect image.Rectangle, srcRect image.R
op.GeoM.Scale(float64(dw)/float64(sw), float64(dh)/float64(sh))
op.GeoM.Translate(float64(dx), float64(dy))
op.GeoM.Translate(float64(dstX), float64(dstY))
r := image.Rect(sx, sy, sx+sw, sy+sh)
op.SourceRect = &r
dst.DrawImage(uiImage, op)
dst.DrawImage(uiImage.SubImage(image.Rect(sx, sy, sx+sw, sy+sh)).(*ebiten.Image), op)
}
}
}
Oops, something went wrong.

0 comments on commit 710f565

Please sign in to comment.
You can’t perform that action at this time.