aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerlaender2016-12-11 14:05:38 +0100
committerAdrian Kummerlaender2016-12-11 14:05:38 +0100
commitd139217ed53516bfcb71de171bf209706f5c842a (patch)
tree94731b40bc3314eb4357050e1295ff0fb5990f73
parent0ab22bf684f4b30f1182eed6901bd3764e428ec7 (diff)
downloadtermlife-d139217ed53516bfcb71de171bf209706f5c842a.tar
termlife-d139217ed53516bfcb71de171bf209706f5c842a.tar.gz
termlife-d139217ed53516bfcb71de171bf209706f5c842a.tar.bz2
termlife-d139217ed53516bfcb71de171bf209706f5c842a.tar.lz
termlife-d139217ed53516bfcb71de171bf209706f5c842a.tar.xz
termlife-d139217ed53516bfcb71de171bf209706f5c842a.tar.zst
termlife-d139217ed53516bfcb71de171bf209706f5c842a.zip
Extract term event polling
-rw-r--r--life.cc9
-rw-r--r--src/util/term.cc12
2 files changed, 14 insertions, 7 deletions
diff --git a/life.cc b/life.cc
index 6f57c20..08a205a 100644
--- a/life.cc
+++ b/life.cc
@@ -42,14 +42,9 @@ int main(int, char*[]) {
draw(worldOffsetX, worldOffsetY, world);
while ( true ) {
- struct tb_event ev;
- int t = tb_poll_event(&ev);
+ struct tb_event ev{ guard.poll() };
- if ( t == -1 ) {
- return -1;
- }
-
- switch ( t ) {
+ switch ( ev.type ) {
case TB_EVENT_KEY:
switch ( ev.key ) {
case TB_KEY_ESC:
diff --git a/src/util/term.cc b/src/util/term.cc
index 9e4c101..704f872 100644
--- a/src/util/term.cc
+++ b/src/util/term.cc
@@ -1,5 +1,7 @@
#include "term.h"
+#include <system_error>
+
#include <termbox.h>
namespace util {
@@ -27,4 +29,14 @@ TermGuard::~TermGuard() {
tb_shutdown();
}
+tb_event TermGuard::poll() const {
+ struct tb_event event;
+
+ if ( tb_poll_event(&event) == -1 ) {
+ throw std::system_error();
+ } else {
+ return event;
+ }
+}
+
}