From 99107be8a997eb453b989e1b31123708685696dc Mon Sep 17 00:00:00 2001 From: Jonas Gunz Date: Mon, 6 May 2019 18:18:32 +0200 Subject: Changed color handling to take ANSI 8bit colors added ansi_color_fg(R,G,B) and ..._bg(...) to generate ANSI-colors from RGB values replaced two spaces with tabs in every file to fix terrible tabbing (thanks atom...) --- src/cInput.cpp | 110 +++++++++++++++++++++++++++--------------------------- src/cInput.h | 50 ++++++++++++------------- src/cObject.cpp | 4 +- src/cRender.cpp | 51 ++++++++++++++----------- src/cRender.h | 65 ++++++++++++++++---------------- src/cWiremesh.cpp | 2 +- src/cWiremesh.h | 4 +- 7 files changed, 146 insertions(+), 140 deletions(-) (limited to 'src') diff --git a/src/cInput.cpp b/src/cInput.cpp index 32da417..891be1f 100644 --- a/src/cInput.cpp +++ b/src/cInput.cpp @@ -2,73 +2,73 @@ cInput::cInput() { - // Save original serial communication configuration for stdin - tcgetattr( STDIN_FILENO, &original); + // Save original serial communication configuration for stdin + tcgetattr( STDIN_FILENO, &original); - // Put stdin in raw mode so keys get through directly without - // requiring pressing enter. - cfmakeraw (&raw); - tcsetattr (STDIN_FILENO, TCSANOW, &raw); + // Put stdin in raw mode so keys get through directly without + // requiring pressing enter. + cfmakeraw (&raw); + tcsetattr (STDIN_FILENO, TCSANOW, &raw); - // Enable mouse tracking - write (STDOUT_FILENO, "\e[?1000h", 8); + // Enable mouse tracking + write (STDOUT_FILENO, "\e[?1000h", 8); } cInput::~cInput() { - //revert changes to console - write (STDOUT_FILENO, "\e[?1000l", 8); - tcsetattr (STDIN_FILENO, TCSANOW, &original); + //revert changes to console + write (STDOUT_FILENO, "\e[?1000l", 8); + tcsetattr (STDIN_FILENO, TCSANOW, &original); } sInputEvent cInput::poll() { - const unsigned int buff_len = 5; - sInputEvent ret; - unsigned char buff [buff_len]; + const unsigned int buff_len = 5; + sInputEvent ret; + unsigned char buff [buff_len]; - //setup for select - fd_set rfds; - struct timeval tv; - FD_ZERO(&rfds); - FD_SET(STDIN_FILENO, &rfds); - tv.tv_sec = 0; - tv.tv_usec = 0; + //setup for select + fd_set rfds; + struct timeval tv; + FD_ZERO(&rfds); + FD_SET(STDIN_FILENO, &rfds); + tv.tv_sec = 0; + tv.tv_usec = 0; - ret.type = _EVENT_NULL; + ret.type = _EVENT_NULL; - //Check for Input. return of none - if(!select(1, &rfds, NULL, NULL, &tv)) - return ret; + //Check for Input. return of none + if(!select(1, &rfds, NULL, NULL, &tv)) + return ret; - read (STDIN_FILENO, &buff, 1); - if (buff[0] == 3) { - // User pressd Ctr+C - ret.type = _EVENT_TERM; - } - else if (buff[0] == '\x1B') //Escape sequence - { - read (STDIN_FILENO, &buff, buff_len); - if(buff[0] == '[') - { - if(buff[1] == 'M') //Mouse Event - { - ret.b = buff[2] - 32; - ret.x = buff[3] - 32 - 1; //Console sees origin at 1,1 - ret.y = buff[4] - 32 - 1; //Program at 0,0 - ret.type = _EVENT_MOUSE; - } - else //e.g. Arrow Keys - { - ret.c = buff[1]; - ret.type = _EVENT_KEY; - } - } - } - else - { - ret.type = _EVENT_CHAR; - ret.c = buff[0]; - } - return ret; + read (STDIN_FILENO, &buff, 1); + if (buff[0] == 3) { + // User pressd Ctr+C + ret.type = _EVENT_TERM; + } + else if (buff[0] == '\x1B') //Escape sequence + { + read (STDIN_FILENO, &buff, buff_len); + if(buff[0] == '[') + { + if(buff[1] == 'M') //Mouse Event + { + ret.b = buff[2] - 32; + ret.x = buff[3] - 32 - 1; //Console sees origin at 1,1 + ret.y = buff[4] - 32 - 1; //Program at 0,0 + ret.type = _EVENT_MOUSE; + } + else //e.g. Arrow Keys + { + ret.c = buff[1]; + ret.type = _EVENT_KEY; + } + } + } + else + { + ret.type = _EVENT_CHAR; + ret.c = buff[0]; + } + return ret; } diff --git a/src/cInput.h b/src/cInput.h index 00ddb7d..f34095f 100644 --- a/src/cInput.h +++ b/src/cInput.h @@ -8,23 +8,23 @@ #ifdef __linux__ #elif _WIN32 - #error "Platforn not supported" + #error "Platforn not supported" #else - #error "Platforn not supported" + #error "Platforn not supported" #endif -#define _EVENT_NULL 0 -#define _EVENT_CHAR 1 -#define _EVENT_KEY 2 -#define _EVENT_MOUSE 3 -#define _EVENT_TERM 4 +#define _EVENT_NULL 0 +#define _EVENT_CHAR 1 +#define _EVENT_KEY 2 +#define _EVENT_MOUSE 3 +#define _EVENT_TERM 4 struct sInputEvent { - unsigned int type; - unsigned char c; - unsigned int b; - int x, y; + unsigned int type; + unsigned char c; + unsigned int b; + int x, y; }; /** * ##cInput @@ -36,23 +36,23 @@ struct sInputEvent class cInput { public: - cInput(); + cInput(); - ~cInput(); + ~cInput(); - /** Reads inputevents - * returns event struct - * ### sInputEvent.type - * * _EVENT_NULL: No input recorded - * * _EVENT_CHAR: A Key was pressed, stored in .c - * * _EVENT_KEY: Escape sequence recorded, stored in .c without escape char - * * _EVENT_MOUSE: Console registered click at (.x, .y) with origin at (0,0) (top left). Mouse button stored in b. - * * _EVENT_TERM: Console registered Ctrl+C - */ - sInputEvent poll(); + /** Reads inputevents + * returns event struct + * ### sInputEvent.type + * * _EVENT_NULL: No input recorded + * * _EVENT_CHAR: A Key was pressed, stored in .c + * * _EVENT_KEY: Escape sequence recorded, stored in .c without escape char + * * _EVENT_MOUSE: Console registered click at (.x, .y) with origin at (0,0) (top left). Mouse button stored in b. + * * _EVENT_TERM: Console registered Ctrl+C + */ + sInputEvent poll(); private: - struct termios original, raw; + struct termios original, raw; }; -#endif /* end of include guard: */ +#endif /* end of include guard: */ diff --git a/src/cObject.cpp b/src/cObject.cpp index a54d4e1..5440c28 100644 --- a/src/cObject.cpp +++ b/src/cObject.cpp @@ -61,9 +61,9 @@ void cObject::setSize(unsigned int _sx, unsigned int _sy) for (unsigned int i = 0; i < _sx; i++) cScreen[i] = (char*)malloc(sizeof *cScreen[i] * _sy); - wColor = (WORD**)malloc(sizeof *wColor * _sx); + wColor = (uint16_t**)malloc(sizeof *wColor * _sx); for (unsigned int i = 0; i < _sx; i++) - wColor[i] = (WORD*)malloc(sizeof *wColor[i] * _sy); + wColor[i] = (uint16_t*)malloc(sizeof *wColor[i] * _sy); for (unsigned int i = 0; i < sizeY; i++) { for (unsigned int o = 0; o < sizeX; o++) { diff --git a/src/cRender.cpp b/src/cRender.cpp index 5e7677f..4ade5e3 100644 --- a/src/cRender.cpp +++ b/src/cRender.cpp @@ -1,7 +1,14 @@ #include "cRender.h" +uint16_t ansi_color_fg(uint8_t R, uint8_t G, uint8_t B) { + return 16 + 36 * (R/51) + 6 * (G/51) + (B/51); +} + +uint16_t ansi_color_bg(uint8_t R, uint8_t G, uint8_t B) { + return ansi_color_fg(R,G,B) << 8; +} -cRender::cRender(char _backound, WORD _color) +cRender::cRender(char _backound, uint16_t _color) { bBlockRender = false; //If this Constructor is used, this instance is not inherited, thus render() doesn't need to be blocked iLastError = _OK_; @@ -75,7 +82,7 @@ cRender::~cRender() #endif //__linux__ } -int cRender::drawPoint(char _c, sPos _pos, WORD _color) +int cRender::drawPoint(char _c, sPos _pos, uint16_t _color) { if (_pos.x >= (int)sizeX || _pos.y >= (int)sizeY || _pos.x < 0 || _pos.y < 0) return _ERR_COORDINATES_INVALID_; @@ -101,7 +108,7 @@ int cRender::drawPoint(char _c, sPos _pos, WORD _color) return 0; } -int cRender::drawLine(char _c, sPos _pos1, sPos _pos2, WORD _color) +int cRender::drawLine(char _c, sPos _pos1, sPos _pos2, uint16_t _color) { if(_pos1.x > _pos2.x) { @@ -155,7 +162,7 @@ int cRender::drawLine(char _c, sPos _pos1, sPos _pos2, WORD _color) return 0; }//drawLine -int cRender::drawText(string _s, sPos _pos, WORD _color) +int cRender::drawText(string _s, sPos _pos, uint16_t _color) { for (unsigned int i = 0; i < _s.length(); i++) { @@ -164,7 +171,7 @@ int cRender::drawText(string _s, sPos _pos, WORD _color) return 0; } -int cRender::drawRectangle(char _border, char _fill, sPos _pos1, sPos _pos2, WORD _borderColor, WORD _fillColor) +int cRender::drawRectangle(char _border, char _fill, sPos _pos1, sPos _pos2, uint16_t _borderColor, uint16_t _fillColor) { //Draw the four outside lines drawLine(_border, _pos1, sPos{ _pos1.x, _pos2.y }, _borderColor); @@ -219,28 +226,28 @@ int cRender::render(void) char buffer[ buffer_len ]; char colorstr[ colorstr_len ]; - uint8_t color[3] = {(uint8_t) (0x0000ff & wColor[o][i]), //Color - (uint8_t)((0x00ff00 & wColor[o][i]) >> 8), //Background - (uint8_t)((0xff0000 & wColor[o][i]) >> 16)};//Modifier + uint8_t color[2] = {(uint8_t) (0x00ff & wColor[o][i]), //Color + (uint8_t)((0xff00 & wColor[o][i]) >> 8)}; //Background {//// int cc = 0; - cc = cc + snprintf(&colorstr[cc], colorstr_len - cc - 1, "%u", color[0]); - if(color[1]) - { - colorstr[cc] = ';'; - colorstr[cc + 1] = color[1]; - cc += 1 + snprintf(&colorstr[cc + 1],colorstr_len - cc - 1, "%u", color[1]); + if(color[0] != _COL_DEFAULT && color[0] != wDefColor) { + cc = cc + snprintf(&colorstr[cc], colorstr_len - cc - 1, "38;5;%u", color[0]); + } + else { + colorstr[0]='0'; + colorstr[1]='\0'; + cc+=2; } - if(color[2]) - { - colorstr[cc] = ';'; - cc += 1 + snprintf(&colorstr[cc + 1], colorstr_len - cc - 1, "%u", color[2]); + if(color[1]) { + colorstr[cc++] = ';'; + //colorstr[cc + 1] = color[1]; + cc += snprintf(&colorstr[cc],colorstr_len - cc - 1, "48;5;%u", color[1]); } }//// int cbuf = snprintf(buffer, buffer_len - 1,"\e[%u;%uH\e[%sm%c\e[0m", i + 1, o + 1, colorstr, cScreen[o][i]); - // Position Color Origin is at 1,1 + // Position Color Origin is at 1,1 if(!bMute) write (STDOUT_FILENO, buffer, cbuf); @@ -391,9 +398,9 @@ void cRender::setBufferSize(sPos _size) for (unsigned int i = 0; i < sizeX; i++) cScreen[i] = (char*)malloc(sizeof *cScreen[i] * sizeY); - wColor = (WORD**)malloc(sizeof *wColor * sizeX); + wColor = (uint16_t**)malloc(sizeof *wColor * sizeX); for (unsigned int i = 0; i < sizeX; i++) - wColor[i] = (WORD*)malloc(sizeof *wColor[i] * sizeY); + wColor[i] = (uint16_t*)malloc(sizeof *wColor[i] * sizeY); bChanged = (bool**)malloc(sizeof *bChanged * sizeX); for (unsigned int i = 0; i < sizeX; i++) @@ -420,7 +427,7 @@ void cRender::setConsoleEcho(bool _enable) { #ifdef WIN32 HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); - DWORD mode; + uint16_t mode; GetConsoleMode(hStdin, &mode); if( !_enable ) diff --git a/src/cRender.h b/src/cRender.h index 5b6189e..e3067bc 100644 --- a/src/cRender.h +++ b/src/cRender.h @@ -14,7 +14,7 @@ #include #include - typedef uint32_t WORD; + //typedef uint32_t uint16_t; #elif _WIN32 #error "Not ported" #include @@ -50,30 +50,24 @@ //FG #define _COL_DEFAULT 0x00 - #define _COL_BLACK 0x1e//30 - #define _COL_RED 0x1f//31 - #define _COL_GREEN 0x20//32 - #define _COL_YELLOW 0x21//33 - #define _COL_BLUE 0x22//34 - #define _COL_WHITE 0x25//37 - - //BG - #define _COL_BLACK_BG 0x1e00 + 0x0a00//30 - #define _COL_RED_BG 0x1f00 + 0x0a00//31 - #define _COL_GREEN_BG 0x2000 + 0x0a00//32 - #define _COL_YELLOW_BG 0x2100 + 0x0a00//33 - #define _COL_BLUE_BG 0x2200 + 0x0a00//34 - #define _COL_WHITE_BG 0x2500 + 0x0a00//37 - - //MOD - #define _COL_BOLD 0x010000 - #define _COL_UNDERLINE 0x040000 - #define _COL_INVERSE 0x070000 - - //Not needed - #define _COL_BOLD_OFF 21 - #define _COL_UNDERLINE_OFF 24 - #define _COL_INVERSE_OFF 27 + #define _COL_BLACK 16//30 + #define _COL_RED 9//31 + #define _COL_GREEN 10//32 + #define _COL_YELLOW 11//33 + #define _COL_BLUE 12//34 + #define _COL_MAGENTA 13 + #define _COL_LBLUE 14 + #define _COL_WHITE 15//37 + + #define _COL_DEFAULT_BG 0x00 + #define _COL_BLACK_BG 16<<8//30 + #define _COL_RED_BG 9<<8//31 + #define _COL_GREEN_BG 10<<8//32 + #define _COL_YELLOW_BG 11<<8//33 + #define _COL_BLUE_BG 12<<8//34 + #define _COL_MAGENTA_BG 13<<8 + #define _COL_LBLUE_BG 14<<8 + #define _COL_WHITE_BG 15<<8//37 #endif // __linux__ @@ -84,6 +78,11 @@ struct sPos int x; int y; }; + +uint16_t ansi_color_fg(uint8_t R, uint8_t G, uint8_t B); + +uint16_t ansi_color_bg(uint8_t R, uint8_t G, uint8_t B); + /** cRender manages a framebuffer the size of the console (window) it is run in. * * puts console in alternate screen mode @@ -96,27 +95,27 @@ public: * Resizes console window for Windows * Sets Size to Console Window Size for Linux. Writes Error for _sx or _sy smaller than Screen. Get by getLastError() */ - cRender(char _backound, WORD _color); + cRender(char _backound, uint16_t _color); virtual ~cRender(); /** Draws _c @ _pos in screenbuffer * _color can be combined by OR: (FG | BG | MOD). Anyone can be left out. */ - int drawPoint(char _c, sPos _pos, WORD _color); + int drawPoint(char _c, sPos _pos, uint16_t _color); /** draws Line from _pos1 to _pos2 in screenbuffer */ - int drawLine(char _c, sPos _pos1, sPos _pos2, WORD _color); + int drawLine(char _c, sPos _pos1, sPos _pos2, uint16_t _color); /** Draws Text _s @ _pos in screenbuffer * First char is @ _pos */ - int drawText(string _s, sPos _pos, WORD _color); + int drawText(string _s, sPos _pos, uint16_t _color); /** writes rectangle to screenbuffer */ - int drawRectangle(char _border, char _fill, sPos _pos1, sPos _pos2, WORD _borderColor, WORD _fillColor); + int drawRectangle(char _border, char _fill, sPos _pos1, sPos _pos2, uint16_t _borderColor, uint16_t _fillColor); /** Dumps screenbuffer to stdout * prints changed pixels @@ -177,14 +176,14 @@ protected: char **cScreen; //* Pixel Map - WORD **wColor; + uint16_t **wColor; //* Color Map bool **bChanged; //* Pixel Change Map char cBackound; //* Default backround - WORD wBackColor; + uint16_t wBackColor; //* Default backround color unsigned int sizeX, sizeY; //* Size of screen array @@ -200,7 +199,7 @@ protected: CONSOLE_SCREEN_BUFFER_INFO csbi; #endif - WORD wDefColor; + uint16_t wDefColor; //* Default Color int iLastError; diff --git a/src/cWiremesh.cpp b/src/cWiremesh.cpp index 537f98a..0778849 100644 --- a/src/cWiremesh.cpp +++ b/src/cWiremesh.cpp @@ -4,7 +4,7 @@ cWiremesh::cWiremesh() : position({0,0,0}), angle({0,0,0}) { } cWiremesh::~cWiremesh() { } -void cWiremesh::addVector(sCoord3d _origin, sCoord3d _vector, char _char, WORD _color) +void cWiremesh::addVector(sCoord3d _origin, sCoord3d _vector, char _char, uint16_t _color) { vectors.push_back(sVector{_origin, _vector, _char, _color}); } diff --git a/src/cWiremesh.h b/src/cWiremesh.h index 82d3bd6..89f8fe3 100644 --- a/src/cWiremesh.h +++ b/src/cWiremesh.h @@ -39,7 +39,7 @@ struct sVector sCoord3d direction; char c; - WORD color; + uint16_t color; }; /** @@ -56,7 +56,7 @@ public: /** * Add a line from _origin to (_origin + _vector) in 3D space. */ - void addVector(sCoord3d _origin, sCoord3d _vector, char _char, WORD _color); + void addVector(sCoord3d _origin, sCoord3d _vector, char _char, uint16_t _color); /** * Rotates by (x,y,z) degrees around the corresponding axis. -- cgit v1.2.3