Better working camera that is constrained in the bounds, but the animations don't follow the bounding.

This commit is contained in:
Zed A. Shaw 2026-01-01 12:59:39 -05:00
parent 6dc9d564c6
commit 51bb74e2d7
9 changed files with 93 additions and 33 deletions

View file

@ -4,9 +4,10 @@
#include <unordered_map>
#include "components.hpp"
#include "config.hpp"
#include <algorithm>
namespace cinematic {
using components::Animation, std::string;
using components::Animation, std::string, std::min, std::clamp;
struct CameraManager {
std::unordered_map<string, Animation> animations;
@ -27,13 +28,38 @@ namespace cinematic {
}
}
Camera::Camera() :
anim(MGR.animations.at("pan"))
Camera::Camera(sf::Vector2f size) :
anim(MGR.animations.at("shake")),
base_size(size),
size(size)
{
}
void Camera::resize(float width, float height) {
size = {width, height};
void Camera::update_camera_bounds(sf::Vector2f size) {
// camera bounds now constrains the x/y so that the mid-point
// of the size won't go too far outside of the frame
camera_bounds = {
{size.x / 2.0f, size.y / 2.0f},
{base_size.x - size.x / 2.0f, base_size.y - size.y / 2.0f}
};
fmt::println("!!!!! CAMERA BOUNDS camera_bounds={},{},{},{}",
camera_bounds.position.x, camera_bounds.position.y,
camera_bounds.size.x, camera_bounds.size.y);
}
void Camera::scale(float ratio) {
size.x = base_size.x * ratio;
size.y = base_size.y * ratio;
update_camera_bounds(size);
}
void Camera::resize(float width) {
dbc::check(width <= base_size.x, "invalid width for camera");
size.x = width;
size.y = base_size.y * (width / base_size.x);
update_camera_bounds(size);
}
void Camera::style(const std::string &name) {
@ -41,11 +67,23 @@ namespace cinematic {
}
void Camera::position(float x, float y) {
aimed_at = {x, y};
aimed_at.x = clamp(x, camera_bounds.position.x, camera_bounds.size.x);
aimed_at.y = clamp(y, camera_bounds.position.y, camera_bounds.size.y);
fmt::println("!!! CAMERA POSITION aimed_at={},{}; x/y={},{}; camera_bounds={},{},{},{}",
aimed_at.x, aimed_at.y, x, y,
camera_bounds.position.x, camera_bounds.position.y,
camera_bounds.size.x, camera_bounds.size.y);
}
void Camera::move(float x, float y) {
going_to = {x, y};
going_to.x = clamp(x, camera_bounds.position.x, camera_bounds.size.x);
going_to.y = clamp(y, camera_bounds.position.y, camera_bounds.size.y);
fmt::println("!!!! CAMERA MOVE going_to={},{}; x/y={},{}; camera_bounds={},{},{},{}",
going_to.x, going_to.y, x, y,
camera_bounds.position.x, camera_bounds.position.y,
camera_bounds.size.x, camera_bounds.size.y);
// BUG: annoying special case
if(anim.motion == ease::SLIDE) {
@ -56,11 +94,14 @@ namespace cinematic {
}
}
void Camera::reset(sf::RenderTexture& target, float width, float height) {
size = {width, height};
aimed_at = {width/2, height/2};
going_to = {width/2, height/2};
void Camera::reset(sf::RenderTexture& target) {
size = {base_size.x, base_size.y};
aimed_at = {base_size.x/2, base_size.y/2};
going_to = {base_size.x/2, base_size.y/2};
view = {aimed_at, size};
camera_bounds = {{0,0}, base_size};
// BUG: is getDefaultView different from view?
target.setView(target.getDefaultView());
}