//
// Heartbeat Clock
//

//
// Do setup.
//
func setup() {
    // Save the virtual screen size.
    screenWidth  = 640;
    screenHeight = 480;

    // Return the window configuration.
    return {
        width: screenWidth,
        height: screenHeight,
        title: "Heartbeat Clock",
        fullscreen: true
    };
}

//
// Do preparation.
//
func start() {
    // Save the start time.
    startTime = Engine.millisec;

    // Load the font.
    Engine.loadFont({
        slot: 0,
        file: "saira.ttf"
    });

    // Create the textures for the digit 0-9.
    digitTex = [];
    for (i in 0..10) {
        digitTex[i] = Engine.createTextTexture({
            slot: 0,
            text: "" + i,
            size: 100,
            r: 255,
            g: 255,
            b: 255,
            a: 255
        });
    }

    // Create the texture for colon.
    colonTex = Engine.createTextTexture({
        slot: 0,
        text: ":",
        size: 100,
        r:    255,
        g:    255,
        b:    255,
        a:    255
    });

    // Load the heart texture.
    heartTex = Engine.loadTexture({ file: "heart.png" });

    // Create the color texture.
    colorTex = Engine.createColorTexture({
        width:  screenWidth,
        height: screenHeight,
        r:      200,
        g:      200,
        b:      255,
        a:      255
    });
}

func update() {
}

func render() {
    var t = (Engine.millisec - startTime) * 0.001 * 3.14159265;

    // Draw the background.
    var alpha = 0.5 * Math.sin(0.15 * t);
    if (alpha < 0)
        alpha = -alpha;
    Engine.renderTexture3D({
        x1:        0,
        y1:        0,
        x2:        screenWidth,
        y2:        0,
        x3:        0,
        y3:        screenHeight,
        x4:        screenWidth,
        y4:        screenHeight,
        texture:   colorTex,
        srcLeft:   0,
        srcTop:    0,
        srcWidth:  colorTex.width,
        srcHeight: colorTex.height,
        alpha:     255 * alpha
    });

    // Draw the heartbeat.
    var scale = 0.2 * Math.sin(t);
    if (scale < 0)
        scale = -scale;
    var x = screenWidth / 2.0;
    var y = screenHeight / 2.0;
    var w = heartTex.width * scale;
    var h = heartTex.height * scale;
    Engine.renderTexture3D({
        x1: x - w / 2.0,
        y1: y - h / 2.0,
        x2: x + w / 2.0,
        y2: y - h / 2.0,
        x3: x - w / 2.0,
        y3: y + h / 2.0,
        x4: x + w / 2.0,
        y4: y + h / 2.0,
        texture:   heartTex,
        srcLeft:   0,
        srcTop:    0,
        srcWidth:  heartTex.width,
        srcHeight: heartTex.height,
        alpha:     255.0 * scale
    });

    // Draw time. HH:MM
    var date = Engine.getDate({});
    Engine.draw({
        texture: digitTex[date.hour / 10],    // H****
        x: 110,
        y: 100
    });
    Engine.draw({
        texture: digitTex[date.hour % 10],    // *H***
        x: 210,
        y: 100
    });
    Engine.draw({
        texture: colonTex,                    // **:**
        x: 310,
        y: 100
    });
    Engine.draw({
        texture: digitTex[date.minute / 10],  // ***M*
        x: 370,
        y: 100
    });
    Engine.draw({
        texture: digitTex[date.minute % 10],  // ****M
        x: 450,
        y: 100
    });
}
