Main > Getting Started > Input Handling

Below app demonstrates usage of the Tiny2D input API.
The app moves the cursor using keyboard, mouse and
touchpad (if available on the device) and displays
circle in its location.

#include "Tiny2D.h"

using namespace Tiny2D;

class InputApp : public App::Callbacks
{
public:
	InputApp() :
		pos(100, 100)
	{}
	
	virtual void OnUpdate(float deltaTime)
	{
		// Keyboard
		
		const float speed = 100.0f;

		if (Input::IsKeyDown(Input::Key_Left)) pos.x -= speed * deltaTime;
		if (Input::IsKeyDown(Input::Key_Right)) pos.x += speed * deltaTime;
		if (Input::IsKeyDown(Input::Key_Up)) pos.y -= speed * deltaTime;
		if (Input::IsKeyDown(Input::Key_Down)) pos.y += speed * deltaTime;
		
		// Mouse
		
		if (Input::HasMouse())
		{
			const float mouseSensitivity = 0.5f;
		
			const Input::MouseState& state = Input::GetMouseState();
			pos += state.movement * mouseSensitivity;
		}
		
		// Touchpad
		
		if (Input::HasTouchpad() && Input::IsTouchpadDown())
		{
			pos = Input::GetTouch(0).position;
		}
	}

	virtual void OnDraw(Texture& renderTarget)
	{
		renderTarget.BeginDrawing(&Color::Black);
		Shape::DrawCircle(pos, 10);
		renderTarget.EndDrawing();
	}

private:
	Vec2 pos;
};

TINY2D_DEFINE_APP(InputApp);





Contact: contact at pixelelephant dot com
(C) 2013 Pixel Elephant - All Rights Reserved