v2 / examples / pendulum-simulation / modules / sim / img / worker.v
40 lines · 33 sloc · 1002 bytes · 017ace6ea7402430a992aa0820d5e472ebca74c7
Raw
1module img
2
3import benchmark
4import sim
5
6pub fn image_worker(mut writer PPMWriter, result_chan chan &sim.SimResult, settings ImageSettings) {
7 width := settings.width
8 height := settings.height
9 total_pixels := width * height
10
11 // as new pixels come in, write them to the image file
12 mut current_index := u64(0)
13 mut pixel_buf := []ValidColor{len: total_pixels, init: ValidColor{
14 valid: false
15 }}
16
17 mut bmark := benchmark.new_benchmark()
18 for {
19 result := <-result_chan or { break }
20
21 // find the closest magnet
22 pixel_buf[result.id].Color = compute_pixel(result)
23 pixel_buf[result.id].valid = true
24
25 for current_index < total_pixels && pixel_buf[current_index].valid {
26 bmark.step()
27 writer.handle_pixel(pixel_buf[current_index].Color) or {
28 bmark.fail()
29 sim.log(@MOD + '.' + @FN + ': pixel handler failed. Error ${err}')
30 break
31 }
32 bmark.ok()
33 current_index++
34 }
35 }
36 bmark.stop()
37 println(bmark.total_message(@FN))
38
39 writer.write() or { panic('Could not write image') }
40}
41