-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoFractal.go
225 lines (195 loc) · 5.44 KB
/
GoFractal.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"fmt"
"github.com/jonhanks/Go-SDL/sdl"
"log"
"runtime"
"time"
)
// Basic size information
type Size struct {
Width, Height int
}
// run the fractal rendering
func doFractal(state *FractalData, screen *sdl.Surface, pal []uint32, done chan bool) {
f := func() {
done <- true
}
defer f()
start := time.Now()
Mandelbrot(state, screen, pal)
delta := time.Since(start)
fmt.Println("Compute and Render done in ", delta)
}
func main() {
WIDTH := 640
HEIGHT := 480
size := Size{Width: WIDTH, Height: HEIGHT}
runtime.GOMAXPROCS(runtime.NumCPU() * 2)
if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
log.Fatal(sdl.GetError())
}
defer sdl.Quit()
var screen = sdl.SetVideoMode(WIDTH, HEIGHT, 32, sdl.RESIZABLE)
if screen == nil {
log.Fatal(sdl.GetError())
}
var video_info = sdl.GetVideoInfo()
println("HW_available = ", video_info.HW_available)
println("WM_available = ", video_info.WM_available)
println("Video_mem = ", video_info.Video_mem, "kb")
sdl.EnableUNICODE(1)
sdl.WM_SetCaption("Go-SDL SDL Test", "")
running := true
redraw := true
requestRedraw := false
redrawFlag := &redraw
fullState := NewFractalData(WIDTH, HEIGHT, 2.0, 200)
fullState.Center = complex(-0.5, 0.0)
state := fullState.FractalState
state.DebugRender = true
state.DebugMerge = true
pal := NewPalette()
keymap := make(map[string]bool)
if sdl.GetKeyName(270) != "[+]" {
log.Fatal("GetKeyName broken")
}
// Note: The following SDL code is highly ineffective.
// It is eating too much CPU. If you intend to use Go-SDL,
// you should to do better than this.
fractalReady := make(chan bool)
for running {
if redraw {
fmt.Println("Parsing keypresses")
if keymap["+"] {
state.Scale *= 0.8
}
if keymap["-"] {
state.Scale *= 1.2
}
if keymap["left"] {
state.Center -= complex(state.Scale*0.1, 0.0)
}
if keymap["right"] {
state.Center += complex(state.Scale*0.1, 0.0)
}
if keymap["up"] {
state.Center += complex(0.0, state.Scale*0.1)
}
if keymap["down"] {
state.Center -= complex(0.0, state.Scale*0.1)
}
if keymap["iterup"] {
state.MaxIterations += 25
if state.MaxIterations > 2000 {
state.MaxIterations = 2000
}
}
if keymap["iterdown"] {
state.MaxIterations -= 25
if state.MaxIterations < 100 {
state.MaxIterations = 100
}
}
if keymap["overlay"] {
state.DebugOverlay = !state.DebugOverlay
}
keymap = make(map[string]bool)
fmt.Println("Queue fractal")
screen.FillRect(nil, 0)
if size.Width == WIDTH && size.Height == HEIGHT {
fullState.Merge(state)
} else {
// resize event
screen = sdl.SetVideoMode(size.Width, size.Height, 32, sdl.RESIZABLE)
if screen == nil {
log.Fatal(sdl.GetError())
}
WIDTH = size.Width
HEIGHT = size.Height
state.Width = WIDTH
state.Height = HEIGHT
fullState = NewFractalData(WIDTH, HEIGHT, state.Scale, state.MaxIterations)
fullState.FractalState = state
}
fmt.Println("Iterations: ", fullState.MaxIterations)
go doFractal(fullState, screen, pal, fractalReady)
redraw = false
requestRedraw = false
redrawFlag = &requestRedraw
}
select {
case _ = <-fractalReady:
fmt.Println("Fractal reports ready")
screen.Flip()
redraw = requestRedraw
requestRedraw = false
redrawFlag = &redraw
case _event := <-sdl.Events:
switch e := _event.(type) {
case sdl.QuitEvent:
running = false
case sdl.KeyboardEvent:
println("")
println(e.Keysym.Sym, ": ", sdl.GetKeyName(sdl.Key(e.Keysym.Sym)))
if e.Keysym.Sym == sdl.K_ESCAPE || e.Keysym.Sym == sdl.K_q {
running = false
}
if e.Type == sdl.KEYDOWN {
if e.Keysym.Sym == sdl.K_KP_PLUS || e.Keysym.Sym == sdl.K_EQUALS {
fmt.Println("zoom in")
keymap["+"] = true
*redrawFlag = true
} else if e.Keysym.Sym == sdl.K_KP_MINUS || e.Keysym.Sym == sdl.K_MINUS {
fmt.Println("zoom out")
keymap["-"] = true
*redrawFlag = true
} else if e.Keysym.Sym == sdl.K_LEFT {
keymap["left"] = true
*redrawFlag = true
} else if e.Keysym.Sym == sdl.K_RIGHT {
keymap["right"] = true
*redrawFlag = true
} else if e.Keysym.Sym == sdl.K_UP {
keymap["up"] = true
*redrawFlag = true
} else if e.Keysym.Sym == sdl.K_DOWN {
keymap["down"] = true
*redrawFlag = true
} else if e.Keysym.Sym == sdl.K_LEFTBRACKET {
keymap["iterup"] = true
*redrawFlag = true
} else if e.Keysym.Sym == sdl.K_RIGHTBRACKET {
keymap["iterdown"] = true
*redrawFlag = true
} else if e.Keysym.Sym == sdl.K_o {
keymap["overlay"] = true
*redrawFlag = true
}
}
/*fmt.Printf("%04x ", e.Type)
for i := 0; i < len(e.Pad0); i++ {
fmt.Printf("%02x ", e.Pad0[i])
}
println()
fmt.Printf("Type: %02x Which: %02x fullState: %02x Pad: %02x\n", e.Type, e.Which, e.State, e.Pad0[0])
fmt.Printf("Scancode: %02x Sym: %08x Mod: %04x Unicode: %04x\n", e.Keysym.Scancode, e.Keysym.Sym, e.Keysym.Mod, e.Keysym.Unicode)
*/
case sdl.MouseButtonEvent:
if e.Type == sdl.MOUSEBUTTONDOWN {
println("Click:", e.X, e.Y)
}
case sdl.ResizeEvent:
println("resize screen ", e.W, e.H)
//panic("Resize not supported yet")
size.Width = int(e.W)
size.Height = int(e.H)
*redrawFlag = true
//screen = sdl.SetVideoMode(int(e.W), int(e.H), 32, sdl.RESIZABLE)
//if screen == nil {
// log.Fatal(sdl.GetError())
//}
}
}
}
}