// Show a few simple splines using the splines library

import "splines"
import "bitmap2"
import "time"
import "print"
import "c64"

const char* PRINT_SCREEN = 0x0400;
const char* BITMAP_SCREEN = 0x5c00;
const char* BITMAP_GRAPHICS = 0x6000;

struct Segment {
    enum SegmentType { MOVE_TO, SPLINE_TO, LINE_TO} type;
    struct SplineVector16 to;
    struct SplineVector16 via;
};

// True type letter c
struct Segment[21] letter_c;

void main() {
    letter_c[0] = { MOVE_TO, {108,146}, {0,0} };
    letter_c[1] = { SPLINE_TO, {89,182}, {103,169} };
    letter_c[2] = { SPLINE_TO, {59,195}, {75,195} };
    letter_c[3] = { SPLINE_TO, {23,178}, {38,195} };
    letter_c[4] = { SPLINE_TO, {9,132}, {9,161} };
    letter_c[5] = { SPLINE_TO, {25,87}, {9,104} };
    letter_c[6] = { SPLINE_TO, {65,69}, {42,69} };
    letter_c[7] = { SPLINE_TO, {93,79}, {82,69} };
    letter_c[8] = { SPLINE_TO, {105,98}, {105,88} };
    letter_c[9] = { SPLINE_TO, {102,106}, {105,103} };
    letter_c[10] = { SPLINE_TO, {93,109}, {98,109} };
    letter_c[11] = { SPLINE_TO, {81,104}, {85,109} };
    letter_c[12] = { SPLINE_TO, {78,93}, {79,101} };
    letter_c[13] = { SPLINE_TO, {73,82}, {78,86} };
    letter_c[14] = { SPLINE_TO, {61,78}, {69,78} };
    letter_c[15] = { SPLINE_TO, {40,88}, {48,78} };
    letter_c[16] = { SPLINE_TO, {29,121}, {29,100} };
    letter_c[17] = { SPLINE_TO, {40,158}, {29,142} };
    letter_c[18] = { SPLINE_TO, {68,174}, {50,174} };
    letter_c[19] = { SPLINE_TO, {91,166}, {80,174} };
    letter_c[20] = { SPLINE_TO, {104,144}, {98,160} };
    letter_c[21] = { LINE_TO, {108,146}, {0,0} };

    bitmap_init(BITMAP_GRAPHICS, BITMAP_SCREEN);
    bitmap_clear(BLACK, WHITE);
    vicSelectGfxBank(BITMAP_SCREEN);
    *D018 = toD018(BITMAP_SCREEN, BITMAP_GRAPHICS);
    *D011 = VIC_BMM|VIC_DEN|VIC_RSEL|3;

    struct SplineVector16 current = {0,0};
    for( byte i: 0..sizeof(letter_c)/sizeof(struct Segment)) {
        struct Segment segment = letter_c[i];
        if(segment.type==MOVE_TO) {
            current = segment.to;
        } else if(segment.type==SPLINE_TO) {
            spline_8segB(current, segment.via, segment.to);
            bitmap_plot_spline_8seg();
            current = segment.to;
        } else {
            bitmap_line((unsigned int)current.x, (unsigned int)current.y, (unsigned int)segment.to.x, (unsigned int)segment.to.y);
            current = segment.to;
        }
    }

    while(true) { (*(PRINT_SCREEN+999))++; }
}

// Plot the spline in the SPLINE_8SEG array
void bitmap_plot_spline_8seg() {
    struct SplineVector16 current = SPLINE_8SEG[0];
    for(char n:1..8) {
        bitmap_line((unsigned int)current.x, (unsigned int)current.y, (unsigned int)SPLINE_8SEG[n].x, (unsigned int)SPLINE_8SEG[n].y);
        //bitmap_plot((unsigned int)current.x, (unsigned char)current.y);
        current = SPLINE_8SEG[n];
    }
}
