File: Device.h

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (27 lines) | stat: -rw-r--r-- 830 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#pragma once

#include "math/Vector2.h"
#include <algorithm>

// Maps the range [0..size] to [-1..+1]
inline float screen_normalised(Vector2::ElementType pos, std::size_t size)
{
	return ((2.0f * pos) / size) - 1.0f;
}

inline Vector2 window_to_normalised_device(const Vector2& window, std::size_t width, std::size_t height)
{
	return Vector2(screen_normalised(window.x(), width), screen_normalised(height - 1 - window.y(), height));
}

// Constrains the value pos to the range [-1..+1]
inline Vector2::ElementType device_constrained(Vector2::ElementType pos)
{
	return std::min(1.0, std::max(-1.0, pos));
}

// See device_constrained(), this cuts off the Vector's components at -1 or +1
inline Vector2 device_constrained(const Vector2& device)
{
	return Vector2(device_constrained(device.x()), device_constrained(device.y()));
}