diff options
| author | Vaino Kauppila <vaino@vke.fi> | 2026-07-02 22:58:25 +0300 |
|---|---|---|
| committer | Vaino Kauppila <vaino@vke.fi> | 2026-07-02 23:47:07 +0300 |
| commit | cd0c08dc7ce754473178ad3f32f7740a1dddc1eb (patch) | |
| tree | 589c676b41d6f3d322ea5ce339ddb0811f29ec31 /src/engine/camera.cpp | |
| download | forts_clone-cd0c08dc7ce754473178ad3f32f7740a1dddc1eb.tar.gz forts_clone-cd0c08dc7ce754473178ad3f32f7740a1dddc1eb.zip | |
Initial import: LibreForts (M0-M5 in progress)
Open-source Forts clone: custom C++20 engine + data-driven (Lua) 2D physics
artillery RTS.
- Building: node/strut graph on a stiff mass-spring solver (canon Forts model),
triangulation rigidity, axial + 30-degree angle-stress breaking, cascading
collapse, fire (DoT + spread), ground destroys debris.
- Weapons (M4): cannon (ballistic) + laser (beam/ignite) with select-and-aim-in-
arc UX; splash / beam damage; bg-brace passthrough.
- Devices + economy (M5): reactors + win/loss + restart, mines/turbines/battery,
metal deposits, storage caps, per-shot energy cost.
- Data-driven via Lua/sol2 with a layered mod loader; enemy-fort scenario mod.
- Renderer: SDL3 + textures + ImGui dev UI. stb_image + ImGui vendored.
- Assets: CC0 placeholders only; real game art loaded at runtime from the user's
own install via forts: paths (bring-your-own; nothing copyrighted committed).
See README.md / CLAUDE.md and research/ + requirements/ for detail.
Diffstat (limited to 'src/engine/camera.cpp')
| -rw-r--r-- | src/engine/camera.cpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/engine/camera.cpp b/src/engine/camera.cpp new file mode 100644 index 0000000..6ab2c57 --- /dev/null +++ b/src/engine/camera.cpp @@ -0,0 +1,55 @@ +#include "engine/camera.hpp" + +void Camera::set_viewport(int width, int height) { + vp_width_ = width; + vp_height_ = height; +} + +void Camera::set_position(glm::vec2 pos) { + pos_ = pos; +} + +void Camera::set_zoom(float zoom) { + if (zoom < min_zoom) zoom = min_zoom; + if (zoom > max_zoom) zoom = max_zoom; + zoom_ = zoom; +} + +void Camera::pan(glm::vec2 delta) { + pos_ += delta; +} + +void Camera::zoom_at(float delta, glm::vec2 world_cursor) { + float new_zoom = zoom_ * (1.0f + delta * zoom_speed); + if (new_zoom < min_zoom) new_zoom = min_zoom; + if (new_zoom > max_zoom) new_zoom = max_zoom; + + // Adjust position so world_cursor stays under the same screen point + float ratio = zoom_ / new_zoom; + pos_ = world_cursor + (pos_ - world_cursor) * ratio; + zoom_ = new_zoom; +} + +void Camera::compute_view_proj() { + float half_w = (vp_width_ / zoom_) * 0.5f; + float half_h = (vp_height_ / zoom_) * 0.5f; + + proj_ = glm::ortho(-half_w, half_w, -half_h, half_h, -1.0f, 1.0f); + view_ = glm::translate(glm::mat4(1.0f), glm::vec3(-pos_.x, -pos_.y, 0.0f)); + + glm::mat4 vp = proj_ * view_; + + // Copy to column-major float array for bgfx + const float* src = &vp[0][0]; + float* dst = &vp_[0][0]; + for (int i = 0; i < 16; i++) { + dst[i] = src[i]; + } +} + +glm::vec2 Camera::screen_to_world(glm::vec2 screen) const { + // Convert from screen (y-down) to world (y-up) + float world_x = pos_.x + (screen.x - vp_width_ * 0.5f) / zoom_; + float world_y = pos_.y - (screen.y - vp_height_ * 0.5f) / zoom_; + return glm::vec2(world_x, world_y); +} |
