uidriver/glfw: Set a default window size

Now RunGame can be called without SetWindowSize.
This commit is contained in:
Hajime Hoshi 2019-12-22 12:46:57 +09:00
parent 0e63241bcb
commit c9863284cf
3 changed files with 28 additions and 24 deletions

View File

@ -82,19 +82,19 @@ type UserInterface struct {
const (
maxInt = int(^uint(0) >> 1)
minInt = -maxInt - 1
invalidVal = minInt
invalidPos = minInt
)
var (
theUI = &UserInterface{
origPosX: invalidVal,
origPosY: invalidVal,
origPosX: invalidPos,
origPosY: invalidPos,
initCursorMode: driver.CursorModeVisible,
initWindowDecorated: true,
initWindowPositionXInDP: invalidVal,
initWindowPositionYInDP: invalidVal,
initWindowWidthInDP: invalidVal,
initWindowHeightInDP: invalidVal,
initWindowPositionXInDP: invalidPos,
initWindowPositionYInDP: invalidPos,
initWindowWidthInDP: 640,
initWindowHeightInDP: 480,
vsync: true,
}
)
@ -306,10 +306,10 @@ func (u *UserInterface) setInitIconImages(iconImages []image.Image) {
func (u *UserInterface) getInitWindowPosition() (int, int) {
u.m.RLock()
defer u.m.RUnlock()
if u.initWindowPositionXInDP != invalidVal && u.initWindowPositionYInDP != invalidVal {
if u.initWindowPositionXInDP != invalidPos && u.initWindowPositionYInDP != invalidPos {
return u.initWindowPositionXInDP, u.initWindowPositionYInDP
}
return invalidVal, invalidVal
return invalidPos, invalidPos
}
func (u *UserInterface) setInitWindowPosition(x, y int) {
@ -712,11 +712,10 @@ func (u *UserInterface) run(context driver.UIContext) error {
}
u.SetWindowPosition(u.getInitWindowPosition())
if w, h := u.getInitWindowSize(); w != invalidVal && h != invalidVal {
w = int(u.toDeviceDependentPixel(float64(w)))
h = int(u.toDeviceDependentPixel(float64(h)))
u.setWindowSize(w, h, u.isFullscreen(), u.vsync)
}
ww, wh := u.getInitWindowSize()
ww = int(u.toDeviceDependentPixel(float64(ww)))
wh = int(u.toDeviceDependentPixel(float64(wh)))
u.setWindowSize(ww, wh, u.isFullscreen(), u.vsync)
_ = u.t.Call(func() error {
u.title = u.getInitTitle()
@ -913,7 +912,7 @@ func (u *UserInterface) setWindowSize(width, height int, fullscreen bool, vsync
u.swapBuffers()
if fullscreen {
if u.origPosX == invalidVal || u.origPosY == invalidVal {
if u.origPosX == invalidPos || u.origPosY == invalidPos {
u.origPosX, u.origPosY = u.window.GetPos()
}
m := u.currentMonitor()
@ -962,7 +961,7 @@ func (u *UserInterface) setWindowSize(width, height int, fullscreen bool, vsync
width = minWindowWidth
}
if u.origPosX != invalidVal && u.origPosY != invalidVal {
if u.origPosX != invalidPos && u.origPosY != invalidPos {
x := u.origPosX
y := u.origPosY
u.window.SetPos(x, y)
@ -972,8 +971,8 @@ func (u *UserInterface) setWindowSize(width, height int, fullscreen bool, vsync
u.window.SetPos(x+1, y)
u.window.SetPos(x, y)
}
u.origPosX = invalidVal
u.origPosY = invalidVal
u.origPosX = invalidPos
u.origPosY = invalidPos
}
// Set the window size after the position. The order matters.

12
run.go
View File

@ -182,7 +182,11 @@ func Run(f func(*Image) error, width, height int, scale float64, title string) e
// The window is resizable if you use RunGame, while you cannot if you use Run.
// RunGame is more sophisticated way than Run and hides the notion of 'scale'.
//
// On desktops, SetWindowSize must be called before RunGame is called.
// While Run specifies the window size, RunGame does not.
// You need to call SetWindowSize before RunGame if you want.
// Otherwise, a default window size is adopted.
//
// Some functions (ScreenScale, SetScreenScale, SetScreenSize) are not available with RunGame.
//
// A window size is based on the given values (width, height and scale).
//
@ -210,11 +214,7 @@ func Run(f func(*Image) error, width, height int, scale float64, title string) e
// Don't call RunGame twice or more in one process.
func RunGame(game Game) error {
if uiDriver().CanHaveWindow() {
w, h := WindowSize()
if w < 0 || h < 0 {
panic("ebiten: SetWindowSize must be called before RunGame on desktops")
}
fixWindowPosition(w, h)
fixWindowPosition(WindowSize())
}
theUIContext = newUIContext(game, 1)
if err := uiDriver().Run(theUIContext, graphicsDriver()); err != nil {

View File

@ -193,7 +193,12 @@ func WindowSize() (int, int) {
// SetWindowSize sets the window size. On fullscreen mode, SetWindowSize sets the original window size.
//
// SetWindowSize panics if width or height is not a positive number.
//
// SetWindowSize is concurrent-safe.
func SetWindowSize(width, height int) {
if width <= 0 || height <= 0 {
panic("ebiten: width and height must be positive")
}
uiDriver().SetWindowSize(width, height)
}