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
|
|
|
|
|
|
|
|
isScreenClearedEveryFrame int32
|
|
|
|
graphicsLibrary int32
|
2023-10-27 04:01:01 +02:00
|
|
|
running int32
|
|
|
|
terminated int32
|
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) {
|
2023-10-15 08:50:39 +02:00
|
|
|
u := &UserInterface{
|
|
|
|
isScreenClearedEveryFrame: 1,
|
|
|
|
graphicsLibrary: 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 {
|
2023-10-24 16:15:29 +02:00
|
|
|
return mipmap.ReadPixels(u.graphicsDriver, pixels, region)
|
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-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 {
|
|
|
|
return atomic.LoadInt32(&u.isScreenClearedEveryFrame) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UserInterface) SetScreenClearedEveryFrame(cleared bool) {
|
|
|
|
v := int32(0)
|
|
|
|
if cleared {
|
|
|
|
v = 1
|
|
|
|
}
|
|
|
|
atomic.StoreInt32(&u.isScreenClearedEveryFrame, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UserInterface) setGraphicsLibrary(library GraphicsLibrary) {
|
|
|
|
atomic.StoreInt32(&u.graphicsLibrary, int32(library))
|
|
|
|
}
|
|
|
|
|
2023-10-15 10:21:56 +02:00
|
|
|
func (u *UserInterface) GraphicsLibrary() GraphicsLibrary {
|
2023-10-15 08:50:39 +02:00
|
|
|
return GraphicsLibrary(atomic.LoadInt32(&u.graphicsLibrary))
|
|
|
|
}
|
2023-10-27 04:01:01 +02:00
|
|
|
|
|
|
|
func (u *UserInterface) isRunning() bool {
|
|
|
|
return atomic.LoadInt32(&u.running) != 0 && !u.isTerminated()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UserInterface) setRunning(running bool) {
|
|
|
|
if running {
|
|
|
|
atomic.StoreInt32(&u.running, 1)
|
|
|
|
} else {
|
|
|
|
atomic.StoreInt32(&u.running, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UserInterface) isTerminated() bool {
|
|
|
|
return atomic.LoadInt32(&u.terminated) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UserInterface) setTerminated() {
|
|
|
|
atomic.StoreInt32(&u.terminated, 1)
|
|
|
|
}
|