main.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2024 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. "fmt"
  17. "math"
  18. "github.com/hajimehoshi/ebiten/v2"
  19. "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  20. )
  21. const (
  22. screenWidth = 640
  23. screenHeight = 480
  24. )
  25. type Game struct {
  26. count int
  27. }
  28. func (g *Game) Update() error {
  29. if g.count > 0 {
  30. g.count--
  31. if g.count == 0 {
  32. ebiten.RequestAttention()
  33. }
  34. }
  35. if ebiten.IsKeyPressed(ebiten.KeyR) {
  36. g.count = ebiten.TPS() * 3
  37. }
  38. return nil
  39. }
  40. func (g *Game) Draw(screen *ebiten.Image) {
  41. if g.count > 0 {
  42. c := int(math.Ceil(float64(g.count) / float64(ebiten.TPS())))
  43. msg := fmt.Sprintf("Requesting attention in %d seconds...", c)
  44. if ebiten.IsFocused() {
  45. msg += "\nPlease unfocus this window to see the effect."
  46. }
  47. ebitenutil.DebugPrint(screen, msg)
  48. return
  49. }
  50. ebitenutil.DebugPrint(screen, "Press R to request attention after 3 seconds.")
  51. }
  52. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  53. return screenWidth, screenHeight
  54. }
  55. func main() {
  56. ebiten.SetWindowSize(screenWidth, screenHeight)
  57. ebiten.SetWindowTitle("Request Attention (Ebitengine Demo)")
  58. if err := ebiten.RunGame(&Game{}); err != nil {
  59. panic(err)
  60. }
  61. }