main.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2023 The Ebitengine Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "flag"
  17. "fmt"
  18. "log"
  19. "strings"
  20. "github.com/hajimehoshi/ebiten/v2"
  21. "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  22. "github.com/hajimehoshi/ebiten/v2/inpututil"
  23. )
  24. const (
  25. windowWidth = 640
  26. windowHeight = 480
  27. )
  28. type Game struct {
  29. monitors []*ebiten.MonitorType
  30. }
  31. func (g *Game) Update() error {
  32. // Refresh monitors.
  33. g.monitors = ebiten.AppendMonitors(g.monitors[:0])
  34. // Handle keypresses.
  35. if inpututil.IsKeyJustReleased(ebiten.KeyF) {
  36. ebiten.SetFullscreen(!ebiten.IsFullscreen())
  37. } else {
  38. for i, m := range g.monitors {
  39. if inpututil.IsKeyJustPressed(ebiten.KeyDigit0 + ebiten.Key(i)) {
  40. ebiten.SetWindowTitle(m.Name())
  41. ebiten.SetMonitor(m)
  42. }
  43. }
  44. }
  45. return nil
  46. }
  47. func (g *Game) Draw(screen *ebiten.Image) {
  48. lines := []string{"F to toggle fullscreen", "0-9 to change monitor"}
  49. lines = append(lines, "")
  50. for i, m := range g.monitors {
  51. lines = append(lines, fmt.Sprintf("%d: %s", i, m.Name()))
  52. }
  53. activeMonitor := ebiten.Monitor()
  54. lines = append(lines, "")
  55. for i, m := range g.monitors {
  56. if m == activeMonitor {
  57. lines = append(lines, fmt.Sprintf("active: %s (%d)", m.Name(), i))
  58. }
  59. }
  60. ebitenutil.DebugPrint(screen, strings.Join(lines, "\n"))
  61. }
  62. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  63. return windowWidth / 2, windowHeight / 2
  64. }
  65. func main() {
  66. g := &Game{}
  67. // Allow the user to pass in a monitor flag to target a specific monitor.
  68. var monitor int
  69. flag.IntVar(&monitor, "monitor", 0, "target monitor index to run the program on")
  70. flag.Parse()
  71. // Read our monitors.
  72. g.monitors = ebiten.AppendMonitors(nil)
  73. // Ensure the user did not supply a monitor index beyond the range of available monitors. If they did, set the monitor to the primary.
  74. if monitor < 0 || monitor >= len(g.monitors) {
  75. monitor = 0
  76. }
  77. targetMonitor := g.monitors[monitor]
  78. ebiten.SetMonitor(targetMonitor)
  79. ebiten.SetWindowTitle(targetMonitor.Name())
  80. ebiten.SetWindowSize(windowWidth, windowHeight)
  81. if err := ebiten.RunGame(g); err != nil {
  82. log.Fatal(err)
  83. }
  84. }