aboutsummaryrefslogtreecommitdiffstats
path: root/src/engine/camera.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine/camera.hpp')
-rw-r--r--src/engine/camera.hpp59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/engine/camera.hpp b/src/engine/camera.hpp
new file mode 100644
index 0000000..ecaadc1
--- /dev/null
+++ b/src/engine/camera.hpp
@@ -0,0 +1,59 @@
+#pragma once
+
+#include <glm/glm.hpp>
+#include <glm/gtc/matrix_transform.hpp>
+
+// 2D orthographic camera. Works in world-space coordinates (Box2D space).
+// x+ = right, y+ = up.
+
+class Camera {
+public:
+ Camera() = default;
+
+ // Set the viewport size in pixels (window size).
+ void set_viewport(int width, int height);
+
+ // World-space position the camera is looking at.
+ void set_position(glm::vec2 pos);
+ glm::vec2 position() const { return pos_; }
+
+ // Zoom: higher = more zoomed in. Clamped to [min_zoom, max_zoom].
+ void set_zoom(float zoom);
+ float zoom() const { return zoom_; }
+
+ // Pan by delta in world units (not pixels).
+ void pan(glm::vec2 delta);
+
+ // Zoom by delta at the given screen position (world-space under cursor).
+ void zoom_at(float delta, glm::vec2 world_cursor);
+
+ // Build view-projection matrix. Call once per frame.
+ void compute_view_proj();
+
+ const float* view_proj() const { return &vp_[0][0]; }
+ glm::mat4 view() const { return view_; }
+ glm::mat4 proj() const { return proj_; }
+
+ // Convert screen coordinates (pixels, y-down) to world coordinates.
+ glm::vec2 screen_to_world(glm::vec2 screen) const;
+
+ int viewport_width() const { return vp_width_; }
+ int viewport_height() const { return vp_height_; }
+
+ // Config
+ float min_zoom = 0.1f;
+ float max_zoom = 200.0f;
+ float zoom_speed = 0.1f;
+ float pan_speed = 500.0f; // pixels per second
+
+private:
+ glm::vec2 pos_ = {0.0f, 5.0f}; // world center at start
+ float zoom_ = 10.0f; // pixels per world unit
+
+ int vp_width_ = 1280;
+ int vp_height_ = 720;
+
+ glm::mat4 view_ = glm::mat4(1.0f);
+ glm::mat4 proj_ = glm::mat4(1.0f);
+ float vp_[4][4] = {}; // column-major for bgfx
+};