Remove ebiten.Rect (#37)

This commit is contained in:
Hajime Hoshi 2014-12-24 01:15:16 +09:00
parent ddd43370bc
commit 84f2e8aad2
10 changed files with 48 additions and 45 deletions

View File

@ -19,6 +19,7 @@ package ebitenutil
import ( import (
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/internal/assets" "github.com/hajimehoshi/ebiten/internal/assets"
"image"
"image/color" "image/color"
"math" "math"
) )
@ -46,11 +47,11 @@ func (d *debugPrintState) drawText(rt *ebiten.Image, str string, x, y int, c col
} }
code := int(c) code := int(c)
const xCharNum = assets.TextImageWidth / assets.TextImageCharWidth const xCharNum = assets.TextImageWidth / assets.TextImageCharWidth
srcX := float64(code%xCharNum) * assets.TextImageCharWidth srcX := (code % xCharNum) * assets.TextImageCharWidth
srcY := float64(code/xCharNum) * assets.TextImageCharHeight srcY := (code / xCharNum) * assets.TextImageCharHeight
parts = append(parts, ebiten.ImagePart{ parts = append(parts, ebiten.ImagePart{
Dst: ebiten.Rect{float64(locationX), float64(locationY), assets.TextImageCharWidth, assets.TextImageCharHeight}, Dst: image.Rect(locationX, locationY, locationX+assets.TextImageCharWidth, locationY+assets.TextImageCharHeight),
Src: ebiten.Rect{srcX, srcY, assets.TextImageCharWidth, assets.TextImageCharHeight}, Src: image.Rect(srcX, srcY, srcX+assets.TextImageCharWidth, srcY+assets.TextImageCharHeight),
}) })
locationX += assets.TextImageCharWidth locationX += assets.TextImageCharWidth
} }

View File

@ -40,7 +40,8 @@ func (r *recorder) update(screen *ebiten.Image) error {
return nil return nil
} }
img := image.NewPaletted(screen.Bounds(), palette.Plan9) 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 r.gif.Image[r.currentFrame] = img
// The actual FPS is 60, but GIF can't have such FPS. Set 50 FPS instead. // The actual FPS is 60, but GIF can't have such FPS. Set 50 FPS instead.
r.gif.Delay[r.currentFrame] = 2 r.gif.Delay[r.currentFrame] = 2

View File

@ -87,9 +87,9 @@ func main() {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
update := update //update := update
// f, _ := os.Create("out.gif") //f, _ := os.Create("out.gif")
// update = ebitenutil.RecordScreenAsGIF(update, f, 100) //update = ebitenutil.RecordScreenAsGIF(update, f, 100)
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Alpha Blending (Ebiten Demo)"); err != nil { if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Alpha Blending (Ebiten Demo)"); err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -18,6 +18,7 @@ package blocks
import ( import (
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"image"
"image/color" "image/color"
"math" "math"
) )
@ -45,11 +46,11 @@ func drawText(rt *ebiten.Image, images *Images, str string, ox, oy, scale int, c
continue continue
} }
code := int(c) code := int(c)
x := float64(code%16) * charWidth x := (code % 16) * charWidth
y := float64((code-32)/16) * charHeight y := ((code - 32) / 16) * charHeight
parts = append(parts, ebiten.ImagePart{ parts = append(parts, ebiten.ImagePart{
Dst: ebiten.Rect{float64(locationX), float64(locationY), charWidth, charHeight}, Dst: image.Rect(locationX, locationY, locationX+charWidth, locationY+charHeight),
Src: ebiten.Rect{x, y, charWidth, charHeight}, Src: image.Rect(x, y, x+charWidth, y+charHeight),
}) })
locationX += charWidth locationX += charWidth
} }

View File

@ -18,6 +18,7 @@ package blocks
import ( import (
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"image"
) )
func init() { func init() {
@ -145,10 +146,11 @@ func drawBlocks(r *ebiten.Image, images *Images, blocks [][]BlockType, geo ebite
if block == BlockTypeNone { if block == BlockTypeNone {
continue continue
} }
locationX := float64(i * blockWidth) locationX := i * blockWidth
locationY := float64(j * blockHeight) locationY := j * blockHeight
dst := ebiten.Rect{locationX, locationY, blockWidth, blockHeight} dst := image.Rect(locationX, locationY, locationX+blockWidth, locationY+blockHeight)
src := ebiten.Rect{float64(int(block)-1) * blockWidth, 0, blockWidth, blockHeight} srcX := (int(block) - 1) * blockWidth
src := image.Rect(srcX, 0, srcX+blockWidth, blockHeight)
parts = append(parts, ebiten.ImagePart{dst, src}) parts = append(parts, ebiten.ImagePart{dst, src})
} }
} }

View File

@ -18,6 +18,7 @@ package blocks
import ( import (
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"image"
"image/color" "image/color"
) )
@ -58,9 +59,11 @@ func drawTitleBackground(r *ebiten.Image, images *Images, c int) {
parts := []ebiten.ImagePart{} parts := []ebiten.ImagePart{}
for j := -1; j < ScreenHeight/imageHeight+1; j++ { for j := -1; j < ScreenHeight/imageHeight+1; j++ {
for i := 0; i < ScreenWidth/imageWidth+1; i++ { for i := 0; i < ScreenWidth/imageWidth+1; i++ {
dstX := i * imageWidth
dstY := j * imageHeight
parts = append(parts, ebiten.ImagePart{ parts = append(parts, ebiten.ImagePart{
Dst: ebiten.Rect{float64(i * imageWidth), float64(j * imageHeight), imageWidth, imageHeight}, Dst: image.Rect(dstX, dstY, dstX+imageWidth, dstY+imageHeight),
Src: ebiten.Rect{0, 0, imageWidth, imageHeight}, Src: image.Rect(0, 0, imageWidth, imageHeight),
}) })
} }
} }

View File

@ -19,6 +19,7 @@ package main
import ( import (
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil" "github.com/hajimehoshi/ebiten/ebitenutil"
"image"
_ "image/jpeg" _ "image/jpeg"
"log" "log"
) )
@ -36,11 +37,11 @@ func Update(screen *ebiten.Image) error {
parts := []ebiten.ImagePart{} parts := []ebiten.ImagePart{}
w, h := gophersImage.Size() w, h := gophersImage.Size()
for i := 0; i < h; i++ { for i := 0; i < h; i++ {
width := float64(w) + float64(i)*0.75 width := w + i*3/4
x := float64(h-i) * 0.75 / 2 x := ((h - i) * 3 / 4) / 2
part := ebiten.ImagePart{ part := ebiten.ImagePart{
Dst: ebiten.Rect{x, float64(i), width, 1}, Dst: image.Rect(x, i, x+width, i+1),
Src: ebiten.Rect{0, float64(i), float64(w), 1}, Src: image.Rect(0, i, w, i+1),
} }
parts = append(parts, part) parts = append(parts, part)
} }

View File

@ -18,29 +18,22 @@ package ebiten
import ( import (
"github.com/hajimehoshi/ebiten/internal/opengl" "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. // An ImagePart represents a part of an image.
type ImagePart struct { type ImagePart struct {
Dst Rect Dst image.Rectangle
Src Rect Src image.Rectangle
} }
// DrawWholeImage draws the whole image. // DrawWholeImage draws the whole image.
func DrawWholeImage(target *Image, image *Image, geo GeometryMatrix, color ColorMatrix) error { func DrawWholeImage(target *Image, img *Image, geo GeometryMatrix, color ColorMatrix) error {
w, h := image.Size() w, h := img.Size()
parts := []ImagePart{ 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. // Filter represents the type of filter to be used when an image is maginified or minified.

View File

@ -18,6 +18,7 @@ package ebiten
import ( import (
"github.com/hajimehoshi/ebiten/internal/opengl" "github.com/hajimehoshi/ebiten/internal/opengl"
"image"
) )
func newGraphicsContext(screenWidth, screenHeight, screenScale int) (*graphicsContext, error) { func newGraphicsContext(screenWidth, screenHeight, screenScale int) (*graphicsContext, error) {
@ -72,7 +73,7 @@ func (c *graphicsContext) postUpdate() error {
clr := ColorMatrixI() clr := ColorMatrixI()
w, h := c.screen.size() w, h := c.screen.size()
parts := []ImagePart{ 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 { if err := c.defaultR.drawImage(c.screen, parts, geo, clr); err != nil {
return err return err

View File

@ -76,14 +76,14 @@ func v(y float64, height int) float32 {
func textureQuads(parts []ImagePart, width, height int) []shader.TextureQuad { func textureQuads(parts []ImagePart, width, height int) []shader.TextureQuad {
quads := make([]shader.TextureQuad, 0, len(parts)) quads := make([]shader.TextureQuad, 0, len(parts))
for _, part := range parts { for _, part := range parts {
x1 := float32(part.Dst.X) x1 := float32(part.Dst.Min.X)
x2 := float32(part.Dst.X + part.Dst.Width) x2 := float32(part.Dst.Max.X)
y1 := float32(part.Dst.Y) y1 := float32(part.Dst.Min.Y)
y2 := float32(part.Dst.Y + part.Dst.Height) y2 := float32(part.Dst.Max.Y)
u1 := u(part.Src.X, width) u1 := u(float64(part.Src.Min.X), width)
u2 := u(part.Src.X+part.Src.Width, width) u2 := u(float64(part.Src.Max.X), width)
v1 := v(part.Src.Y, height) v1 := v(float64(part.Src.Min.Y), height)
v2 := v(part.Src.Y+part.Src.Height, height) v2 := v(float64(part.Src.Max.Y), height)
quad := shader.TextureQuad{x1, x2, y1, y2, u1, u2, v1, v2} quad := shader.TextureQuad{x1, x2, y1, y2, u1, u2, v1, v2}
quads = append(quads, quad) quads = append(quads, quad)
} }