Skip to content

SetMonitor can take a while to be applied and affect subsequent window operations #3211

@tinne26

Description

@tinne26

Ebitengine Version

v2.8.6

Operating System

  • Windows
  • macOS
  • Linux
  • FreeBSD
  • OpenBSD
  • Android
  • iOS
  • Nintendo Switch
  • PlayStation 5
  • Xbox
  • Web Browsers

Go Version (go version)

go1.23.2 linux/amd64

What steps will reproduce the problem?

(requires two monitors connected)

package main

import (
	"fmt"

	"github.com/hajimehoshi/ebiten/v2"
)

type App struct {
	tick int
}

func (app *App) Layout(w, h int) (int, int) {
	return w, h
}

func (app *App) Update() error {
	if ebiten.IsKeyPressed(ebiten.KeyEscape) {
		return ebiten.Termination
	}

	switch app.tick {
	case 0:
		monitor := ebiten.AppendMonitors(nil)[1]
		fmt.Printf("changing monitor to '%s'\n", monitor.Name())
		ebiten.SetMonitor(monitor)
		//time.Sleep(1 * time.Millisecond)
		ebiten.SetFullscreen(true)
		fmt.Printf("current monitor: '%s'\n", ebiten.Monitor().Name())
	}

	app.tick += 1
	return nil
}

func (app *App) Draw(*ebiten.Image) {}

func main() {
	fmt.Printf("primary monitor: '%s'\n", ebiten.Monitor().Name())
	err := ebiten.RunGame(&App{})
	if err != nil {
		panic(err)
	}
}

What is the expected result?

Fullscreening applies to the newly set monitor.

What happens instead?

Fullscreening happens on the (previous) main monitor.

Anything else you feel useful to add?

I realized this happens because the monitor change can take a bit of time to happen. That's why I added the time.Sleep code, which can be uncommented to test. Sometimes, even just moving the printf of the current monitor before the SetFullscreen affects the results. I've even had the printf print that the monitor hasn't changed, but the fullscreen happen in the secondary monitor. Using a 1ms time sleep always seems to make the change work.

So, basically, the monitor change can take a short time to be applied, and in the meantime window operations might be applied to the "previous" monitor.

Tested on archlinux/KDE

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions