diff options
-rw-r--r-- | example/3d.cpp | 28 | ||||
-rw-r--r-- | example/collision.cpp | 42 | ||||
-rw-r--r-- | example/pong.cpp | 136 | ||||
-rw-r--r-- | example/test.cpp | 110 | ||||
-rw-r--r-- | src/cInput.cpp | 110 | ||||
-rw-r--r-- | src/cInput.h | 50 | ||||
-rw-r--r-- | src/cObject.cpp | 4 | ||||
-rw-r--r-- | src/cRender.cpp | 51 | ||||
-rw-r--r-- | src/cRender.h | 65 | ||||
-rw-r--r-- | src/cWiremesh.cpp | 2 | ||||
-rw-r--r-- | src/cWiremesh.h | 4 |
11 files changed, 304 insertions, 298 deletions
diff --git a/example/3d.cpp b/example/3d.cpp index e488da1..1b7e9e1 100644 --- a/example/3d.cpp +++ b/example/3d.cpp @@ -14,27 +14,27 @@ class testobject : cObject { public: - testobject() - { - setSize(10,5); - cc = 0; + testobject() + { + setSize(10,5); + cc = 0; - drawRectangle('#', NULL, {0,0}, {9,4}, _COL_GREEN, _COL_DEFAULT); - } + drawRectangle('#', NULL, {0,0}, {9,4}, _COL_GREEN, _COL_DEFAULT); + } - ~testobject() { destruct(); } + ~testobject() { destruct(); } - virtual void onClick(sPos _pos, unsigned int _button) - { - cc++; - drawText(std::to_string(cc), {2,2}, _COL_RED); + virtual void onClick(sPos _pos, unsigned int _button) + { + cc++; + drawText(std::to_string(cc), {2,2}, _COL_RED); - drawPoint('Q', _pos, _COL_YELLOW); - } + drawPoint('Q', _pos, _COL_YELLOW); + } virtual void onChar(unsigned char _c) { drawPoint(_c, {1,1}, _COL_BLUE); } private: - int cc; + int cc; }; int main(int argc, char* argv[]) diff --git a/example/collision.cpp b/example/collision.cpp index e46c423..3dd823f 100644 --- a/example/collision.cpp +++ b/example/collision.cpp @@ -14,35 +14,35 @@ class testobject : cObject { public: - testobject() - { - setSize(10,5); - cc = 0; - kc = 0; + testobject() + { + setSize(10,5); + cc = 0; + kc = 0; - drawRectangle('#', 0, {0,0}, {9,4}, _COL_GREEN, _COL_DEFAULT); - } + drawRectangle('#', 0, {0,0}, {9,4}, _COL_GREEN, _COL_DEFAULT); + } - ~testobject() { destruct(); } + ~testobject() { destruct(); } - virtual void onClick(sPos _pos, unsigned int _button) - { - cc++; - drawText(std::to_string(cc), {2,2}, _COL_RED); + virtual void onClick(sPos _pos, unsigned int _button) + { + cc++; + drawText(std::to_string(cc), {2,2}, _COL_RED); - drawPoint('Q', _pos, _COL_YELLOW); - } + drawPoint('Q', _pos, _COL_YELLOW); + } - virtual bool onCollisionActive(sPos _hit, int _passiveObject){ - kc++; - drawText(std::to_string(kc), {0,0}, _COL_RED); - return true; - } + virtual bool onCollisionActive(sPos _hit, int _passiveObject){ + kc++; + drawText(std::to_string(kc), {0,0}, _COL_RED); + return true; + } virtual void onChar(unsigned char _c) { drawPoint(_c, {1,1}, _COL_BLUE); } private: - int cc; - int kc; + int cc; + int kc; }; int main(int argc, char* argv[]) diff --git a/example/pong.cpp b/example/pong.cpp index df91b53..2716fa7 100644 --- a/example/pong.cpp +++ b/example/pong.cpp @@ -7,93 +7,93 @@ class ball : public cObject { public: - ball() : v({1,1}) { setSize(1,1); drawPoint('O', {0,0}, _COL_RED); } - ~ball() { destruct(); } - virtual bool onCollisionActive(sPos _hit, int _passiveObject) - { - if(_passiveObject == 1) - v.y = v.y * (-1); - else if (_passiveObject == 2) - v.x = v.x * (-1); - - drawText(std::to_string(_passiveObject), {0,0}, _COL_GREEN); - return true; - } - sPos getV() { return v; } + ball() : v({1,1}) { setSize(1,1); drawPoint('O', {0,0}, ansi_color_fg(0,0,255) | ansi_color_bg(255,0,0)); } + ~ball() { destruct(); } + virtual bool onCollisionActive(sPos _hit, int _passiveObject) + { + if(_passiveObject == 1) + v.y = v.y * (-1); + else if (_passiveObject == 2) + v.x = v.x * (-1); + + //drawText(std::to_string(_passiveObject), {0,0}, _COL_GREEN); + return true; + } + sPos getV() { return v; } private: - sPos v; + sPos v; }; class bar : public cObject { public: - bar() { setSize(1,5); drawLine('|', {0,0},{0,4}, _COL_BLUE);} - ~bar() { destruct(); } - virtual int onCollisionPassive(sPos _hit){ return 2; } - virtual bool onCollisionActive(sPos _hit, int _passiveObject){ return true; } + bar() { setSize(1,5); drawLine('|', {0,0},{0,4}, _COL_BLUE);} + ~bar() { destruct(); } + virtual int onCollisionPassive(sPos _hit){ return 2; } + virtual bool onCollisionActive(sPos _hit, int _passiveObject){ return true; } }; class edge : public cObject { public: - edge(unsigned int x, unsigned int y) { setSize(x,y); drawLine('#', {0,0},{(int)x-1,(int)y-1}, _COL_DEFAULT);} - ~edge() { destruct(); } - virtual int onCollisionPassive(sPos _hit) - { - if(getSize().x > getSize().y) - return 1; - else - return 2; - } + edge(unsigned int x, unsigned int y) { setSize(x,y); drawLine('#', {0,0},{(int)x-1,(int)y-1}, _COL_DEFAULT);} + ~edge() { destruct(); } + virtual int onCollisionPassive(sPos _hit) + { + if(getSize().x > getSize().y) + return 1; + else + return 2; + } }; int main() { - cRender render(' ', _COL_DEFAULT); - cObjectHandler screen(&render); - cInput input; + cRender render(' ', _COL_DEFAULT); + cObjectHandler screen(&render); + cInput input; - render.render(); - render.setTargetFPS(20); + render.render(); + render.setTargetFPS(20); - sPos size = render.getSize(); + sPos size = render.getSize(); - bar barLeft; - bar barRight; - ball aball; - edge edgeLeft(1,size.y - 2); - edge edgeRight(1,size.y - 2); - edge edgeTop(size.x - 2,1); - edge edgeBottom(size.x - 2,1); + bar barLeft; + bar barRight; + ball aball; + edge edgeLeft(1,size.y - 2); + edge edgeRight(1,size.y - 2); + edge edgeTop(size.x - 2,1); + edge edgeBottom(size.x - 2,1); - int iEdgeTop = screen.createObject(&edgeTop); - screen.moveObject(iEdgeTop, {0,0}, _MOVE_FORCE_ABSOLUTE); + int iEdgeTop = screen.createObject(&edgeTop); + screen.moveObject(iEdgeTop, {0,0}, _MOVE_FORCE_ABSOLUTE); - int iEdgeBottom = screen.createObject(&edgeBottom); - screen.moveObject(iEdgeBottom, {0,size.y - 3}, _MOVE_FORCE_ABSOLUTE); + int iEdgeBottom = screen.createObject(&edgeBottom); + screen.moveObject(iEdgeBottom, {0,size.y - 3}, _MOVE_FORCE_ABSOLUTE); - int iEdgeLeft = screen.createObject(&edgeLeft); - screen.moveObject(iEdgeLeft, {0,0}, _MOVE_FORCE_ABSOLUTE); + int iEdgeLeft = screen.createObject(&edgeLeft); + screen.moveObject(iEdgeLeft, {0,0}, _MOVE_FORCE_ABSOLUTE); - int iEdgeRight = screen.createObject(&edgeRight); - screen.moveObject(iEdgeRight, {size.x - 2,0}, _MOVE_FORCE_ABSOLUTE); + int iEdgeRight = screen.createObject(&edgeRight); + screen.moveObject(iEdgeRight, {size.x - 2,0}, _MOVE_FORCE_ABSOLUTE); - int iAball= screen.createObject(&aball); - screen.moveObject(iAball, {size.x/2,size.y/2}, _MOVE_FORCE_ABSOLUTE); + int iAball= screen.createObject(&aball); + screen.moveObject(iAball, {size.x/2,size.y/2}, _MOVE_FORCE_ABSOLUTE); - unsigned int iBarLeft = screen.createObject(&barLeft); - screen.moveObject(iBarLeft, {10,4}, _MOVE_FORCE_ABSOLUTE); + unsigned int iBarLeft = screen.createObject(&barLeft); + screen.moveObject(iBarLeft, {10,4}, _MOVE_FORCE_ABSOLUTE); - unsigned int iBarRight = screen.createObject(&barRight); - screen.moveObject(iBarRight, {size.x - 10,4}, _MOVE_FORCE_ABSOLUTE); + unsigned int iBarRight = screen.createObject(&barRight); + screen.moveObject(iBarRight, {size.x - 10,4}, _MOVE_FORCE_ABSOLUTE); - unsigned int cc = 0; + unsigned int cc = 0; - while(1) - { - sInputEvent ie = input.poll(); + while(1) + { + sInputEvent ie = input.poll(); if(ie.type != _EVENT_NULL) { @@ -124,22 +124,22 @@ int main() break; case 's': screen.moveObject(iBarLeft, {0,1}, _MOVE_RELATIVE); - break; - case 'g': - render.enableDebugInfo(true); + break; + case 'g': + render.enableDebugInfo(true); }; } else if (ie.type == _EVENT_TERM) { return 0; } - } - if(!(++cc % 3)) - screen.moveObject(iAball, aball.getV(), _MOVE_RELATIVE); + } + if(!(++cc % 3)) + screen.moveObject(iAball, aball.getV(), _MOVE_RELATIVE); - screen.write(); - render.render(); - } + screen.write(); + render.render(); + } - return 0; + return 0; } diff --git a/example/test.cpp b/example/test.cpp index 7c1e3a0..034c365 100644 --- a/example/test.cpp +++ b/example/test.cpp @@ -6,88 +6,88 @@ class ball : public cObject { public: - ball() : v({1,1}) { setSize(1,1); drawPoint('O', {0,0}, _COL_RED); } - ~ball() { destruct(); } - virtual bool onCollisionActive(sPos _hit, int _passiveObject) - { - if(_passiveObject == 1) - v.y = v.y * (-1); - else if (_passiveObject == 2) - v.x = v.x * (-1); - - drawText(std::to_string(_passiveObject), {0,0}, _COL_GREEN); - return true; - } - sPos getV() { return v; } + ball() : v({1,1}) { setSize(1,1); drawPoint('O', {0,0}, _COL_RED); } + ~ball() { destruct(); } + virtual bool onCollisionActive(sPos _hit, int _passiveObject) + { + if(_passiveObject == 1) + v.y = v.y * (-1); + else if (_passiveObject == 2) + v.x = v.x * (-1); + + drawText(std::to_string(_passiveObject), {0,0}, _COL_GREEN); + return true; + } + sPos getV() { return v; } private: - sPos v; + sPos v; }; class edge : public cObject { public: - edge(unsigned int x, unsigned int y) { setSize(x,y); drawLine('#', {0,0},{(int)x-1,(int)y-1}, _COL_DEFAULT);} - ~edge() { destruct(); } - virtual int onCollisionPassive(sPos _hit) - { - if(getSize().x > getSize().y) - return 1; - else - return 2; - } + edge(unsigned int x, unsigned int y) { setSize(x,y); drawLine('#', {0,0},{(int)x-1,(int)y-1}, _COL_DEFAULT);} + ~edge() { destruct(); } + virtual int onCollisionPassive(sPos _hit) + { + if(getSize().x > getSize().y) + return 1; + else + return 2; + } }; int main() { - cRender render(' ', _COL_DEFAULT); - cObjectHandler screen(&render); - cInput input; + cRender render(' ', _COL_DEFAULT); + cObjectHandler screen(&render); + cInput input; - render.forceScreenSize({20,20}); - render.mute(true); + render.forceScreenSize({20,20}); + render.mute(true); - ball aball; - edge edgeLeft(1,10); - edge edgeRight(1,10); - edge edgeTop(10,1); - edge edgeBottom(10,1); + ball aball; + edge edgeLeft(1,10); + edge edgeRight(1,10); + edge edgeTop(10,1); + edge edgeBottom(10,1); - int iEdgeTop = screen.createObject(&edgeTop); - screen.moveObject(iEdgeTop, {0,0}, _MOVE_FORCE_ABSOLUTE); + int iEdgeTop = screen.createObject(&edgeTop); + screen.moveObject(iEdgeTop, {0,0}, _MOVE_FORCE_ABSOLUTE); - int iEdgeBottom = screen.createObject(&edgeBottom); - screen.moveObject(iEdgeBottom, {0,10}, _MOVE_FORCE_ABSOLUTE); + int iEdgeBottom = screen.createObject(&edgeBottom); + screen.moveObject(iEdgeBottom, {0,10}, _MOVE_FORCE_ABSOLUTE); - int iEdgeLeft = screen.createObject(&edgeLeft); - screen.moveObject(iEdgeLeft, {0,0}, _MOVE_FORCE_ABSOLUTE); + int iEdgeLeft = screen.createObject(&edgeLeft); + screen.moveObject(iEdgeLeft, {0,0}, _MOVE_FORCE_ABSOLUTE); - int iEdgeRight = screen.createObject(&edgeRight); - screen.moveObject(iEdgeRight, {9,0}, _MOVE_FORCE_ABSOLUTE); + int iEdgeRight = screen.createObject(&edgeRight); + screen.moveObject(iEdgeRight, {9,0}, _MOVE_FORCE_ABSOLUTE); - int iAball= screen.createObject(&aball); - screen.moveObject(iAball, {2,2}, _MOVE_FORCE_ABSOLUTE); + int iAball= screen.createObject(&aball); + screen.moveObject(iAball, {2,2}, _MOVE_FORCE_ABSOLUTE); - render.render(); + render.render(); - for(unsigned int cc = 0; cc < 9999; cc++) - { + for(unsigned int cc = 0; cc < 9999; cc++) + { - sInputEvent ie = input.poll(); + sInputEvent ie = input.poll(); if(ie.type != _EVENT_NULL) { - if (ie.type == _EVENT_TERM) + if (ie.type == _EVENT_TERM) { return 0; } - } + } - if(!(++cc % 3)) - screen.moveObject(iAball, aball.getV(), _MOVE_RELATIVE); + if(!(++cc % 3)) + screen.moveObject(iAball, aball.getV(), _MOVE_RELATIVE); - screen.write(); - render.render(); - } + screen.write(); + render.render(); + } - return 0; + return 0; } 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 <sys/ioctl.h> #include <stdint.h> - typedef uint32_t WORD; + //typedef uint32_t uint16_t; #elif _WIN32 #error "Not ported" #include <Windows.h> @@ -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. |