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

' Initialize HUG
INIT

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

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

CONST XSize = 640
CONST YSize = 480

CONST message$ = "S T A R S"

DECLARE x[501], y[501], z[501] TYPE int

' The function which is called to draw the scene
FUNCTION Draw_Stars

        LOCAL i

        ' Calculate next position of stars
        FOR i = 0 TO 500
                x[i] = x[i] - z[i]
                IF x[i] < 0 THEN x[i] = x[i] + XSize
        NEXT

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

        ' We have a black space
        SQUARE("#000000", 0, 0, XSize, YSize, TRUE)

        ' Now actually draw the stars
        FOR i = 0 TO 500
                PIXEL("#FFFFFF", x[i], y[i])
        NEXT

        ' Show the message. OpenGL letters from OUT always are 8 pixels wide
        OUT(message$, "#FFFF00", "#000000", XSize/2-(LEN(message$)*8)/2, YSize/2)

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

        ' Pressing a key should end the demo
        IF KEY() THEN QUIT

        ' Run this Draw_Stars function again for star animation
        RETURN TRUE

END FUNCTION

' Initialize star positions
FOR i = 0 TO 500
        x[i] = RANDOM(XSize)
        y[i] = RANDOM(YSize)
        z[i] = RANDOM(8)+1
NEXT

' Define GUI
win = WINDOW("Stars Demo with HUG", XSize, YSize)
canvas = CANVAS(XSize, YSize)
ATTACH(win, canvas, 0, 0)

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

' Display window
DISPLAY