-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
115 lines (98 loc) · 2.29 KB
/
main.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
package main
import (
"flag"
"log"
"math/rand"
"os"
"runtime/pprof"
"strconv"
"time"
"github.com/DiscoViking/goBrains/config"
"github.com/DiscoViking/goBrains/entity"
"github.com/DiscoViking/goBrains/entitymanager"
"github.com/DiscoViking/goBrains/events"
"github.com/DiscoViking/goBrains/iomanager"
"github.com/DiscoViking/goBrains/iomanager/sdl"
"github.com/DiscoViking/goBrains/iomanager/web"
)
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
var headless = flag.Bool("headless", false, "run in headless mode")
var port = flag.Int("port", 9999, "port to listen on for web connections")
var (
drawing = true
running = true
rateLimit = false
)
func main() {
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
// Run for 30 seconds and stop.
stop := time.After(30 * time.Second)
go func() {
<-stop
running = false
}()
}
rand.Seed(time.Now().UnixNano())
config.Load("config.gcfg")
em := entitymanager.New()
em.Reset()
io := iomanager.New(em.LocationManager())
defer io.Shutdown()
if !*headless {
sdl.Start(io)
rateLimit = true
}
drawTimer := time.Tick(16 * time.Millisecond)
ticktime := 16 * time.Millisecond
tickTimer := time.Tick(ticktime)
web.Start(io, strconv.Itoa(*port))
events.Global.Register(events.TERMINATE,
func(e events.Event) { running = false })
events.Global.Register(events.TOGGLE_DRAW,
func(e events.Event) { drawing = !drawing })
events.Global.Register(events.TOGGLE_FRAME_LIMIT,
func(e events.Event) { rateLimit = !rateLimit })
events.Global.Register(events.SPEED_UP,
func(e events.Event) {
ticktime /= 2
tickTimer = time.Tick(ticktime)
})
events.Global.Register(events.SPEED_DOWN,
func(e events.Event) {
ticktime *= 2
tickTimer = time.Tick(ticktime)
})
drawFunc := func() {
if drawing {
io.Distribute(em.Entities())
} else {
io.Distribute([]entity.Entity{})
}
}
frames := 0
before := time.Now()
for running {
frames += 1
if time.Since(before) > 2*time.Second {
before = time.Now()
log.Printf("FPS: %v\n", frames/2)
frames = 0
}
em.Spin()
if rateLimit {
<-tickTimer
}
select {
case <-drawTimer:
drawFunc()
default:
}
}
}