From ae6213e80aa6f7c8963d38e80f67538e7715911a Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 30 Dec 2018 05:51:17 +0100 Subject: add fire demo --- fire/main.go | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 fire/main.go diff --git a/fire/main.go b/fire/main.go new file mode 100644 index 0000000..3d47d11 --- /dev/null +++ b/fire/main.go @@ -0,0 +1,101 @@ +// http://fabiensanglard.net/doom_fire_psx/ + +package main + +import ( + "fmt" + "image" + "image/color" + "log" + "math/rand" + "time" + + "github.com/disintegration/imaging" + "github.com/hajimehoshi/ebiten" + "github.com/hajimehoshi/ebiten/ebitenutil" +) + +var palette = color.Palette{ + color.RGBA{0x00, 0x00, 0x00, 0xff}, + color.RGBA{0x07, 0x07, 0x07, 0xff}, + color.RGBA{0x1f, 0x07, 0x07, 0xff}, + color.RGBA{0x2f, 0x0f, 0x07, 0xff}, + color.RGBA{0x47, 0x0f, 0x07, 0xff}, + color.RGBA{0x57, 0x17, 0x07, 0xff}, + color.RGBA{0x67, 0x1f, 0x07, 0xff}, + color.RGBA{0x77, 0x1f, 0x07, 0xff}, + color.RGBA{0x8f, 0x27, 0x07, 0xff}, + color.RGBA{0x9f, 0x2f, 0x07, 0xff}, + color.RGBA{0xaf, 0x3f, 0x07, 0xff}, + color.RGBA{0xbf, 0x47, 0x07, 0xff}, + color.RGBA{0xc7, 0x47, 0x07, 0xff}, + color.RGBA{0xdf, 0x4f, 0x07, 0xff}, + color.RGBA{0xdf, 0x57, 0x07, 0xff}, + color.RGBA{0xdf, 0x57, 0x07, 0xff}, + color.RGBA{0xd7, 0x5f, 0x07, 0xff}, + color.RGBA{0xd7, 0x67, 0x0f, 0xff}, + color.RGBA{0xcf, 0x6f, 0x0f, 0xff}, + color.RGBA{0xcf, 0x77, 0x0f, 0xff}, + color.RGBA{0xcf, 0x7f, 0x0f, 0xff}, + color.RGBA{0xcf, 0X87, 0x17, 0xff}, + color.RGBA{0xc7, 0x87, 0x17, 0xff}, + color.RGBA{0xc7, 0x8f, 0x17, 0xff}, + color.RGBA{0xc7, 0x97, 0x1f, 0xff}, + color.RGBA{0xbf, 0X9f, 0X1f, 0xff}, + color.RGBA{0xbf, 0x9f, 0x1f, 0xff}, + color.RGBA{0xbf, 0xa7, 0x27, 0xff}, + color.RGBA{0xbf, 0xa7, 0x27, 0xff}, + color.RGBA{0xbf, 0xaf, 0x2f, 0xff}, + color.RGBA{0xb7, 0xaf, 0x2f, 0xff}, + color.RGBA{0xb7, 0xb7, 0x2f, 0xff}, + color.RGBA{0xb7, 0xb7, 0x37, 0xff}, + color.RGBA{0xcf, 0xcf, 0x6f, 0xff}, + color.RGBA{0xdf, 0xdf, 0x9f, 0xff}, + color.RGBA{0xef, 0xef, 0xc7, 0xff}, + color.RGBA{0xff, 0xff, 0xff, 0xff}, +} + +const ( + screenWidth = 320 + screenHeight = 200 + scale = 2 +) + +type drawContext struct { + buf *image.Paletted +} + +func newDrawContext(x, y int) *drawContext { + rand.Seed(time.Now().UnixNano()) + img := image.NewPaletted(image.Rect(0, 0, x, y), palette) + for x := 0; x < screenWidth; x++ { + img.SetColorIndex(x, 0, uint8(len(palette)-1)) + } + return &drawContext{buf: img} +} + +func (dc *drawContext) update(screen *ebiten.Image) error { + r := screen.Bounds() + for x := 0; x < r.Max.X; x++ { + for y := 1; y < r.Max.Y; y++ { + z := rand.Intn(3) + n := dc.buf.ColorIndexAt(x, y-1) + if n > 0 && z&1 == 0 { + n-- + } + dc.buf.SetColorIndex(x-z+1, y, n) + } + } + if !ebiten.IsDrawingSkipped() { + screen.ReplacePixels(imaging.FlipV(dc.buf).Pix) + ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.CurrentTPS())) + } + return nil +} + +func main() { + dc := newDrawContext(screenWidth, screenHeight) + if err := ebiten.Run(dc.update, screenWidth, screenHeight, scale, "Fire"); err != nil { + log.Fatal(err) + } +} -- cgit v1.2.3