#define I_AM_THE_PROGRAM 1
#include "LD29Program.h"
#include "Wumbo-InputManager.h"
#include "Wumbo-Renderer.h"
#include "Wumbo-Shader.h"
#include "Wumbo-ShaderBuilder.h"
#include "Wumbo-Quicky.h"
#include "GM.h"
#include "Wumbo-Header.h"
#include "Wumbo-Texture.h"
#include "Globals.h"
#include "FjordsSound.h"
#include "Title.h"
#include "Credits.h"

#define BENCHMARK 0

Wumbo::Program *WUMBOFRAMEWORK_GETPROGRAM()
{
	static Wumbo::Program *p = new LD29Program();
	return p;
}

void LD29Program::begin()
{
	WINDOW_SCALE = 1;
	FILE *f = fopen("options.txt","rb");
	if (f != NULL)
		if (fgetc(f) == '2')
			WINDOW_SCALE = 2;
	fclose(f);
	textbatch = new Batch();
	ss_init();

	music_null = ss_loadsound("Audio/music_null.ogg");
	music_underground = ss_loadsound("Audio/music_underground.ogg");
	music_magic = ss_loadsound("Audio/music_magic.ogg");
	music_outside = ss_loadsound("Audio/music_outside.ogg");

	snd_voice_scribble.push_back(ss_loadsound("Audio/vo_scribble_1.ogg"));
	snd_voice_scribble.push_back(ss_loadsound("Audio/vo_scribble_2.ogg"));

	snd_voice_default.push_back(ss_loadsound("Audio/vo_default_1.ogg"));

	snd_voice_bird.push_back(ss_loadsound("Audio/vo_bird_1.ogg"));

	snd_hop.push_back(ss_loadsound("Audio/hop_1.ogg"));
	snd_hop.push_back(ss_loadsound("Audio/hop_2.ogg"));

	snd_fall.push_back(ss_loadsound("Audio/fall1.ogg"));
	snd_fall.push_back(ss_loadsound("Audio/fall2.ogg"));
	snd_hardland.push_back(ss_loadsound("Audio/land1.ogg"));
	snd_whoosh.push_back(ss_loadsound("Audio/whoosh1.ogg"));
	snd_whoosh.push_back(ss_loadsound("Audio/whoosh2.ogg"));

	srand(time(NULL));

	Wumbo::Program::begin();
	// build the shader
	Wumbo::ShaderBuilder builder;
	builder.loadVertexShader(std::string("Graphics/Shader29.vsh"));
	builder.loadFragmentShader(std::string("Graphics/Shader29.fsh"));
	builder.addAttrib(Wumbo::WumboVertexAttribPosition, "position");
	builder.addAttrib(Wumbo::WumboVertexAttribColor, "color");
	builder.addAttrib(Wumbo::WumboVertexAttribTexCoord0, "texcoord0");
	builder.addAttrib(Wumbo::WumboVertexAttribNormal, "normal");
	builder.addUniform("supermatrix");
	megaShader = builder.generate();
	if (megaShader == NULL)
	{
		megaShader = new Wumbo::Shader();
		printf("megashader is null");
	}
	program = megaShader->getProgramID();
	matrixUniformID = megaShader->getUniformIDFromName("supermatrix");
	printf("program is %i\n",program);
	// initialize input
	inputManager = new Wumbo::InputManager(this);
	inputManager->initialize();
	inputManager->setReceiveKeyboardUpdates(true);
	inputManager->setReceiveXbox360Updates(true);
	inputManager->setReceiveTouchUpdates(true);
	inputManager->setReceiveAccelerometerUpdates(true);
	inputManager->setReceiveMouseUpdates(true);
	
	// set up the renderer
	//if (!Wumbo::OpenGL2())
	//	renderer = new Wumbo::Renderer(this,1);
	//else
	renderer = new Wumbo::Renderer(this,2);
	renderer->setRotation(Wumbo::ScreenRotation_DOWN);
	renderer->resetVirtualRenderTarget();
#if BENCHMARK 
	setAssignedFrameRate(10000);
#else
	setAssignedFrameRate(30);
#endif
	renderer->setTitle("MORTON");

	GM::gm_wumbo_init(this);
	Wumbo::Quicky_SetProgram(this);
	
	// set the display size (the size of the window)
#if PLATFORM_IOS
	if (Wumbo::Platform_iOS::iPad())
		renderer->setDisplaySize(768,1024);
	else if (Wumbo::Platform_iOS::FourInch())
		renderer->setDisplaySize(320,568);
	else
		renderer->setDisplaySize(320,480);
#else
	renderer->setDisplaySize(WINDOW_WIDTH*WINDOW_SCALE,WINDOW_HEIGHT*WINDOW_SCALE);
#endif
	// and center the window on the screen. haha if I don't do this
	// then the window ends up in crazy places
	renderer->centerDisplayOnScreen();
	
	(title = new Wumbo::Texture())->load("Graphics/title.png");
	(tex = new Wumbo::Texture())->load("Graphics/texture.png");
	(white = new Wumbo::Texture())->load("Graphics/white.png");


	//setScene(new Level(this,"void_1",-1,-1,false,-1));

	setScene(new Title(this));
	//setScene(new Credits(this));

	/*potato = new Batch();
	potato->begin();
		potato->addCube(8,-1.5f,-1.5f,11,1.5f,1.5f,SHARECART_GREEN);
		potato->addCube(5,-1,-1,8,1,1,SHARECART_GREEN);
		potato->addCube(-0,-2,-2,5,2,2,SHARECART_RED);
		potato->addCube(-5,-1.5f,-1.5f,0,1.5f,1.5f,SHARECART_RED);
	potato->finish();

	rabbit = new Rabbit();
	rabbit->init();*/
}

void LD29Program::render()
{
#if BENCHMARK 
	static clock_t last = clock();
	clock_t now = clock();
	printf("TICK %ims\n",(now-last));
	last = now;
#endif
	if (renderer->getGLVersion() == 2)
		glUseProgram(program);
	Wumbo::Program::render();
}

void LD29Program::update()
{
	Wumbo::Program::update();
}

void LD29Program::end()
{
	Wumbo::Program::end();
#if PLATFORM_PC
	delete sfmlwindow;
#endif
}