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. "github.com/hajimehoshi/ebiten/v2"
  21. "github.com/hajimehoshi/ebiten/v2/examples/resources/images"
  22. )
  23. const (
  24. screenWidth = 320
  25. screenHeight = 240
  26. )
  27. const mosaicRatio = 16
  28. var (
  29. gophersImage *ebiten.Image
  30. )
  31. func init() {
  32. // Decode an image from the image file's byte slice.
  33. img, _, err := image.Decode(bytes.NewReader(images.Gophers_jpg))
  34. if err != nil {
  35. log.Fatal(err)
  36. }
  37. gophersImage = ebiten.NewImageFromImage(img)
  38. }
  39. type Game struct {
  40. gophersRenderTarget *ebiten.Image
  41. }
  42. func (g *Game) Update() error {
  43. return nil
  44. }
  45. func (g *Game) Draw(screen *ebiten.Image) {
  46. // Shrink the image once.
  47. op := &ebiten.DrawImageOptions{}
  48. op.GeoM.Scale(1.0/mosaicRatio, 1.0/mosaicRatio)
  49. g.gophersRenderTarget.DrawImage(gophersImage, op)
  50. // Enlarge the shrunk image.
  51. // The filter is the nearest filter, so the result will be mosaic.
  52. op = &ebiten.DrawImageOptions{}
  53. op.GeoM.Scale(mosaicRatio, mosaicRatio)
  54. screen.DrawImage(g.gophersRenderTarget, op)
  55. }
  56. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  57. return screenWidth, screenHeight
  58. }
  59. func main() {
  60. w, h := gophersImage.Bounds().Dx(), gophersImage.Bounds().Dy()
  61. g := &Game{
  62. gophersRenderTarget: ebiten.NewImage(w/mosaicRatio, h/mosaicRatio),
  63. }
  64. ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
  65. ebiten.SetWindowTitle("Mosaic (Ebitengine Demo)")
  66. if err := ebiten.RunGame(g); err != nil {
  67. log.Fatal(err)
  68. }
  69. }