Skip to content

Commit

Permalink
add Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelmcallister committed Aug 21, 2021
2 parents ae5f86c + e7920db commit b709ecb
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 97 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build/
html/*.wasm
html/*.js
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.PHONY: wasm
wasm:
rm -f ./html/*.js ./html/*.wasm
cp $$(go env GOROOT)/misc/wasm/wasm_exec.js ./html/wasm_exec.js
GOOS=js GOARCH=wasm go build -o ./html/main.wasm .

.PHONY: native
native:
go build -o ./build/doomfire .
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/michaelmcallister/doomfire

go 1.15

require github.com/hajimehoshi/ebiten/v2 v2.0.3
require github.com/hajimehoshi/ebiten/v2 v2.0.6
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200707082815-5321531c36a2/go.mod h1:tQ2
github.com/gofrs/flock v0.8.0 h1:MSdYClljsF3PbENUUEx85nkWfJSGfzYI9yEBZOJz6CY=
github.com/gofrs/flock v0.8.0/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/hajimehoshi/bitmapfont/v2 v2.1.0/go.mod h1:2BnYrkTQGThpr/CY6LorYtt/zEPNzvE/ND69CRTaHMs=
github.com/hajimehoshi/ebiten/v2 v2.0.3 h1:hlHTz2kIMRG0FO8RcuF70kE49W7kEz6ea7io275XF+w=
github.com/hajimehoshi/ebiten/v2 v2.0.3/go.mod h1:AbHP/SS226aFTex/izULVwW0D2AuGyqC4AVwilmRjOg=
github.com/hajimehoshi/ebiten/v2 v2.0.6 h1:sHNymgI+q80xasP69oFyrpup6r2qCNsKxqwsGEh6PWE=
github.com/hajimehoshi/ebiten/v2 v2.0.6/go.mod h1:uS3OjMW3f2DRDMtWoIF7yMMmrMkv+fZ6pXcwR1pfA0Y=
github.com/hajimehoshi/file2byteslice v0.0.0-20200812174855-0e5e8a80490e/go.mod h1:CqqAHp7Dk/AqQiwuhV1yT2334qbA/tFWQW0MD2dGqUE=
github.com/hajimehoshi/go-mp3 v0.3.1/go.mod h1:qMJj/CSDxx6CGHiZeCgbiq2DSUkbK0UbtXShQcnfyMM=
github.com/hajimehoshi/oto v0.6.1/go.mod h1:0QXGEkbuJRohbJaxr7ZQSxnju7hEhseiPx2hrh6raOI=
Expand Down Expand Up @@ -34,8 +34,8 @@ golang.org/x/image v0.0.0-20200927104501-e162460cd6b5 h1:QelT11PB4FXiDEXucrfNckH
golang.org/x/image v0.0.0-20200927104501-e162460cd6b5/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/mobile v0.0.0-20190415191353-3e0bab5405d6/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
golang.org/x/mobile v0.0.0-20200801112145-973feb4309de h1:OVJ6QQUBAesB8CZijKDSsXX7xYVtUhrkY0gwMfbi4p4=
golang.org/x/mobile v0.0.0-20200801112145-973feb4309de/go.mod h1:skQtrUTUwhdJvXM/2KKJzY8pDgNr9I/FOMqDVRPBUS4=
golang.org/x/mobile v0.0.0-20210208171126-f462b3930c8f h1:aEcjdTsycgPqO/caTgnxfR9xwWOltP/21vtJyFztEy0=
golang.org/x/mobile v0.0.0-20210208171126-f462b3930c8f/go.mod h1:skQtrUTUwhdJvXM/2KKJzY8pDgNr9I/FOMqDVRPBUS4=
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.1.1-0.20191209134235-331c550502dd/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
Expand Down
1 change: 1 addition & 0 deletions html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
go.run(result.instance);
});
</script>

110 changes: 18 additions & 92 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package main

import (
"flag"
"fmt"
"image/color"
"math/rand"
"os"
"sync"

"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)

// rgb is the colour palette to use for the Fire.
Expand Down Expand Up @@ -55,32 +51,17 @@ var rgb = []color.RGBA{
{0xFF, 0xFF, 0xFF, 0xFF},
}

var (
widthFlag = flag.Int("width", 600, "width (in pixels)")
heightFlag = flag.Int("height", 400, "height (in pixels)")
)

type direction int

const (
left direction = iota
right
)

// Doom implements the ebiten.Game interface.
type Doom struct {
width, height int
firePixels []int
screenBuffer []byte
waitGroup sync.WaitGroup
printDebug bool
buffer []byte
}

// NewDoom creates a new instance of Doom
func NewDoom(width, height int) *Doom {
d := &Doom{width: width, height: height}
d.firePixels = make([]int, d.width*d.height)
d.screenBuffer = make([]byte, d.width*d.height*4)
// Set whole screen to 0 (color: 0x07,0x07,0x07)
for i := 0; i < d.width*d.height; i++ {
d.firePixels[i] = 0
Expand All @@ -101,96 +82,41 @@ func (d *Doom) spreadFire(src int) {
}
randIdx := rand.Intn(3)
dst := src - randIdx + 1
i := dst - d.width
if i > 0 {
d.firePixels[i] = pixel - (randIdx & 1)
}
}

func (d *Doom) addWind(dir direction) {
switch dir {
case left:
for x := d.width - 50; x < d.width; x++ {
for y := d.height; y > d.height/4; y-- {
fmt.Println("y: ", y)
dst := y*d.width + x
if dst > len(d.firePixels) {
continue
}
if d.firePixels[dst] != 0 {
d.firePixels[y*d.width+x] = 3
}
}
}
case right:
fmt.Println("right")
}
d.firePixels[dst-d.width] = pixel - (randIdx & 1)
}

// Update applies the fire spread on each frame.
func (d *Doom) Update() error {
d.buffer = make([]byte, d.width*d.height*4)
for x := 0; x < d.width; x++ {
d.waitGroup.Add(1)
go func(x int) {
defer d.waitGroup.Done()
for y := 1; y < d.height; y++ {
d.spreadFire(y*d.width + x)
}
}(x)
}
d.waitGroup.Wait()

if inpututil.IsKeyJustPressed(ebiten.KeyD) {
d.printDebug = !d.printDebug
}

if inpututil.IsKeyJustPressed(ebiten.KeyEnter) {
ebiten.SetFullscreen(!ebiten.IsFullscreen())
}

if inpututil.IsKeyJustPressed(ebiten.KeyQ) {
os.Exit(0)
}

if ebiten.IsKeyPressed(ebiten.KeyLeft) {
d.addWind(left)
}
if ebiten.IsKeyPressed(ebiten.KeyRight) {
d.addWind(right)
for y := 1; y < d.height; y++ {
i := y*d.width + x
d.spreadFire(i)
c := d.firePixels[i]
r, g, b, a := rgb[c].RGBA()
d.buffer[4*i] = byte(r)
d.buffer[4*i+1] = byte(g)
d.buffer[4*i+2] = byte(b)
d.buffer[4*i+3] = byte(a)
}
}

return nil
}

// Draw plots the current fire framebuffer.
func (d *Doom) Draw(scr *ebiten.Image) {
l := d.width * d.height
for i := 0; i < l; i++ {
c := d.firePixels[i]
r, g, b, a := rgb[c].RGBA()
d.screenBuffer[4*i] = byte(r)
d.screenBuffer[4*i+1] = byte(g)
d.screenBuffer[4*i+2] = byte(b)
d.screenBuffer[4*i+3] = byte(a)
}
scr.ReplacePixels(d.screenBuffer)

if d.printDebug {
ebitenutil.DebugPrint(scr, fmt.Sprintf("TPS: %0.2f", ebiten.CurrentTPS()))
}
func (d *Doom) Draw(screen *ebiten.Image) {
screen.ReplacePixels(d.buffer)
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %f\n", ebiten.CurrentTPS()))
}

// Layout takes the outside size (e.g., the window size) and returns the (logical) screen size.
func (d *Doom) Layout(outsideWidth, outsideHeight int) (int, int) {
func (d *Doom) Layout(_, _ int) (int, int) {
return d.width, d.height
}

func main() {
flag.Parse()
ebiten.SetWindowTitle("DOOM")
ebiten.SetMaxTPS(ebiten.UncappedTPS)
ebiten.SetWindowSize(*widthFlag, *heightFlag)
d := NewDoom(*widthFlag, *heightFlag)
d := NewDoom(600, 400)

if err := ebiten.RunGame(d); err != nil {
panic(err)
Expand Down

0 comments on commit b709ecb

Please sign in to comment.