ebiten/example/paint/main.go

83 lines
1.9 KiB
Go
Raw Normal View History

2014-12-10 17:59:38 +01:00
package main
import (
"fmt"
2014-12-10 17:59:38 +01:00
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
2014-12-14 17:45:41 +01:00
"image/color"
2014-12-10 17:59:38 +01:00
"log"
2014-12-14 17:45:41 +01:00
"math"
2014-12-10 17:59:38 +01:00
"runtime"
)
const (
screenWidth = 320
screenHeight = 240
)
type Game struct {
2014-12-14 17:45:41 +01:00
count int
2014-12-14 15:06:21 +01:00
brushRenderTarget ebiten.RenderTargetID
2014-12-10 17:59:38 +01:00
canvasRenderTarget ebiten.RenderTargetID
}
func (g *Game) Update() error {
2014-12-14 17:45:41 +01:00
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
g.count++
}
2014-12-10 17:59:38 +01:00
return nil
}
func (g *Game) Draw(gr ebiten.GraphicsContext) error {
2014-12-14 15:06:21 +01:00
if g.brushRenderTarget.IsNil() {
var err error
g.brushRenderTarget, err = ebiten.NewRenderTargetID(1, 1, ebiten.FilterNearest)
if err != nil {
return err
}
gr.PushRenderTarget(g.brushRenderTarget)
2014-12-14 17:45:41 +01:00
gr.Fill(0xff, 0xff, 0xff)
2014-12-14 15:06:21 +01:00
gr.PopRenderTarget()
}
2014-12-10 17:59:38 +01:00
if g.canvasRenderTarget.IsNil() {
var err error
2014-12-14 09:28:19 +01:00
g.canvasRenderTarget, err = ebiten.NewRenderTargetID(screenWidth, screenHeight, ebiten.FilterNearest)
2014-12-10 17:59:38 +01:00
if err != nil {
return err
}
gr.PushRenderTarget(g.canvasRenderTarget)
2014-12-10 17:59:38 +01:00
gr.Fill(0xff, 0xff, 0xff)
gr.PopRenderTarget()
2014-12-10 17:59:38 +01:00
}
2014-12-14 15:06:21 +01:00
mx, my := ebiten.CursorPosition()
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
gr.PushRenderTarget(g.canvasRenderTarget)
geo := ebiten.GeometryMatrixI()
geo.Concat(ebiten.TranslateGeometry(float64(mx), float64(my)))
2014-12-14 17:45:41 +01:00
clr := ebiten.ColorMatrixI()
clr.Concat(ebiten.ScaleColor(color.RGBA{0xff, 0xff, 0x00, 0xff}))
2014-12-14 17:45:41 +01:00
theta := 2.0 * math.Pi * float64(g.count%60) / 60.0
clr.Concat(ebiten.RotateHue(theta))
ebiten.DrawWhole(gr.RenderTarget(g.brushRenderTarget), 1, 1, geo, clr)
2014-12-14 15:06:21 +01:00
gr.PopRenderTarget()
}
2014-12-10 17:59:38 +01:00
ebiten.DrawWhole(gr.RenderTarget(g.canvasRenderTarget), screenWidth, screenHeight, ebiten.GeometryMatrixI(), ebiten.ColorMatrixI())
2014-12-14 09:28:19 +01:00
ebitenutil.DebugPrint(gr, fmt.Sprintf("(%d, %d)", mx, my))
2014-12-10 17:59:38 +01:00
return nil
}
func init() {
runtime.LockOSThread()
}
func main() {
game := new(Game)
2014-12-14 09:28:19 +01:00
if err := ebiten.Run(game, screenWidth, screenHeight, 2, "Paint (Ebiten Demo)", 60); err != nil {
2014-12-10 17:59:38 +01:00
log.Fatal(err)
}
}