From ac500f4a786d975b492cc879bf6195a066b0a398 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sun, 19 Mar 2017 11:27:32 +0100 Subject: Add colours to terminal sim output --- screenbuffer.cpp | 41 +++++++++++++++++++++++++++++++++-------- screenbuffer.h | 18 +++++++++++++++++- world.cpp | 14 ++++++++++++++ 3 files changed, 64 insertions(+), 9 deletions(-) diff --git a/screenbuffer.cpp b/screenbuffer.cpp index d224181..781e646 100644 --- a/screenbuffer.cpp +++ b/screenbuffer.cpp @@ -10,6 +10,15 @@ using namespace std; +bool operator!=(const ScreenBuffer::Style &a,const ScreenBuffer::Style &b){ + return memcmp(&a,&b,sizeof(ScreenBuffer::Style))!=0; +} + +bool operator!=(const ScreenBuffer::Cell &a,const ScreenBuffer::Cell &b){ + return memcmp(&a,&b,sizeof(ScreenBuffer::Cell))!=0; +} + + static void initTerminal(){ cout<<"\x1B[?1049h" // start alternate screen "\x1B[2J" // clear screen @@ -25,10 +34,8 @@ static void deinitTerminal(){ ScreenBuffer::ScreenBuffer(int W,int H) :W(W),H(H){ - screen=new char[W*H]; - prevscreen=new char[W*H]; - memset(screen,' ',W*H); - memset(prevscreen,' ',W*H); + screen=new Cell[W*H]; + prevscreen=new Cell[W*H]; initTerminal(); } @@ -56,7 +63,8 @@ void ScreenBuffer::printstr(const char *buf){ curx=basex; cury++; } - screen[W*cury+curx]=buf[i]; + screen[W*cury+curx].c=buf[i]; + screen[W*cury+curx].style=curstyle; curx++; } else { if(cury==H-1)break; @@ -103,6 +111,16 @@ int ScreenBuffer::mvprintf(int x,int y,const char *format,...){ return ret; } +void ScreenBuffer::setfg(int fg){ + assert((fg>=0&&fg<=7)||fg==9); + curstyle.fg=fg; +} + +void ScreenBuffer::setbg(int bg){ + assert((bg>=0&&bg<=7)||bg==9); + curstyle.bg=bg; +} + void ScreenBuffer::draw(){ bool changed[W*H]; for(int i=0;i #include #include +#include #include "world.h" @@ -351,6 +352,17 @@ Robot* World::targetbot(const Robot *r){ return *targetbotptr(r); } +static int colourCode(const Team *team){ + static unordered_map mappings; + static int nextCode=1; + auto it=mappings.find(team); + if(it!=mappings.end())return it->second; + int code=nextCode; + mappings[team]=code; + nextCode=nextCode%6+1; + return code; +} + void World::print(ScreenBuffer &sb) const { for(int y=0;yheading%4]; char c1=board[y][x]->active?c2:'X'; + sb.setfg(colourCode(board[y][x]->team)); sb.printf("%c%c",c1,c2); + sb.setfg(9); } sb.printf(" "); } -- cgit v1.2.3