Advertisement
Redxone

[C++] DisplayBlocks console graphics namespace (WIP)

May 11th, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.45 KB | None | 0 0
  1. #include <sstream>
  2. #include <windows.h>
  3. #include <math.h>
  4. // For file loading
  5. #include<iostream>
  6. #include<fstream>
  7.  
  8. #ifndef DISPLAY_H_INCLUDED
  9. #define DISPLAY_H_INCLUDED
  10.  
  11.    // Define namespace for blocks.
  12.  
  13.     namespace DisplayBlocks
  14.     {
  15.         // Define screen parameters
  16.         int SCR_WIDTH  = 80;
  17.         int SCR_HEIGHT = 25;
  18.         int SCR_POS_X  = 0;
  19.         int SCR_POS_Y  = 0;
  20.         short SCR_COLOR = 0xF0;
  21.  
  22.         const short textColors[20]
  23.         {
  24.             0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
  25.             0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
  26.             0x0C, 0x0D, 0x0E, 0x0F
  27.         };
  28.  
  29.         const short backColors[20]
  30.         {
  31.             0x00, 0x10, 0x20, 0x30, 0x40, 0x50,
  32.             0x60, 0x70, 0x80, 0x90, 0xA0, 0xB0,
  33.             0xC0, 0xD0, 0xE0, 0xF0
  34.         };
  35.  
  36.         namespace colors {
  37.             int black      =  0;
  38.             int blue       =  1;
  39.             int green      =  2;
  40.             int cyan       =  3;
  41.             int red        =  4;
  42.             int purple     =  5;
  43.             int brown      =  6;
  44.             int lightGray  =  7;
  45.             int gray       =  8;
  46.             int lightBlue  =  9;
  47.             int lime       = 10;
  48.             int neonBlue   = 11;
  49.             int neonRed    = 12;
  50.             int neonPurple = 13;
  51.             int yellow     = 14;
  52.             int white      = 15;
  53.         };
  54.  
  55.         // Screen buffer events
  56.         void setResolution(int w, int h)
  57.         {
  58.             SCR_WIDTH = w;
  59.             SCR_HEIGHT = h;
  60.  
  61.  
  62.             // C++ stupidly long and only way to convert an int to a string.
  63.             std::ostringstream stream;
  64.             stream << SCR_WIDTH;
  65.             std::string STR_WIDTH = stream.str();
  66.             stream.str("");
  67.             stream.clear();
  68.             stream << SCR_HEIGHT;
  69.             std::string STR_HEIGHT = stream.str();
  70.  
  71.             //Resize CMD
  72.             system( ("mode " + STR_WIDTH + "," + STR_HEIGHT).c_str());
  73.         };
  74.  
  75.         void setScreenPos(int x, int y)
  76.         {
  77.             SCR_POS_X = x-1;
  78.             SCR_POS_Y = y-1;
  79.             COORD pos = {SCR_POS_X, SCR_POS_Y};
  80.             SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
  81.         }
  82.  
  83.         int getScreenX(){
  84.             return SCR_POS_X;
  85.         };
  86.         int getScreenY(){
  87.             return SCR_POS_Y;
  88.         };
  89.  
  90.         // Pixel manipulation
  91.         void setColors(int textcolor, int backcolor){
  92.             // Usually used in unison with the Colors namespace.
  93.             // Takes 2 bytes and adds them together, Back being MSB, and Text being LSB.
  94.             SCR_COLOR = backColors[backcolor] + textColors[textcolor];
  95.             // Set console color
  96.             SetConsoleTextAttribute(GetStdHandle( STD_OUTPUT_HANDLE ), SCR_COLOR);
  97.         };
  98.  
  99.  
  100.         void drawPixel(int x, int y){
  101.             COORD pos = {x-1, y-1};
  102.             SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
  103.             std::cout << " ";
  104.             // Load old pos.
  105.             COORD dpos = {SCR_POS_X, SCR_POS_Y};
  106.             SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
  107.         };
  108.         void write(std::string text){
  109.             std::cout << text;
  110.         };
  111.  
  112.     /*
  113.         std::string loadPixelStringFrom(std::string file)
  114.         {
  115.             std::ifstream input( (char*) &file[0] );
  116.             std::string online;
  117.             int next = 0;
  118.             std::string pixels = "";
  119.             while( std::getline( input, online ) ) {
  120.                 // Convert string into array of chars
  121.                 for(int c = 0; c < online.length(); c++)
  122.                 {
  123.                     pixels += online[c];
  124.                 }
  125.             }
  126.             input.close();
  127.             return pixels;
  128.         }
  129.  
  130.     */
  131.  
  132.         void drawPixelsFrom(std::string file)
  133.         {
  134.             std::ifstream input( (char*) &file[0] );
  135.             std::string online;
  136.             int dx = SCR_POS_X;
  137.             int dy = SCR_POS_Y;
  138.             while( std::getline( input, online ) ) {
  139.                 COORD pos = {dx, dy};
  140.                 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
  141.                 std::cout << online;
  142.                 dy++;
  143.             }
  144.             input.close();
  145.             COORD dpos = {SCR_POS_X, SCR_POS_Y};
  146.             SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), dpos);
  147.         }
  148.  
  149.         std::string * getStringMapFrom(std::string file)
  150.         {
  151.             std::ifstream inputLookup( (char*) &file[0] );
  152.             std::string online;
  153.             int dx = SCR_POS_X;
  154.             int dy = SCR_POS_Y;
  155.             int next = 0;
  156.             // Count size.
  157.             while( std::getline( inputLookup, online ) ) {
  158.                 next++;
  159.             }
  160.             inputLookup.close();
  161.             // Insert data into pixel map.
  162.             std::ifstream inputRead( (char*) &file[0] );
  163.             std::string* pixs = new std::string[next];
  164.             next = 0;
  165.             while( std::getline( inputRead, online ) ) {
  166.                 pixs[next] = online;
  167.                 next++;
  168.             }
  169.             inputRead.close();
  170.             return pixs;
  171.         }
  172.  
  173.     /*
  174.         void drawPixelArray(char[] arr)
  175.         {
  176.             int dx = SCR_POS_X;
  177.             int dy = SCR_POS_Y;
  178.             for(int x = 0; x < sizeof(arr); x++)
  179.             {
  180.                 if(x > SCR_WIDTH)
  181.                 {
  182.                     dy = ( x / SCR_WIDTH );
  183.                     dx = ( x - (SCR_WIDTH*dy) );
  184.                 }
  185.                 else
  186.                 {
  187.                     dx = x;
  188.                     dy = 1;
  189.                 }
  190.                 COORD pos = {dx, dy};
  191.                 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
  192.  
  193.                 std::cout << arr[x];
  194.             }
  195.         }
  196.     */
  197.         void drawPixelString(std::string arr)
  198.         {
  199.             std::string Line;
  200.             int dx = SCR_POS_X;
  201.             int dy = SCR_POS_Y;
  202.             int lines = arr.length() / SCR_WIDTH;
  203.             if(lines < 2)
  204.             {
  205.                 for(int x = 0; x < sizeof(arr); x++)
  206.                 {
  207.                     Line += arr[x];
  208.                 }
  209.                 COORD pos = {dx, dy};
  210.                 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
  211.                 std::cout << Line;
  212.             }
  213.             else
  214.             {
  215.                  for(int y = 0; y < lines; y++)
  216.                  {
  217.                      Line = "";
  218.                     for(int x = 0; x < SCR_WIDTH; x++)
  219.                     {
  220.                         Line += arr[x];
  221.                     }
  222.                     COORD pos = {dx, y+dy};
  223.                     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
  224.                     std::cout << Line;
  225.                  }
  226.             }
  227.  
  228.             COORD dpos = {SCR_POS_X, SCR_POS_Y};
  229.             SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), dpos);
  230.         }
  231.  
  232.         char readStringMap(std::string * cmap, int x, int y)
  233.         {
  234.             // Equal to string "text", where "text" is the value of the memory location of cmap plus y byte(s).
  235.             // Then a simple get char from the string at x-1.
  236.             return ((std::string&) *(cmap + y-1))[x-1];
  237.         }
  238.  
  239.         void clearScreen() {
  240.             std::cout << std::string((SCR_WIDTH*SCR_HEIGHT),'\n');
  241.         };
  242. }
  243.  
  244.  
  245. #endif // DISPLAY_H_INCLUDED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement