ebiten/examples/mandelbrot/main.go

107 lines
2.4 KiB
Go
Raw Normal View History

2017-12-16 12:35:01 +01:00
// Copyright 2017 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"log"
"math"
2020-10-03 19:35:13 +02:00
"github.com/hajimehoshi/ebiten/v2"
2017-12-16 12:35:01 +01:00
)
const (
screenWidth = 640
screenHeight = 640
maxIt = 128
)
var (
palette [maxIt]byte
2017-12-16 12:35:01 +01:00
)
func init() {
for i := range palette {
palette[i] = byte(math.Sqrt(float64(i)/float64(len(palette))) * 0x80)
}
}
2017-12-16 12:35:01 +01:00
func color(it int) (r, g, b byte) {
if it == maxIt {
2017-12-16 12:37:24 +01:00
return 0xff, 0xff, 0xff
2017-12-16 12:35:01 +01:00
}
c := palette[it]
return c, c, c
}
type Game struct {
offscreen *ebiten.Image
offscreenPix []byte
}
func NewGame() *Game {
g := &Game{
offscreen: ebiten.NewImage(screenWidth, screenHeight),
offscreenPix: make([]byte, screenWidth*screenHeight*4),
}
// Now it is not feasible to call updateOffscreen every frame due to performance.
g.updateOffscreen(-0.75, 0.25, 2)
return g
}
func (gm *Game) updateOffscreen(centerX, centerY, size float64) {
2017-12-16 12:35:01 +01:00
for j := 0; j < screenHeight; j++ {
for i := 0; i < screenHeight; i++ {
x := float64(i)*size/screenWidth - size/2 + centerX
y := (screenHeight-float64(j))*size/screenHeight - size/2 + centerY
c := complex(x, y)
2017-12-16 13:17:53 +01:00
z := complex(0, 0)
2017-12-16 12:35:01 +01:00
it := 0
for ; it < maxIt; it++ {
2017-12-16 13:17:53 +01:00
z = z*z + c
if real(z)*real(z)+imag(z)*imag(z) > 4 {
2017-12-16 12:35:01 +01:00
break
}
}
r, g, b := color(it)
p := 4 * (i + j*screenWidth)
gm.offscreenPix[p] = r
gm.offscreenPix[p+1] = g
gm.offscreenPix[p+2] = b
gm.offscreenPix[p+3] = 0xff
2017-12-16 12:35:01 +01:00
}
}
gm.offscreen.WritePixels(gm.offscreenPix)
2020-05-11 19:11:09 +02:00
}
2017-12-16 12:35:01 +01:00
func (g *Game) Update() error {
2017-12-16 12:35:01 +01:00
return nil
}
2020-05-11 19:11:09 +02:00
func (g *Game) Draw(screen *ebiten.Image) {
screen.DrawImage(g.offscreen, nil)
2020-05-11 19:11:09 +02:00
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
2017-12-16 12:35:01 +01:00
func main() {
2020-05-11 19:11:09 +02:00
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Mandelbrot (Ebitengine Demo)")
if err := ebiten.RunGame(NewGame()); err != nil {
2017-12-16 12:35:01 +01:00
log.Fatal(err)
}
}