Lose Canvas

You Lost

The LoseCanvas becomes visible when the Player dies. To ensure it always fits the screen and remains centered, its size is updated every frame to match the window dimensions, and its position is recalculated to be in the center of the window on each frame. This allows it to respond dynamically to any window resizing.

game/lose_canvas.h
#pragma once

#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/RenderTarget.hpp>
#include <SFML/Graphics/Text.hpp>

#include "engine/node.h"

namespace game {

class LoseCanvas : public ng::Node {
 public:
  explicit LoseCanvas(ng::App* app);

  void Enable();
  void Disable();

 protected:
  void Draw(sf::RenderTarget& target) override;

 private:
  bool is_enabled_ = false;
  sf::RectangleShape background_;
  sf::Text title_text_;
  sf::Text restart_text_;
};

}  // namespace game

The GameManager handles the instantiation of the LoseCanvas. The LoseCanvas is instantiated as a direct child of the scene, rather than a child of the GameManager itself. This is because the LoseCanvas belongs to a different rendering layer (UI) than the GameManager (default). Due to the layer filtering mechanism in the Node system, which halts rendering of a subtree when layers don't match at the root, this separate instantiation is necessary to ensure the UI is rendered correctly.

Last updated