diff options
Diffstat (limited to 'screenbuffer.cpp')
-rw-r--r-- | screenbuffer.cpp | 41 |
1 files changed, 33 insertions, 8 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<W*H;i++){ @@ -116,6 +134,8 @@ void ScreenBuffer::draw(){ bool prepped=false; + Style termstyle; + for(int y=0;y<H;y++){ bool needtomove=true; for(int x=0;x<W;x++){ @@ -124,14 +144,19 @@ void ScreenBuffer::draw(){ continue; } if(!prepped){ - cout<<"\x1B[?25l"; // invisible cursor + cout<<"\x1B[?25l" // invisible cursor + "\x1B[0m"; // reset style prepped=true; } if(needtomove){ cout<<"\x1B["<<y+1<<';'<<x+1<<'H'; needtomove=false; } - cout.put(screen[W*y+x]); + if(screen[W*y+x].style!=termstyle){ + cout<<"\x1B[0;3"<<screen[W*y+x].style.fg<<";4"<<screen[W*y+x].style.bg<<"m"; + termstyle=screen[W*y+x].style; + } + cout.put(screen[W*y+x].c); } } if(prepped){ @@ -140,7 +165,7 @@ void ScreenBuffer::draw(){ <<flush; } - memcpy(prevscreen,screen,W*H); + memcpy(prevscreen,screen,W*H*sizeof(Cell)); } void ScreenBuffer::emergencyDeinit(){ |