
#include <Spectre/Graphics.h>
#include <Spectre/Graphics/OpenGL.h>

Graphics::Graphics(PlatformApplication *platform)
{
	m_width = 800;
	m_height = 600;

	m_display = new Display();
}

Graphics::~Graphics()
{
	shutdown();
	delete m_display;
}

bool Graphics::init()
{
	DisplayMode mode(m_width, m_height);
	DisplayDescription desc(mode);

	desc.decoration = DisplayDecorate::Menu | DisplayDecorate::Close | DisplayDecorate::Resize;

	m_display->create(desc);

	setClearColor(0.0f, 0.0f, 0.0f);

	swapBuffers();

	return true;
}

void Graphics::shutdown()
{
	m_display->destroy();
}

std::string Graphics::getVersion() const
{
	char buf[512];

	char *ver = (char*) glGetString(GL_VERSION);
	char *ven = (char*) glGetString(GL_VENDOR);
	char *ren = (char*) glGetString(GL_RENDERER);
	char *prof = "Compability";
	GLint flags;

	glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &flags);
	if (flags & GL_CONTEXT_CORE_PROFILE_BIT) {
		prof = "Core";
	}

	snprintf(buf, sizeof(buf), "OpenGL %s %s profile - %s %s", ver, prof, ren, ven);

	return std::string(buf);
}

void Graphics::setDisplayMode(Display::Mode mode)
{
	m_display->setVideoMode(mode);
}

void Graphics::setSize(int width, int height)
{
	m_display->setSize(width, height);
}

void Graphics::setViewport(int x, int y, int width, int height)
{
	glViewport(x, y, width, height);
}

void Graphics::setClearColor(float r, float g, float b)
{
	glClearColor(r, g, b, 1.0f);
}

void Graphics::clearBuffer()
{
	glClear(GL_COLOR_BUFFER_BIT);
}

void Graphics::swapBuffers()
{
	m_display->swapBuffers();
}
