mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-27 11:12:44 +01:00
parent
931fec9100
commit
837f767f45
@ -33,7 +33,6 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
count int
|
|
||||||
brushImage *ebiten.Image
|
brushImage *ebiten.Image
|
||||||
canvasImage *ebiten.Image
|
canvasImage *ebiten.Image
|
||||||
)
|
)
|
||||||
@ -60,55 +59,64 @@ func init() {
|
|||||||
canvasImage.Fill(color.White)
|
canvasImage.Fill(color.White)
|
||||||
}
|
}
|
||||||
|
|
||||||
// paint draws the brush on the given canvas image at the position (x, y).
|
type Game struct {
|
||||||
func paint(canvas *ebiten.Image, x, y int) {
|
count 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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func update(screen *ebiten.Image) error {
|
func (g *Game) Update(screen *ebiten.Image) error {
|
||||||
drawn := false
|
drawn := false
|
||||||
|
|
||||||
// Paint the brush by mouse dragging
|
// Paint the brush by mouse dragging
|
||||||
mx, my := ebiten.CursorPosition()
|
mx, my := ebiten.CursorPosition()
|
||||||
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
|
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
|
||||||
paint(canvasImage, mx, my)
|
g.paint(canvasImage, mx, my)
|
||||||
drawn = true
|
drawn = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Paint the brush by touches
|
// Paint the brush by touches
|
||||||
for _, t := range ebiten.TouchIDs() {
|
for _, t := range ebiten.TouchIDs() {
|
||||||
x, y := ebiten.TouchPosition(t)
|
x, y := ebiten.TouchPosition(t)
|
||||||
paint(canvasImage, x, y)
|
g.paint(canvasImage, x, y)
|
||||||
drawn = true
|
drawn = true
|
||||||
}
|
}
|
||||||
if drawn {
|
if drawn {
|
||||||
count++
|
g.count++
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
if ebiten.IsDrawingSkipped() {
|
// paint draws the brush on the given canvas image at the position (x, y).
|
||||||
return nil
|
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)
|
screen.DrawImage(canvasImage, nil)
|
||||||
|
|
||||||
|
mx, my := ebiten.CursorPosition()
|
||||||
msg := fmt.Sprintf("(%d, %d)", mx, my)
|
msg := fmt.Sprintf("(%d, %d)", mx, my)
|
||||||
for _, t := range ebiten.TouchIDs() {
|
for _, t := range ebiten.TouchIDs() {
|
||||||
x, y := ebiten.TouchPosition(t)
|
x, y := ebiten.TouchPosition(t)
|
||||||
msg += fmt.Sprintf("\n(%d, %d) touch %d", x, y, t)
|
msg += fmt.Sprintf("\n(%d, %d) touch %d", x, y, t)
|
||||||
}
|
}
|
||||||
ebitenutil.DebugPrint(screen, msg)
|
ebitenutil.DebugPrint(screen, msg)
|
||||||
return nil
|
}
|
||||||
|
|
||||||
|
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
||||||
|
return screenWidth, screenHeight
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
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)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
func (g *Game) Update(screen *ebiten.Image) error {
|
||||||
if sprites.Len() < 500 && rand.Intn(4) < 3 {
|
if g.sprites == nil {
|
||||||
// Emit
|
g.sprites = list.New()
|
||||||
sprites.PushBack(newSprite(smokeImage))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 := e.Value.(*sprite)
|
||||||
s.update()
|
s.update()
|
||||||
if s.terminated() {
|
if s.terminated() {
|
||||||
defer sprites.Remove(e)
|
defer g.sprites.Remove(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
if ebiten.IsDrawingSkipped() {
|
func (g *Game) Draw(screen *ebiten.Image) {
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
screen.Fill(color.RGBA{0x99, 0xcc, 0xff, 0xff})
|
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 := e.Value.(*sprite)
|
||||||
s.draw(screen)
|
s.draw(screen)
|
||||||
}
|
}
|
||||||
|
|
||||||
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f\nSprites: %d", ebiten.CurrentTPS(), sprites.Len()))
|
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f\nSprites: %d", ebiten.CurrentTPS(), g.sprites.Len()))
|
||||||
return nil
|
}
|
||||||
|
|
||||||
|
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
||||||
|
return screenWidth, screenHeight
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
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)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user