raycaster/camera.cpp

81 lines
1.7 KiB
C++

#include "camera.hpp"
#include <fmt/core.h>
#include "animation.hpp"
#include <unordered_map>
#include "components.hpp"
#include "config.hpp"
namespace cinematic {
using components::Animation, std::string;
struct CameraManager {
std::unordered_map<string, Animation> animations;
};
static CameraManager MGR;
static bool initialized = false;
void init() {
if(!initialized) {
auto cameras = settings::get("cameras");
for(auto &[name, data] : cameras.json().items()) {
auto anim = components::convert<Animation>(data);
MGR.animations.try_emplace(name, anim);
}
initialized = true;
}
}
Camera::Camera() :
anim(MGR.animations.at("pan"))
{
}
void Camera::resize(float width, float height) {
size = {width, height};
}
void Camera::style(const std::string &name) {
anim = MGR.animations.at(name);
}
void Camera::position(float x, float y) {
aimed_at = {x, y};
}
void Camera::move(float x, float y) {
going_to = {x, y};
// BUG: annoying special case
if(anim.motion == ease::SLIDE) {
anim.min_x = aimed_at.x;
anim.min_y = aimed_at.y;
anim.max_x = going_to.x;
anim.max_y = going_to.y;
}
}
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};
view = {aimed_at, size};
target.setView(target.getDefaultView());
}
void Camera::render(sf::RenderTexture& target) {
if(anim.playing) {
anim.apply(view, going_to, size);
target.setView(view);
}
}
bool Camera::playing() {
return anim.playing;
}
void Camera::play() {
anim.play();
}
}