Camera

Observers of the World

A Camera serves to render the Nodes within a scene from a specific viewpoint. Additionally, it employs Layers to selectively render content based on a Node's assigned layer and the Camera's configured rendering layers. Internally, the Camera class utilizes an sf::View to define this alternative perspective of the game world. For proper operation, each Camera instance automatically registers itself with the scene's CameraManager.

engine/camera.h
#pragma once

#include <SFML/Graphics/View.hpp>
#include <SFML/System/Vector2.hpp>
#include <cstdint>

#include "layer.h"
#include "node.h"

namespace ng {

/// @brief Represents a camera in the game world, defining the viewport and visible layers.
class Camera : public Node {
 public:
  /// @brief Constructs a Camera with a default draw order of 0 and rendering the default layer.
  /// @param app A pointer to the App instance this camera belongs to. This pointer must not be null.
  explicit Camera(App* app);

  /// @brief Constructs a Camera with a specified draw order and rendering layers.
  /// @param app A pointer to the App instance this camera belongs to. This pointer must not be null.
  /// @param draw_order The order in which this camera should be processed for drawing (lower values are processed first).
  /// @param layers A bitmask of Layer flags indicating which layers this camera should render.
  Camera(App* app, int32_t draw_order, Layer layers);

  /// @brief Returns the SFML View associated with this camera.
  /// @return A constant reference to the SFML View.
  [[nodiscard]] const sf::View& GetView() const;

  /// @brief Returns the draw order of this camera.
  /// @return The draw order value.
  [[nodiscard]] int32_t GetDrawOrder() const;

  /// @brief Returns the bitmask of layers that this camera renders.
  /// @return The Layer flags for rendering.
  [[nodiscard]] Layer GetRenderLayers() const;

  /// @brief Sets the size of the camera's view.
  /// @param size The new size of the view.
  void SetViewSize(sf::Vector2f size);

 protected:
  void OnAdd() override;
  void Update() override;
  void OnDestroy() override;

 private:
  // The SFML View representing the camera's viewport.
  sf::View view_;
  // The order in which this camera is processed for drawing.
  int32_t draw_order_ = 0;
  // Bitmask of layers this camera should render.
  Layer render_layers_ = Layer::kDefault;
};

}  // namespace ng

A Node has a rendering layer that determines whether it is visible by a camera or not. While usually a Node is on one layer, the system allows for Nodes to exist on multiple rendering layers simultaneously.

The InternalDraw function now implements a visibility check based on layers. It uses a bitmask operation to compare the current Node's layer against the camera's specified rendering layers. A non-zero result from this operation indicates a match, meaning the Node should be drawn. If the bitmask operation yields zero, the Node and all its descendants will be skipped during the rendering process for this camera.

By setting the UI layer on the ScoreManager, the text will be drawn only by the cameras that include the UI layer.

Last updated