aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--life.cc41
-rw-r--r--src/world.h42
2 files changed, 54 insertions, 29 deletions
diff --git a/life.cc b/life.cc
index 7f0ace7..6d98a08 100644
--- a/life.cc
+++ b/life.cc
@@ -2,13 +2,52 @@
#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++;
+ }
+}
+
+template<
+ std::size_t WIDTH,
+ std::size_t HEIGHT
+>
+void draw(life::World<WIDTH, HEIGHT>& world) {
+ tb_clear();
+
+ for ( std::size_t i = 0; i < HEIGHT; i++ ) {
+ for ( std::size_t j = 0; j < WIDTH; j++ ) {
+ if ( world.isLifeAt(i,j) ) {
+ tb_change_cell(i, j, 0x2588, TB_BLACK, TB_GREEN);
+ } else {
+ tb_change_cell(i, j, 0x2591, TB_BLACK, TB_BLUE);
+ }
+ }
+ }
+
+ print_tb(1, 21, TB_WHITE, TB_DEFAULT, "Age:");
+ print_tb(6, 21, 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);
life::World<20,20> world;
+ draw(world);
+
while ( true ) {
struct tb_event ev;
int t = tb_poll_event(&ev);
@@ -39,7 +78,7 @@ int main(int, char*[]) {
break;
}
- world.draw();
+ draw(world);
}
return 0;
diff --git a/src/world.h b/src/world.h
index 297d0c5..d951cf5 100644
--- a/src/world.h
+++ b/src/world.h
@@ -6,8 +6,6 @@
#include <tuple>
#include <cstdint>
-#include <termbox.h>
-
namespace life {
template<
@@ -24,15 +22,7 @@ class World {
}
}
- void summonLifeAt(std::size_t x, std::size_t y) {
- this->matrix_[y][x] = true;
- }
-
- void extinguishLifeAt(std::size_t x, std::size_t y) {
- this->matrix_[y][x] = false;
- }
-
- bool isLifeAt(std::ptrdiff_t x, std::ptrdiff_t y) {
+ bool isLifeAt(std::ptrdiff_t x, std::ptrdiff_t y) const {
if ( x >= 0 &&
x < WIDTH &&
y >= 0 &&
@@ -43,7 +33,7 @@ class World {
}
}
- std::uint8_t lifeDensityAt(std::size_t x, std::size_t y) {
+ std::uint8_t lifeDensityAt(std::size_t x, std::size_t y) const {
std::uint8_t counter = 0;
if ( this->isLifeAt(x - 1, y) ) {
@@ -74,6 +64,18 @@ class World {
return counter;
}
+ std::size_t getAge() const {
+ return this->age_;
+ }
+
+ void summonLifeAt(std::size_t x, std::size_t y) {
+ this->matrix_[y][x] = true;
+ }
+
+ void extinguishLifeAt(std::size_t x, std::size_t y) {
+ this->matrix_[y][x] = false;
+ }
+
void tick() {
this->age_++;
@@ -118,22 +120,6 @@ class World {
}
}
- void draw() {
- tb_clear();
-
- for ( std::size_t j = 0; j < HEIGHT; j++ ) {
- for ( std::size_t i = 0; i < WIDTH; i++ ) {
- if ( this->matrix_[j][i] ) {
- tb_change_cell(i, j, 0x2588, TB_BLACK, TB_GREEN);
- } else {
- tb_change_cell(i, j, 0x2591, TB_BLACK, TB_BLUE);
- }
- }
- }
-
- tb_present();
- }
-
private:
std::size_t age_ = 0;