main.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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/colorm"
  23. "github.com/hajimehoshi/ebiten/v2/examples/resources/images"
  24. )
  25. const (
  26. screenWidth = 640
  27. screenHeight = 480
  28. )
  29. var (
  30. gophersImage *ebiten.Image
  31. )
  32. type Game struct {
  33. count int
  34. }
  35. func (g *Game) Update() error {
  36. g.count++
  37. return nil
  38. }
  39. func (g *Game) Draw(screen *ebiten.Image) {
  40. // Center the image on the screen.
  41. s := gophersImage.Bounds().Size()
  42. op := &colorm.DrawImageOptions{}
  43. op.GeoM.Translate(-float64(s.X)/2, -float64(s.Y)/2)
  44. op.GeoM.Scale(2, 2)
  45. op.GeoM.Translate(float64(screenWidth)/2, float64(screenHeight)/2)
  46. // Rotate the hue.
  47. var c colorm.ColorM
  48. c.RotateHue(float64(g.count%360) * 2 * math.Pi / 360)
  49. colorm.DrawImage(screen, gophersImage, c, op)
  50. }
  51. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  52. return screenWidth, screenHeight
  53. }
  54. func main() {
  55. // Decode an image from the image file's byte slice.
  56. img, _, err := image.Decode(bytes.NewReader(images.Gophers_jpg))
  57. if err != nil {
  58. log.Fatal(err)
  59. }
  60. gophersImage = ebiten.NewImageFromImage(img)
  61. ebiten.SetWindowSize(screenWidth, screenHeight)
  62. ebiten.SetWindowTitle("Hue (Ebitengine Demo)")
  63. if err := ebiten.RunGame(&Game{}); err != nil {
  64. log.Fatal(err)
  65. }
  66. }