1
0

main.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2014 Hajime Hoshi
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "fmt"
  17. "image"
  18. "image/color"
  19. "log"
  20. "math"
  21. "github.com/hajimehoshi/ebiten/v2"
  22. "github.com/hajimehoshi/ebiten/v2/colorm"
  23. "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  24. )
  25. const (
  26. screenWidth = 640
  27. screenHeight = 480
  28. )
  29. var (
  30. brushImage *ebiten.Image
  31. )
  32. func init() {
  33. const (
  34. a0 = 0x40
  35. a1 = 0xc0
  36. a2 = 0xff
  37. )
  38. pixels := []uint8{
  39. a0, a1, a1, a0,
  40. a1, a2, a2, a1,
  41. a1, a2, a2, a1,
  42. a0, a1, a1, a0,
  43. }
  44. brushImage = ebiten.NewImageFromImage(&image.Alpha{
  45. Pix: pixels,
  46. Stride: 4,
  47. Rect: image.Rect(0, 0, 4, 4),
  48. })
  49. }
  50. type touch struct {
  51. id ebiten.TouchID
  52. pos pos
  53. }
  54. type pos struct {
  55. x int
  56. y int
  57. }
  58. type Game struct {
  59. cursor pos
  60. touches []touch
  61. count int
  62. canvasImage *ebiten.Image
  63. }
  64. func NewGame() *Game {
  65. g := &Game{
  66. canvasImage: ebiten.NewImage(screenWidth, screenHeight),
  67. }
  68. g.canvasImage.Fill(color.White)
  69. return g
  70. }
  71. func (g *Game) Update() error {
  72. drawn := false
  73. // Paint the brush by mouse dragging
  74. mx, my := ebiten.CursorPosition()
  75. if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
  76. g.paint(g.canvasImage, mx, my)
  77. drawn = true
  78. }
  79. g.cursor = pos{
  80. x: mx,
  81. y: my,
  82. }
  83. // Paint the brush by touches
  84. g.touches = g.touches[:0]
  85. for _, id := range ebiten.AppendTouchIDs(nil) {
  86. x, y := ebiten.TouchPosition(id)
  87. g.paint(g.canvasImage, x, y)
  88. g.touches = append(g.touches, touch{
  89. id: id,
  90. pos: pos{
  91. x: x,
  92. y: y,
  93. },
  94. })
  95. drawn = true
  96. }
  97. if drawn {
  98. g.count++
  99. }
  100. return nil
  101. }
  102. // paint draws the brush on the given canvas image at the position (x, y).
  103. func (g *Game) paint(canvas *ebiten.Image, x, y int) {
  104. op := &colorm.DrawImageOptions{}
  105. op.GeoM.Translate(float64(x), float64(y))
  106. var cm colorm.ColorM
  107. // Scale the color and rotate the hue so that colors vary on each frame.
  108. cm.Scale(1.0, 0.50, 0.125, 1.0)
  109. tps := ebiten.TPS()
  110. theta := 2.0 * math.Pi * float64(g.count%tps) / float64(tps)
  111. cm.RotateHue(theta)
  112. colorm.DrawImage(canvas, brushImage, cm, op)
  113. }
  114. func (g *Game) Draw(screen *ebiten.Image) {
  115. screen.DrawImage(g.canvasImage, nil)
  116. msg := fmt.Sprintf("(%d, %d)", g.cursor.x, g.cursor.y)
  117. for _, t := range g.touches {
  118. msg += fmt.Sprintf("\n(%d, %d) touch %d", t.pos.x, t.pos.y, t.id)
  119. }
  120. ebitenutil.DebugPrint(screen, msg)
  121. }
  122. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  123. return screenWidth, screenHeight
  124. }
  125. func main() {
  126. ebiten.SetWindowSize(screenWidth, screenHeight)
  127. ebiten.SetWindowTitle("Paint (Ebitengine Demo)")
  128. if err := ebiten.RunGame(NewGame()); err != nil {
  129. log.Fatal(err)
  130. }
  131. }