mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
parent
c021d6be6a
commit
9b2f864fc8
@ -18,6 +18,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
@ -25,6 +26,8 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"math"
|
"math"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
@ -33,7 +36,12 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten/inpututil"
|
"github.com/hajimehoshi/ebiten/inpututil"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
flagWindowPosition = flag.String("windowposition", "", "window position (e.g., 100,200)")
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
flag.Parse()
|
||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,22 +85,38 @@ func update(screen *ebiten.Image) error {
|
|||||||
vsyncEnabled := ebiten.IsVsyncEnabled()
|
vsyncEnabled := ebiten.IsVsyncEnabled()
|
||||||
tps := ebiten.MaxTPS()
|
tps := ebiten.MaxTPS()
|
||||||
decorated := ebiten.IsWindowDecorated()
|
decorated := ebiten.IsWindowDecorated()
|
||||||
|
positionX, positionY := ebiten.WindowPosition()
|
||||||
|
|
||||||
if inpututil.IsKeyJustPressed(ebiten.KeyUp) {
|
if ebiten.IsKeyPressed(ebiten.KeyShift) {
|
||||||
screenHeight += d
|
if inpututil.IsKeyJustPressed(ebiten.KeyUp) {
|
||||||
}
|
screenHeight += d
|
||||||
if inpututil.IsKeyJustPressed(ebiten.KeyDown) {
|
|
||||||
if 16 < screenHeight && d < screenHeight {
|
|
||||||
screenHeight -= d
|
|
||||||
}
|
}
|
||||||
}
|
if inpututil.IsKeyJustPressed(ebiten.KeyDown) {
|
||||||
if inpututil.IsKeyJustPressed(ebiten.KeyLeft) {
|
if 16 < screenHeight && d < screenHeight {
|
||||||
if 16 < screenWidth && d < screenWidth {
|
screenHeight -= d
|
||||||
screenWidth -= d
|
}
|
||||||
|
}
|
||||||
|
if inpututil.IsKeyJustPressed(ebiten.KeyLeft) {
|
||||||
|
if 16 < screenWidth && d < screenWidth {
|
||||||
|
screenWidth -= d
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if inpututil.IsKeyJustPressed(ebiten.KeyRight) {
|
||||||
|
screenWidth += d
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if inpututil.IsKeyJustPressed(ebiten.KeyUp) {
|
||||||
|
positionY -= 4
|
||||||
|
}
|
||||||
|
if inpututil.IsKeyJustPressed(ebiten.KeyDown) {
|
||||||
|
positionY += 4
|
||||||
|
}
|
||||||
|
if inpututil.IsKeyJustPressed(ebiten.KeyLeft) {
|
||||||
|
positionX -= 4
|
||||||
|
}
|
||||||
|
if inpututil.IsKeyJustPressed(ebiten.KeyRight) {
|
||||||
|
positionX += 4
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if inpututil.IsKeyJustPressed(ebiten.KeyRight) {
|
|
||||||
screenWidth += d
|
|
||||||
}
|
}
|
||||||
if inpututil.IsKeyJustPressed(ebiten.KeyS) {
|
if inpututil.IsKeyJustPressed(ebiten.KeyS) {
|
||||||
switch screenScale {
|
switch screenScale {
|
||||||
@ -146,6 +170,7 @@ func update(screen *ebiten.Image) error {
|
|||||||
ebiten.SetVsyncEnabled(vsyncEnabled)
|
ebiten.SetVsyncEnabled(vsyncEnabled)
|
||||||
ebiten.SetMaxTPS(tps)
|
ebiten.SetMaxTPS(tps)
|
||||||
ebiten.SetWindowDecorated(decorated)
|
ebiten.SetWindowDecorated(decorated)
|
||||||
|
ebiten.SetWindowPosition(positionX, positionY)
|
||||||
|
|
||||||
if inpututil.IsKeyJustPressed(ebiten.KeyI) {
|
if inpututil.IsKeyJustPressed(ebiten.KeyI) {
|
||||||
ebiten.SetWindowIcon([]image.Image{createRandomIconImage()})
|
ebiten.SetWindowIcon([]image.Image{createRandomIconImage()})
|
||||||
@ -173,7 +198,7 @@ func update(screen *ebiten.Image) error {
|
|||||||
if t := ebiten.MaxTPS(); t != ebiten.UncappedTPS {
|
if t := ebiten.MaxTPS(); t != ebiten.UncappedTPS {
|
||||||
tpsStr = fmt.Sprintf("%d", t)
|
tpsStr = fmt.Sprintf("%d", t)
|
||||||
}
|
}
|
||||||
msg := fmt.Sprintf(`Press arrow keys to change the window size
|
msg := fmt.Sprintf(`Press shift + arrow keys to change the window size
|
||||||
Press S key to change the window scale (only for desktops)
|
Press S key to change the window scale (only for desktops)
|
||||||
Press F key to switch the fullscreen state (only for desktops)
|
Press F key to switch the fullscreen state (only for desktops)
|
||||||
Press B key to switch the run-in-background state
|
Press B key to switch the run-in-background state
|
||||||
@ -191,6 +216,25 @@ Device Scale Factor: %0.2f`, wx, wy, cx, cy, ebiten.CurrentTPS(), tpsStr, ebiten
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseWindowPosition() (int, int, bool) {
|
||||||
|
if *flagWindowPosition == "" {
|
||||||
|
return 0, 0, false
|
||||||
|
}
|
||||||
|
tokens := strings.Split(*flagWindowPosition, ",")
|
||||||
|
if len(tokens) != 2 {
|
||||||
|
return 0, 0, false
|
||||||
|
}
|
||||||
|
x, err := strconv.Atoi(tokens[0])
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, false
|
||||||
|
}
|
||||||
|
y, err := strconv.Atoi(tokens[1])
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, false
|
||||||
|
}
|
||||||
|
return x, y, true
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Printf("Device scale factor: %0.2f\n", ebiten.DeviceScaleFactor())
|
fmt.Printf("Device scale factor: %0.2f\n", ebiten.DeviceScaleFactor())
|
||||||
w, h := ebiten.ScreenSizeInFullscreen()
|
w, h := ebiten.ScreenSizeInFullscreen()
|
||||||
@ -213,6 +257,10 @@ func main() {
|
|||||||
|
|
||||||
ebiten.SetWindowIcon([]image.Image{createRandomIconImage()})
|
ebiten.SetWindowIcon([]image.Image{createRandomIconImage()})
|
||||||
|
|
||||||
|
if x, y, ok := parseWindowPosition(); ok {
|
||||||
|
ebiten.SetWindowPosition(x, y)
|
||||||
|
}
|
||||||
|
|
||||||
if err := ebiten.Run(update, initScreenWidth, initScreenHeight, initScreenScale, "Window Size (Ebiten Demo)"); err != nil {
|
if err := ebiten.Run(update, initScreenWidth, initScreenHeight, initScreenScale, "Window Size (Ebiten Demo)"); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -57,6 +57,7 @@ type UI interface {
|
|||||||
SetWindowIcon(iconImages []image.Image)
|
SetWindowIcon(iconImages []image.Image)
|
||||||
SetWindowResizable(resizable bool)
|
SetWindowResizable(resizable bool)
|
||||||
SetWindowTitle(title string)
|
SetWindowTitle(title string)
|
||||||
|
SetWindowPosition(x, y int)
|
||||||
|
|
||||||
Input() Input
|
Input() Input
|
||||||
}
|
}
|
||||||
|
@ -36,15 +36,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type UserInterface struct {
|
type UserInterface struct {
|
||||||
title string
|
title string
|
||||||
window *glfw.Window
|
window *glfw.Window
|
||||||
width int
|
width int
|
||||||
windowWidth int
|
windowWidth int
|
||||||
height int
|
height int
|
||||||
initMonitor *glfw.Monitor
|
|
||||||
initFullscreenWidth int
|
|
||||||
initFullscreenHeight int
|
|
||||||
|
|
||||||
scale float64
|
scale float64
|
||||||
fullscreenScale float64
|
fullscreenScale float64
|
||||||
|
|
||||||
@ -57,11 +53,16 @@ type UserInterface struct {
|
|||||||
|
|
||||||
lastActualScale float64
|
lastActualScale float64
|
||||||
|
|
||||||
initFullscreen bool
|
initMonitor *glfw.Monitor
|
||||||
initCursorVisible bool
|
initFullscreenWidth int
|
||||||
initWindowDecorated bool
|
initFullscreenHeight int
|
||||||
initWindowResizable bool
|
initFullscreen bool
|
||||||
initIconImages []image.Image
|
initCursorVisible bool
|
||||||
|
initWindowDecorated bool
|
||||||
|
initWindowResizable bool
|
||||||
|
initWindowPositionX *int
|
||||||
|
initWindowPositionY *int
|
||||||
|
initIconImages []image.Image
|
||||||
|
|
||||||
reqWidth int
|
reqWidth int
|
||||||
reqHeight int
|
reqHeight int
|
||||||
@ -667,8 +668,14 @@ func (u *UserInterface) run(width, height int, scale float64, title string, cont
|
|||||||
u.window.SetTitle(title)
|
u.window.SetTitle(title)
|
||||||
u.window.Show()
|
u.window.Show()
|
||||||
|
|
||||||
x := mx + (v.Width-w)/2
|
x, y := 0, 0
|
||||||
y := my + (v.Height-h)/3
|
if u.initWindowPositionX != nil && u.initWindowPositionY != nil {
|
||||||
|
x = *u.initWindowPositionX
|
||||||
|
y = *u.initWindowPositionY
|
||||||
|
} else {
|
||||||
|
x = mx + (v.Width-w)/2
|
||||||
|
y = my + (v.Height-h)/3
|
||||||
|
}
|
||||||
// Adjusting the position is needed only when the monitor is primary. (#829)
|
// Adjusting the position is needed only when the monitor is primary. (#829)
|
||||||
if mx == 0 && my == 0 {
|
if mx == 0 && my == 0 {
|
||||||
x, y = adjustWindowPosition(x, y)
|
x, y = adjustWindowPosition(x, y)
|
||||||
@ -997,6 +1004,24 @@ func (u *UserInterface) currentMonitor() *glfw.Monitor {
|
|||||||
return u.currentMonitorFromPosition()
|
return u.currentMonitorFromPosition()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *UserInterface) SetWindowPosition(x, y int) {
|
||||||
|
if !u.isRunning() {
|
||||||
|
if u.initWindowPositionX == nil {
|
||||||
|
u.initWindowPositionX = new(int)
|
||||||
|
}
|
||||||
|
if u.initWindowPositionY == nil {
|
||||||
|
u.initWindowPositionY = new(int)
|
||||||
|
}
|
||||||
|
*u.initWindowPositionX = x
|
||||||
|
*u.initWindowPositionY = y
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_ = u.t.Call(func() error {
|
||||||
|
u.window.SetPos(x, y)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func (u *UserInterface) WindowPosition() (int, int) {
|
func (u *UserInterface) WindowPosition() (int, int) {
|
||||||
if !u.isRunning() {
|
if !u.isRunning() {
|
||||||
panic("ui: Run is not called yet")
|
panic("ui: Run is not called yet")
|
||||||
|
@ -479,6 +479,10 @@ func (u *UserInterface) updateScreenSize() {
|
|||||||
u.sizeChanged = true
|
u.sizeChanged = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *UserInterface) SetWindowPosition(x, y int) {
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
|
||||||
func (u *UserInterface) WindowPosition() (int, int) {
|
func (u *UserInterface) WindowPosition() (int, int) {
|
||||||
return 0, 0
|
return 0, 0
|
||||||
}
|
}
|
||||||
|
@ -443,6 +443,10 @@ func (u *UserInterface) DeviceScaleFactor() float64 {
|
|||||||
return deviceScale()
|
return deviceScale()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *UserInterface) SetWindowPosition(x, y int) {
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
|
||||||
func (u *UserInterface) WindowPosition() (int, int) {
|
func (u *UserInterface) WindowPosition() (int, int) {
|
||||||
return 0, 0
|
return 0, 0
|
||||||
}
|
}
|
||||||
|
11
window.go
11
window.go
@ -100,6 +100,15 @@ func SetWindowIcon(iconImages []image.Image) {
|
|||||||
// WindowPosition panics before Run is called.
|
// WindowPosition panics before Run is called.
|
||||||
//
|
//
|
||||||
// WindowPosition returns (0, 0) on browsers and mobiles.
|
// WindowPosition returns (0, 0) on browsers and mobiles.
|
||||||
func WindowPosition() (int, int) {
|
func WindowPosition() (x, y int) {
|
||||||
return uiDriver().WindowPosition()
|
return uiDriver().WindowPosition()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetWindowPosition sets the window position.
|
||||||
|
//
|
||||||
|
// SetWindowPosition works before and after Run is called.
|
||||||
|
//
|
||||||
|
// SetWindowPosition does nothing on browsers and mobiles.
|
||||||
|
func SetWindowPosition(x, y int) {
|
||||||
|
uiDriver().SetWindowPosition(x, y)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user