Camera

namespace tnt
namespace doo

Variables

struct tnt::doo::cameras_sys cameras
struct camera_comp

Public Functions

constexpr camera_comp(float angle_, Vector const &pos_, float w_, float h_, float speed_ = 5.f, float shaking_ = .7f, float decrease_ = 1.f, Vector const &scale_ = Vector{1.f, 1.f}) noexcept

Create a new camera.

Parameters
  • angle_: The angle of the camera.

  • pos_: The position of the camera.

  • w_: The width of the camera.

  • h_: The height of the camera.

  • speed_: The speed the camera follows its target (if any). 5 by default.

  • shaking_: The amplitude of shaking. .7f by default.

  • decrease_: The amount of units the shaking factors decreases each second. 1.f by default.

  • scale_: The scale of the objects drawed inside the camera. {1.f, 1.f} by default.

Public Members

float angle
float w

< The angle of the camera.

float h

< The width of the camera.

float speed

< The height of the camera.

float shaking

< The speed of the camera.

float shakeLoss

< The amplitude of shaking.

Vector pos

< The amount of shaking decrease for second.

Vector scale

< The position of the camera.

struct cameras_sys

Public Functions

void add_object(camera_comp const &data)

Add a new camera to the system.

Parameters
  • data: The data of the camera component.

void from_json(nlohmann::json const &j)

Load camera data from a json chunk.

Parameters
  • j: The json chunk.

void to_json(camera const &id, nlohmann::json &j)

Store camera data of a specific object to a json chunk.

Parameters
  • id: The id of the camera to serialize to json.

  • j: The json chunk where the data will be saved.

void draw_imgui(camera const &id, Window const &win) noexcept

Draw widgets on the given window to modify the datas of the system.

Parameters
  • id: The id of the desired camera.

  • win: The window where the widgets will be drawed.

void follow(camera const &cam, object const &id, float time_, Vector const &off = Vector{0.f, 0.f}) noexcept

Center the given object to the center of the camera. Maintain.

Parameters
  • cam: The id of the camera.

  • id: The id of the object that should be centered.

  • time_: The elapsed time since the last frame.

  • off: The offset that the object can have from the center of the camera. Defaulted to the center.

void shake(camera const &cam) noexcept

Shake the camera for the given amount of time.

Todo:

Return to the original posiition after shaking.

Shake for a fixed amount of time with shake_for().

Note

Based on https://gist.github.com/ftvs/5822103

Parameters
  • cam: The id of the camera that should be shaked.

void zoom_to_fit(camera const &cam, std::span<object> objs)

Set the camera zoom to fit all the given objects.

Parameters
  • cam: The id of the camera to zoom.

  • objs: The id-s of the objects that should fit on the camera.

void remove(camera const &id) noexcept

Remove the given object from the camera system.

Parameters
  • id: The id of the object you want to remove.

void clear() noexcept

Remove all the objects from the camera system.

Public Members

sparse_set<camera> active
std::vector<float> angle

< The id-s of all the active cameras. Not related to the id-s of the objects.

std::vector<float> width

< The angles of all the cameras.

std::vector<float> height

< The width of all the cameras.

std::vector<float> speed

< The height of all the cameras.

std::vector<float> shaking

< The speed the camera will approach to the player.

std::vector<float> shakeLoss

< The amount of shaking for each camera.

std::vector<float> shake_time

< The amount of decreasing on the shaking of each camera per second.

std::vector<object> target

< The amount of time in milliseconds a camera should shake, if it is shaking.

std::vector<Vector> pos

< The id-s of the object each camera is centered to. -1 if none.

std::vector<Vector> scale

< The position of each camera.

std::vector<Vector> offset

< The scale of the objects drawed on each camera.