
#include <Spectre/Display/Display.h>
#include <Spectre/Graphics/BatchRenderer2D.h>
#include <Spectre/System/MessageHandler.h>
#include <Spectre/System/Log.h>
#include "DisplayExample.h"

void DisplayExample::init()
{
	m_renderer = new sp::BatchRenderer2D();
	m_mode = sp::Display::WINDOWED;
	getMessageHandler()->registerListener(this);

	m_texture.create("assets/textures/tux.png");

	m_sprite.setTexture(m_texture);
	m_sprite.setPosition(sp::vec2f(50, 50));
	m_sprite.setSize(sp::vec2f(100, 100));

	m_renderer->setCamera(m_camera);
}

void DisplayExample::onEvent(const sp::Event& event)
{
	if (event.type == sp::Event::Key && event.key.pressed == false) {

		if (event.key.code == sp::Keyboard::W) {

			if (m_mode == sp::Display::WINDOWED) {
				m_mode = sp::Display::WINDOWEDFULLSCREEN;
				sp::Log::info("Windowed Fullscreen");
			} else {
				m_mode = sp::Display::WINDOWED;
				sp::Log::info("Windowed");
			}

			getGraphics()->setDisplayMode(m_mode);

		} else if (event.key.code == sp::Keyboard::Space) {

			if (m_mode == sp::Display::WINDOWED) {
				m_mode = sp::Display::FULLSCREEN;
				sp::Log::info("Fullscreen");
			} else {
				m_mode = sp::Display::WINDOWED;
				sp::Log::info("Windowed");
			}

			getGraphics()->setDisplayMode(m_mode);
		}
	}
}

void DisplayExample::update(double dt)
{
	// Nothing to do
}

void DisplayExample::render()
{
	sp::Graphics* g = getGraphics();

	g->clearBuffer();

	m_renderer->begin();
	m_renderer->submit(m_sprite);
	m_renderer->render();

	g->swapBuffers();
}
