2022-02-13 08:30:33 +01:00
|
|
|
// Copyright 2022 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// 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 ui
|
|
|
|
|
|
|
|
import (
|
2022-04-01 11:16:03 +02:00
|
|
|
"fmt"
|
2022-02-13 10:31:28 +01:00
|
|
|
"math"
|
2022-02-13 11:50:19 +01:00
|
|
|
"sync"
|
2022-02-13 08:30:33 +01:00
|
|
|
"sync/atomic"
|
2022-02-13 09:23:52 +01:00
|
|
|
|
2022-06-07 16:57:56 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/atlas"
|
2022-02-13 10:12:28 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/buffered"
|
2022-02-13 09:23:52 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/clock"
|
2022-02-13 10:12:28 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/debug"
|
2022-04-01 14:17:26 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
2022-02-13 12:08:59 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
2022-02-13 12:02:49 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/hooks"
|
2022-10-02 11:19:26 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/mipmap"
|
2022-02-13 08:30:33 +01:00
|
|
|
)
|
|
|
|
|
2022-10-02 09:05:32 +02:00
|
|
|
const screenShaderSrc = `package main
|
2022-09-06 11:21:33 +02:00
|
|
|
|
|
|
|
var Scale vec2
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
sourceSize := imageSrcTextureSize()
|
|
|
|
// texelSize is one pixel size in texel sizes.
|
|
|
|
texelSize := 1 / sourceSize
|
|
|
|
halfScaledTexelSize := texelSize / 2 / Scale
|
|
|
|
|
|
|
|
// Shift 1/512 [texel] to avoid the tie-breaking issue.
|
|
|
|
// As all the vertex positions are aligned to 1/16 [pixel], this shiting should work in most cases.
|
|
|
|
pos := texCoord
|
|
|
|
p0 := pos - halfScaledTexelSize + (texelSize / 512)
|
|
|
|
p1 := pos + halfScaledTexelSize + (texelSize / 512)
|
|
|
|
|
|
|
|
// Texels must be in the source rect, so it is not necessary to check.
|
|
|
|
c0 := imageSrc0UnsafeAt(p0)
|
|
|
|
c1 := imageSrc0UnsafeAt(vec2(p1.x, p0.y))
|
|
|
|
c2 := imageSrc0UnsafeAt(vec2(p0.x, p1.y))
|
|
|
|
c3 := imageSrc0UnsafeAt(p1)
|
|
|
|
|
|
|
|
// p is the p1 value in one pixel assuming that the pixel's upper-left is (0, 0) and the lower-right is (1, 1).
|
|
|
|
p := fract(p1 * sourceSize)
|
|
|
|
|
|
|
|
// rate indicates how much the 4 colors are mixed. rate is in between [0, 1].
|
|
|
|
//
|
|
|
|
// 0 <= p <= 1/Scale: The rate is in between [0, 1]
|
|
|
|
// 1/Scale < p: Don't care. Adjacent colors (e.g. c0 vs c1 in an X direction) should be the same.
|
|
|
|
rate := clamp(p*Scale, 0, 1)
|
|
|
|
return mix(mix(c0, c1, rate.x), mix(c2, c3, rate.x), rate.y)
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2022-10-02 09:05:32 +02:00
|
|
|
var (
|
2022-10-02 11:19:26 +02:00
|
|
|
screenShader *Shader
|
2022-10-02 13:33:43 +02:00
|
|
|
NearestFilterShader = &Shader{shader: mipmap.NearestFilterShader}
|
|
|
|
LinearFilterShader = &Shader{shader: mipmap.LinearFilterShader}
|
2022-10-02 09:05:32 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
{
|
|
|
|
ir, err := graphics.CompileShader([]byte(screenShaderSrc))
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("ui: compiling the screen shader failed: %v", err))
|
|
|
|
}
|
|
|
|
screenShader = NewShader(ir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-13 12:18:41 +01:00
|
|
|
type Game interface {
|
2022-04-01 11:16:03 +02:00
|
|
|
NewOffscreenImage(width, height int) *Image
|
|
|
|
Layout(outsideWidth, outsideHeight int) (int, int)
|
2022-02-13 12:18:41 +01:00
|
|
|
Update() error
|
2022-04-01 11:16:03 +02:00
|
|
|
Draw()
|
2022-02-13 08:30:33 +01:00
|
|
|
}
|
|
|
|
|
2022-04-01 10:59:44 +02:00
|
|
|
type context struct {
|
2022-02-13 12:18:41 +01:00
|
|
|
game Game
|
2022-02-13 08:30:33 +01:00
|
|
|
|
2022-02-13 12:02:49 +01:00
|
|
|
updateCalled bool
|
|
|
|
|
2022-04-01 11:16:03 +02:00
|
|
|
offscreen *Image
|
|
|
|
screen *Image
|
|
|
|
|
2022-02-13 11:50:19 +01:00
|
|
|
// The following members must be protected by the mutex m.
|
2022-02-13 12:18:41 +01:00
|
|
|
outsideWidth float64
|
|
|
|
outsideHeight float64
|
2022-02-13 11:50:19 +01:00
|
|
|
|
|
|
|
m sync.Mutex
|
2022-02-13 09:23:52 +01:00
|
|
|
}
|
|
|
|
|
2022-04-01 10:59:44 +02:00
|
|
|
func newContext(game Game) *context {
|
|
|
|
return &context{
|
2022-02-13 12:18:41 +01:00
|
|
|
game: game,
|
2022-02-13 09:23:52 +01:00
|
|
|
}
|
2022-02-13 08:30:33 +01:00
|
|
|
}
|
|
|
|
|
2022-09-25 17:46:03 +02:00
|
|
|
func (c *context) updateFrame(graphicsDriver graphicsdriver.Graphics, outsideWidth, outsideHeight float64, deviceScaleFactor float64, ui *userInterfaceImpl) error {
|
2022-02-13 09:23:52 +01:00
|
|
|
// TODO: If updateCount is 0 and vsync is disabled, swapping buffers can be skipped.
|
2022-09-25 17:46:03 +02:00
|
|
|
return c.updateFrameImpl(graphicsDriver, clock.UpdateFrame(), outsideWidth, outsideHeight, deviceScaleFactor, ui)
|
2022-02-13 08:30:33 +01:00
|
|
|
}
|
|
|
|
|
2022-09-25 17:46:03 +02:00
|
|
|
func (c *context) forceUpdateFrame(graphicsDriver graphicsdriver.Graphics, outsideWidth, outsideHeight float64, deviceScaleFactor float64, ui *userInterfaceImpl) error {
|
2022-02-11 12:38:45 +01:00
|
|
|
n := 1
|
|
|
|
if graphicsDriver.IsDirectX() {
|
|
|
|
// On DirectX, both framebuffers in the swap chain should be updated.
|
|
|
|
// Or, the rendering result becomes unexpected when the window is resized.
|
|
|
|
n = 2
|
|
|
|
}
|
|
|
|
for i := 0; i < n; i++ {
|
2022-09-25 17:46:03 +02:00
|
|
|
if err := c.updateFrameImpl(graphicsDriver, 1, outsideWidth, outsideHeight, deviceScaleFactor, ui); err != nil {
|
2022-02-11 12:38:45 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2022-02-13 10:12:28 +01:00
|
|
|
}
|
|
|
|
|
2022-09-25 17:46:03 +02:00
|
|
|
func (c *context) updateFrameImpl(graphicsDriver graphicsdriver.Graphics, updateCount int, outsideWidth, outsideHeight float64, deviceScaleFactor float64, ui *userInterfaceImpl) (err error) {
|
2022-03-04 17:44:59 +01:00
|
|
|
if err := theGlobalState.error(); err != nil {
|
2022-02-13 08:30:33 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-02-13 09:23:52 +01:00
|
|
|
|
2022-09-25 17:46:03 +02:00
|
|
|
ui.beginFrame()
|
|
|
|
defer ui.endFrame()
|
2022-09-25 16:29:58 +02:00
|
|
|
|
2022-02-13 18:48:28 +01:00
|
|
|
// The given outside size can be 0 e.g. just after restoring from the fullscreen mode on Windows (#1589)
|
|
|
|
// Just ignore such cases. Otherwise, creating a zero-sized framebuffer causes a panic.
|
|
|
|
if outsideWidth == 0 || outsideHeight == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-13 10:12:28 +01:00
|
|
|
debug.Logf("----\n")
|
|
|
|
|
2022-03-21 08:48:33 +01:00
|
|
|
if err := buffered.BeginFrame(graphicsDriver); err != nil {
|
2022-02-13 10:12:28 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-04-25 18:12:52 +02:00
|
|
|
defer func() {
|
|
|
|
// All the vertices data are consumed at the end of the frame, and the data backend can be
|
|
|
|
// available after that. Until then, lock the vertices backend.
|
|
|
|
err1 := graphics.LockAndResetVertices(func() error {
|
|
|
|
if err := buffered.EndFrame(graphicsDriver); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// ForceUpdate can be invoked even if the context is not initialized yet (#1591).
|
|
|
|
if w, h := c.layoutGame(outsideWidth, outsideHeight, deviceScaleFactor); w == 0 || h == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2022-02-13 10:31:28 +01:00
|
|
|
|
2022-02-13 12:02:49 +01:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
debug.Logf("Update count per frame: %d\n", updateCount)
|
|
|
|
|
|
|
|
// Update the game.
|
|
|
|
for i := 0; i < updateCount; i++ {
|
|
|
|
if err := hooks.RunBeforeUpdateHooks(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-13 12:18:41 +01:00
|
|
|
if err := c.game.Update(); err != nil {
|
2022-02-13 12:02:49 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-03-04 17:44:59 +01:00
|
|
|
// Catch the error that happened at (*Image).At.
|
|
|
|
if err := theGlobalState.error(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-25 17:46:03 +02:00
|
|
|
ui.resetForTick()
|
2022-02-13 12:02:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the game.
|
2022-04-01 11:16:03 +02:00
|
|
|
c.drawGame(graphicsDriver)
|
2022-02-13 10:12:28 +01:00
|
|
|
|
|
|
|
// All the vertices data are consumed at the end of the frame, and the data backend can be
|
|
|
|
// available after that. Until then, lock the vertices backend.
|
2022-04-25 18:12:52 +02:00
|
|
|
return nil
|
2022-02-13 08:30:33 +01:00
|
|
|
}
|
|
|
|
|
2022-04-01 11:16:03 +02:00
|
|
|
func (c *context) drawGame(graphicsDriver graphicsdriver.Graphics) {
|
2022-06-07 16:16:40 +02:00
|
|
|
if c.offscreen.volatile != theGlobalState.isScreenClearedEveryFrame() {
|
|
|
|
w, h := c.offscreen.width, c.offscreen.height
|
|
|
|
c.offscreen.MarkDisposed()
|
|
|
|
c.offscreen = c.game.NewOffscreenImage(w, h)
|
|
|
|
}
|
2022-04-01 11:16:03 +02:00
|
|
|
|
|
|
|
// Even though updateCount == 0, the offscreen is cleared and Draw is called.
|
|
|
|
// Draw should not update the game state and then the screen should not be updated without Update, but
|
|
|
|
// users might want to process something at Draw with the time intervals of FPS.
|
|
|
|
if theGlobalState.isScreenClearedEveryFrame() {
|
|
|
|
c.offscreen.clear()
|
|
|
|
}
|
|
|
|
c.game.Draw()
|
|
|
|
|
|
|
|
if graphicsDriver.NeedsClearingScreen() {
|
|
|
|
// This clear is needed for fullscreen mode or some mobile platforms (#622).
|
|
|
|
c.screen.clear()
|
|
|
|
}
|
|
|
|
|
|
|
|
ga := 1.0
|
|
|
|
gd := 1.0
|
|
|
|
gtx := 0.0
|
|
|
|
gty := 0.0
|
|
|
|
|
|
|
|
screenScale, offsetX, offsetY := c.screenScaleAndOffsets()
|
|
|
|
s := screenScale
|
|
|
|
switch y := graphicsDriver.FramebufferYDirection(); y {
|
|
|
|
case graphicsdriver.Upward:
|
|
|
|
ga *= s
|
|
|
|
gd *= -s
|
|
|
|
gty += float64(c.offscreen.height) * s
|
|
|
|
case graphicsdriver.Downward:
|
|
|
|
ga *= s
|
|
|
|
gd *= s
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("ui: invalid y-direction: %d", y))
|
|
|
|
}
|
|
|
|
|
|
|
|
gtx += offsetX
|
|
|
|
gty += offsetY
|
|
|
|
|
2022-10-02 08:51:20 +02:00
|
|
|
var shader *Shader
|
2022-04-01 11:16:03 +02:00
|
|
|
switch {
|
|
|
|
case !theGlobalState.isScreenFilterEnabled():
|
2022-10-02 13:33:43 +02:00
|
|
|
shader = NearestFilterShader
|
2022-04-01 11:16:03 +02:00
|
|
|
case math.Floor(s) == s:
|
2022-10-02 13:33:43 +02:00
|
|
|
shader = NearestFilterShader
|
2022-04-01 11:16:03 +02:00
|
|
|
case s > 1:
|
2022-10-02 09:05:32 +02:00
|
|
|
shader = screenShader
|
2022-04-01 11:16:03 +02:00
|
|
|
default:
|
2022-09-06 11:21:33 +02:00
|
|
|
// screenShader works with >=1 scale, but does not well with <1 scale.
|
2022-04-01 11:16:03 +02:00
|
|
|
// Use regular FilterLinear instead so far (#669).
|
2022-10-02 13:33:43 +02:00
|
|
|
shader = LinearFilterShader
|
2022-04-01 11:16:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dstRegion := graphicsdriver.Region{
|
|
|
|
X: 0,
|
|
|
|
Y: 0,
|
|
|
|
Width: float32(c.screen.width),
|
|
|
|
Height: float32(c.screen.height),
|
|
|
|
}
|
|
|
|
|
|
|
|
vs := graphics.QuadVertices(
|
|
|
|
0, 0, float32(c.offscreen.width), float32(c.offscreen.height),
|
|
|
|
float32(ga), 0, 0, float32(gd), float32(gtx), float32(gty),
|
|
|
|
1, 1, 1, 1)
|
|
|
|
is := graphics.QuadIndices()
|
|
|
|
|
2022-07-12 18:46:02 +02:00
|
|
|
srcs := [graphics.ShaderImageCount]*Image{c.offscreen}
|
2022-09-06 11:21:33 +02:00
|
|
|
|
2022-10-02 08:51:20 +02:00
|
|
|
dstWidth, dstHeight := c.screen.width, c.screen.height
|
|
|
|
srcWidth, srcHeight := c.offscreen.width, c.offscreen.height
|
2022-10-02 09:02:44 +02:00
|
|
|
var uniforms [][]float32
|
2022-10-02 09:05:32 +02:00
|
|
|
if shader == screenShader {
|
2022-10-02 09:02:44 +02:00
|
|
|
uniforms = shader.ConvertUniforms(map[string]interface{}{
|
|
|
|
"Scale": []float32{
|
|
|
|
float32(dstWidth) / float32(srcWidth),
|
|
|
|
float32(dstHeight) / float32(srcHeight),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2022-10-02 16:09:47 +02:00
|
|
|
c.screen.DrawTriangles(srcs, vs, is, graphicsdriver.CompositeModeCopy, dstRegion, graphicsdriver.Region{}, [graphics.ShaderImageCount - 1][2]float32{}, shader, uniforms, false, true)
|
2022-04-01 11:16:03 +02:00
|
|
|
}
|
|
|
|
|
2022-04-01 10:59:44 +02:00
|
|
|
func (c *context) layoutGame(outsideWidth, outsideHeight float64, deviceScaleFactor float64) (int, int) {
|
2022-02-13 11:50:19 +01:00
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
|
|
|
|
2022-02-13 19:39:02 +01:00
|
|
|
c.outsideWidth = outsideWidth
|
|
|
|
c.outsideHeight = outsideHeight
|
2022-04-01 11:16:03 +02:00
|
|
|
|
2022-09-20 15:50:34 +02:00
|
|
|
// Adjust the outside size to integer values.
|
|
|
|
// Even if the original value is less than 1, the value must be a positive integer (#2340).
|
|
|
|
iow, ioh := int(outsideWidth), int(outsideHeight)
|
|
|
|
if iow == 0 {
|
|
|
|
iow = 1
|
|
|
|
}
|
|
|
|
if ioh == 0 {
|
|
|
|
ioh = 1
|
|
|
|
}
|
|
|
|
ow, oh := c.game.Layout(iow, ioh)
|
2022-04-01 11:16:03 +02:00
|
|
|
if ow <= 0 || oh <= 0 {
|
|
|
|
panic("ui: Layout must return positive numbers")
|
|
|
|
}
|
|
|
|
|
|
|
|
sw, sh := int(outsideWidth*deviceScaleFactor), int(outsideHeight*deviceScaleFactor)
|
|
|
|
if c.screen != nil {
|
|
|
|
if c.screen.width != sw || c.screen.height != sh {
|
|
|
|
c.screen.MarkDisposed()
|
|
|
|
c.screen = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.screen == nil {
|
2022-06-07 16:57:56 +02:00
|
|
|
c.screen = NewImage(sw, sh, atlas.ImageTypeScreen)
|
2022-04-01 11:16:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.offscreen != nil {
|
|
|
|
if c.offscreen.width != ow || c.offscreen.height != oh {
|
|
|
|
c.offscreen.MarkDisposed()
|
|
|
|
c.offscreen = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.offscreen == nil {
|
|
|
|
c.offscreen = c.game.NewOffscreenImage(ow, oh)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ow, oh
|
2022-02-13 11:50:19 +01:00
|
|
|
}
|
|
|
|
|
2022-04-01 10:59:44 +02:00
|
|
|
func (c *context) adjustPosition(x, y float64, deviceScaleFactor float64) (float64, float64) {
|
2022-04-01 11:16:03 +02:00
|
|
|
s, ox, oy := c.screenScaleAndOffsets()
|
2022-02-13 12:18:41 +01:00
|
|
|
// The scale 0 indicates that the screen is not initialized yet.
|
2022-02-13 10:31:28 +01:00
|
|
|
// As any cursor values don't make sense, just return NaN.
|
|
|
|
if s == 0 {
|
|
|
|
return math.NaN(), math.NaN()
|
|
|
|
}
|
|
|
|
return (x*deviceScaleFactor - ox) / s, (y*deviceScaleFactor - oy) / s
|
|
|
|
}
|
|
|
|
|
2022-04-01 11:16:03 +02:00
|
|
|
func (c *context) screenScaleAndOffsets() (float64, float64, float64) {
|
2022-02-13 11:50:19 +01:00
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
2022-02-13 10:31:28 +01:00
|
|
|
|
2022-04-01 11:16:03 +02:00
|
|
|
if c.screen == nil {
|
2022-02-13 11:50:19 +01:00
|
|
|
return 0, 0, 0
|
2022-02-13 10:31:28 +01:00
|
|
|
}
|
2022-02-13 11:50:19 +01:00
|
|
|
|
2022-04-01 11:16:03 +02:00
|
|
|
scaleX := float64(c.screen.width) / float64(c.offscreen.width)
|
|
|
|
scaleY := float64(c.screen.height) / float64(c.offscreen.height)
|
2022-02-13 11:50:19 +01:00
|
|
|
scale := math.Min(scaleX, scaleY)
|
2022-04-01 11:16:03 +02:00
|
|
|
width := float64(c.offscreen.width) * scale
|
|
|
|
height := float64(c.offscreen.height) * scale
|
|
|
|
x := (float64(c.screen.width) - width) / 2
|
|
|
|
y := (float64(c.screen.height) - height) / 2
|
2022-02-13 11:50:19 +01:00
|
|
|
return scale, x, y
|
2022-02-13 08:30:33 +01:00
|
|
|
}
|
|
|
|
|
2022-02-13 09:23:52 +01:00
|
|
|
var theGlobalState = globalState{
|
2022-02-13 12:30:32 +01:00
|
|
|
isScreenClearedEveryFrame_: 1,
|
2022-02-23 19:46:27 +01:00
|
|
|
screenFilterEnabled_: 1,
|
2022-02-13 09:23:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// globalState represents a global state in this package.
|
|
|
|
// This is available even before the game loop starts.
|
|
|
|
type globalState struct {
|
2022-03-07 16:08:53 +01:00
|
|
|
err_ error
|
|
|
|
errM sync.Mutex
|
|
|
|
|
2022-02-13 12:30:32 +01:00
|
|
|
fpsMode_ int32
|
|
|
|
isScreenClearedEveryFrame_ int32
|
2022-02-23 19:46:27 +01:00
|
|
|
screenFilterEnabled_ int32
|
2022-07-30 19:56:16 +02:00
|
|
|
graphicsLibrary_ int32
|
2022-02-13 09:23:52 +01:00
|
|
|
}
|
|
|
|
|
2022-03-04 17:44:59 +01:00
|
|
|
func (g *globalState) error() error {
|
2022-03-07 16:08:53 +01:00
|
|
|
g.errM.Lock()
|
|
|
|
defer g.errM.Unlock()
|
|
|
|
return g.err_
|
2022-02-13 09:23:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *globalState) setError(err error) {
|
2022-03-07 16:08:53 +01:00
|
|
|
g.errM.Lock()
|
|
|
|
defer g.errM.Unlock()
|
|
|
|
if g.err_ == nil {
|
|
|
|
g.err_ = err
|
|
|
|
}
|
2022-02-13 09:23:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *globalState) fpsMode() FPSModeType {
|
2022-02-13 12:30:32 +01:00
|
|
|
return FPSModeType(atomic.LoadInt32(&g.fpsMode_))
|
2022-02-13 09:23:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *globalState) setFPSMode(fpsMode FPSModeType) {
|
2022-02-13 12:30:32 +01:00
|
|
|
atomic.StoreInt32(&g.fpsMode_, int32(fpsMode))
|
2022-02-13 09:23:52 +01:00
|
|
|
}
|
|
|
|
|
2022-02-13 12:30:32 +01:00
|
|
|
func (g *globalState) isScreenClearedEveryFrame() bool {
|
|
|
|
return atomic.LoadInt32(&g.isScreenClearedEveryFrame_) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *globalState) setScreenClearedEveryFrame(cleared bool) {
|
|
|
|
v := int32(0)
|
|
|
|
if cleared {
|
|
|
|
v = 1
|
|
|
|
}
|
|
|
|
atomic.StoreInt32(&g.isScreenClearedEveryFrame_, v)
|
2022-02-13 08:30:33 +01:00
|
|
|
}
|
|
|
|
|
2022-02-23 19:46:27 +01:00
|
|
|
func (g *globalState) isScreenFilterEnabled() bool {
|
2022-10-02 16:20:16 +02:00
|
|
|
return atomic.LoadInt32(&g.screenFilterEnabled_) != 0
|
2022-02-23 19:46:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *globalState) setScreenFilterEnabled(enabled bool) {
|
|
|
|
v := int32(0)
|
|
|
|
if enabled {
|
|
|
|
v = 1
|
|
|
|
}
|
|
|
|
atomic.StoreInt32(&g.screenFilterEnabled_, v)
|
|
|
|
}
|
|
|
|
|
2022-07-30 19:56:16 +02:00
|
|
|
func (g *globalState) setGraphicsLibrary(library GraphicsLibrary) {
|
|
|
|
atomic.StoreInt32(&g.graphicsLibrary_, int32(library))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *globalState) graphicsLibrary() GraphicsLibrary {
|
|
|
|
return GraphicsLibrary(atomic.LoadInt32(&g.graphicsLibrary_))
|
|
|
|
}
|
|
|
|
|
2022-02-13 09:23:52 +01:00
|
|
|
func FPSMode() FPSModeType {
|
|
|
|
return theGlobalState.fpsMode()
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetFPSMode(fpsMode FPSModeType) {
|
|
|
|
theGlobalState.setFPSMode(fpsMode)
|
2022-04-01 11:16:03 +02:00
|
|
|
theUI.SetFPSMode(fpsMode)
|
2022-02-13 09:23:52 +01:00
|
|
|
}
|
|
|
|
|
2022-02-13 12:30:32 +01:00
|
|
|
func IsScreenClearedEveryFrame() bool {
|
|
|
|
return theGlobalState.isScreenClearedEveryFrame()
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetScreenClearedEveryFrame(cleared bool) {
|
|
|
|
theGlobalState.setScreenClearedEveryFrame(cleared)
|
|
|
|
}
|
2022-02-23 19:46:27 +01:00
|
|
|
|
|
|
|
func IsScreenFilterEnabled() bool {
|
|
|
|
return theGlobalState.isScreenFilterEnabled()
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetScreenFilterEnabled(enabled bool) {
|
|
|
|
theGlobalState.setScreenFilterEnabled(enabled)
|
|
|
|
}
|
2022-07-30 19:56:16 +02:00
|
|
|
|
|
|
|
func GetGraphicsLibrary() GraphicsLibrary {
|
|
|
|
return theGlobalState.graphicsLibrary()
|
|
|
|
}
|