@@ -14,14 +14,47 @@ import (
1414 "golang.org/x/text/message"
1515)
1616
17- func Iterative (scene * Scene , file string , width , height , depth int , direct bool ) error {
17+ type Limits struct {
18+ DumpPeriod time.Duration
19+ Time time.Duration
20+ Frames int
21+ }
22+
23+ func NewLimits (d int , t int , f int ) * Limits {
24+ if d == 0 {
25+ d = math .MaxInt32
26+ }
27+ if t == 0 {
28+ t = math .MaxInt32
29+ }
30+ return & Limits {
31+ DumpPeriod : time .Duration (d ) * time .Second ,
32+ Time : time .Duration (t ) * time .Second ,
33+ Frames : f ,
34+ }
35+ }
36+
37+ func Iterative (scene * Scene , limits * Limits , file string , width , height , depth int , direct bool ) error {
1838 kill := make (chan os.Signal , 2 )
1939 signal .Notify (kill , os .Interrupt , syscall .SIGTERM )
2040
41+ if limits == nil {
42+ limits = NewLimits (6 , 0 , 0 )
43+ fmt .Print ("derp\n " )
44+ }
45+
2146 frame := scene .Render (width , height , depth , direct )
2247 defer frame .Stop ()
23- ticker := time .NewTicker (6 * time . Second ) // 10 .s = 1 minute, 100 .s = 1 hr
48+ ticker := time .NewTicker (limits . DumpPeriod )
2449 defer ticker .Stop ()
50+ limiter := time .NewTicker (limits .Time )
51+ defer limiter .Stop ()
52+ framelim := time .NewTicker (1 * time .Second )
53+ if limits .Frames == 0 {
54+ framelim .Stop ()
55+ } else {
56+ defer framelim .Stop ()
57+ }
2558
2659 start := time .Now ().UnixNano ()
2760 max := 0
@@ -31,6 +64,14 @@ func Iterative(scene *Scene, file string, width, height, depth int, direct bool)
3164 select {
3265 case <- kill :
3366 frame .Stop ()
67+ case <- limiter .C :
68+ frame .Stop ()
69+ fmt .Printf ("\n Time limit reached.\n " )
70+ case <- framelim .C :
71+ if _ , n := frame .Sample (); n > limits .Frames {
72+ frame .Stop ()
73+ fmt .Print ("\n Frame limit reached.\n " )
74+ }
3475 case <- ticker .C :
3576 if sample , n := frame .Sample (); n > max {
3677 max = n
@@ -43,12 +84,16 @@ func Iterative(scene *Scene, file string, width, height, depth int, direct bool)
4384 }
4485
4586 stop := time .Now ().UnixNano ()
46- sample , _ := frame .Sample ()
87+ sample , frames := frame .Sample ()
4788 total := sample .Total ()
4889 p := message .NewPrinter (language .English )
4990 secs := float64 (stop - start ) / 1e9
5091 sps := math .Round (float64 (total ) / secs ) // TODO: rename to pixels/sec for clarity
5192 p .Printf ("\n %v samples in %.1f seconds (%.0f samples/sec)\n " , total , secs , sps )
93+ p .Printf ("\n %v frames (%.1f frames/sec)\n " , frames , float64 (frames )/ secs )
94+ if err := writePng (file , sample .Image ()); err != nil {
95+ return err
96+ }
5297
5398 return nil
5499}
0 commit comments