Skip to content

Instantly share code, notes, and snippets.

@gdkrmr
Last active December 29, 2018 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gdkrmr/ea4e7259f58e1504dbc46538dc4ad0e5 to your computer and use it in GitHub Desktop.
Save gdkrmr/ea4e7259f58e1504dbc46538dc4ad0e5 to your computer and use it in GitHub Desktop.
fire animation of Doom
# The fire animation of doom implemented in julia
# code inspired from http://fabiensanglard.net/doom_fire_psx/
using Images, ImageView, Colors
h, w = (200, 500)
pal = [
"#070707", "#1f0707", "#2f0f07", "#470f07",
"#571707", "#671f07", "#771f07", "#8f2707",
"#9f2f07", "#af3f07", "#bf4707", "#c74707",
"#df4f07", "#df5707", "#df5707", "#d75f07",
"#d7670f", "#cf6f0f", "#cf770f", "#cf7f0f",
"#cf8717", "#c78717", "#c78f17", "#c7971f",
"#bf9f1f", "#bf9f1f", "#bfa727", "#bfa727",
"#bfaf2f", "#b7af2f", "#b7b72f", "#b7b737",
"#cfcf6f", "#dfdf9f", "#efefc7", "#ffffff"
] |> x -> parse.(Colorant, x)
function spread_fire1!(arr, j, i, l_pal)
arr[j + 1, i] = clamp(arr[j, i] - 0x01, 0x01, l_pal)
end
function spread_fire2!(arr, j, i, l_pal)
rnd = round(eltype(arr), rand() * 3.0) & 0x03
arr[j + 1, i] = clamp(arr[j, i] - (rnd & 0x01), 0x01, l_pal)
end
function spread_fire3!(arr, j, i, l_pal)
rnd = round(eltype(arr), rand() * 3.0) & 0x03
arr[j - 1, clamp(i - rnd + 1, 1, size(arr, 2))] = clamp(arr[j, i] - (rnd & 0x01), 0x01, l_pal)
end
function do_fire!(arr, l_pal, f)
h, w = size(arr)
for i in 1:w, j in 2:h
f(arr, j, i, l_pal)
end
end
fill_fire!(arr_col, arr, pal) = map!(x -> pal[x], arr_col, arr)
function init_fire(h, w, pal)
img = fill(0x01, h, w)
img[end, :] .= length(pal)
img_col = fill(RGB(0, 0, 0), h, w)
fill_fire!(img_col, img, pal)
return img, img_col
end
function paint_fire(nframes, h, w, pal)
img, img_col = init_fire(h, w, pal)
imdata = imshow(img_col)
canv = imdata["gui"]["canvas"]
for i in 1:nframes
do_fire!(img, length(pal), spread_fire3!)
fill_fire!(img_col, img, pal)
imshow(canv, img_col)
sleep(0.01)
end
end
paint_fire(1000, h, w, pal)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment