1
0

main.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2018 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. "bytes"
  17. "fmt"
  18. "image"
  19. "image/color"
  20. _ "image/jpeg"
  21. "log"
  22. "math"
  23. "runtime"
  24. "github.com/hajimehoshi/ebiten/v2"
  25. "github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
  26. "github.com/hajimehoshi/ebiten/v2/examples/resources/images"
  27. "github.com/hajimehoshi/ebiten/v2/inpututil"
  28. "github.com/hajimehoshi/ebiten/v2/text/v2"
  29. )
  30. var (
  31. gophersImage *ebiten.Image
  32. mplusFaceSource *text.GoTextFaceSource
  33. )
  34. func init() {
  35. // Decode an image from the image file's byte slice.
  36. img, _, err := image.Decode(bytes.NewReader(images.Gophers_jpg))
  37. if err != nil {
  38. log.Fatal(err)
  39. }
  40. gophersImage = ebiten.NewImageFromImage(img)
  41. }
  42. func init() {
  43. s, err := text.NewGoTextFaceSource(bytes.NewReader(fonts.MPlus1pRegular_ttf))
  44. if err != nil {
  45. log.Fatal(err)
  46. }
  47. mplusFaceSource = s
  48. }
  49. type Game struct {
  50. count int
  51. }
  52. func (g *Game) Update() error {
  53. g.count++
  54. if runtime.GOOS == "js" {
  55. if ebiten.IsKeyPressed(ebiten.KeyF) || len(inpututil.AppendJustPressedTouchIDs(nil)) > 0 {
  56. ebiten.SetFullscreen(true)
  57. }
  58. }
  59. if runtime.GOOS != "js" && ebiten.IsKeyPressed(ebiten.KeyQ) {
  60. return ebiten.Termination
  61. }
  62. return nil
  63. }
  64. func (g *Game) Draw(screen *ebiten.Image) {
  65. scale := ebiten.Monitor().DeviceScaleFactor()
  66. w, h := gophersImage.Bounds().Dx(), gophersImage.Bounds().Dy()
  67. op := &ebiten.DrawImageOptions{}
  68. op.GeoM.Translate(-float64(w)/2, -float64(h)/2)
  69. op.GeoM.Scale(scale, scale)
  70. op.GeoM.Rotate(float64(g.count%360) * 2 * math.Pi / 360)
  71. sw, sh := screen.Bounds().Dx(), screen.Bounds().Dy()
  72. op.GeoM.Translate(float64(sw)/2, float64(sh)/2)
  73. op.Filter = ebiten.FilterLinear
  74. screen.DrawImage(gophersImage, op)
  75. fw, fh := ebiten.Monitor().Size()
  76. msg := "This is an example of the finest fullscreen.\n"
  77. if runtime.GOOS == "js" {
  78. msg += "Press F or touch the screen to enter fullscreen (again).\n"
  79. } else {
  80. msg += "Press Q to quit.\n"
  81. }
  82. msg += fmt.Sprintf("Screen size in fullscreen: %d, %d\n", fw, fh)
  83. msg += fmt.Sprintf("Game's screen size: %d, %d\n", sw, sh)
  84. msg += fmt.Sprintf("Device scale factor: %0.2f\n", scale)
  85. textOp := &text.DrawOptions{}
  86. textOp.GeoM.Translate(50*scale, 50*scale)
  87. textOp.ColorScale.ScaleWithColor(color.White)
  88. textOp.LineSpacing = 12 * ebiten.Monitor().DeviceScaleFactor() * 1.5
  89. text.Draw(screen, msg, &text.GoTextFace{
  90. Source: mplusFaceSource,
  91. Size: 12 * ebiten.Monitor().DeviceScaleFactor(),
  92. }, textOp)
  93. }
  94. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  95. s := ebiten.Monitor().DeviceScaleFactor()
  96. return int(float64(outsideWidth) * s), int(float64(outsideHeight) * s)
  97. }
  98. func main() {
  99. ebiten.SetFullscreen(true)
  100. ebiten.SetWindowTitle("Fullscreen (Ebitengine Demo)")
  101. if err := ebiten.RunGame(&Game{}); err != nil {
  102. log.Fatal(err)
  103. }
  104. }