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_mandel.yjasl
/* graph_mandel.nscript : Calculates Mandelbrot fractals
   Fractals will be saved to disk as .PNG and as .ITER which stores iteration data for
   faster recalculation with different color tables (these are gzip files)
   (w) by Neotec in 2006
   Uses Yjasl v2.00, running under Yjasl Graphics Runtime Environment */

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

width       = 640                                       // Screen width
height      = 480                                       // Screen height
imax        = 2304                                      // Maximum iterations
circular    = true                                      // Wrap color table?
pixels      = true                                      // Pixel per pixel update?
render_all  = true                                      // Render all mandelbrot fractals in list?
overwrite   = false                                     // Overwrite calculated fractals during render_all?
color_max   = {.r = 0, .g = 0, .b = 0}                  // Color for maximum iteration value (if !map)
pwinfract   = "maps\\"                                  // Path to Winfract/Fractint map files
map_type    = 4                                         // Map type number
fnum        = 3                                         // Fractal number

mparams     = {}
mparams[ 0] = {.name = "altern", .map = true}
mparams[ 1] = {.name = "blues", .map = true}
mparams[ 2] = {.name = "bw16", .map = true}
mparams[ 3] = {.name = "bw32", .map = true}
mparams[ 4] = {.name = "bw64", .map = true}
mparams[ 5] = {.name = "chroma", .map = true}
mparams[ 6] = {.name = "defaultw", .map = true}
mparams[ 7] = {.name = "filament", .map = true}         // From FRACTINT.PAR - Filament
mparams[ 8] = {.name = "firestrm", .map = true}
mparams[ 9] = {.name = "glasses2", .map = true}
mparams[10] = {.name = "neon", .map = true}
mparams[11] = {.name = "random", .map = true}
mparams[12] = {.name = "royal", .map = true}
mparams[13] = {.name = "standard", .map = false}
mparams[14] = {.name = "topo", .map = true}
mparams[15] = {.name = "volcano", .map = true}

fparams     = {}
fparams[ 0] = {-2.5, 1.5, -1.5, 1.5}
fparams[ 1] = {-0.67, -0.47, 0.56, 0.71}
fparams[ 2] = {-0.748, -0.741, 0.132, 0.137}
fparams[ 3] = {-1.860573964886, -1.860573955145, -0.000000937963, -0.000000930786}
fparams[ 4] = {-0.144886188, -0.142549766, 0.887085252, 0.888842007}
fparams[ 5] = {-1.63742981, -1.61804453, -0.00661551021, 0.00794620067}
fparams[ 6] = {-0.274188704403, -0.274188253684, 0.841196557584, 0.841196897085}
fparams[ 7] = {-0.756142580573, -0.755810462997, 0.063936666834, 0.064188893918}
fparams[ 8] = {0.25545802, 0.255471414, 0.000646917149, 0.000656958669}
fparams[ 9] = {0.0142306089, 0.0910453238, 0.620278612, 0.67793702}
fparams[10] = {0.053016752, 0.0621620696, 0.65284525, 0.659712534}
fparams[11] = {0.0533606429, 0.0538874846, 0.657753872, 0.658148838}
fparams[12] = {-1.86561969, -1.8570414, -0.00343991257, 0.00300228037}
fparams[13] = {-1.98554866, -1.98535072, -0.000073082745, 0.000075308606}
fparams[14] = {-1.40796558, -1.4078986, 0.136930868, 0.136982191}
fparams[15] = {-0.189516153, -0.185453221, 0.649839532, 0.652892154}
fparams[16] = {-1.1414851, -1.14145892, 0.210522372, 0.210542001}
fparams[17] = {0.413359627551, 0.413361299095, 0.349679018431, 0.349680281948}
fparams[18] = {-1.629272209987, -1.629272209811, 0.020688346119, 0.020688346252}
fparams[19] = {0.315711855808, 0.315712001396, 0.029772060555, 0.029772170317}
fparams[20] = {0.3157113057952, 0.3157125563438, 0.029771625714741, 0.029772567058}
fparams[21] = {-0.637915280085, -0.637914271429, 0.388118260435, 0.388119017273}
fparams[22] = {-1.861408968987, -1.861408961972, 0.008042630316, 0.008042635619}
fparams[23] = {-1.861408966082, -1.861408964884, 0.008042632526, 0.008042633433}

cparams = {
    {32,   64,   0,   0, 255,   0,   0},
    {32,  255,   0,   0, 255, 255,   0},
    {64,  255, 255,   0,   0,   0, 255},
    {128,   0,   0, 255, 255, 255, 255},
    {128, 255, 255, 255,   0,   0, 255},
    {128,   0,   0, 255, 255, 255,   0},
    {64,  255, 255,   0,  64,   0,   0}
}

global x_min, x_max, y_min, y_max, x_step, y_step, ctab = {}, cmax, cfak, itab, has_itab

sub main();                                             // Main
sub coltab(c, r0, g0, b0, r1, g1, b1);                  // Calculates a RGB color table
sub iterat();                                           // Calculates a fractal pixel per pixel
sub iterat(xs, ys, xo, yo, dobox, bw, bh);              // Calculates a fractal using a mosaic effect
sub loadmap(fname);                                     // Loads a Fractint 256 color map
sub save_mandel();                                      // Saves the current fractal to disk
sub all_mandels();                                      // Calculates all fractals using all color maps
sub calc_mandel();                                      // Calculates a fractal
sub create_colors();                                    // Create color table
sub init_itab();                                        // Initializes the iteration array
sub load_itab();                                        // Loads stored iteration data
sub save_itab();                                        // Saves iteration data
sub check_dirs();                                       // Creates missing directories
function itab_name();                                   // Creates an .iter filename
function mandel_name();                                 // Creates a .png filename
function map_name();                                    // Creates a .map name

main()

sub main()
    check_dirs()
    init_itab()

    if render_all then
        all_mandels()
    else
        load_itab()
        create_colors()
        calc_mandel()
        save_mandel()
        if !has_itab then
            save_itab()
        end
    end
end

sub create_colors()
    ctab = {}

    if mparams[map_type].map then
        loadmap(map_name())
        color_max = ctab[0]
    else
        for i = 0, arrcount(cparams) - 1 do
            coltab(cparams[i][0], cparams[i][1], cparams[i][2], cparams[i][3], cparams[i][4], cparams[i][5], cparams[i][6])
        end
    end

    cmax   = arrcount(ctab)
    cfak   = (cmax - 1) / (imax - 1)
end


sub calc_mandel()
    local t0, t1, time, min, sec

    graph::clear()

    x_min   = fparams[fnum][0]
    x_max   = fparams[fnum][1]
    y_min   = fparams[fnum][2]
    y_max   = fparams[fnum][3]

    x_step  = (x_max - x_min) / width
    y_step  = (y_min - y_max) / height

    t0      = timetick()

    if pixels then
        graph::statprint("Calculating ...");
        iterat()
    else
        graph::statprint("Pass  1/10");    iterat(8, 8, 0, 0, true,  8, 8)
        graph::statprint("Pass  2/10");    iterat(8, 8, 4, 4, true,  4, 4)
        graph::statprint("Pass  3/10");    iterat(8, 8, 0, 4, true,  4, 4)
        graph::statprint("Pass  4/10");    iterat(8, 8, 4, 0, true,  4, 4)
        graph::statprint("Pass  5/10");    iterat(4, 4, 2, 2, true,  2, 2)
        graph::statprint("Pass  6/10");    iterat(4, 4, 0, 2, true,  2, 2)
        graph::statprint("Pass  7/10");    iterat(4, 4, 2, 0, true,  2, 2)
        graph::statprint("Pass  8/10");    iterat(2, 2, 1, 1, false, 0, 0)
        graph::statprint("Pass  9/10");    iterat(2, 2, 0, 1, false, 0, 0)
        graph::statprint("Pass 10/10");    iterat(2, 2, 1, 0, false, 0, 0)
    end

    t1     = timetick();

    time   = t1 - t0
    min    = floor(time / 60000)
    sec    = floor((time - min * 60000) / 1000)

    graph::statprint("READY. Time: "..min..":"..strpadl(sec, 2, "0"))
end


sub coltab(c, r0, g0, b0, r1, g1, b1)
    local x, rs, gs, bs, r, g, b, d

    rs = (r1 - r0) / c
    gs = (g1 - g0) / c
    bs = (b1 - b0) / c
    r = r0
    g = g0
    b = b0
    for x = 1, c do
        d = arrcount(ctab)
        ctab[d] = {}
        ctab[d].r = r
        ctab[d].g = g
        ctab[d].b = b

        r = r + rs
        g = g + gs
        b = b + bs
    end
end

sub iterat()
    local x, y, vx, vy, i, zx, zy, tx, ty, tx1, ty1, col, cnum

    for y = 0, height - 1 do
        vy = y_max + y * y_step
        for x = 0, width - 1 do
            if has_itab then
                i = itab[x][y]
            else
                vx = x_min + x * x_step
                i = 0
                zx = 0
                zy = 0

                do
                    tx = zx + vx
                    ty = zy + vy
                    tx1 = tx * tx
                    ty1 = ty * ty
                    zx = tx1 - ty1
                    zy = (tx + tx) * ty
                    i = i + 1
                until (i == imax) || ((tx1 + ty1) > 4)
                itab[x][y] = i
            end

            if i == imax then
                col = color_max
            else
                if circular then
                    cnum = mod(i, cmax)
                    if cnum == 0 then
                        cnum = 1
                    end
                else
                    cnum = floor(i * cfak)
                end

                col = ctab[cnum]
            end

            graph::pset(x, y, col)

        end
        if has_itab then
            if !(y & 15) then
                graph::refresh()
            end
        else
            graph::refresh()
        end
    end
    graph::refresh()
end

sub iterat(xs, ys, xo, yo, dobox, bw, bh)
    local x, y, vx, vy, i, zx, zy, tx, ty, tx1, ty1, col, cnum

    y = yo

    do
        x = xo
        vy = y_max + y * y_step
        do
            if has_itab then
                i = itab[x][y]
            else
                vx = x_min + x * x_step

                i = 0
                zx = 0
                zy = 0

                do
                    tx = zx + vx
                    ty = zy + vy
                    tx1 = tx * tx
                    ty1 = ty * ty
                    zx = tx1 - ty1
                    zy = (tx + tx) * ty
                    i = i + 1
                until (i == imax) || ((tx1 + ty1) > 4)
                itab[x][y] = i
            end

            if i == imax then
                col = color_max
            else
                if circular then
                    cnum = mod(i, cmax)
                    if cnum == 0 then
                        cnum = 1
                    end
                else
                    cnum = floor(i * cfak)
                end

                col = ctab[cnum]
            end

            if dobox then
                graph::sbox(x, y, bw, bh, col)
            else
                graph::pset(x, y, col)
            end

            x = x + xs
        until x >= width
        y = y + ys
        graph::refresh()
    until y >= height
end


sub loadmap(fname)
    local str, str2, i, c = {0, 0, 0}, d, cc = 0

    if !fexists(fname) then
        error("File \'"..fname.."\' not found!")
    end

    file = fopen(fname, "r")

    while true do
        str = freadln(file)
        if str == null then
            break
        end

        tok = strtok(str, " ")

        d = 0

        for i = 0, arrcount(tok) - 1 do
            str2 = strtrim(tok[i])
            if strlen(str2) > 0 then
                c[d] = tonum(str2)
                d = d + 1
                if d == 3 then
                    break
                end
            end
        end

        ctab[cc] = {}
        ctab[cc].r = c[0]
        ctab[cc].g = c[1]
        ctab[cc].b = c[2]
        cc = cc + 1
    end

    fclose(file)
end

sub save_mandel()
    graph::savepng(mandel_name())
end

sub init_itab()
    local x
    itab = array(width)
    for x = 0, width - 1 do
        itab[x] = array(height)
    end
end

sub load_itab()
    local x, y, i, file

    if fexists(itab_name()) then
        has_itab = true
        file = gzip::open(itab_name(), "r")
        for y = 0, height - 1 do
            for x = 0, width - 1 do
                i = gzip::read(file) * 256
                i = i + gzip::read(file)
                itab[x][y] = i
            end
        end
        gzip::close(file)
    else
        has_itab = false
    end
end

sub save_itab()
    local file, y, x, i

    file = gzip::open(itab_name(), "w")

    for y = 0, height - 1 do
        for x = 0, width - 1 do
            i = itab[x][y]
            gzip::write(file, floor(i / 256))
            gzip::write(file, i & 255)
        end
    end

    gzip::close(file)
end

sub check_dirs()
    if !dexists("mandelbrot") then
        mkdir("mandelbrot")
    end
    if !dexists("mandelbrot\\iterations") then
        mkdir("mandelbrot\\iterations")
    end
end

sub all_mandels()
    for fnum = 0, arrcount(fparams) - 1 do
        has_itab = false

        for map_type = 0, arrcount(mparams) - 1 do
            if !overwrite && fexists(mandel_name()) then
                continue
            end

            if !has_itab then
                load_itab()
            end

            if !has_itab then
                pixels = false
            else
                pixels = true
            end

            create_colors()
            calc_mandel()
            save_mandel()

            if !has_itab then
                save_itab()
                has_itab = true
            end
        end
    end
end

function itab_name()
    return "mandelbrot\\iterations\\mandel_"..strpadl(fnum, 2, "0").."_"..width.."x"..height.."x"..imax..".iter"
end

function mandel_name()
    return "mandelbrot\\mandel_"..strpadl(fnum, 2, "0").."_"..mparams[map_type].name..".png"
end

function map_name()
    return pwinfract..mparams[map_type].name..".map"
end

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