2023-08-30 14:02:04 +02:00
|
|
|
// Copyright 2023 The Ebitengine Authors
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2023-09-23 08:56:41 +02:00
|
|
|
"strings"
|
2023-08-30 14:02:04 +02:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
2023-09-23 08:56:41 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
2023-08-30 14:02:04 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-09-23 08:56:41 +02:00
|
|
|
windowWidth = 640
|
|
|
|
windowHeight = 480
|
2023-08-30 14:02:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Game struct {
|
|
|
|
monitors []*ebiten.MonitorType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) Update() error {
|
|
|
|
// Refresh monitors.
|
|
|
|
g.monitors = ebiten.AppendMonitors(g.monitors[:0])
|
|
|
|
|
|
|
|
// Handle keypresses.
|
|
|
|
if inpututil.IsKeyJustReleased(ebiten.KeyF) {
|
|
|
|
ebiten.SetFullscreen(!ebiten.IsFullscreen())
|
|
|
|
} else {
|
|
|
|
for i, m := range g.monitors {
|
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyDigit0 + ebiten.Key(i)) {
|
|
|
|
ebiten.SetWindowTitle(m.Name())
|
|
|
|
ebiten.SetMonitor(m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) Draw(screen *ebiten.Image) {
|
2023-09-23 08:56:41 +02:00
|
|
|
lines := []string{"F to toggle fullscreen", "0-9 to change monitor"}
|
2023-08-30 14:02:04 +02:00
|
|
|
|
2023-09-23 08:56:41 +02:00
|
|
|
lines = append(lines, "")
|
2023-08-30 14:02:04 +02:00
|
|
|
for i, m := range g.monitors {
|
2023-09-23 11:24:03 +02:00
|
|
|
lines = append(lines, fmt.Sprintf("%d: %s", i, m.Name()))
|
2023-08-30 14:02:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
activeMonitor := ebiten.Monitor()
|
2023-09-23 08:56:41 +02:00
|
|
|
lines = append(lines, "")
|
2023-08-30 14:02:04 +02:00
|
|
|
for i, m := range g.monitors {
|
|
|
|
if m == activeMonitor {
|
2023-09-23 08:56:41 +02:00
|
|
|
lines = append(lines, fmt.Sprintf("active: %s (%d)", m.Name(), i))
|
2023-08-30 14:02:04 +02:00
|
|
|
}
|
|
|
|
}
|
2023-09-23 08:56:41 +02:00
|
|
|
|
|
|
|
ebitenutil.DebugPrint(screen, strings.Join(lines, "\n"))
|
2023-08-30 14:02:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
2023-09-23 08:56:41 +02:00
|
|
|
return windowWidth / 2, windowHeight / 2
|
2023-08-30 14:02:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
g := &Game{}
|
|
|
|
|
|
|
|
// Allow the user to pass in a monitor flag to target a specific monitor.
|
|
|
|
var monitor int
|
|
|
|
flag.IntVar(&monitor, "monitor", 0, "target monitor index to run the program on")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
// Read our monitors.
|
|
|
|
g.monitors = ebiten.AppendMonitors(nil)
|
|
|
|
|
|
|
|
// Ensure the user did not supply a monitor index beyond the range of available monitors. If they did, set the monitor to the primary.
|
|
|
|
if monitor < 0 || monitor >= len(g.monitors) {
|
|
|
|
monitor = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
targetMonitor := g.monitors[monitor]
|
|
|
|
ebiten.SetMonitor(targetMonitor)
|
|
|
|
ebiten.SetWindowTitle(targetMonitor.Name())
|
2023-09-23 08:56:41 +02:00
|
|
|
ebiten.SetWindowSize(windowWidth, windowHeight)
|
2023-08-30 14:02:04 +02:00
|
|
|
|
|
|
|
if err := ebiten.RunGame(g); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|