2022-02-06 09:43:52 +01:00
|
|
|
// Copyright 2022 The Ebiten Authors
|
2019-12-14 04:30:03 +01:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2022-02-06 09:43:52 +01:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2023-04-27 17:55:10 +02:00
|
|
|
"image"
|
2023-10-15 08:50:39 +02:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2022-03-21 14:47:13 +01:00
|
|
|
|
2024-01-28 14:37:50 +01:00
|
|
|
_ "github.com/ebitengine/hideconsole"
|
|
|
|
|
2022-03-21 14:47:13 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/atlas"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/mipmap"
|
2023-10-29 10:34:18 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/thread"
|
2022-02-06 09:43:52 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// RegularTermination represents a regular termination.
|
|
|
|
// Run can return this error, and if this error is received,
|
|
|
|
// the game loop should be terminated as soon as possible.
|
|
|
|
var RegularTermination = errors.New("regular termination")
|
|
|
|
|
2022-02-13 09:23:52 +01:00
|
|
|
type FPSModeType int
|
2022-02-06 09:43:52 +01:00
|
|
|
|
|
|
|
const (
|
2022-02-13 09:23:52 +01:00
|
|
|
FPSModeVsyncOn FPSModeType = iota
|
2022-02-06 09:43:52 +01:00
|
|
|
FPSModeVsyncOffMaximum
|
|
|
|
FPSModeVsyncOffMinimum
|
|
|
|
)
|
2019-12-14 04:30:03 +01:00
|
|
|
|
|
|
|
type CursorMode int
|
|
|
|
|
|
|
|
const (
|
2021-04-10 20:29:09 +02:00
|
|
|
CursorModeVisible CursorMode = iota
|
2019-12-14 04:30:03 +01:00
|
|
|
CursorModeHidden
|
|
|
|
CursorModeCaptured
|
|
|
|
)
|
2021-04-11 07:21:38 +02:00
|
|
|
|
|
|
|
type CursorShape int
|
|
|
|
|
|
|
|
const (
|
|
|
|
CursorShapeDefault CursorShape = iota
|
|
|
|
CursorShapeText
|
|
|
|
CursorShapeCrosshair
|
|
|
|
CursorShapePointer
|
2021-05-02 07:50:50 +02:00
|
|
|
CursorShapeEWResize
|
|
|
|
CursorShapeNSResize
|
2023-01-15 18:26:41 +01:00
|
|
|
CursorShapeNESWResize
|
|
|
|
CursorShapeNWSEResize
|
|
|
|
CursorShapeMove
|
|
|
|
CursorShapeNotAllowed
|
2021-04-11 07:21:38 +02:00
|
|
|
)
|
2021-12-29 14:08:59 +01:00
|
|
|
|
|
|
|
type WindowResizingMode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
WindowResizingModeDisabled WindowResizingMode = iota
|
|
|
|
WindowResizingModeOnlyFullscreenEnabled
|
|
|
|
WindowResizingModeEnabled
|
|
|
|
)
|
2022-03-21 14:47:13 +01:00
|
|
|
|
2022-03-21 15:00:50 +01:00
|
|
|
type UserInterface struct {
|
2023-10-15 08:50:39 +02:00
|
|
|
err error
|
|
|
|
errM sync.Mutex
|
|
|
|
|
2024-04-29 12:02:23 +02:00
|
|
|
isScreenClearedEveryFrame atomic.Bool
|
2024-04-29 14:50:08 +02:00
|
|
|
graphicsLibrary atomic.Int32
|
2024-04-29 12:02:23 +02:00
|
|
|
running atomic.Bool
|
|
|
|
terminated atomic.Bool
|
2024-08-23 18:04:11 +02:00
|
|
|
tick atomic.Uint64
|
2023-10-15 08:50:39 +02:00
|
|
|
|
2023-10-14 17:06:07 +02:00
|
|
|
whiteImage *Image
|
|
|
|
|
2023-12-17 07:36:12 +01:00
|
|
|
mainThread thread.Thread
|
|
|
|
|
2022-03-21 15:00:50 +01:00
|
|
|
userInterfaceImpl
|
|
|
|
}
|
|
|
|
|
2023-10-14 17:06:07 +02:00
|
|
|
var (
|
|
|
|
theUI *UserInterface
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// newUserInterface() must be called in the main goroutine.
|
|
|
|
u, err := newUserInterface()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
theUI = u
|
|
|
|
}
|
2022-03-21 15:00:50 +01:00
|
|
|
|
|
|
|
func Get() *UserInterface {
|
|
|
|
return theUI
|
|
|
|
}
|
|
|
|
|
2023-10-14 17:06:07 +02:00
|
|
|
// newUserInterface must be called from the main thread.
|
|
|
|
func newUserInterface() (*UserInterface, error) {
|
2024-04-29 14:50:08 +02:00
|
|
|
u := &UserInterface{}
|
2024-04-29 12:02:23 +02:00
|
|
|
u.isScreenClearedEveryFrame.Store(true)
|
2024-04-29 14:50:08 +02:00
|
|
|
u.graphicsLibrary.Store(int32(GraphicsLibraryUnknown))
|
2023-10-14 17:06:07 +02:00
|
|
|
|
|
|
|
u.whiteImage = u.NewImage(3, 3, atlas.ImageTypeRegular)
|
|
|
|
pix := make([]byte, 4*u.whiteImage.width*u.whiteImage.height)
|
|
|
|
for i := range pix {
|
|
|
|
pix[i] = 0xff
|
|
|
|
}
|
|
|
|
// As a white image is used at Fill, use WritePixels instead.
|
|
|
|
u.whiteImage.WritePixels(pix, image.Rect(0, 0, u.whiteImage.width, u.whiteImage.height))
|
|
|
|
|
|
|
|
if err := u.init(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
2023-04-27 17:55:10 +02:00
|
|
|
func (u *UserInterface) readPixels(mipmap *mipmap.Mipmap, pixels []byte, region image.Rectangle) error {
|
2024-05-03 09:35:39 +02:00
|
|
|
if !u.running.Load() {
|
|
|
|
panic("ui: ReadPixels cannot be called before the game starts")
|
|
|
|
}
|
|
|
|
|
2024-03-26 04:59:31 +01:00
|
|
|
ok, err := mipmap.ReadPixels(u.graphicsDriver, pixels, region)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadPixels failed since this was called in between two frames.
|
|
|
|
// Try this again at the next frame.
|
|
|
|
if !ok {
|
|
|
|
// If this function is called from the same sequence as a game's Update and Draw,
|
|
|
|
// this causes a dead lock.
|
|
|
|
// This never happens so far, but if handling inputs after EndFrame is implemented,
|
|
|
|
// this might be possible (#1704).
|
|
|
|
|
|
|
|
var err1 error
|
|
|
|
u.context.runInFrame(func() {
|
|
|
|
ok, err := mipmap.ReadPixels(u.graphicsDriver, pixels, region)
|
|
|
|
if err != nil {
|
|
|
|
err1 = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
// This never reaches since this function must be called in a frame.
|
|
|
|
panic("ui: ReadPixels unexpectedly failed")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return err1
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2022-03-21 14:47:13 +01:00
|
|
|
}
|
|
|
|
|
2022-08-31 06:10:41 +02:00
|
|
|
func (u *UserInterface) dumpScreenshot(mipmap *mipmap.Mipmap, name string, blackbg bool) (string, error) {
|
2022-03-21 16:02:37 +01:00
|
|
|
return mipmap.DumpScreenshot(u.graphicsDriver, name, blackbg)
|
2022-03-21 14:47:13 +01:00
|
|
|
}
|
|
|
|
|
2022-08-31 06:10:41 +02:00
|
|
|
func (u *UserInterface) dumpImages(dir string) (string, error) {
|
2022-03-21 16:02:37 +01:00
|
|
|
return atlas.DumpImages(u.graphicsDriver, dir)
|
2022-03-21 14:47:13 +01:00
|
|
|
}
|
2022-12-09 06:07:04 +01:00
|
|
|
|
|
|
|
type RunOptions struct {
|
2022-12-09 13:23:41 +01:00
|
|
|
GraphicsLibrary GraphicsLibrary
|
|
|
|
InitUnfocused bool
|
|
|
|
ScreenTransparent bool
|
2022-12-12 17:03:19 +01:00
|
|
|
SkipTaskbar bool
|
2023-11-04 16:00:17 +01:00
|
|
|
SingleThread bool
|
2024-08-19 16:43:48 +02:00
|
|
|
DisableHiDPI bool
|
2024-02-11 11:06:04 +01:00
|
|
|
X11ClassName string
|
|
|
|
X11InstanceName string
|
2022-12-09 06:07:04 +01:00
|
|
|
}
|
2023-08-30 14:02:04 +02:00
|
|
|
|
|
|
|
// InitialWindowPosition returns the position for centering the given second width/height pair within the first width/height pair.
|
|
|
|
func InitialWindowPosition(mw, mh, ww, wh int) (x, y int) {
|
|
|
|
return (mw - ww) / 2, (mh - wh) / 3
|
|
|
|
}
|
2023-10-15 08:50:39 +02:00
|
|
|
|
|
|
|
func (u *UserInterface) error() error {
|
|
|
|
u.errM.Lock()
|
|
|
|
defer u.errM.Unlock()
|
|
|
|
return u.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UserInterface) setError(err error) {
|
|
|
|
u.errM.Lock()
|
|
|
|
defer u.errM.Unlock()
|
|
|
|
if u.err == nil {
|
|
|
|
u.err = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UserInterface) IsScreenClearedEveryFrame() bool {
|
2024-04-29 12:02:23 +02:00
|
|
|
return u.isScreenClearedEveryFrame.Load()
|
2023-10-15 08:50:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UserInterface) SetScreenClearedEveryFrame(cleared bool) {
|
2024-04-29 12:02:23 +02:00
|
|
|
u.isScreenClearedEveryFrame.Store(cleared)
|
2023-10-15 08:50:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UserInterface) setGraphicsLibrary(library GraphicsLibrary) {
|
2024-04-29 14:50:08 +02:00
|
|
|
u.graphicsLibrary.Store(int32(library))
|
2023-10-15 08:50:39 +02:00
|
|
|
}
|
|
|
|
|
2023-10-15 10:21:56 +02:00
|
|
|
func (u *UserInterface) GraphicsLibrary() GraphicsLibrary {
|
2024-04-29 14:50:08 +02:00
|
|
|
return GraphicsLibrary(u.graphicsLibrary.Load())
|
2023-10-15 08:50:39 +02:00
|
|
|
}
|
2023-10-27 04:01:01 +02:00
|
|
|
|
|
|
|
func (u *UserInterface) isRunning() bool {
|
2024-04-29 12:02:23 +02:00
|
|
|
return u.running.Load() && !u.isTerminated()
|
2023-10-27 04:01:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UserInterface) setRunning(running bool) {
|
2024-04-29 12:02:23 +02:00
|
|
|
u.running.Store(running)
|
2023-10-27 04:01:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UserInterface) isTerminated() bool {
|
2024-04-29 12:02:23 +02:00
|
|
|
return u.terminated.Load()
|
2023-10-27 04:01:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UserInterface) setTerminated() {
|
2024-04-29 12:02:23 +02:00
|
|
|
u.terminated.Store(true)
|
2023-10-27 04:01:01 +02:00
|
|
|
}
|
2024-08-23 18:04:11 +02:00
|
|
|
|
|
|
|
func (u *UserInterface) Tick() uint64 {
|
|
|
|
return u.tick.Load()
|
|
|
|
}
|