-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathinit.go
98 lines (86 loc) · 2.52 KB
/
init.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
package oak
import (
"fmt"
"image"
"math/rand"
"os"
"strings"
"time"
"github.com/oakmound/oak/v4/dlog"
"github.com/oakmound/oak/v4/oakerr"
"github.com/oakmound/oak/v4/scene"
"github.com/oakmound/oak/v4/timing"
)
var (
zeroPoint = image.Point{0, 0}
)
// Init initializes the oak engine.
// After the configuration options have been parsed and validated, this will run concurrent
// routines drawing to an OS window or app, forwarding OS inputs to this window's configured
// event handler, and running scenes: first the predefined 'loading' scene, then firstScene
// as provided here, then scenes following commands sent to the window or returned by ending
// scenes.
func (w *Window) Init(firstScene string, configOptions ...ConfigOption) error {
var err error
w.config, err = NewConfig(configOptions...)
if err != nil {
return fmt.Errorf("failed to create config: %w", err)
}
lvl, err := dlog.ParseDebugLevel(w.config.Debug.Level)
if err != nil {
return fmt.Errorf("failed to parse debug config: %w", err)
}
dlog.SetFilter(func(msg string) bool {
return strings.Contains(msg, w.config.Debug.Filter)
})
// This error cannot happen as it would surface in Parse above
_ = dlog.SetLogLevel(lvl)
err = oakerr.SetLanguageString(w.config.Language)
if err != nil {
return err
}
w.ScreenWidth = w.config.Screen.Width
w.ScreenHeight = w.config.Screen.Height
w.FrameRate = w.config.FrameRate
w.DrawFrameRate = w.config.DrawFrameRate
w.IdleDrawFrameRate = w.config.IdleDrawFrameRate
// assume we are in focus on window creation
w.inFocus = true
w.Driver = w.config.Driver
w.DrawTicker = time.NewTicker(timing.FPSToFrameDelay(w.DrawFrameRate))
if w.config.TrackInputChanges {
trackJoystickChanges(w.eventHandler)
}
if !w.config.SkipRNGSeed {
// seed math/rand with time.Now, useful for minimal examples
//that would tend to forget to do this.
rand.Seed(time.Now().UTC().UnixNano())
}
overrideInit(w)
err = w.SceneMap.AddScene(oakLoadingScene, scene.Scene{
Start: func(ctx *scene.Context) {
if w.config.BatchLoad {
go func() {
w.loadAssets(w.config.Assets.ImagePath, w.config.Assets.AudioPath)
w.endLoad()
}()
} else {
go w.endLoad()
}
},
End: func() (string, *scene.Result) {
return w.firstScene, &scene.Result{
NextSceneInput: w.FirstSceneInput,
}
},
})
if err != nil {
return err
}
go w.sceneLoop(firstScene, w.config.TrackInputChanges)
if w.config.EnableDebugConsole {
go w.debugConsole(os.Stdin, os.Stdout)
}
w.Driver(w.lifecycleLoop)
return w.exitError
}