Tag Archive for 4dgl monitor

µVGA II – Field of Stars

This is a remake of the windows screensaver starfield, and it is far from perfect but a good intermidiate program to learn how to program for the µVGA II [µVGA 2, uVGA II, uVGA 2] in 4DGL.

The μVGA-II(GFX) module is a compact and cost effective standalone VGA graphics engine powered by the PICASO-GFX graphics controller. It can provide QVGA/VGA/WVGA graphics solution to any embedded project with its powerful graphics, text, image, animation and countless more features built inside the module.

#platform "uVGA-II_GFX2"
#inherit "4DGL_16bitColours.fnc"

#constant count  180
var xL[count], yL[count];   // list of x and l locations of stars
var xOL[count], yOL[count]; // list of x and y offset of stars

var starColor := WHITE;

func main()
    var c := 0;     // used to navigate through the arrays
    var r[count];   // radius of stars

    // set the start location of the stars
    while(c < count)
        xL[c] := (RAND() % 40) + 320;
        yL[c] := (RAND() % 20) + 240;
        c++;
    wend

    c := 0;
    // set the start radius to 20, but in reality it is 1 because it gets devided by 20
    while(c < count)
        r[c] := 20;
        c++;
    wend

    c := 0;
    // determine the apropriate offset directions depending on which quadrant the star is in
    while(c < count)
        while(xOL[c] == 0)
            if(xL[c] < 320)
                xOL[c] := RAND() % 10;
                xOL[c] := ABS(xOL[c]);
                xOL[c] := -xOL[c];
            else
                xOL[c] := RAND() % 10;
                xOL[c] := ABS(xOL[c]);
            endif
        wend

        while(yOL[c] == 0)
            if(yL[c] < 240)
                yOL[c] := RAND() % 10;
                yOL[c] := ABS(yOL[c]);
                yOL[c] := -yOL[c];
            else
                yOL[c] := RAND() % 10;
                yOL[c] := ABS(yOL[c]);
            endif
        wend
        c++;
    wend

    c := 0;
    // main loop
    while(1)
        gfx_RectangleFilled(xL[c], yL[c], xL[c] + r[c] / 20, yL[c] + r[c] / 20, starColor);

        // if star goes out of hte screen
        if( r[c] > 100 || xL[c] < 0 || yL[c] < 0 || yL[c] > 480 || xL[c] > 640)
            gfx_RectangleFilled(xL[c], yL[c], xL[c] + r[c] / 20, yL[c] + r[c] / 20, BLACK); // erase one last time

            // set new location
            xL[c] := (RAND() % 40) + 320;
            yL[c] := (RAND() % 20) + 240;

            // reset radius and offset
            xOL[c] := 0;
            yOL[c] := 0;
            r[c] := 20;

            // determine new offset
            while(xOL[c] == 0)
                if(xL[c] < 320)
                    xOL[c] := RAND() % 10;
                    xOL[c] := ABS(xOL[c]);
                    xOL[c] := -xOL[c];
                else
                    xOL[c] := RAND() % 10;
                    xOL[c] := ABS(xOL[c]);
                endif
            wend

            while(yOL[c] == 0)
                if(yL[c] < 240)
                    yOL[c] := RAND() % 10;
                    yOL[c] := ABS(yOL[c]);
                    yOL[c] := -yOL[c];
                else
                    yOL[c] := RAND() % 10;
                    yOL[c] := ABS(yOL[c]);
                endif
            wend
        endif

        // move to next star
        c++;
        // set the radius randomly from 0 to 4
        r[c] := r[c] + ABS(RAND() % 4);
        if(c == count)
            c := 0;
        endif

        // erase and offset
        gfx_RectangleFilled(xL[c], yL[c], xL[c] + r[c] / 20, yL[c] + r[c] / 20, BLACK);
        xL[c] := xL[c] + xOL[c];
        yL[c] := yL[c] + yOL[c];

        //gfx_Hline(240, 0, 640, GREEN);
        //gfx_Vline(320, 0, 480, GREEN);

    wend
endfunc