/src/BeatChart.cpp
#include "BeatChart.h"

#include <GLES2/gl2.h>
#include <string.h>
#include "ShaderDepot.h"

BeatChart::BeatChart(Clock* clock) : Component() {
	this->clock = clock;
	this->beats_per_measure = 4;
	this->beat = 0;
	this->beat_start = 0;
	setSize(320, 10);
	this->shader = (RShader*) loadShader("RShader");
}

BeatChart::~BeatChart(void) {
}

void BeatChart::setSize(int w, int h) {
	Component::setSize(w, h);

	blip_w = (float)w / (float)this->beats_per_measure;
	blipVertices[0] = 0;
	blipVertices[1] = 0;
	blipVertices[2] = (GLshort)blip_w;
	blipVertices[3] = 0;
	blipVertices[4] = (GLshort)blip_w;
	blipVertices[5] = h;
	blipVertices[6] = 0;
	blipVertices[7] = h;
}

void BeatChart::draw(int t) {
	shader->useProgram();
	int beat = clock->getBeat();
	if (beat != this->beat) {
		this->beat = beat;
		this->beat_start = t;
	}
	int delta = t - this->beat_start;
	this->fade_time = (int)(60000.0 / clock->getBPM());
	if (delta < this->fade_time) {
		shader->setFade(1.0f - (GLfloat)delta / (GLfloat)this->fade_time);
	} else {
		shader->setFade(0.0f);
	}
	shader->setColor(0, 0, 0, 1);
	shader->setFadeColor(0.75, 0.75, 0.75, 1.0);
	shader->setPositions(4, this->blipVertices);
	shader->setTranslation(this->x + (int)(blip_w * (beat % this->beats_per_measure)), this->y);
	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

	shader->resetTransform();
}