main.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2021 The Ebiten 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. "log"
  17. "github.com/hajimehoshi/ebiten/v2"
  18. "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  19. "github.com/hajimehoshi/ebiten/v2/inpututil"
  20. )
  21. type Game struct {
  22. windowClosingHandled bool
  23. }
  24. func (g *Game) Update() error {
  25. if ebiten.IsWindowBeingClosed() {
  26. g.windowClosingHandled = true
  27. }
  28. if g.windowClosingHandled {
  29. if inpututil.IsKeyJustPressed(ebiten.KeyY) {
  30. return ebiten.Termination
  31. }
  32. if inpututil.IsKeyJustPressed(ebiten.KeyN) {
  33. g.windowClosingHandled = false
  34. }
  35. }
  36. return nil
  37. }
  38. func (g *Game) Draw(screen *ebiten.Image) {
  39. if !g.windowClosingHandled {
  40. ebitenutil.DebugPrint(screen, "Try to close this window. This works only on desktops.")
  41. return
  42. }
  43. ebitenutil.DebugPrint(screen, "Do you really want to close this window? [y/n]")
  44. }
  45. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  46. return outsideWidth, outsideHeight
  47. }
  48. func main() {
  49. ebiten.SetWindowClosingHandled(true)
  50. ebiten.SetWindowTitle("Window Closing (Ebitengine Demo)")
  51. if err := ebiten.RunGame(&Game{}); err != nil {
  52. log.Fatal(err)
  53. }
  54. }