#include "Globals.h"
#include "Batch.h"
#include "Rabbit.h"
#include "Level.h"
#include "FjordsSound.h"
int WINDOW_SCALE;
void playLevelMusic(unsigned int type)
{
	if (type == RT_NULL)
	{
		ss_setvol(music_null,5000);
		ss_loopsound(music_null);
	}
	else if (type == RT_UNDERGROUND)
	{
		ss_setvol(music_underground,7500);
		ss_loopsound(music_underground);
	}
	else if (type == RT_MAGIC)
	{
		ss_setvol(music_magic,7500);
		ss_loopsound(music_magic);
	}
	else if (type == RT_OUTSIDE)
	{
		ss_setvol(music_outside,7500);
		ss_loopsound(music_outside);
	}
}

void stopAllMusic()
{
	ss_stopsound(music_null);
	ss_stopsound(music_underground);
	ss_stopsound(music_magic);
	ss_stopsound(music_outside);
}

void ss_playsound_distort(double soundid)
{
	float pitch[] = {44100 * .9f, 44100, 44100 * 1.1f};
	ss_setfreq(soundid,pitch[rand()%3]);
	ss_playsound(soundid);
}

void ss_playsound_distort_vector(std::vector<double> *vec)
{
	if (vec != NULL)
		if (vec->size() > 0)
			ss_playsound_distort(vec->at(rand()%vec->size()));
}

void ss_playsound_vector(std::vector<double> *vec)
{
	if (vec != NULL)
		if (vec->size() > 0)
			ss_playsound(vec->at(rand()%vec->size()));
}




Batch *textbatch;

void draw_rectangle(int x, int y, int w, int h)
{
	int L = x;
	int R = x+w;
	int T = y;
	int B = y+h;
	textbatch->begin();
		textbatch->addUVTile(L,T,0,L+6,T+6,0,0xFFFFFFFF,0.f,160.f/256.f,6.f/256.f,6.f/256.f);
		textbatch->addUVTile(L+6,T,0,R-6,T+6,0,0xFFFFFFFF,6.f/256.f,160.f/256.f,4.f/256.f,6.f/256.f);
		textbatch->addUVTile(R-6,T,0,R,T+6,0,0xFFFFFFFF,10.f/256.f,160.f/256.f,6.f/256.f,6.f/256.f);

		textbatch->addUVTile(L,B-6,0,T+6,B,0,0xFFFFFFFF,0.f,170.f/256.f,6.f/256.f,6.f/256.f);
		textbatch->addUVTile(L+6,B-6,0,R-6,B,0,0xFFFFFFFF,6.f/256.f,170.f/256.f,4.f/256.f,6.f/256.f);
		textbatch->addUVTile(R-6,B-6,0,R,B,0,0xFFFFFFFF,10.f/256.f,170.f/256.f,6.f/256.f,6.f/256.f);

		textbatch->addUVTile(L,T+6,0,L+6,B-6,0,0xFFFFFFFF,0.f/256.f,166.f/256.f,6.f/256.f,4.f/256.f);
		textbatch->addUVTile(R-6,T+6,0,R,B-6,0,0xFFFFFFFF,10.f/256.f,166.f/256.f,6.f/256.f,4.f/256.f);
	textbatch->finish();
	matrixBegin();
	matrixUpload();
	textbatch->draw();
}
void draw_text(int x, int y, std::string text, int tricount)
{
	textbatch->begin();
	int xx = 0;
	int yy = 0;
	int c;
	for(int i=0;i<text.size();i++)
	{
		c = text.at(i);
		if (c == '`')
		{
			int prevc = c;
			c = 0xFF;
			textbatch->addUVTile(10*xx,10*yy,0,10*(xx+1),10*(yy+1),0,0xFFFFFFFF,10.f/256.f*(c % 16),10.f/256.f*(c / 16),10.f/256.f,10.f/256.f);
			c = prevc;
			continue;
		}
		if (c == 13)
			if (i+1 < text.size())
				if (text.at(i+1) == 10)
				{
					int prevc = c;
					c = 0xFF;
					textbatch->addUVTile(10*xx,10*yy,0,10*(xx+1),10*(yy+1),0,0xFFFFFFFF,10.f/256.f*(c % 16),10.f/256.f*(c / 16),10.f/256.f,10.f/256.f);
					c = prevc;
					xx = 0;
					yy++;
					i++;
					continue;
				}
		textbatch->addUVTile(10*xx,10*yy,0,10*(xx+1),10*(yy+1),0,0xFFFFFFFF,10.f/256.f*(c % 16),10.f/256.f*(c / 16),10.f/256.f,10.f/256.f);
		xx++;
	}
	textbatch->finish();
	matrixBegin();
	matrixTranslate(x,y,0);
	matrixUpload();
	textbatch->draw(tricount);
}





Wumbo::Texture *tex;
Wumbo::Texture *title;
Wumbo::Texture *white;
Wumbo::Shader *megaShader;

GLvoid glClearColor(unsigned int color)
{
	glClearColor(((color & 0x00FF0000) >> 16)/255.f,((color & 0x0000FF00) >> 8)/255.f, ((color & 0x000000FF) >> 0)/255.f,((color & 0xFF000000) >> 24)/255.f);
}

GLint matrixUniformID = 0;
glm::mat4x4 VIEW, PROJ;
glm::mat4x4 glmat;
GLvoid matrixBegin()
{
	glmat = glm::mat4x4(1.f) * PROJ * VIEW;
}
GLvoid matrixRotate(float degrees, float x, float y, float z)
{
	glmat *= glm::rotate(glm::mat4x4(1.f),degrees, glm::vec3(x,y,z));
}
GLvoid matrixTranslate(float x, float y, float z)
{
	glmat *= glm::translate(glm::mat4x4(1.f),glm::vec3(x,y,z));
}
GLvoid matrixScale(float x, float y, float z)
{
	glmat *= glm::scale(glm::mat4x4(1.f),glm::vec3(x,y,z));
}
GLvoid matrixUpload()
{
	glUniformMatrix4fv(matrixUniformID, 1, GL_TRUE, glm::value_ptr(glmat));
}

int degreemod(int degrees)
{
	degrees %= 360;
	if (degrees < 0)
		degrees += 360;
	return degrees;
}