examples: Use RunGame

Updates #1111
This commit is contained in:
Hajime Hoshi 2020-05-15 03:51:40 +09:00
parent 931fec9100
commit 837f767f45
2 changed files with 53 additions and 35 deletions

View File

@ -33,7 +33,6 @@ const (
)
var (
count int
brushImage *ebiten.Image
canvasImage *ebiten.Image
)
@ -60,55 +59,64 @@ func init() {
canvasImage.Fill(color.White)
}
// paint draws the brush on the given canvas image at the position (x, y).
func paint(canvas *ebiten.Image, x, y int) {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(x), float64(y))
// Scale the color and rotate the hue so that colors vary on each frame.
op.ColorM.Scale(1.0, 0.50, 0.125, 1.0)
tps := ebiten.MaxTPS()
theta := 2.0 * math.Pi * float64(count%tps) / float64(tps)
op.ColorM.RotateHue(theta)
canvas.DrawImage(brushImage, op)
type Game struct {
count int
}
func update(screen *ebiten.Image) error {
func (g *Game) Update(screen *ebiten.Image) error {
drawn := false
// Paint the brush by mouse dragging
mx, my := ebiten.CursorPosition()
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
paint(canvasImage, mx, my)
g.paint(canvasImage, mx, my)
drawn = true
}
// Paint the brush by touches
for _, t := range ebiten.TouchIDs() {
x, y := ebiten.TouchPosition(t)
paint(canvasImage, x, y)
g.paint(canvasImage, x, y)
drawn = true
}
if drawn {
count++
g.count++
}
return nil
}
if ebiten.IsDrawingSkipped() {
return nil
}
// paint draws the brush on the given canvas image at the position (x, y).
func (g *Game) paint(canvas *ebiten.Image, x, y int) {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(x), float64(y))
// Scale the color and rotate the hue so that colors vary on each frame.
op.ColorM.Scale(1.0, 0.50, 0.125, 1.0)
tps := ebiten.MaxTPS()
theta := 2.0 * math.Pi * float64(g.count%tps) / float64(tps)
op.ColorM.RotateHue(theta)
canvas.DrawImage(brushImage, op)
}
func (g *Game) Draw(screen *ebiten.Image) {
screen.DrawImage(canvasImage, nil)
mx, my := ebiten.CursorPosition()
msg := fmt.Sprintf("(%d, %d)", mx, my)
for _, t := range ebiten.TouchIDs() {
x, y := ebiten.TouchPosition(t)
msg += fmt.Sprintf("\n(%d, %d) touch %d", x, y, t)
}
ebitenutil.DebugPrint(screen, msg)
return nil
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func main() {
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Paint (Ebiten Demo)"); err != nil {
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Paint (Ebiten Demo)")
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err)
}
}

View File

@ -137,38 +137,48 @@ func newSprite(img *ebiten.Image) *sprite {
}
}
var sprites = list.New()
type Game struct {
sprites *list.List
}
func update(screen *ebiten.Image) error {
if sprites.Len() < 500 && rand.Intn(4) < 3 {
// Emit
sprites.PushBack(newSprite(smokeImage))
func (g *Game) Update(screen *ebiten.Image) error {
if g.sprites == nil {
g.sprites = list.New()
}
for e := sprites.Front(); e != nil; e = e.Next() {
if g.sprites.Len() < 500 && rand.Intn(4) < 3 {
// Emit
g.sprites.PushBack(newSprite(smokeImage))
}
for e := g.sprites.Front(); e != nil; e = e.Next() {
s := e.Value.(*sprite)
s.update()
if s.terminated() {
defer sprites.Remove(e)
defer g.sprites.Remove(e)
}
}
return nil
}
if ebiten.IsDrawingSkipped() {
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
screen.Fill(color.RGBA{0x99, 0xcc, 0xff, 0xff})
for e := sprites.Front(); e != nil; e = e.Next() {
for e := g.sprites.Front(); e != nil; e = e.Next() {
s := e.Value.(*sprite)
s.draw(screen)
}
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f\nSprites: %d", ebiten.CurrentTPS(), sprites.Len()))
return nil
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f\nSprites: %d", ebiten.CurrentTPS(), g.sprites.Len()))
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func main() {
if err := ebiten.Run(update, screenWidth, screenHeight, 1, "Particles (Ebiten demo)"); err != nil {
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Particles (Ebiten demo)")
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err)
}
}