Camera::Camera(int w, int h, const Vector<float> &t)
: target(&t)
-, offset(w/2, h/2)
+, size(w, h)
+, offset(size / 2)
, zoom(1)
, zoomAcc(0) {
void Camera::Resize(int w, int h) {
- offset = Vector<float>(w/2, h/2);
+ size = Vector<int>(w, h);
+ offset = size / 2;
}
void Camera::Update(float delta) {
void SetTarget(const Vector<float> &t) { target = &t; }
void SetZoom(float z) { zoom = z; }
+ Vector<int> ScreenSize() const { return size; }
+ float Zoom() const { return zoom; }
+
void Resize(int w, int h);
void Update(float deltaT);
void StartShrink();
void StopShrink();
+ /// transform v from world coords to screen coords
Vector<int> ToScreen(Vector<float> v) const {
- return Vector<int>(OffsetOf(v)) + offset;
- }
- Vector<float> OffsetOf(Vector<float> v) const {
- return ToScale(v - *target);
+ return Vector<int>(ToScale(v - *target)) + offset;
}
+ /// scale v to current zoom level
Vector<float> ToScale(Vector<float> v) const {
return v * zoom;
}
+ /// transform v from screen coords to world coords
+ Vector<float> FromScreen(Vector<int> v) const {
+ return FromScale(v - offset) + *target;
+ }
+ /// scale v back from current zoom level
+ Vector<float> FromScale(Vector<float> v) const {
+ return v / zoom;
+ }
+
private:
const Vector<float> *target;
+ Vector<int> size;
Vector<int> offset;
float zoom;
public:
const Vector<Scalar> &Pos() const { return pos; }
Vector<Scalar> Vel() const { return Vector<Scalar>(dir) * speed; }
+ void SetSpeed(float s) { speed = s; }
void Update(Scalar delta) {
pos += Vel() * delta;