diff --git a/ebitenutil/debugprint.go b/ebitenutil/debugprint.go index ee74d1375..dd5cab429 100644 --- a/ebitenutil/debugprint.go +++ b/ebitenutil/debugprint.go @@ -19,6 +19,7 @@ package ebitenutil import ( "github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten/internal/assets" + "image" "image/color" "math" ) @@ -46,11 +47,11 @@ func (d *debugPrintState) drawText(rt *ebiten.Image, str string, x, y int, c col } code := int(c) const xCharNum = assets.TextImageWidth / assets.TextImageCharWidth - srcX := float64(code%xCharNum) * assets.TextImageCharWidth - srcY := float64(code/xCharNum) * assets.TextImageCharHeight + srcX := (code % xCharNum) * assets.TextImageCharWidth + srcY := (code / xCharNum) * assets.TextImageCharHeight parts = append(parts, ebiten.ImagePart{ - Dst: ebiten.Rect{float64(locationX), float64(locationY), assets.TextImageCharWidth, assets.TextImageCharHeight}, - Src: ebiten.Rect{srcX, srcY, assets.TextImageCharWidth, assets.TextImageCharHeight}, + Dst: image.Rect(locationX, locationY, locationX+assets.TextImageCharWidth, locationY+assets.TextImageCharHeight), + Src: image.Rect(srcX, srcY, srcX+assets.TextImageCharWidth, srcY+assets.TextImageCharHeight), }) locationX += assets.TextImageCharWidth } diff --git a/ebitenutil/gif.go b/ebitenutil/gif.go index cd4643151..b35059315 100644 --- a/ebitenutil/gif.go +++ b/ebitenutil/gif.go @@ -40,7 +40,8 @@ func (r *recorder) update(screen *ebiten.Image) error { return nil } img := image.NewPaletted(screen.Bounds(), palette.Plan9) - draw.Draw(img, img.Bounds(), screen, screen.Bounds().Min, draw.Over) + // TODO: This is too slow. + draw.Draw(img, img.Bounds(), screen, screen.Bounds().Min, draw.Src) r.gif.Image[r.currentFrame] = img // The actual FPS is 60, but GIF can't have such FPS. Set 50 FPS instead. r.gif.Delay[r.currentFrame] = 2 diff --git a/example/alphablending/main.go b/example/alphablending/main.go index d9eb0f510..44accb72e 100644 --- a/example/alphablending/main.go +++ b/example/alphablending/main.go @@ -87,9 +87,9 @@ func main() { if err != nil { log.Fatal(err) } - update := update - // f, _ := os.Create("out.gif") - // update = ebitenutil.RecordScreenAsGIF(update, f, 100) + //update := update + //f, _ := os.Create("out.gif") + //update = ebitenutil.RecordScreenAsGIF(update, f, 100) if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Alpha Blending (Ebiten Demo)"); err != nil { log.Fatal(err) } diff --git a/example/blocks/blocks/font.go b/example/blocks/blocks/font.go index 8897ce10b..5e58886a0 100644 --- a/example/blocks/blocks/font.go +++ b/example/blocks/blocks/font.go @@ -18,6 +18,7 @@ package blocks import ( "github.com/hajimehoshi/ebiten" + "image" "image/color" "math" ) @@ -45,11 +46,11 @@ func drawText(rt *ebiten.Image, images *Images, str string, ox, oy, scale int, c continue } code := int(c) - x := float64(code%16) * charWidth - y := float64((code-32)/16) * charHeight + x := (code % 16) * charWidth + y := ((code - 32) / 16) * charHeight parts = append(parts, ebiten.ImagePart{ - Dst: ebiten.Rect{float64(locationX), float64(locationY), charWidth, charHeight}, - Src: ebiten.Rect{x, y, charWidth, charHeight}, + Dst: image.Rect(locationX, locationY, locationX+charWidth, locationY+charHeight), + Src: image.Rect(x, y, x+charWidth, y+charHeight), }) locationX += charWidth } diff --git a/example/blocks/blocks/piece.go b/example/blocks/blocks/piece.go index fb6c3ff10..0cb876502 100644 --- a/example/blocks/blocks/piece.go +++ b/example/blocks/blocks/piece.go @@ -18,6 +18,7 @@ package blocks import ( "github.com/hajimehoshi/ebiten" + "image" ) func init() { @@ -145,10 +146,11 @@ func drawBlocks(r *ebiten.Image, images *Images, blocks [][]BlockType, geo ebite if block == BlockTypeNone { continue } - locationX := float64(i * blockWidth) - locationY := float64(j * blockHeight) - dst := ebiten.Rect{locationX, locationY, blockWidth, blockHeight} - src := ebiten.Rect{float64(int(block)-1) * blockWidth, 0, blockWidth, blockHeight} + locationX := i * blockWidth + locationY := j * blockHeight + dst := image.Rect(locationX, locationY, locationX+blockWidth, locationY+blockHeight) + srcX := (int(block) - 1) * blockWidth + src := image.Rect(srcX, 0, srcX+blockWidth, blockHeight) parts = append(parts, ebiten.ImagePart{dst, src}) } } diff --git a/example/blocks/blocks/titlescene.go b/example/blocks/blocks/titlescene.go index cb0f54481..743464242 100644 --- a/example/blocks/blocks/titlescene.go +++ b/example/blocks/blocks/titlescene.go @@ -18,6 +18,7 @@ package blocks import ( "github.com/hajimehoshi/ebiten" + "image" "image/color" ) @@ -58,9 +59,11 @@ func drawTitleBackground(r *ebiten.Image, images *Images, c int) { parts := []ebiten.ImagePart{} for j := -1; j < ScreenHeight/imageHeight+1; j++ { for i := 0; i < ScreenWidth/imageWidth+1; i++ { + dstX := i * imageWidth + dstY := j * imageHeight parts = append(parts, ebiten.ImagePart{ - Dst: ebiten.Rect{float64(i * imageWidth), float64(j * imageHeight), imageWidth, imageHeight}, - Src: ebiten.Rect{0, 0, imageWidth, imageHeight}, + Dst: image.Rect(dstX, dstY, dstX+imageWidth, dstY+imageHeight), + Src: image.Rect(0, 0, imageWidth, imageHeight), }) } } diff --git a/example/perspective/main.go b/example/perspective/main.go index 44afbe4e0..b9165af28 100644 --- a/example/perspective/main.go +++ b/example/perspective/main.go @@ -19,6 +19,7 @@ package main import ( "github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten/ebitenutil" + "image" _ "image/jpeg" "log" ) @@ -36,11 +37,11 @@ func Update(screen *ebiten.Image) error { parts := []ebiten.ImagePart{} w, h := gophersImage.Size() for i := 0; i < h; i++ { - width := float64(w) + float64(i)*0.75 - x := float64(h-i) * 0.75 / 2 + width := w + i*3/4 + x := ((h - i) * 3 / 4) / 2 part := ebiten.ImagePart{ - Dst: ebiten.Rect{x, float64(i), width, 1}, - Src: ebiten.Rect{0, float64(i), float64(w), 1}, + Dst: image.Rect(x, i, x+width, i+1), + Src: image.Rect(0, i, w, i+1), } parts = append(parts, part) } diff --git a/graphics.go b/graphics.go index e3ab0f63d..4e7935bf7 100644 --- a/graphics.go +++ b/graphics.go @@ -18,29 +18,22 @@ package ebiten import ( "github.com/hajimehoshi/ebiten/internal/opengl" + "image" ) -// A Rect represents a rectangle. -type Rect struct { - X float64 - Y float64 - Width float64 - Height float64 -} - // An ImagePart represents a part of an image. type ImagePart struct { - Dst Rect - Src Rect + Dst image.Rectangle + Src image.Rectangle } // DrawWholeImage draws the whole image. -func DrawWholeImage(target *Image, image *Image, geo GeometryMatrix, color ColorMatrix) error { - w, h := image.Size() +func DrawWholeImage(target *Image, img *Image, geo GeometryMatrix, color ColorMatrix) error { + w, h := img.Size() parts := []ImagePart{ - {Rect{0, 0, float64(w), float64(h)}, Rect{0, 0, float64(w), float64(h)}}, + {image.Rect(0, 0, w, h), image.Rect(0, 0, w, h)}, } - return target.DrawImage(image, parts, geo, color) + return target.DrawImage(img, parts, geo, color) } // Filter represents the type of filter to be used when an image is maginified or minified. diff --git a/graphicscontext.go b/graphicscontext.go index 353a2e267..d40a378c9 100644 --- a/graphicscontext.go +++ b/graphicscontext.go @@ -18,6 +18,7 @@ package ebiten import ( "github.com/hajimehoshi/ebiten/internal/opengl" + "image" ) func newGraphicsContext(screenWidth, screenHeight, screenScale int) (*graphicsContext, error) { @@ -72,7 +73,7 @@ func (c *graphicsContext) postUpdate() error { clr := ColorMatrixI() w, h := c.screen.size() parts := []ImagePart{ - {Rect{0, 0, float64(w), float64(h)}, Rect{0, 0, float64(w), float64(h)}}, + {image.Rect(0, 0, w, h), image.Rect(0, 0, w, h)}, } if err := c.defaultR.drawImage(c.screen, parts, geo, clr); err != nil { return err diff --git a/image.go b/image.go index 4453ff0c1..33a870238 100644 --- a/image.go +++ b/image.go @@ -76,14 +76,14 @@ func v(y float64, height int) float32 { func textureQuads(parts []ImagePart, width, height int) []shader.TextureQuad { quads := make([]shader.TextureQuad, 0, len(parts)) for _, part := range parts { - x1 := float32(part.Dst.X) - x2 := float32(part.Dst.X + part.Dst.Width) - y1 := float32(part.Dst.Y) - y2 := float32(part.Dst.Y + part.Dst.Height) - u1 := u(part.Src.X, width) - u2 := u(part.Src.X+part.Src.Width, width) - v1 := v(part.Src.Y, height) - v2 := v(part.Src.Y+part.Src.Height, height) + x1 := float32(part.Dst.Min.X) + x2 := float32(part.Dst.Max.X) + y1 := float32(part.Dst.Min.Y) + y2 := float32(part.Dst.Max.Y) + u1 := u(float64(part.Src.Min.X), width) + u2 := u(float64(part.Src.Max.X), width) + v1 := v(float64(part.Src.Min.Y), height) + v2 := v(float64(part.Src.Max.Y), height) quad := shader.TextureQuad{x1, x2, y1, y2, u1, u2, v1, v2} quads = append(quads, quad) }