forked from hajimehoshi/ebiten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uicontext.go
362 lines (303 loc) · 8.97 KB
/
uicontext.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Copyright 2014 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ebiten
import (
"fmt"
"math"
"sync"
"sync/atomic"
"github.com/hajimehoshi/ebiten/internal/buffered"
"github.com/hajimehoshi/ebiten/internal/clock"
"github.com/hajimehoshi/ebiten/internal/driver"
"github.com/hajimehoshi/ebiten/internal/hooks"
)
type defaultGame struct {
update func(screen *Image) error
width int
height int
context *uiContext
}
func (d *defaultGame) Update(screen *Image) error {
return d.update(screen)
}
func (d *defaultGame) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
// Ignore the outside size.
d.context.m.Lock()
w, h := d.width, d.height
d.context.m.Unlock()
return w, h
}
type uiContext struct {
game Game
offscreen *Image
screen *Image
updateCalled bool
// scaleForWindow is the scale of a window. This doesn't represent the scale on fullscreen. This value works
// only on desktops.
//
// scaleForWindow is for backward compatibility and is used to calculate the window size when SetScreenSize
// is called.
scaleForWindow float64
outsideSizeUpdated bool
outsideWidth float64
outsideHeight float64
err atomic.Value
m sync.Mutex
}
var theUIContext = &uiContext{}
func (c *uiContext) set(game Game, scaleForWindow float64) {
c.m.Lock()
defer c.m.Unlock()
c.game = game
if g, ok := game.(*defaultGame); ok {
c.scaleForWindow = scaleForWindow
g.context = c
}
}
func (c *uiContext) setError(err error) {
c.err.Store(err)
}
func (c *uiContext) setScaleForWindow(scale float64) {
c.m.Lock()
defer c.m.Unlock()
if c.game == nil {
panic("ebiten: setScaleForWindow can be called only after the main loop starts")
}
g, ok := c.game.(*defaultGame)
if !ok {
panic("ebiten: setScaleForWindow can be called only when Run is used")
}
if w := uiDriver().Window(); w != nil {
ww, wh := g.width, g.height
c.scaleForWindow = scale
w.SetSize(int(float64(ww)*scale), int(float64(wh)*scale))
}
}
func (c *uiContext) getScaleForWindow() float64 {
c.m.Lock()
defer c.m.Unlock()
if c.game == nil {
panic("ebiten: getScaleForWindow can be called only after the main loop starts")
}
if _, ok := c.game.(*defaultGame); !ok {
panic("ebiten: getScaleForWindow can be called only when Run is used")
}
s := c.scaleForWindow
return s
}
// setScreenSize sets the (logical) screen size and adjusts the window size.
//
// setScreenSize is for backward compatibility. This is called from ebiten.SetScreenSize.
func (c *uiContext) setScreenSize(width, height int) {
c.m.Lock()
defer c.m.Unlock()
if c.game == nil {
panic("ebiten: SetScreenSize can be called only after the main loop starts")
}
g, ok := c.game.(*defaultGame)
if !ok {
panic("ebiten: SetScreenSize can be called only when Run is used")
}
g.width = width
g.height = height
if w := uiDriver().Window(); w != nil {
s := c.scaleForWindow
w.SetSize(int(float64(width)*s), int(float64(height)*s))
}
}
func (c *uiContext) Layout(outsideWidth, outsideHeight float64) {
c.outsideSizeUpdated = true
c.outsideWidth = outsideWidth
c.outsideHeight = outsideHeight
}
func (c *uiContext) updateOffscreen() {
sw, sh := c.game.Layout(int(c.outsideWidth), int(c.outsideHeight))
if sw <= 0 || sh <= 0 {
panic("ebiten: Layout must return positive numbers")
}
if c.offscreen != nil && !c.outsideSizeUpdated {
if w, h := c.offscreen.Size(); w == sw && h == sh {
return
}
}
c.outsideSizeUpdated = false
if c.screen != nil {
_ = c.screen.Dispose()
c.screen = nil
}
if c.offscreen != nil {
if w, h := c.offscreen.Size(); w != sw || h != sh {
_ = c.offscreen.Dispose()
c.offscreen = nil
}
}
if c.offscreen == nil {
c.offscreen = newImage(sw, sh, FilterDefault)
c.offscreen.mipmap.SetVolatile(IsScreenClearedEveryFrame())
}
// The window size is automatically adjusted when Run is used.
if _, ok := c.game.(*defaultGame); ok {
c.setScreenSize(sw, sh)
}
// TODO: This is duplicated with mobile/ebitenmobileview/funcs.go. Refactor this.
d := uiDriver().DeviceScaleFactor()
c.screen = newScreenFramebufferImage(int(c.outsideWidth*d), int(c.outsideHeight*d))
// Do not have to update scaleForWindow since this is used only for backward compatibility.
// Then, if a window is resizable, scaleForWindow (= ebiten.ScreenScale) might not match with the actual
// scale. This is fine since ebiten.ScreenScale will be deprecated.
}
func (c *uiContext) setScreenClearedEveryFrame(cleared bool) {
c.m.Lock()
defer c.m.Unlock()
if c.offscreen != nil {
c.offscreen.mipmap.SetVolatile(cleared)
}
}
func (c *uiContext) setWindowResizable(resizable bool) {
c.m.Lock()
defer c.m.Unlock()
if resizable && c.game != nil {
if _, ok := c.game.(*defaultGame); ok {
panic("ebiten: a resizable window works with RunGame, not Run")
}
}
if w := uiDriver().Window(); w != nil {
w.SetResizable(resizable)
}
}
func (c *uiContext) screenScale() float64 {
if c.offscreen == nil {
return 0
}
sw, sh := c.offscreen.Size()
d := uiDriver().DeviceScaleFactor()
scaleX := c.outsideWidth / float64(sw) * d
scaleY := c.outsideHeight / float64(sh) * d
return math.Min(scaleX, scaleY)
}
func (c *uiContext) offsets() (float64, float64) {
if c.offscreen == nil {
return 0, 0
}
sw, sh := c.offscreen.Size()
d := uiDriver().DeviceScaleFactor()
s := c.screenScale()
width := float64(sw) * s
height := float64(sh) * s
return (c.outsideWidth*d - width) / 2, (c.outsideHeight*d - height) / 2
}
func (c *uiContext) Update() error {
// TODO: If updateCount is 0 and vsync is disabled, swapping buffers can be skipped.
if err, ok := c.err.Load().(error); ok && err != nil {
return err
}
if err := buffered.BeginFrame(); err != nil {
return err
}
if err := c.update(); err != nil {
return err
}
if err := buffered.EndFrame(); err != nil {
return err
}
return nil
}
func (c *uiContext) Draw() error {
if err, ok := c.err.Load().(error); ok && err != nil {
return err
}
if err := buffered.BeginFrame(); err != nil {
return err
}
c.draw()
if err := buffered.EndFrame(); err != nil {
return err
}
return nil
}
func (c *uiContext) update() error {
_, hasDraw := c.game.(interface{ Draw(*Image) })
updateCount := clock.Update(MaxTPS())
// Ensure that Update is called once before Draw so that Update can be used for initialization.
if !c.updateCalled && updateCount == 0 {
updateCount = 1
c.updateCalled = true
}
for i := 0; i < updateCount; i++ {
c.updateOffscreen()
// When the game's Draw exists, rendering should be always skipped.
//
// When the game's Draw does not exist, the last Update call should process drawing.
// This assumes that (*uiContext).Update and (*uiContext).Draw are called successively.
//
// TODO: Make (Game).Draw mandatory and remove this assumption when we can update the major version.
// Move the clock usage to the UI driver side.
setDrawingSkipped(hasDraw || i < updateCount-1)
if err := hooks.RunBeforeUpdateHooks(); err != nil {
return err
}
// Multiple successive Clear call should be integrated into one graphics command, then
// calling Clear on every Update should not affect the performance.
if IsScreenClearedEveryFrame() {
c.offscreen.Clear()
}
if err := c.game.Update(c.offscreen); err != nil {
return err
}
uiDriver().ResetForFrame()
}
return nil
}
func (c *uiContext) draw() {
// c.screen might be nil when updateCount is 0 in the initial state (#1039).
if c.screen == nil {
return
}
if game, ok := c.game.(interface{ Draw(*Image) }); ok {
if IsScreenClearedEveryFrame() {
c.offscreen.Clear()
}
game.Draw(c.offscreen)
}
// This clear is needed for fullscreen mode or some mobile platforms (#622).
c.screen.Clear()
op := &DrawImageOptions{}
s := c.screenScale()
switch vd := uiDriver().Graphics().FramebufferYDirection(); vd {
case driver.Upward:
op.GeoM.Scale(s, -s)
_, h := c.offscreen.Size()
op.GeoM.Translate(0, float64(h)*s)
case driver.Downward:
op.GeoM.Scale(s, s)
default:
panic(fmt.Sprintf("ebiten: invalid v-direction: %d", vd))
}
op.GeoM.Translate(c.offsets())
op.CompositeMode = CompositeModeCopy
// filterScreen works with >=1 scale, but does not well with <1 scale.
// Use regular FilterLinear instead so far (#669).
if s >= 1 {
op.Filter = filterScreen
} else {
op.Filter = FilterLinear
}
_ = c.screen.DrawImage(c.offscreen, op)
}
func (c *uiContext) AdjustPosition(x, y float64) (float64, float64) {
d := uiDriver().DeviceScaleFactor()
ox, oy := c.offsets()
s := c.screenScale()
return (x*d - ox) / s, (y*d - oy) / s
}