main.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2014 Hajime Hoshi
  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. "image"
  18. _ "image/jpeg"
  19. "log"
  20. "math"
  21. "github.com/hajimehoshi/ebiten/v2"
  22. "github.com/hajimehoshi/ebiten/v2/examples/resources/images"
  23. )
  24. const (
  25. screenWidth = 640
  26. screenHeight = 480
  27. )
  28. var (
  29. gophersImage *ebiten.Image
  30. )
  31. type Game struct {
  32. count int
  33. }
  34. func (g *Game) Update() error {
  35. g.count++
  36. return nil
  37. }
  38. func (g *Game) Draw(screen *ebiten.Image) {
  39. s := gophersImage.Bounds().Size()
  40. op := &ebiten.DrawImageOptions{}
  41. // Move the image's center to the screen's upper-left corner.
  42. // This is a preparation for rotating. When geometry matrices are applied,
  43. // the origin point is the upper-left corner.
  44. op.GeoM.Translate(-float64(s.X)/2, -float64(s.Y)/2)
  45. // Rotate the image. As a result, the anchor point of this rotate is
  46. // the center of the image.
  47. op.GeoM.Rotate(float64(g.count%360) * 2 * math.Pi / 360)
  48. // Move the image to the screen's center.
  49. op.GeoM.Translate(screenWidth/2, screenHeight/2)
  50. screen.DrawImage(gophersImage, op)
  51. }
  52. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  53. return screenWidth, screenHeight
  54. }
  55. func main() {
  56. // Decode an image from the image file's byte slice.
  57. img, _, err := image.Decode(bytes.NewReader(images.Gophers_jpg))
  58. if err != nil {
  59. log.Fatal(err)
  60. }
  61. gophersImage = ebiten.NewImageFromImage(img)
  62. ebiten.SetWindowSize(screenWidth, screenHeight)
  63. ebiten.SetWindowTitle("Rotate (Ebitengine Demo)")
  64. if err := ebiten.RunGame(&Game{}); err != nil {
  65. log.Fatal(err)
  66. }
  67. }