Yjasl Examples

Introduction :: Reference Manual :: Internals :: Yjasl Examples :: Programming Guidelines :: Download/sf.net :: About/Contact

snake.yjasl :: A snake game (console)
mandel.yjasl :: Mandelbrot (console)
gali.yjasl :: Game Of Life using 1D arrays (console)
gali2.yjasl :: Game Of Life using 2D arrays (console)
graph_mandel.yjasl :: Mandelbrot (GDI bindings 640x480)
graph_plasma.yjasl :: Plasma (GDI bindings 640x480)

graph_plasma.yjasl
/* graph_plasma.nscript : Calculates plasmas
   (w) by Neotec in 2006
   Uses Jasl v2.00, running under Jasl Graphics Runtime Environment */

if runenv() != "window" then
    error("This program needs a graphics interpreter")
end

f       = 1
max_x   = 640
max_y   = 480
color_r = 100
color_g = 100
color_b = 255

sub         s_set(x, y, c);
function    s_get(x, y);
sub         adjust(xa, ya, x, y, xb, yb);
sub         subdivide(x1, y1, x2, y2);

screen = array(max_x)
for x = 0, max_x - 1 do
    screen[x] = array(max_y, 0)
end

t0 = timetick()
t1 = t0

s_set(0, 0, rand(32, 256))
s_set(max_x - 1, 0, rand(32, 256))
s_set(0, max_y - 1, rand(32, 256))
s_set(max_x - 1, max_y - 1, rand(32, 256))

subdivide(0, 0, max_x - 1, max_y - 1)

t2 = timetick()

graph::statprint(((t2 - t1) / 1000).." seconds")

graph::refresh()

sub adjust(xa, ya, x, y, xb, yb)
    local v, d

    if !s_get(x, y) then
        d = abs(xa - xb) + abs(ya - yb)
        v = ((s_get(xa, ya) + s_get(xb, yb)) / 2) + (rand(-50, 50) * d * f / 100)
        if v < 1 then
            v = 1
        elseif v > 255 then
            v = 255
        end
        s_set(x, y, v)
    end
end

sub subdivide(x1, y1, x2, y2)
    local x, y, v

    if ((x2 - x1) >= 2) || ((y2 - y1) >= 2) then
        x = floor((x1 + x2) / 2)
        y = floor((y1 + y2) / 2)

        adjust(x1, y1, x, y1, x2, y1)
        adjust(x2, y1, x2, y, x2, y2)
        adjust(x1, y2, x, y2, x2, y2)
        adjust(x1, y1, x1, y, x1, y2)

        if !s_get(x, y) then
            v = (s_get(x1, y1) + s_get(x2, y1) + s_get(x2, y2) + s_get(x1, y2)) / 4
            s_set(x, y, v)
        end

        subdivide(x1, y1, x, y)
        subdivide(x, y1, x2, y)
        subdivide(x, y, x2, y2)
        subdivide(x1, y, x, y2)
    end

    if timetick() - t0 > 1000 then
        t0 = timetick()
        graph::refresh()
    end
end

sub s_set(x, y, c)
    screen[x][y] = c
    c = c / 255
    graph::pset(x, y, color_r * c, color_g * c, color_b * c)
end

function s_get(x, y)
    return screen[x][y]
end

© by René 'Neotec' Jeschke in 2006, 2007
Last updated: 14 JUN 2007 (under construction)
SourceForge.net Logo