main.go 2.1 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. "github.com/hajimehoshi/ebiten/v2"
  21. "github.com/hajimehoshi/ebiten/v2/examples/resources/images"
  22. )
  23. const (
  24. screenWidth = 640
  25. screenHeight = 480
  26. )
  27. var (
  28. gophersImage *ebiten.Image
  29. )
  30. type Game struct{}
  31. func (g *Game) Update() error {
  32. return nil
  33. }
  34. func (g *Game) Draw(screen *ebiten.Image) {
  35. // Split the image into horizontal lines and draw them with different scales.
  36. op := &ebiten.DrawImageOptions{}
  37. w, h := gophersImage.Bounds().Dx(), gophersImage.Bounds().Dy()
  38. for i := 0; i < h; i++ {
  39. op.GeoM.Reset()
  40. // Move the image's center to the upper-left corner.
  41. op.GeoM.Translate(-float64(w)/2, -float64(h)/2)
  42. // Scale each lines and adjust the position.
  43. lineW := w + i*3/4
  44. x := -float64(lineW) / float64(w) / 2
  45. op.GeoM.Scale(float64(lineW)/float64(w), 1)
  46. op.GeoM.Translate(x, float64(i))
  47. // Move the image's center to the screen's center.
  48. op.GeoM.Translate(screenWidth/2, screenHeight/2)
  49. screen.DrawImage(gophersImage.SubImage(image.Rect(0, i, w, i+1)).(*ebiten.Image), op)
  50. }
  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("Perspective (Ebitengine Demo)")
  64. if err := ebiten.RunGame(&Game{}); err != nil {
  65. log.Fatal(err)
  66. }
  67. }