#pragma once

class Render
{
public:
	glm::mat4 *view;
	glm::mat4 projection; 
	GLuint wIDTH;
	GLuint hEIGHT;
	unsigned int deltaTime = 0;
	float scale = 30;
	float ratioWindow;
	GLFWwindow* window;
	void(*process)();
	std::vector<Sprite> Sprites;
	Sprite *objectMap[2000][100][4];

	Render(GLuint WIDTH, GLuint HEIGHT)
	{

		// размеры окна
		HDC hDCScreen = GetDC(NULL);
		int Horres = GetDeviceCaps(hDCScreen, HORZRES);
		int Vertres = GetDeviceCaps(hDCScreen, VERTRES);
		ReleaseDC(NULL, hDCScreen);

		wIDTH = Horres;
		hEIGHT = Vertres;
		ratioWindow = (float(wIDTH) / float(hEIGHT));

		// инициализация OPENGL
		glfwInit();
		glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
		glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
		glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
		glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
		window = glfwCreateWindow(wIDTH, hEIGHT, "Game One", glfwGetPrimaryMonitor(), NULL);
		//window = glfwCreateWindow(wIDTH, hEIGHT, "Test", nullptr, nullptr);
		glfwMakeContextCurrent(window);
		glewExperimental = GL_TRUE;
		glewInit();
		glViewport(0, 0, wIDTH, hEIGHT);
		// пармаетры Opengl
		glEnable(GL_DEPTH_TEST);
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

		// Проекция
		projection = glm::scale(glm::vec3(1.0f / scale, ratioWindow / scale, 1.0f));
	}

	void renderStart()
	{
		unsigned int start_time;
		unsigned int end_time;

		while (!glfwWindowShouldClose(window))
		{
			start_time = clock(); // начальное время
			process();
			glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

			for (int i = 0; i < Sprites.size(); i++)
			{
				glUseProgram(Sprites[i].shader);
				glBindVertexArray(Sprites[i].meshSprite);
				glBindTexture(GL_TEXTURE_2D, Sprites[i].texture);
				glUniformMatrix4fv(glGetUniformLocation(Sprites[i].shader, "location"), 1, GL_FALSE, glm::value_ptr(Sprites[i].spriteLocations));
				glUniformMatrix4fv(glGetUniformLocation(Sprites[i].shader, "view"), 1, GL_FALSE, glm::value_ptr(*view));
				glUniformMatrix4fv(glGetUniformLocation(Sprites[i].shader, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
				glDrawArrays(GL_TRIANGLES, 0, 6);
			}

			glBindVertexArray(0);
			glfwSwapBuffers(window);

			end_time = clock(); // конечное время
			deltaTime = end_time - start_time;
		}
	}

	void objectMapStart()
	{
		for (int i = 0; i < Sprites.size(); i++)
		{

		}
	}
};