
#include <Spectre/Input/InputModule.h>
#include <Spectre/Input/Keyboard.h>
#include <Spectre/Input/Mouse.h>

#include <Spectre/Graphics/BatchRenderer2D.h>
#include <Spectre/System/Log.h>
#include "InputExample.h"

void InputExample::init()
{
	m_renderer = new sp::BatchRenderer2D();

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

	m_kb_sprite.setTexture(m_tux_texture);
	m_kb_sprite.setColor(sp::Color::Green);
	m_kb_sprite.setSize(sp::vec2f(100, 100));

	m_mouse_sprite.setTexture(m_tux_texture);
	m_mouse_sprite.setColor(sp::Color::Red);
	m_mouse_sprite.setSize(sp::vec2f(100, 100));
	m_mouse_sprite.setPosition(sp::vec2f(50, 50));

	m_renderer->setCamera(m_camera);
}

void InputExample::update(double dt)
{
	sp::Keyboard* keyboard = getInput()->getKeyboard();
	sp::Mouse* mouse = getInput()->getMouse();
	double delta = 0.3 * dt;

	// Handle keyboard input.
	if (keyboard->isKeyDown(sp::Keyboard::Left)) {
		m_kb_sprite.move(sp::vec2f(-delta, 0));
	} else if (keyboard->isKeyDown(sp::Keyboard::Right)) {
		m_kb_sprite.move(sp::vec2f( delta, 0));
	}

	if (keyboard->isKeyDown(sp::Keyboard::Up)) {
		m_kb_sprite.move(sp::vec2f(0, -delta));
	} else if (keyboard->isKeyDown(sp::Keyboard::Down)) {
		m_kb_sprite.move(sp::vec2f(0, delta));
	}

	// Handle mouse input.
	if (mouse->isButtonDown(sp::Mouse::Left)) {
		m_mouse_sprite.setPosition(mouse->getPosition());
	}
}

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

	g->clearBuffer();

	m_renderer->begin();
	m_renderer->submit(m_mouse_sprite);
	m_renderer->submit(m_kb_sprite);
	m_renderer->render();

	g->swapBuffers();
}
