-
Notifications
You must be signed in to change notification settings - Fork 0
/
koipond.go
105 lines (93 loc) · 2.46 KB
/
koipond.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"fmt"
"image"
"log"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
const (
screenWidth = 200
screenHeight = 200
cols = 200
rows = 200
damping = float32(0.95)
)
var (
current = [cols][rows]float32{}
previous = [cols][rows]float32{}
)
type Game struct {
noiseImage *image.RGBA
}
func (g *Game) Init() {
// for i := 0; i < cols; i++ {
// for j := 0; j < rows; j++ {
// current[i][j] = 0
// previous[i][i] = 0
// }
// }
//previous[100][100] = 255
}
func (g *Game) Update() error {
mx, my := ebiten.CursorPosition()
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
if (0 <= mx && mx < screenWidth) && (0 <= my && my < screenHeight) {
previous[mx][my] = 255
}
} else {
var touchIDs []ebiten.TouchID
touchIDs = inpututil.AppendJustPressedTouchIDs(touchIDs[:0])
for _, id := range touchIDs {
mx, my := ebiten.TouchPosition(id)
if (mx != 0) && (my != 0) {
if (0 <= mx && mx < screenWidth) && (0 <= my && my < screenHeight) {
previous[mx][my] = 255
}
}
}
}
// Generate the noise with random RGB values.
for i := 1; i < cols-1; i++ {
for j := 1; j < rows-1; j++ {
current[i][j] = (previous[i-1][j]+previous[i+1][j]+
previous[i][j-1]+previous[i][j+1]+
previous[i-1][j-1]+previous[i-1][j+1]+
previous[i+1][j-1]+previous[i+1][j+1])/4 - current[i][j]
current[i][j] = current[i][j] * damping
//current[i][j] = 0.5
index := (i + j*cols) * 4
//fmt.Println(index)
g.noiseImage.Pix[index+0] = 0 //uint8(current[i][j] * 255)
g.noiseImage.Pix[index+1] = 0 //uint8(current[i][j] * 255)
g.noiseImage.Pix[index+2] = uint8(current[i][j] * 255)
g.noiseImage.Pix[index+3] = 255
}
}
//fmt.Println(current)
//swap
temp := previous
previous = current
current = temp
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
screen.ReplacePixels(g.noiseImage.Pix)
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f\nFPS: %0.2f", ebiten.CurrentTPS(), ebiten.CurrentFPS()))
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func main() {
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Noise (Ebiten Demo)")
g := &Game{
noiseImage: image.NewRGBA(image.Rect(0, 0, screenWidth, screenHeight)),
}
g.Init()
//ebiten.SetMaxTPS(5)
if err := ebiten.RunGame(g); err != nil {
log.Fatal(err)
}
}