#include "Reassembly2.h"

#include <GL/gl.h>
#include <iostream>

Reassembly2::~Reassembly2() {
	std::cout << "Game got destroyed!" << std::endl;
	delete assets;
	delete obj;
	delete inputHandler;
	for(unsigned int i = 0; i<rainDrops.size();i++) {
		delete rainDrops[i];
	}
}

void Reassembly2::create () {
	std::cout << "Hello World!" << std::endl;
	assets = new Assets();
	assets->loadTexture("bucket.png",false,"bucket");
	assets->loadTexture("drop.png",false,"drop");
	assets->loadShader("vert.shader","frag.shader",nullptr,"defaultShader");
	assets->loadShader("bitmapvert.shader","bitmapfrag.shader",nullptr,"bitmapShader");

	obj = new GameObject();
	inputHandler = new InputHandler();
	font = new BitmapFont("arial.ttf");

	glClearColor(1.f,1.f,1.f,1.f);
	spawnRainDrop();
}

void Reassembly2::loop(SDL_Event *event, unsigned int delta) {
	static unsigned int accumulator = 0, ups = 0, fps = 0, acc = 0;
	accumulator += delta;
	acc += delta;

	while(accumulator >= 17) {
		accumulator -= 17;
		update(event, delta);
		ups++;
	}

	if(acc >= 1000) {
		std::cout << "Updates per second: " << ups << " Frames per Second: " << fps << std::endl;
		acc -= 1000;
		ups = 0;
		fps = 0;
	}
	render(delta);
	fps++;
}

void Reassembly2::update(SDL_Event *event, unsigned int delta) {
	inputHandler->handleInput(event);
}

void Reassembly2::render(unsigned int delta) {
	glClear(GL_COLOR_BUFFER_BIT);

	//Drawing
	Shader defaultShader = assets->getShader("defaultShader");
	defaultShader.Use();
	//Draw Triangle	
	obj->draw();

	//texttest
	Shader bitmapShader = assets->getShader("bitmapShader");
	font->drawText(bitmapShader, "Hallo du noob", 25.0f, 25.0f, 1.f, glm::vec3(0.5f,0.8f,0.2f));

	SDL_GL_SwapWindow(gWindow);
}

void Reassembly2::resize(int width, int height){
}

void Reassembly2::spawnRainDrop() {
	SDL_Rect *rainDrop = new SDL_Rect;
	rainDrop->x = 100;
	rainDrop->y = 100;
	rainDrop->w = 64;
	rainDrop->h= 64;
	rainDrops.push_back(rainDrop);
}
