1
0

main.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. package main
  15. import (
  16. "log"
  17. "math"
  18. "github.com/hajimehoshi/ebiten/v2"
  19. )
  20. const (
  21. screenWidth = 640
  22. screenHeight = 640
  23. maxIt = 128
  24. )
  25. var (
  26. palette [maxIt]byte
  27. )
  28. func init() {
  29. for i := range palette {
  30. palette[i] = byte(math.Sqrt(float64(i)/float64(len(palette))) * 0x80)
  31. }
  32. }
  33. func color(it int) (r, g, b byte) {
  34. if it == maxIt {
  35. return 0xff, 0xff, 0xff
  36. }
  37. c := palette[it]
  38. return c, c, c
  39. }
  40. type Game struct {
  41. offscreen *ebiten.Image
  42. offscreenPix []byte
  43. }
  44. func NewGame() *Game {
  45. g := &Game{
  46. offscreen: ebiten.NewImage(screenWidth, screenHeight),
  47. offscreenPix: make([]byte, screenWidth*screenHeight*4),
  48. }
  49. // Now it is not feasible to call updateOffscreen every frame due to performance.
  50. g.updateOffscreen(-0.75, 0.25, 2)
  51. return g
  52. }
  53. func (gm *Game) updateOffscreen(centerX, centerY, size float64) {
  54. for j := 0; j < screenHeight; j++ {
  55. for i := 0; i < screenHeight; i++ {
  56. x := float64(i)*size/screenWidth - size/2 + centerX
  57. y := (screenHeight-float64(j))*size/screenHeight - size/2 + centerY
  58. c := complex(x, y)
  59. z := complex(0, 0)
  60. it := 0
  61. for ; it < maxIt; it++ {
  62. z = z*z + c
  63. if real(z)*real(z)+imag(z)*imag(z) > 4 {
  64. break
  65. }
  66. }
  67. r, g, b := color(it)
  68. p := 4 * (i + j*screenWidth)
  69. gm.offscreenPix[p] = r
  70. gm.offscreenPix[p+1] = g
  71. gm.offscreenPix[p+2] = b
  72. gm.offscreenPix[p+3] = 0xff
  73. }
  74. }
  75. gm.offscreen.WritePixels(gm.offscreenPix)
  76. }
  77. func (g *Game) Update() error {
  78. return nil
  79. }
  80. func (g *Game) Draw(screen *ebiten.Image) {
  81. screen.DrawImage(g.offscreen, nil)
  82. }
  83. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  84. return screenWidth, screenHeight
  85. }
  86. func main() {
  87. ebiten.SetWindowSize(screenWidth, screenHeight)
  88. ebiten.SetWindowTitle("Mandelbrot (Ebitengine Demo)")
  89. if err := ebiten.RunGame(NewGame()); err != nil {
  90. log.Fatal(err)
  91. }
  92. }