mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 11:18:54 +01:00
Remove ebiten.Rect (#37)
This commit is contained in:
parent
ddd43370bc
commit
84f2e8aad2
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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})
|
||||
}
|
||||
}
|
||||
|
@ -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),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
21
graphics.go
21
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.
|
||||
|
@ -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
|
||||
|
16
image.go
16
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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user