aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerlaender2016-12-11 13:39:58 +0100
committerAdrian Kummerlaender2016-12-11 13:39:58 +0100
commit0ab22bf684f4b30f1182eed6901bd3764e428ec7 (patch)
tree6dc38375a726b53ae16dbcabf8dd11d6cde53fc8
parent6622e436442e37190c43aa40770f0f01bc2427e9 (diff)
downloadtermlife-0ab22bf684f4b30f1182eed6901bd3764e428ec7.tar
termlife-0ab22bf684f4b30f1182eed6901bd3764e428ec7.tar.gz
termlife-0ab22bf684f4b30f1182eed6901bd3764e428ec7.tar.bz2
termlife-0ab22bf684f4b30f1182eed6901bd3764e428ec7.tar.lz
termlife-0ab22bf684f4b30f1182eed6901bd3764e428ec7.tar.xz
termlife-0ab22bf684f4b30f1182eed6901bd3764e428ec7.tar.zst
termlife-0ab22bf684f4b30f1182eed6901bd3764e428ec7.zip
Extract terminal functions
-rw-r--r--CMakeLists.txt3
-rw-r--r--life.cc26
-rw-r--r--src/util/term.cc30
3 files changed, 37 insertions, 22 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d517ca3..900c31f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -13,6 +13,9 @@ include_directories(
add_executable(
life
life.cc
+ src/util/term.cc
+ src/util/box_indicator.cc
+ src/util/box_traverser.cc
)
target_link_libraries(
diff --git a/life.cc b/life.cc
index 847fe14..6f57c20 100644
--- a/life.cc
+++ b/life.cc
@@ -1,19 +1,7 @@
#include <termbox.h>
#include "world.h"
-
-void print_tb(
- std::size_t x,
- std::size_t y,
- std::uint16_t fg,
- std::uint16_t bg,
- const std::string& text
-) {
- for ( const char& c : text ) {
- tb_change_cell(x, y, c, fg, bg);
- x++;
- }
-}
+#include "util/term.h"
template<
std::size_t WIDTH,
@@ -38,18 +26,14 @@ void draw(
}
}
- print_tb(1, 1, TB_WHITE, TB_DEFAULT, "Age:");
- print_tb(6, 1, TB_WHITE, TB_DEFAULT, std::to_string(world.getAge()));
+ util::print_tb(1, 1, TB_WHITE, TB_DEFAULT, "Age:");
+ util::print_tb(6, 1, TB_WHITE, TB_DEFAULT, std::to_string(world.getAge()));
tb_present();
}
int main(int, char*[]) {
- tb_init();
-
- tb_select_output_mode(TB_OUTPUT_NORMAL);
- tb_select_input_mode(TB_INPUT_ESC | TB_INPUT_MOUSE);
-
+ util::TermGuard guard;
life::World<40,20> world;
std::size_t worldOffsetX = tb_width() / 2 - world.width / 2;
@@ -62,7 +46,6 @@ int main(int, char*[]) {
int t = tb_poll_event(&ev);
if ( t == -1 ) {
- tb_shutdown();
return -1;
}
@@ -70,7 +53,6 @@ int main(int, char*[]) {
case TB_EVENT_KEY:
switch ( ev.key ) {
case TB_KEY_ESC:
- tb_shutdown();
return 0;
case TB_KEY_SPACE:
world.tick();
diff --git a/src/util/term.cc b/src/util/term.cc
new file mode 100644
index 0000000..9e4c101
--- /dev/null
+++ b/src/util/term.cc
@@ -0,0 +1,30 @@
+#include "term.h"
+
+#include <termbox.h>
+
+namespace util {
+
+void print_tb(
+ std::size_t x,
+ std::size_t y,
+ std::uint16_t fg,
+ std::uint16_t bg,
+ const std::string& text
+) {
+ for ( const char& c : text ) {
+ tb_change_cell(x, y, c, fg, bg);
+ x++;
+ }
+}
+
+TermGuard::TermGuard() {
+ tb_init();
+ tb_select_output_mode(TB_OUTPUT_NORMAL);
+ tb_select_input_mode(TB_INPUT_ESC | TB_INPUT_MOUSE);
+}
+
+TermGuard::~TermGuard() {
+ tb_shutdown();
+}
+
+}