Advertisement
CVSoft

more correcter

Feb 21st, 2015
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.58 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////
  2. // from CVSoft: console.h
  3. #ifndef CONSOLE_H__
  4. #define CONSOLE_H__
  5.  
  6. #include <Windows.h> // because we need the COORD struct, I don't have my own (yet)
  7.  
  8. typedef struct
  9. {
  10.     unsigned short fg;
  11.     unsigned short bg;
  12. } FullColor; // this lets us combine fg and bg, but we need more methods for this x.x
  13.  
  14. class FullColorUtils
  15. {
  16. public:
  17.     FullColorUtils();
  18.     FullColor buildFC(int fg, int bg);
  19. };
  20.  
  21. class Console
  22. {
  23. public:
  24.     Console();
  25.     ~Console();
  26.     COORD setPos(int r);
  27.     COORD setPos(int r, int c);
  28.     COORD output(int r, int c, char* s);
  29.     COORD output(int r, int c, char* s, int color);
  30.     COORD output(int r, int c, char* s, int fg, int bg);
  31.     COORD output(int r, int c, char* s, FullColor fc);
  32.     int setColor(int c);
  33.     int setColor(int fg, int bg);
  34.     FullColor setColor(FullColor fc);
  35.     int getColor();
  36.     FullColor getFullColor();
  37.     int getWidth();
  38.     int getHeight();
  39.     COORD getPos();
  40.     //void test();
  41. private:
  42.     COORD currentPos_;
  43.     FullColorUtils FCUtils;
  44. };
  45.  
  46. class ConsoleUtils
  47. {
  48. public:
  49.     ConsoleUtils();
  50.     ConsoleUtils(FullColor fc);
  51.     ConsoleUtils(int fg, int bg);
  52.     ~ConsoleUtils();
  53.     FullColor setActiveColor(FullColor fc);
  54.     FullColor setDefaultColor(FullColor fc);
  55.     FullColor getActiveColor();
  56.     FullColor getDefaultColor();
  57.     void background();
  58.     void background(FullColor fc);
  59.     void background(int fg, int bg); // wasn't this something else? oh well
  60.     COORD output(int r, int c, char* s, FullColor fc);
  61.     COORD output(int r, int c, char* s);
  62.     COORD output(int r, int c, int toOut, FullColor fc);
  63.     COORD output(int r, int c, int toOut);
  64.     COORD output(int r, int c, float toOut, FullColor fc);
  65.     COORD output(int r, int c, float toOut);
  66.     COORD outputCentered(int r, char* s);
  67.     COORD outputCentered(int r, int toOut);
  68.     COORD outputCentered(int r, float toOut);
  69.     COORD outputCentered(int r, char* s, int c);
  70.     COORD outputCentered(int r, char* s, int fg, int bg);
  71.     COORD outputCentered(int r, char* s, FullColor fc);
  72.     COORD outputCentered(int r, int toOut, FullColor fc);
  73.     COORD outputCentered(int r, float toOut, FullColor fc);
  74.     void pause();
  75.     void pause(int fg, int bg);
  76. private:
  77.     Console con;
  78.     FullColorUtils FCUtils;
  79.     FullColor defaultColor_;
  80.     int bgcolor_, fgcolor_;
  81. };
  82.  
  83. // here's the colors
  84. enum ConsoleColors
  85. {
  86.     BLACK,
  87.     DARK_BLUE,
  88.     DARK_GREEN,
  89.     DARK_CYAN,
  90.     DARK_RED,
  91.     DARK_MAGENTA,
  92.     DARK_YELLOW,
  93.     LIGHT_GRAY,
  94.     DARK_GRAY,
  95.     BLUE,
  96.     GREEN,
  97.     CYAN,
  98.     RED,
  99.     MAGENTA,
  100.     YELLOW,
  101.     WHITE
  102. };
  103.  
  104. #endif
  105. ////////////////////////////////////////////////////////////////
  106. // from CVSoft: console.cpp
  107. #include <cstdio>
  108. #include <iostream>
  109. #include <iomanip>
  110. #include <conio.h>
  111. #include <Windows.h>
  112. #include "console.h"
  113.  
  114. using namespace std;
  115.  
  116. // ========== below is FullColorUtils ======== //
  117.  
  118. #pragma region FCUtils
  119. FullColorUtils::FullColorUtils()
  120. {
  121.     // not sure if anything will ever be needed here...
  122. }
  123.  
  124. FullColor FullColorUtils::buildFC(int fg, int bg)
  125. {
  126.     FullColor fc = {fg, bg};
  127.     return fc;
  128. }
  129. #pragma endregion
  130.  
  131. // ============= below is Console ============ //
  132.  
  133. #pragma region ConsoleConstructs
  134.  
  135. Console::Console()
  136. {
  137.     /*
  138.     system("cls");
  139.     setColor(7); // windows default
  140.     setPos(0, 0);
  141.     */
  142. }
  143.  
  144. Console::~Console()
  145. {
  146.     /*
  147.     system("cls");
  148.     setColor(7); // back to normal
  149.     setPos(0, 0);
  150.     */
  151. }
  152.  
  153. #pragma endregion
  154.  
  155. #pragma region ConsoleOutputs
  156.  
  157. COORD Console::output(int r, int c, char* s)
  158. {
  159.     setPos(r, c);
  160.     cout << s;
  161.     return getPos();
  162. }
  163.  
  164. COORD Console::output(int r, int c, char* s, FullColor fc)
  165. {
  166.     setPos(r, c);
  167.     setColor(fc);
  168.     cout << s;
  169.     return getPos();
  170. }
  171.  
  172. // deprecated
  173. COORD Console::output(int r, int c, char* s, int color)
  174. {
  175.     return output(r, c, s, FCUtils.buildFC(color%16, color/16));
  176. }
  177.  
  178. // deprecated
  179. COORD Console::output(int r, int c, char* s, int fg, int bg)
  180. {
  181.     return output(r, c, s, FCUtils.buildFC(fg, bg));
  182. }
  183.  
  184. #pragma endregion All outputs
  185.  
  186. #pragma region ConsoleGets
  187.  
  188. int Console::getWidth()
  189. {
  190.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  191.     GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
  192.     return (int)csbi.dwSize.X;
  193. }
  194.  
  195. int Console::getHeight()
  196. {
  197.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  198.     GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
  199.     return (int)(1+csbi.srWindow.Bottom-csbi.srWindow.Top);
  200. }
  201.  
  202. COORD Console::getPos()
  203. {
  204.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  205.     GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
  206.     currentPos_ = csbi.dwCursorPosition;
  207.     return currentPos_;
  208. }
  209.  
  210. int Console::getColor()
  211. {
  212.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  213.     GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
  214.     return (int)csbi.wAttributes % 256;
  215. }
  216.  
  217. FullColor Console::getFullColor()
  218. {
  219.     int c = getColor();
  220.     FullColor fc = {c%16, c/16};
  221.     return fc;
  222. }
  223.  
  224. #pragma endregion Color, fullcolor, position
  225.  
  226. #pragma region ConsoleSets
  227.  
  228. COORD Console::setPos(int r)
  229. {
  230.     COORD old = getPos();
  231.     COORD cpos = {old.X, r};
  232.     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cpos);
  233.     return old;
  234. }
  235.  
  236. COORD Console::setPos(int r, int c)
  237. {
  238.     COORD old = getPos();
  239.     COORD cpos = {c, r};
  240.     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cpos);
  241.     return old;
  242. }
  243.  
  244. int Console::setColor(int c)
  245. {
  246.     int old = getColor();
  247.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
  248.     return old;
  249. }
  250.  
  251. int Console::setColor(int fg, int bg)
  252. {
  253.     return setColor(fg+16*bg);
  254. }
  255.  
  256. FullColor Console::setColor(FullColor fc)
  257. {
  258.     FullColor old = getFullColor();
  259.     setColor(fc.fg+16*fc.bg);
  260.     return old;
  261. }
  262.  
  263. #pragma endregion color, position
  264.  
  265. // ========== below is ConsoleUtils ========== //
  266.  
  267. #pragma region CUtilsConstructs
  268.  
  269. ConsoleUtils::ConsoleUtils()
  270. {
  271.     int i = con.getColor(); // we save a call to con.getColor here. speed is good.
  272.     fgcolor_ = i % 16;
  273.     bgcolor_ = i / 16;
  274.     defaultColor_ = FCUtils.buildFC(fgcolor_, bgcolor_);
  275. }
  276.  
  277. ConsoleUtils::ConsoleUtils(int fg, int bg)
  278. {
  279.     fgcolor_ = fg;
  280.     bgcolor_ = bg;
  281.     defaultColor_ = FCUtils.buildFC(fg, bg);
  282. }
  283.  
  284. ConsoleUtils::ConsoleUtils(FullColor fc)
  285. {
  286.     defaultColor_ = fc;
  287.     fgcolor_ = fc.fg;
  288.     bgcolor_ = fc.bg;
  289. }
  290.  
  291. ConsoleUtils::~ConsoleUtils()
  292. {
  293.     /* nothing to do here anymore */
  294. }
  295.  
  296. #pragma endregion
  297.  
  298. #pragma region CUtilsSetGets
  299.  
  300. FullColor ConsoleUtils::setActiveColor(FullColor fc)
  301. {
  302.     FullColor old = con.getFullColor();
  303.     con.setColor(fc);
  304.     return old;
  305. }
  306.  
  307. FullColor ConsoleUtils::setDefaultColor(FullColor fc)
  308. {
  309.     FullColor old = defaultColor_;
  310.     defaultColor_ = fc;
  311.     return old;
  312. }
  313.  
  314. FullColor ConsoleUtils::getActiveColor()
  315. {
  316.     return con.getFullColor();
  317. }
  318.  
  319. FullColor ConsoleUtils::getDefaultColor()
  320. {
  321.     return defaultColor_;
  322. }
  323.  
  324. #pragma endregion
  325.  
  326. #pragma region CUtilsBackground
  327.  
  328. void ConsoleUtils::background()
  329. {
  330.     background(defaultColor_.bg, 0);
  331. }
  332.  
  333. void ConsoleUtils::background(int fg, int bg)
  334. {
  335.     background(FCUtils.buildFC(fg, bg));
  336. }
  337.  
  338. void ConsoleUtils::background(FullColor fc)
  339. {
  340.     char s[300];
  341.     memset(s, 0xB1, sizeof(s));
  342.     int h = con.getHeight();
  343.     int x;
  344.     s[con.getWidth()] = 0; // hah I eliminated a for loop
  345.     con.setColor(fc);
  346.     for(x = 0; x < h; x++) con.output(x, 0, s);
  347.     con.setPos(0, 0);
  348. }
  349.  
  350. #pragma endregion
  351.  
  352. #pragma region CUtilsOutput
  353.  
  354. COORD ConsoleUtils::output(int r, int c, char* s, FullColor fc)
  355. {
  356.     return con.output(r, c, s, fc);
  357. }
  358.  
  359. COORD ConsoleUtils::output(int r, int c, char* s)
  360. {
  361.     return con.output(r, c, s, defaultColor_);
  362. }
  363.  
  364. COORD ConsoleUtils::output(int r, int c, int toOut, FullColor fc)
  365. {
  366.     char s[128];
  367.     sprintf_s(s, "%d", toOut);
  368.     return con.output(r, c, s, fc);
  369. }
  370.  
  371. COORD ConsoleUtils::output(int r, int c, int toOut)
  372. {
  373.     char s[128];
  374.     sprintf_s(s, "%d", toOut);
  375.     return con.output(r, c, s, defaultColor_);
  376. }
  377.  
  378. COORD ConsoleUtils::output(int r, int c, float toOut, FullColor fc)
  379. {
  380.     char s[128];
  381.     sprintf_s(s, "%.4f", toOut);
  382.     return con.output(r, c, s, fc);
  383. }
  384.  
  385. COORD ConsoleUtils::output(int r, int c, float toOut)
  386. {
  387.     char s[128];
  388.     sprintf_s(s, "%.4f", toOut);
  389.     return con.output(r, c, s, defaultColor_);
  390. }
  391.  
  392. #pragma endregion
  393.  
  394. #pragma region CUtilsCenteredOutput
  395.  
  396. COORD ConsoleUtils::outputCentered(int r, char *s)
  397. {
  398.     int l = 0;
  399.     int w = con.getWidth();
  400.     // string length
  401.     for(l = 0; s[l]; l++);
  402.     con.output(r, (w-l)/2, s);
  403.     return con.getPos();
  404. }
  405.  
  406. COORD ConsoleUtils::outputCentered(int r, char *s, int c) // deprecated
  407. {
  408.     FullColor fc = {c%16, c/16};
  409.     return outputCentered(r, s, fc); // I'm transitioning to the new FullColor type. Yes, this goes through twice. Sorry.
  410. }
  411.  
  412. COORD ConsoleUtils::outputCentered(int r, char *s, int fg, int bg) // deprecated. please use FullColor type
  413. {
  414.     int l = 0;
  415.     int w = con.getWidth();
  416.     for(l = 0; s[l]; l++);
  417.     con.output(r, (w-l)/2, s, FCUtils.buildFC(fg, bg));
  418.     return con.getPos();
  419. }
  420.  
  421. COORD ConsoleUtils::outputCentered(int r, char *s, FullColor fc)
  422. {
  423.     return outputCentered(r, s, fc.fg, fc.bg);
  424. }
  425.  
  426. COORD ConsoleUtils::outputCentered(int r, int toOut)
  427. {
  428.     return outputCentered(r, toOut, getActiveColor());
  429. }
  430.  
  431. COORD ConsoleUtils::outputCentered(int r, int toOut, FullColor fc)
  432. {
  433.     char s[100];
  434.     sprintf_s(s, "%d", toOut);
  435.     return outputCentered(r, s, fc.fg, fc.bg);
  436. }
  437.  
  438. COORD ConsoleUtils::outputCentered(int r, float toOut)
  439. {
  440.     return outputCentered(r, toOut, getActiveColor());
  441. }
  442.  
  443. COORD ConsoleUtils::outputCentered(int r, float toOut, FullColor fc)
  444. {
  445.     char s[100];
  446.     sprintf_s(s, "%.4f", toOut);
  447.     return outputCentered(r, s, fc.fg, fc.bg);
  448. }
  449.  
  450. #pragma endregion
  451.  
  452. #pragma region CUtilsPause
  453. void ConsoleUtils::pause()
  454. {
  455.     pause(10, 1);
  456. }
  457.  
  458. void ConsoleUtils::pause(int fg, int bg)
  459. {
  460.     outputCentered(con.getHeight()-2, "Press any key to continue . . . ", fg, bg);
  461.     _getch();
  462. }
  463. #pragma endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement