: AyaanScript+ — Auto-adaptive 3D
: Uses r3d_* commands which transparently switch between GPU and CPU based
: on measured frame rate. Script writer doesn't need to think about which
: backend is in use — the runtime handles it.

const W = 800
const H = 800
r3d_init W H "AyaanScript+ - Auto Adaptive"

set ay = 0
set running = 1
set frame = 0

while running == 1
    r3d_open running
    if running == 0
        break
    endif

    r3d_clear "#0a0a25"
    set ay = {ay + 0.018}
    r3d_rotate 0.4 ay 0
    r3d_camera 6

    : draw a 3x3 grid
    set i = 0
    repeat 3 i
        set j = 0
        repeat 3 j
            set x = {(i - 2) * 1.7}
            set y = {(j - 2) * 1.7}
            set hue = {(i + j * 3) * 40}
            hsv_to_rgb hue 0.85 1.0 -> col
            r3d_cube x y 0 1.2 col
        endrepeat
    endrepeat

    r3d_swap

    set frame = {frame + 1}
    if frame > 600
        set running = 0
    endif
endwhile

r3d_close
r3d_mode -> finalmode
psyska -/ "Final backend: {finalmode}"
psyska -/ "Rendered {frame} frames"
