
#include <iostream>

#include <glad/glad.h>
#define STB_IMAGE_IMPLEMENTATION
#include <stb/stb_image.h>

class Texture
{
public:
	unsigned int ID;
	int width, height, nrChannels;
	
	Texture(const char* texturePath, bool flip = false)
	{
		// Create the Texture and set our ID
		glGenTextures(1, &ID);
		glBindTexture(GL_TEXTURE_2D, ID);
		
		// set the texture wrapping/filtering options (on the currently bound texture object)
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);	
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		
		// Grab the texture from our path
		if (flip)
			stbi_set_flip_vertically_on_load(true);
		
		unsigned char *data = stbi_load(texturePath, &width, &height, &nrChannels, 0);
		if (data)
		{
			GLenum format = (nrChannels == 4) ? GL_RGBA : GL_RGB;
			glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
			glGenerateMipmap(GL_TEXTURE_2D);
		}
		else
		{
			std::cout << "Failed to load texture" << std::endl;
		}
		
		if (flip)
			stbi_set_flip_vertically_on_load(false);
		
		stbi_image_free(data);
	}
	
	void Draw() const
	{
		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D, ID);
	}
};
