-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcontext.go
206 lines (169 loc) · 4.78 KB
/
context.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
package ge
import (
"encoding/json"
"fmt"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/audio"
resource "github.com/quasilyte/ebitengine-resource"
"github.com/quasilyte/gmath"
"github.com/quasilyte/ge/input"
"github.com/quasilyte/ge/internal/gamedata"
"github.com/quasilyte/ge/langs"
)
type Context struct {
Loader *resource.Loader
Renderer *Renderer
Input input.System
Audio AudioSystem
Dict *langs.Dictionary
Rand gmath.Rand
CurrentScene *RootScene
nextScene *RootScene
// If non-nil, this function is used to create a scene controller that will handle the panic.
// The single arguments holds the occurred panic information.
// When game panics for whatever reason, instead of crashing, you can assign a
// recovery controller constructor here.
// You can just show the error to the user and crash or you may want to recover the game somehow
// (e.g. run the main menu controller again).
NewPanicController func(panicInfo *PanicInfo) SceneController
// updateFn - user defined function is called in every update cycle if not null
updateFn func(delta float64)
OnCriticalError func(err error)
GameName string
FullScreen bool
WindowTitle string
WindowWidth float64
WindowHeight float64
ScreenWidth float64
ScreenHeight float64
layoutWidth int
layoutHeight int
firstController SceneController
fixedDelta float64
imageCache imageCache
}
type PanicInfo struct {
// A controller that was active during the panic.
Controller SceneController
// The error trace.
Trace string
// A value retrieved from recover().
Value any
}
type TimeDeltaMode int
const (
TimeDeltaComputed60 TimeDeltaMode = iota
TimeDeltaComputed120
TimeDeltaFixed60
TimeDeltaFixed120
)
type ContextConfig struct {
Mute bool
// UpdateFn - user defined function is called in every update cycle if not null
UpdateFn func(delta float64)
TimeDeltaMode TimeDeltaMode
}
func NewContext(config ContextConfig) *Context {
ctx := &Context{
WindowTitle: "GE Game",
}
switch config.TimeDeltaMode {
case TimeDeltaComputed60:
// Nothing to do.
case TimeDeltaComputed120:
ebiten.SetTPS(120)
case TimeDeltaFixed60:
ctx.fixedDelta = 1.0 / 60.0
case TimeDeltaFixed120:
ctx.fixedDelta = 1.0 / 120.0
ebiten.SetTPS(120)
}
audioContext := audio.NewContext(44100)
ctx.Loader = resource.NewLoader(audioContext)
if config.Mute {
ctx.Audio.muted = true
} else {
ctx.Audio.init(audioContext, ctx.Loader)
}
ctx.Renderer = NewRenderer()
ctx.Rand.SetSeed(0)
// TODO: some platforms don't need touches
ctx.Input.Init(input.SystemConfig{DevicesEnabled: input.AnyDevice})
ctx.OnCriticalError = func(err error) {
panic(err)
}
ctx.updateFn = config.UpdateFn
ctx.imageCache.Init()
return ctx
}
func (ctx *Context) ChangeScene(controller SceneController) {
ctx.nextScene = ctx.newRootScene(controller)
}
func (ctx *Context) newRootScene(controller SceneController) *RootScene {
rootScene := newRootScene()
rootScene.context = ctx
rootScene.controller = controller
return rootScene
}
func (ctx *Context) Draw(screen *ebiten.Image) {
ctx.Renderer.Draw(screen, &ctx.CurrentScene.graphics)
}
func (ctx *Context) WindowRect() gmath.Rect {
return gmath.Rect{
Max: gmath.Vec{X: ctx.WindowWidth, Y: ctx.WindowHeight},
}
}
func (ctx *Context) LocateGameData(key string) string {
return gamedata.Locate(ctx.GameName, key)
}
func (ctx *Context) SaveGameData(key string, data any) {
if ctx.GameName == "" {
panic("can't save game data with empty Context.GameName")
}
jsonData, err := json.Marshal(data)
if err != nil {
panic(fmt.Sprintf("can't save game data with key %q: %v", key, err))
}
err = gamedata.Save(ctx.GameName, key, jsonData)
if err != nil {
panic(fmt.Sprintf("can't save game data with key %q: %v", key, err))
}
}
func (ctx *Context) CheckGameData(key string) bool {
exists, err := gamedata.Exists(ctx.GameName, key)
return exists && err == nil
}
func (ctx *Context) ReadGameData(key string) ([]byte, error) {
if ctx.GameName == "" {
panic("can't load game data with empty Context.GameName")
}
exists, err := gamedata.Exists(ctx.GameName, key)
if err != nil {
return nil, fmt.Errorf("touch data with key %q: %w", key, err)
}
if !exists {
return nil, nil
}
data, err := gamedata.Load(ctx.GameName, key)
if err != nil {
return nil, fmt.Errorf("load game data with key %q: %w", key, err)
}
return data, nil
}
func (ctx *Context) LoadGameData(key string, dst any) error {
data, err := ctx.ReadGameData(key)
if err != nil {
return err
}
if data == nil {
ctx.SaveGameData(key, dst)
return nil
}
return json.Unmarshal(data, dst)
}
func (ctx *Context) InferDisplayRatio() (int, int) {
return inferDisplayRatio(ctx.layoutWidth, ctx.layoutHeight)
}
func (ctx *Context) LayoutSize() (int, int) {
return ctx.layoutWidth, ctx.layoutHeight
}