aboutsummaryrefslogtreecommitdiff
path: root/src/engine/graphics
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine/graphics')
-rw-r--r--src/engine/graphics/scene.cpp34
-rw-r--r--src/engine/graphics/scene.hpp6
2 files changed, 33 insertions, 7 deletions
diff --git a/src/engine/graphics/scene.cpp b/src/engine/graphics/scene.cpp
index 52613d8..b0d77b8 100644
--- a/src/engine/graphics/scene.cpp
+++ b/src/engine/graphics/scene.cpp
@@ -13,36 +13,60 @@
Scene::Scene(
IMatrixFactory<std::string_view> matrix_factory,
std::shared_ptr<ICursorController> cursor_controller) noexcept
- : _is_shown(false),
- _matrix(matrix_factory(size() - Bounds({.width = 0U, .height = 1U}))),
- _cursor_controller(std::move(cursor_controller))
+ : _matrix(matrix_factory(size() - Bounds({.width = 0U, .height = 1U}))),
+ _cursor_controller(std::move(cursor_controller)),
+ _is_shown(false)
{
_matrix->fill(" ");
}
void Scene::enter() noexcept
{
- if (_is_shown)
+ if (_is_shown || _original_termios != nullptr)
{
return;
}
+ // Enable alternative buffer
fmt::print(ENABLE_ALT_BUFFER, fmt::arg("esc", ESC));
std::cout.flush();
+ // Create a backup of the current terminal state
+ _original_termios = std::make_shared<termios>();
+ tcgetattr(STDIN_FILENO, _original_termios.get());
+
+ auto new_termios = termios(*_original_termios);
+
+ // Set the local mode flags of the new termios structure
+ //
+ // The following flags are disabled:
+ // ECHO - Echoing input characters
+ // ICANON - Canonical mode (line by line input)
+ // ISIG - Generate the corresponding signals for the characters
+ // INTR, QUIT, SUSP and DSUSP
+ new_termios.c_lflag &= static_cast<uint32_t>(~(ECHO | ICANON | ISIG));
+
+ // Set a new terminal state
+ tcsetattr(STDIN_FILENO, TCSAFLUSH, &new_termios);
+
_is_shown = true;
}
void Scene::leave() noexcept
{
- if (!_is_shown)
+ if (!_is_shown || _original_termios == nullptr)
{
return;
}
+ // Disable alternative buffer
fmt::print(DISABLE_ALT_BUFFER, fmt::arg("esc", ESC));
std::cout.flush();
+ // Restore the original terminal state
+ tcsetattr(STDIN_FILENO, TCSAFLUSH, _original_termios.get());
+
+ _original_termios = nullptr;
_is_shown = false;
}
diff --git a/src/engine/graphics/scene.hpp b/src/engine/graphics/scene.hpp
index 268f8dc..e57e1f8 100644
--- a/src/engine/graphics/scene.hpp
+++ b/src/engine/graphics/scene.hpp
@@ -8,6 +8,7 @@
#include <memory>
#include <string_view>
+#include <termios.h>
constexpr fmt::string_view ENABLE_ALT_BUFFER = "{esc}[?1049h";
constexpr fmt::string_view DISABLE_ALT_BUFFER = "{esc}[?1049l";
@@ -29,8 +30,9 @@ public:
-> const std::shared_ptr<IMatrix<std::string_view>> & override;
private:
- bool _is_shown;
-
std::shared_ptr<IMatrix<std::string_view>> _matrix;
std::shared_ptr<ICursorController> _cursor_controller;
+
+ bool _is_shown;
+ std::shared_ptr<termios> _original_termios = nullptr;
};