' Use HUG by selective INCLUDE'ing the functions we need
INCLUDE "hug.bac", INIT, HUGOPTIONS, WINDOW, CANVAS, ATTACH, TIMEOUT, DISPLAY, PIXEL, DRAW, SYNC

' Initialize HUG
INIT

' Set OpenGL canvas
HUGOPTIONS("CANVAS GL")
HUGOPTIONS("GL_FREEZE 1")

' No scaling of the window
HUGOPTIONS("NOSCALING")

' Window size
CONST x_size = 400
CONST y_size = 400

' Array to calculate colors
DECLARE cols[3][256]

' Create the actual drawing
FUNCTION Draw_Plasma

    LOCAL x, y TYPE float
    LOCAL c, r, g, b

    ' As GL FREEZE option is enabled, we must prepare the canvas explicitly
    DRAW(canvas)

    FOR y = 1 TO y_size
        FOR x = 1 TO x_size

            c = FLOOR(SIN(x/35)*128 + SIN(y/28)*32 + SIN((x+y)/16)*64) & 255

            r = cols[0][c] & 255
            g = cols[1][c] & 255
            b = cols[2][c] & 255

            IF r < 16 THEN red$ = CONCAT$("0", HEX$(r))
            ELSE red$ = HEX$(r)

            IF g < 16 THEN green$ = CONCAT$("0", HEX$(g))
            ELSE green$ = HEX$(g)

            IF b < 16 THEN blue$ = CONCAT$("0", HEX$(b))
            ELSE blue$ = HEX$(b)

            PIXEL(CONCAT$("#", red$, green$, blue$), x, y)
        NEXT
    NEXT

    ' As GL FREEZE option is enabled, we must SYNC explicitly
    SYNC

    ' Do not call this function again
    RETURN FALSE

END FUNCTION

' Initialize colors
FOR i = 0 TO 255
    cols[0][i] = ABS(128 - 127 * SIN(i * PI / 32))
    cols[1][i] = ABS(128 - 127 * SIN(i * PI/ 64))
    cols[2][i] = ABS(128 - 127 * SIN(i * PI / 128))
NEXT

' Define GUI
win = WINDOW("Plasma Demo with HUG", x_size, y_size)
canvas = CANVAS(x_size, y_size)
ATTACH(win, canvas, 0, 0)

' Make sure to call the drawing function *after* the Window is displayed
TIMEOUT(50, Draw_Plasma)

' Display window
DISPLAY