main.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2017 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. // This example is just to check if Ebitengine can draw fine checker pattern evenly.
  15. // If there is something wrong in the implementation, the result might include
  16. // uneven patterns (#459).
  17. package main
  18. import (
  19. "log"
  20. "github.com/hajimehoshi/ebiten/v2"
  21. "github.com/hajimehoshi/ebiten/v2/inpututil"
  22. )
  23. const (
  24. screenWidth = 640
  25. screenHeight = 480
  26. initScreenScale = 1
  27. )
  28. var (
  29. dots []byte
  30. dotsWidth int
  31. dotsHeight int
  32. )
  33. func getDots(width, height int) []byte {
  34. if dotsWidth == width && dotsHeight == height {
  35. return dots
  36. }
  37. dotsWidth = width
  38. dotsHeight = height
  39. dots = make([]byte, width*height*4)
  40. for j := 0; j < height; j++ {
  41. for i := 0; i < width; i++ {
  42. if (i+j)%2 == 0 {
  43. dots[(i+j*width)*4+0] = 0xff
  44. dots[(i+j*width)*4+1] = 0xff
  45. dots[(i+j*width)*4+2] = 0xff
  46. dots[(i+j*width)*4+3] = 0xff
  47. }
  48. }
  49. }
  50. return dots
  51. }
  52. type game struct {
  53. scale float64
  54. }
  55. func (g *game) Layout(outsideWidth, outsideHeight int) (int, int) {
  56. return screenWidth, screenHeight
  57. }
  58. func (g *game) Update() error {
  59. fullscreen := ebiten.IsFullscreen()
  60. if inpututil.IsKeyJustPressed(ebiten.KeyS) {
  61. switch g.scale {
  62. case 0.5:
  63. g.scale = 1
  64. case 1:
  65. g.scale = 1.5
  66. case 1.5:
  67. g.scale = 2
  68. case 2:
  69. g.scale = 0.5
  70. default:
  71. panic("not reached")
  72. }
  73. ebiten.SetWindowSize(int(screenWidth*g.scale), int(screenHeight*g.scale))
  74. }
  75. if inpututil.IsKeyJustPressed(ebiten.KeyF) {
  76. fullscreen = !fullscreen
  77. ebiten.SetFullscreen(fullscreen)
  78. }
  79. return nil
  80. }
  81. func (g *game) Draw(screen *ebiten.Image) {
  82. s := screen.Bounds().Size()
  83. screen.WritePixels(getDots(s.X, s.Y))
  84. }
  85. func main() {
  86. g := &game{
  87. scale: initScreenScale,
  88. }
  89. ebiten.SetWindowSize(screenWidth*initScreenScale, screenHeight*initScreenScale)
  90. ebiten.SetWindowTitle("Moire (Ebitengine Demo)")
  91. ebiten.SetWindowResizable(true)
  92. if err := ebiten.RunGame(g); err != nil {
  93. log.Fatal(err)
  94. }
  95. }