mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 03:08:54 +01:00
Simplify examples
This commit is contained in:
parent
b3581d1c7a
commit
0fe8387a5a
@ -36,7 +36,7 @@ var (
|
||||
saved bool
|
||||
)
|
||||
|
||||
func Update(r *ebiten.Image) error {
|
||||
func Update(screen *ebiten.Image) error {
|
||||
count++
|
||||
count %= 600
|
||||
diff := float64(count) * 0.2
|
||||
@ -58,17 +58,17 @@ func Update(r *ebiten.Image) error {
|
||||
}
|
||||
}
|
||||
|
||||
r.Fill(color.NRGBA{0x00, 0x00, 0x80, 0xff})
|
||||
screen.Fill(color.NRGBA{0x00, 0x00, 0x80, 0xff})
|
||||
for i := 0; i < 10; i++ {
|
||||
geo := ebiten.TranslateGeometry(0, float64(i)*(diff))
|
||||
clr := ebiten.ColorMatrixI()
|
||||
if err := ebiten.DrawWholeImage(r, tmpRenderTarget, geo, clr); err != nil {
|
||||
if err := ebiten.DrawWholeImage(screen, tmpRenderTarget, geo, clr); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if !saved && ebiten.IsKeyPressed(ebiten.KeySpace) {
|
||||
if err := ebitenutil.SaveImageAsPNG("out.png", r); err != nil {
|
||||
if err := ebitenutil.SaveImageAsPNG("out.png", screen); err != nil {
|
||||
return err
|
||||
}
|
||||
saved = true
|
||||
|
@ -30,33 +30,32 @@ const (
|
||||
|
||||
const mosaicRatio = 16
|
||||
|
||||
type Game struct {
|
||||
var (
|
||||
gophersImage *ebiten.Image
|
||||
gophersRenderTarget *ebiten.Image
|
||||
}
|
||||
)
|
||||
|
||||
func (g *Game) Update(r *ebiten.Image) error {
|
||||
func Update(screen *ebiten.Image) error {
|
||||
geo := ebiten.ScaleGeometry(1.0/mosaicRatio, 1.0/mosaicRatio)
|
||||
ebiten.DrawWholeImage(g.gophersRenderTarget, g.gophersImage, geo, ebiten.ColorMatrixI())
|
||||
ebiten.DrawWholeImage(gophersRenderTarget, gophersImage, geo, ebiten.ColorMatrixI())
|
||||
|
||||
geo = ebiten.ScaleGeometry(mosaicRatio/2.0, mosaicRatio/2.0)
|
||||
ebiten.DrawWholeImage(r, g.gophersRenderTarget, geo, ebiten.ColorMatrixI())
|
||||
ebiten.DrawWholeImage(screen, gophersRenderTarget, geo, ebiten.ColorMatrixI())
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
g := new(Game)
|
||||
var err error
|
||||
g.gophersImage, _, err = ebitenutil.NewImageFromFile("images/gophers.jpg", ebiten.FilterNearest)
|
||||
gophersImage, _, err = ebitenutil.NewImageFromFile("images/gophers.jpg", ebiten.FilterNearest)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
w, h := g.gophersImage.Size()
|
||||
g.gophersRenderTarget, err = ebiten.NewImage(w/mosaicRatio, h/mosaicRatio, ebiten.FilterNearest)
|
||||
w, h := gophersImage.Size()
|
||||
gophersRenderTarget, err = ebiten.NewImage(w/mosaicRatio, h/mosaicRatio, ebiten.FilterNearest)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := ebiten.Run(g.Update, screenWidth, screenHeight, 2, "Mosaic (Ebiten Demo)"); err != nil {
|
||||
if err := ebiten.Run(Update, screenWidth, screenHeight, 2, "Mosaic (Ebiten Demo)"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
@ -30,21 +30,15 @@ const (
|
||||
screenHeight = 240
|
||||
)
|
||||
|
||||
type Game struct {
|
||||
inited bool
|
||||
var (
|
||||
count int
|
||||
brushRenderTarget *ebiten.Image
|
||||
canvasRenderTarget *ebiten.Image
|
||||
}
|
||||
)
|
||||
|
||||
func (g *Game) Update(r *ebiten.Image) error {
|
||||
func Update(screen *ebiten.Image) error {
|
||||
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
|
||||
g.count++
|
||||
}
|
||||
if !g.inited {
|
||||
g.brushRenderTarget.Fill(color.White)
|
||||
g.canvasRenderTarget.Fill(color.White)
|
||||
g.inited = true
|
||||
count++
|
||||
}
|
||||
|
||||
mx, my := ebiten.CursorPosition()
|
||||
@ -52,29 +46,32 @@ func (g *Game) Update(r *ebiten.Image) error {
|
||||
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
|
||||
geo := ebiten.TranslateGeometry(float64(mx), float64(my))
|
||||
clr := ebiten.ScaleColor(1.0, 0.25, 0.25, 1.0)
|
||||
theta := 2.0 * math.Pi * float64(g.count%60) / 60.0
|
||||
theta := 2.0 * math.Pi * float64(count%60) / 60.0
|
||||
clr.Concat(ebiten.RotateHue(theta))
|
||||
ebiten.DrawWholeImage(g.canvasRenderTarget, g.brushRenderTarget, geo, clr)
|
||||
ebiten.DrawWholeImage(canvasRenderTarget, brushRenderTarget, geo, clr)
|
||||
}
|
||||
|
||||
ebiten.DrawWholeImage(r, g.canvasRenderTarget, ebiten.GeometryMatrixI(), ebiten.ColorMatrixI())
|
||||
ebiten.DrawWholeImage(screen, canvasRenderTarget, ebiten.GeometryMatrixI(), ebiten.ColorMatrixI())
|
||||
|
||||
ebitenutil.DebugPrint(r, fmt.Sprintf("(%d, %d)", mx, my))
|
||||
ebitenutil.DebugPrint(screen, fmt.Sprintf("(%d, %d)", mx, my))
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
g := new(Game)
|
||||
var err error
|
||||
g.brushRenderTarget, err = ebiten.NewImage(1, 1, ebiten.FilterNearest)
|
||||
brushRenderTarget, err = ebiten.NewImage(4, 4, ebiten.FilterNearest)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
g.canvasRenderTarget, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest)
|
||||
brushRenderTarget.Fill(color.White)
|
||||
|
||||
canvasRenderTarget, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := ebiten.Run(g.Update, screenWidth, screenHeight, 2, "Paint (Ebiten Demo)"); err != nil {
|
||||
canvasRenderTarget.Fill(color.White)
|
||||
|
||||
if err := ebiten.Run(Update, screenWidth, screenHeight, 2, "Paint (Ebiten Demo)"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
@ -28,13 +28,13 @@ const (
|
||||
screenHeight = 240
|
||||
)
|
||||
|
||||
type Game struct {
|
||||
var (
|
||||
gophersImage *ebiten.Image
|
||||
}
|
||||
)
|
||||
|
||||
func (g *Game) Update(r *ebiten.Image) error {
|
||||
func Update(screen *ebiten.Image) error {
|
||||
parts := []ebiten.ImagePart{}
|
||||
w, h := g.gophersImage.Size()
|
||||
w, h := gophersImage.Size()
|
||||
for i := 0; i < h; i++ {
|
||||
width := float64(w) + float64(i)*0.75
|
||||
x := float64(h-i) * 0.75 / 2
|
||||
@ -48,18 +48,17 @@ func (g *Game) Update(r *ebiten.Image) error {
|
||||
geo := ebiten.TranslateGeometry(-maxWidth/2, -float64(h)/2)
|
||||
geo.Concat(ebiten.ScaleGeometry(0.4, 0.4))
|
||||
geo.Concat(ebiten.TranslateGeometry(screenWidth/2, screenHeight/2))
|
||||
r.DrawImage(g.gophersImage, parts, geo, ebiten.ColorMatrixI())
|
||||
screen.DrawImage(gophersImage, parts, geo, ebiten.ColorMatrixI())
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
g := new(Game)
|
||||
var err error
|
||||
g.gophersImage, _, err = ebitenutil.NewImageFromFile("images/gophers.jpg", ebiten.FilterNearest)
|
||||
gophersImage, _, err = ebitenutil.NewImageFromFile("images/gophers.jpg", ebiten.FilterNearest)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := ebiten.Run(g.Update, screenWidth, screenHeight, 2, "Perspective (Ebiten Demo)"); err != nil {
|
||||
if err := ebiten.Run(Update, screenWidth, screenHeight, 2, "Perspective (Ebiten Demo)"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user