From fb0cdfe743869f6592090e441a071f0904a80634 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Thu, 2 Mar 2017 21:23:39 +0100 Subject: Optimize terminal usage The ^C-catching stuff will not work on windows; need to look into that --- main.cpp | 22 ++++++-- screenbuffer.cpp | 152 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ screenbuffer.h | 24 +++++++++ world.cpp | 24 +++++++-- world.h | 4 +- 5 files changed, 216 insertions(+), 10 deletions(-) create mode 100644 screenbuffer.cpp create mode 100644 screenbuffer.h diff --git a/main.cpp b/main.cpp index 3d4a048..d75c253 100644 --- a/main.cpp +++ b/main.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include "params.h" #include "world.h" @@ -210,6 +211,16 @@ string preprocess(istream &file){ } +static ScreenBuffer *sb; + +static void signalHandler(int sig){ + if(sig==SIGINT){ + sb->emergencyDeinit(); + _exit(130); + } +} + + int main(int argc,char **argv){ struct timeval tv; gettimeofday(&tv,nullptr); @@ -249,11 +260,14 @@ int main(int argc,char **argv){ } } - world.print(); + sb=new ScreenBuffer(SIZE*3,SIZE); + signal(SIGINT,signalHandler); + world.print(*sb); for(int i=0;idraw(); } + delete sb; } diff --git a/screenbuffer.cpp b/screenbuffer.cpp new file mode 100644 index 0000000..a382e82 --- /dev/null +++ b/screenbuffer.cpp @@ -0,0 +1,152 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include "screenbuffer.h" + +using namespace std; + + +static void initTerminal(){ + cout<<"\x1B[?1049h" // start alternate screen + "\x1B[2J" // clear screen + "\x1B[H" // move to top left + <=0&&x=0&&yv<"[board[y][x]->heading%4]); - cout<<' '; + if(board[y][x]==nullptr)os<<".."; + else os<v<"[board[y][x]->heading%4]); + os<<' '; + } + os<heading%4]; + sb.printf("%c%c",c,c); + } + sb.printf(" "); } - cout< #include #include "params.h" +#include "screenbuffer.h" using namespace std; @@ -100,7 +101,8 @@ public: void tick(); Robot* targetbot(const Robot *r); - void print() const; + void print(ostream &os) const; + void print(ScreenBuffer &sb) const; }; -- cgit v1.2.3-70-g09d2