Skip to content

Instantly share code, notes, and snippets.

@creationix
Created December 11, 2021 14: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 creationix/b3a04cb2880e42a16cab39be7aa0b9df to your computer and use it in GitHub Desktop.
Save creationix/b3a04cb2880e42a16cab39be7aa0b9df to your computer and use it in GitHub Desktop.
// ESC := "^"
ESC := "\x1b"
WIDTH := 33
HEIGHT := 32
FPS := 120
SIZE := WIDTH * HEIGHT
pixels := ByteArray(SIZE)
dirty_x1 := 0
dirty_y1 := 0
dirty_x2 := WIDTH - 1
dirty_y2 := HEIGHT - 1
clear:
SIZE.repeat: pixels[it] = 0
dirty_x1 = 0
dirty_y1 = 0
dirty_x2 = WIDTH - 1
dirty_y2 = HEIGHT - 1
set x y c:
pixels[x + y * WIDTH] = c
if x < dirty_x1: dirty_x1 = x
if x > dirty_x2: dirty_x2 = x
if y < dirty_y1: dirty_y1 = y
if y > dirty_y2: dirty_y2 = y
main:
print "$(ESC)[2J$(ESC)[?25l"
period := Duration --ms=(1000/FPS)
while true:
start := Time.now
render
draw
extra := Duration.until start + period
sleep extra
o := 0
sx := 0
sy := 0
mx := 1
my := 1
render:
o = (o + 1) % 256
set sx sy o
sx += mx
sy += my
if sx >= WIDTH:
sx = WIDTH - 1
mx = -mx
else if sx < 0:
sx = 0
mx = -mx
if sy >= HEIGHT:
sy = HEIGHT - 1
my = -my
else if sy < 0:
sy = 0
my = -my
// clear
// o += 2
// HEIGHT.repeat: |y|
// WIDTH.repeat: |x|
// pixels[y * WIDTH + x] = (o + x + y) % 256
draw:
// print [sx, sy, dirty_x1, dirty_y1, dirty_x2, dirty_y2]
image := ""
odd := dirty_y1 % 2
for y := dirty_y1 - odd; y <= dirty_y2; y += 2:
// print "y=$(y) y>>1=$(y>>1) y1=$(dirty_y1) y2=$(dirty_y2)"
old_top := -1
old_bottom := -1
image += "$(ESC)[$((y>>1)+1);$(dirty_x1+1)H"
for x := dirty_x1; x <= dirty_x2; x += 1:
i := x + y * WIDTH
top := pixels[i]
bottom := pixels[i + WIDTH]
if top != old_top:
old_top = top
image += "$(ESC)[38;5;$(top)m"
if bottom != old_bottom:
old_bottom = bottom
image += "$(ESC)[48;5;$(bottom)m"
image += "▀"
image += "$(ESC)[0m"
print(image)
dirty_x1 = WIDTH
dirty_y1 = HEIGHT
dirty_x2 = -1
dirty_y2 = -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment