/*
**  Cube.c
**
**  LDJ v1.0 6/05/06
*/
#include <p24fj128ga010.h>
#include <stdlib.h>
#include "Geometry.h"
#include "GraphicDB.h"

#define SIDE    66

// list of poligon vertexes
const point list[] = { 
    {-50, 40,  0,  1},    // 2 top
    {-45, 50,  0,  1},
    {-30, 50,  0,  1},
    {-20, 40,  0,  1},
    {-20, 20,  0,  1},
    {-30, 10,  0,  1},
    {-45, 10,  0,  1},  
    {-50,  0,  0,  1},
    {-50,-20,  0,  1},
    {-20,-20,  0,  1},
    {  0,  0,  0,  0},  // end of vertexes series 
    
    { 20, 50,  0,  1},  // 4
    { 20, 10,  0,  1},  
    { 50, 10,  0,  1},  
    {  0,  0,  0,  0},

    { 40, 30,  0,  1},  
    { 40,-20,  0,  1},  
    { 40, 30,  0,  1},  
    {  0,  0,  0,  0},

    {-SIDE,-SIDE,  0,  1}, // xy side
    {-SIDE, SIDE,  0,  1},
    { SIDE, SIDE,  0,  1},
    { SIDE,-SIDE,  0,  1},
    {-SIDE,-SIDE,  0,  1},
    {  0,  0,  0,  0},  // end of vertexes series 

    {-SIDE,-SIDE,   0,  1}, // xz side
    {-SIDE,-SIDE, SIDE,  1},
    { SIDE,-SIDE, SIDE,  1},
    { SIDE,-SIDE,   0,  1},
    {-SIDE,-SIDE,   0,  1},
    {  0,  0,   0,  0},  // end of vertexes series 

    {-SIDE,-SIDE,  0,  1}, // yz side
    {-SIDE,-SIDE,SIDE,  1},
    {-SIDE, SIDE,SIDE,  1},
    {-SIDE, SIDE,  0,  1},
    {-SIDE,-SIDE,  0,  1},
    {  0,  0,  0,  0},  // end of vertexes series 

    {  0,  0,  0,  0}   // end of list
};

// world transformation matrix
tmatrix tmWorld; 
   
int main()
{
    int i, a, b, g, x0, y0, z0, r, j;
    
    // init angles and offsets
    x0 = y0 = 200;
    z0 = 0;
    a = b = g = 0;

    // init editing function
    i = 0;
    TRISA = 0xfff0;
    T1CON = 0x8030;
    srand( 12);

    initVideo();
    clearAScreen();
        
    // 3. main loop
    while( 1)
    {
        // pick random movements
        r = (rand() % 2)? 1:-1;         // random verse
        i = ( rand() % 3);              // random axis
        j = 5 + (rand() % 32);          // random rotation
        // animate
        while ( j-- > 0)
        {  
            clearHScreen();              // clear the screen
            // draw on hidden screen
            initWorld( tmWorld, a, b, g, x0, y0, z0);
            drawPoligons( tmWorld, (point *)list);
            // make drawing screen visible
            swapV();

            //perform the update
            switch ( i){
            case 0:
                a += r;
                break;
            case 1: 
                b += r;
                break;
            case 2:
                g += r;
                break;
            } // switch
        } // while animation           
    } // main loop
} // main
