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 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) (limited to 'screenbuffer.cpp') 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