Advertisement
Redxone

[C++] Computercraft API

Aug 19th, 2016
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.05 KB | None | 0 0
  1. #ifndef COMPUTERCRAFT_H
  2. #define COMPUTERCRAFT_H
  3.  
  4.  
  5. class Color
  6. {
  7.     public:
  8.         int black      =  0;
  9.         int blue       =  1;
  10.         int green      =  2;
  11.         int cyan       =  3;
  12.         int red        =  4;
  13.         int purple     =  5;
  14.         int brown      =  6;
  15.         int lightGray  =  7;
  16.         int gray       =  8;
  17.         int lightBlue  =  9;
  18.         int lime       = 10;
  19.         int neonBlue   = 11;
  20.         int neonRed    = 12;
  21.         int neonPurple = 13;
  22.         int yellow     = 14;
  23.         int white      = 15;
  24. };
  25.  
  26.  
  27. class Term
  28. {
  29.     public:
  30.         WORD curTextColor = 0x0F;
  31.         WORD curBackColor = 0x00;
  32.         WORD colorbit = curBackColor + curTextColor;
  33.         int printx = 1;
  34.         int printy = 1;
  35.  
  36.  
  37.  
  38.  
  39.         const WORD textcolors[20]
  40.         {
  41.             0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
  42.             0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
  43.             0x0C, 0x0D, 0x0E, 0x0F
  44.         };
  45.  
  46.         const WORD backcolors[20]
  47.         {
  48.             0x00, 0x10, 0x20, 0x30, 0x40, 0x50,
  49.             0x60, 0x70, 0x80, 0x90, 0xA0, 0xB0,
  50.             0xC0, 0xD0, 0xE0, 0xF0
  51.         };
  52.  
  53.     // Color Setting \\
  54.  
  55.         void setTextColor(WORD newtextcolor)
  56.         {
  57.             curTextColor = textcolors[newtextcolor];
  58.             WORD colorbit = curBackColor + curTextColor;
  59.             SetConsoleTextAttribute(GetStdHandle( STD_OUTPUT_HANDLE ), colorbit);
  60.  
  61.         };
  62.  
  63.         void setCursorBlink(bool showFlag)
  64.         {
  65.             HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
  66.  
  67.             CONSOLE_CURSOR_INFO     cursorInfo;
  68.  
  69.             GetConsoleCursorInfo(out, &cursorInfo);
  70.             cursorInfo.bVisible = showFlag; // set the cursor visibility
  71.             SetConsoleCursorInfo(out, &cursorInfo);
  72.         }
  73.  
  74.  
  75.         void setBackgroundColor(WORD newbackcolor)
  76.         {
  77.             curBackColor = backcolors[newbackcolor];
  78.             WORD colorbit = curBackColor + curTextColor;
  79.             SetConsoleTextAttribute(GetStdHandle( STD_OUTPUT_HANDLE ), colorbit);
  80.         };
  81.  
  82.         void setColorFromBit(WORD bit)
  83.         {
  84.             SetConsoleTextAttribute(GetStdHandle( STD_OUTPUT_HANDLE ), bit);
  85.             WORD colorbit = bit;
  86.         };
  87.  
  88.         void Clear()
  89.         {
  90.             system("cls");
  91.         };
  92.  
  93.         void setCursorPos(int x, int y)
  94.         {
  95.             COORD pos = {x, y};
  96.             printx = x;
  97.             printy = y;
  98.             SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
  99.         };
  100.  
  101.         void write(char* text)
  102.         {
  103.             DWORD dwBytesWritten = 0;
  104.             WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), text, strlen(text), &dwBytesWritten, NULL);
  105.         };
  106.  
  107.     protected:
  108.  
  109.     private:
  110. };
  111.  
  112. class Paintutils: public Term
  113. {
  114.     public:
  115.  
  116.         Term *trm;
  117.  
  118.         void initialize(Term *nterm)
  119.         {
  120.             trm = nterm;
  121.         };
  122.  
  123.         void drawBox(int sx, int sy, int ex, int ey, int color)
  124.         {
  125.             int prevTextColor = trm->curTextColor;
  126.             int prevBackColor = trm->curBackColor;
  127.             trm->setCursorPos(sx,sy);
  128.             trm->setBackgroundColor(color);
  129.  
  130.             for(int xx = sx; xx < ex; xx++)
  131.             {
  132.                 trm->setCursorPos(xx,sy);
  133.                 trm->write(" ");
  134.             };
  135.  
  136.             for(int yy = sy; yy < ey; yy++)
  137.             {
  138.                 trm->setCursorPos(sx,yy);
  139.                 trm->write(" ");
  140.             };
  141.  
  142.             for(int yy = sy; yy < ey; yy++)
  143.             {
  144.                 trm->setCursorPos(ex,yy);
  145.                 trm->write(" ");
  146.             };
  147.  
  148.             for(int xx = sx; xx < ex+1; xx++)
  149.             {
  150.                 trm->setCursorPos(xx,ey);
  151.                 trm->write(" ");
  152.             };
  153.         };
  154.  
  155.         void drawBoxFilled(int sx, int sy, int ex, int ey, int colorborder, int color)
  156.         {
  157.             int prevTextColor = trm->curTextColor;
  158.             int prevBackColor = trm->curBackColor;
  159.             trm->setCursorPos(sx,sy);
  160.             trm->setBackgroundColor(colorborder);
  161.  
  162.             for(int xx = sx; xx < ex; xx++)
  163.             {
  164.                 trm->setCursorPos(xx,sy);
  165.                 trm->write(" ");
  166.             };
  167.  
  168.             for(int yy = sy; yy < ey; yy++)
  169.             {
  170.                 trm->setCursorPos(sx,yy);
  171.                 trm->write(" ");
  172.             };
  173.  
  174.             for(int yy = sy; yy < ey; yy++)
  175.             {
  176.                 trm->setCursorPos(ex,yy);
  177.                 trm->write(" ");
  178.             };
  179.  
  180.             for(int xx = sx; xx < ex+1; xx++)
  181.             {
  182.                 trm->setCursorPos(xx,ey);
  183.                 trm->write(" ");
  184.             };
  185.  
  186.             // fill inner rectangle.
  187.             trm->setBackgroundColor(color);
  188.  
  189.             for(int yy = sy+1; yy < ey; yy++)
  190.             {
  191.                 for(int xx = sx+1; xx < ex; xx++)
  192.                 {
  193.                     trm->setCursorPos(xx,yy);
  194.                     trm->write(" ");
  195.                 };
  196.             };
  197.  
  198.         };
  199. };
  200.  
  201. #endif // CONSOLECOLOR_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement