Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////////////////////
- // from CVSoft: console.h
- #ifndef CONSOLE_H__
- #define CONSOLE_H__
- #include <Windows.h> // because we need the COORD struct, I don't have my own (yet)
- typedef struct
- {
- unsigned short fg;
- unsigned short bg;
- } FullColor; // this lets us combine fg and bg, but we need more methods for this x.x
- class FullColorUtils
- {
- public:
- FullColorUtils();
- FullColor buildFC(int fg, int bg);
- };
- class Console
- {
- public:
- Console();
- ~Console();
- COORD setPos(int r);
- COORD setPos(int r, int c);
- COORD output(int r, int c, char* s);
- COORD output(int r, int c, char* s, int color);
- COORD output(int r, int c, char* s, int fg, int bg);
- COORD output(int r, int c, char* s, FullColor fc);
- int setColor(int c);
- int setColor(int fg, int bg);
- FullColor setColor(FullColor fc);
- int getColor();
- FullColor getFullColor();
- int getWidth();
- int getHeight();
- COORD getPos();
- //void test();
- private:
- COORD currentPos_;
- FullColorUtils FCUtils;
- };
- class ConsoleUtils
- {
- public:
- ConsoleUtils();
- ConsoleUtils(FullColor fc);
- ConsoleUtils(int fg, int bg);
- ~ConsoleUtils();
- FullColor setActiveColor(FullColor fc);
- FullColor setDefaultColor(FullColor fc);
- FullColor getActiveColor();
- FullColor getDefaultColor();
- void background();
- void background(FullColor fc);
- void background(int fg, int bg); // wasn't this something else? oh well
- COORD output(int r, int c, char* s, FullColor fc);
- COORD output(int r, int c, char* s);
- COORD output(int r, int c, int toOut, FullColor fc);
- COORD output(int r, int c, int toOut);
- COORD output(int r, int c, float toOut, FullColor fc);
- COORD output(int r, int c, float toOut);
- COORD outputCentered(int r, char* s);
- COORD outputCentered(int r, int toOut);
- COORD outputCentered(int r, float toOut);
- COORD outputCentered(int r, char* s, int c);
- COORD outputCentered(int r, char* s, int fg, int bg);
- COORD outputCentered(int r, char* s, FullColor fc);
- COORD outputCentered(int r, int toOut, FullColor fc);
- COORD outputCentered(int r, float toOut, FullColor fc);
- void pause();
- void pause(int fg, int bg);
- private:
- Console con;
- FullColorUtils FCUtils;
- FullColor defaultColor_;
- int bgcolor_, fgcolor_;
- };
- // here's the colors
- enum ConsoleColors
- {
- BLACK,
- DARK_BLUE,
- DARK_GREEN,
- DARK_CYAN,
- DARK_RED,
- DARK_MAGENTA,
- DARK_YELLOW,
- LIGHT_GRAY,
- DARK_GRAY,
- BLUE,
- GREEN,
- CYAN,
- RED,
- MAGENTA,
- YELLOW,
- WHITE
- };
- #endif
- ////////////////////////////////////////////////////////////////
- // from CVSoft: console.cpp
- #include <cstdio>
- #include <iostream>
- #include <iomanip>
- #include <conio.h>
- #include <Windows.h>
- #include "console.h"
- using namespace std;
- // ========== below is FullColorUtils ======== //
- #pragma region FCUtils
- FullColorUtils::FullColorUtils()
- {
- // not sure if anything will ever be needed here...
- }
- FullColor FullColorUtils::buildFC(int fg, int bg)
- {
- FullColor fc = {fg, bg};
- return fc;
- }
- #pragma endregion
- // ============= below is Console ============ //
- #pragma region ConsoleConstructs
- Console::Console()
- {
- /*
- system("cls");
- setColor(7); // windows default
- setPos(0, 0);
- */
- }
- Console::~Console()
- {
- /*
- system("cls");
- setColor(7); // back to normal
- setPos(0, 0);
- */
- }
- #pragma endregion
- #pragma region ConsoleOutputs
- COORD Console::output(int r, int c, char* s)
- {
- setPos(r, c);
- cout << s;
- return getPos();
- }
- COORD Console::output(int r, int c, char* s, FullColor fc)
- {
- setPos(r, c);
- setColor(fc);
- cout << s;
- return getPos();
- }
- // deprecated
- COORD Console::output(int r, int c, char* s, int color)
- {
- return output(r, c, s, FCUtils.buildFC(color%16, color/16));
- }
- // deprecated
- COORD Console::output(int r, int c, char* s, int fg, int bg)
- {
- return output(r, c, s, FCUtils.buildFC(fg, bg));
- }
- #pragma endregion All outputs
- #pragma region ConsoleGets
- int Console::getWidth()
- {
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
- return (int)csbi.dwSize.X;
- }
- int Console::getHeight()
- {
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
- return (int)(1+csbi.srWindow.Bottom-csbi.srWindow.Top);
- }
- COORD Console::getPos()
- {
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
- currentPos_ = csbi.dwCursorPosition;
- return currentPos_;
- }
- int Console::getColor()
- {
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
- return (int)csbi.wAttributes % 256;
- }
- FullColor Console::getFullColor()
- {
- int c = getColor();
- FullColor fc = {c%16, c/16};
- return fc;
- }
- #pragma endregion Color, fullcolor, position
- #pragma region ConsoleSets
- COORD Console::setPos(int r)
- {
- COORD old = getPos();
- COORD cpos = {old.X, r};
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cpos);
- return old;
- }
- COORD Console::setPos(int r, int c)
- {
- COORD old = getPos();
- COORD cpos = {c, r};
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cpos);
- return old;
- }
- int Console::setColor(int c)
- {
- int old = getColor();
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
- return old;
- }
- int Console::setColor(int fg, int bg)
- {
- return setColor(fg+16*bg);
- }
- FullColor Console::setColor(FullColor fc)
- {
- FullColor old = getFullColor();
- setColor(fc.fg+16*fc.bg);
- return old;
- }
- #pragma endregion color, position
- // ========== below is ConsoleUtils ========== //
- #pragma region CUtilsConstructs
- ConsoleUtils::ConsoleUtils()
- {
- int i = con.getColor(); // we save a call to con.getColor here. speed is good.
- fgcolor_ = i % 16;
- bgcolor_ = i / 16;
- defaultColor_ = FCUtils.buildFC(fgcolor_, bgcolor_);
- }
- ConsoleUtils::ConsoleUtils(int fg, int bg)
- {
- fgcolor_ = fg;
- bgcolor_ = bg;
- defaultColor_ = FCUtils.buildFC(fg, bg);
- }
- ConsoleUtils::ConsoleUtils(FullColor fc)
- {
- defaultColor_ = fc;
- fgcolor_ = fc.fg;
- bgcolor_ = fc.bg;
- }
- ConsoleUtils::~ConsoleUtils()
- {
- /* nothing to do here anymore */
- }
- #pragma endregion
- #pragma region CUtilsSetGets
- FullColor ConsoleUtils::setActiveColor(FullColor fc)
- {
- FullColor old = con.getFullColor();
- con.setColor(fc);
- return old;
- }
- FullColor ConsoleUtils::setDefaultColor(FullColor fc)
- {
- FullColor old = defaultColor_;
- defaultColor_ = fc;
- return old;
- }
- FullColor ConsoleUtils::getActiveColor()
- {
- return con.getFullColor();
- }
- FullColor ConsoleUtils::getDefaultColor()
- {
- return defaultColor_;
- }
- #pragma endregion
- #pragma region CUtilsBackground
- void ConsoleUtils::background()
- {
- background(defaultColor_.bg, 0);
- }
- void ConsoleUtils::background(int fg, int bg)
- {
- background(FCUtils.buildFC(fg, bg));
- }
- void ConsoleUtils::background(FullColor fc)
- {
- char s[300];
- memset(s, 0xB1, sizeof(s));
- int h = con.getHeight();
- int x;
- s[con.getWidth()] = 0; // hah I eliminated a for loop
- con.setColor(fc);
- for(x = 0; x < h; x++) con.output(x, 0, s);
- con.setPos(0, 0);
- }
- #pragma endregion
- #pragma region CUtilsOutput
- COORD ConsoleUtils::output(int r, int c, char* s, FullColor fc)
- {
- return con.output(r, c, s, fc);
- }
- COORD ConsoleUtils::output(int r, int c, char* s)
- {
- return con.output(r, c, s, defaultColor_);
- }
- COORD ConsoleUtils::output(int r, int c, int toOut, FullColor fc)
- {
- char s[128];
- sprintf_s(s, "%d", toOut);
- return con.output(r, c, s, fc);
- }
- COORD ConsoleUtils::output(int r, int c, int toOut)
- {
- char s[128];
- sprintf_s(s, "%d", toOut);
- return con.output(r, c, s, defaultColor_);
- }
- COORD ConsoleUtils::output(int r, int c, float toOut, FullColor fc)
- {
- char s[128];
- sprintf_s(s, "%.4f", toOut);
- return con.output(r, c, s, fc);
- }
- COORD ConsoleUtils::output(int r, int c, float toOut)
- {
- char s[128];
- sprintf_s(s, "%.4f", toOut);
- return con.output(r, c, s, defaultColor_);
- }
- #pragma endregion
- #pragma region CUtilsCenteredOutput
- COORD ConsoleUtils::outputCentered(int r, char *s)
- {
- int l = 0;
- int w = con.getWidth();
- // string length
- for(l = 0; s[l]; l++);
- con.output(r, (w-l)/2, s);
- return con.getPos();
- }
- COORD ConsoleUtils::outputCentered(int r, char *s, int c) // deprecated
- {
- FullColor fc = {c%16, c/16};
- return outputCentered(r, s, fc); // I'm transitioning to the new FullColor type. Yes, this goes through twice. Sorry.
- }
- COORD ConsoleUtils::outputCentered(int r, char *s, int fg, int bg) // deprecated. please use FullColor type
- {
- int l = 0;
- int w = con.getWidth();
- for(l = 0; s[l]; l++);
- con.output(r, (w-l)/2, s, FCUtils.buildFC(fg, bg));
- return con.getPos();
- }
- COORD ConsoleUtils::outputCentered(int r, char *s, FullColor fc)
- {
- return outputCentered(r, s, fc.fg, fc.bg);
- }
- COORD ConsoleUtils::outputCentered(int r, int toOut)
- {
- return outputCentered(r, toOut, getActiveColor());
- }
- COORD ConsoleUtils::outputCentered(int r, int toOut, FullColor fc)
- {
- char s[100];
- sprintf_s(s, "%d", toOut);
- return outputCentered(r, s, fc.fg, fc.bg);
- }
- COORD ConsoleUtils::outputCentered(int r, float toOut)
- {
- return outputCentered(r, toOut, getActiveColor());
- }
- COORD ConsoleUtils::outputCentered(int r, float toOut, FullColor fc)
- {
- char s[100];
- sprintf_s(s, "%.4f", toOut);
- return outputCentered(r, s, fc.fg, fc.bg);
- }
- #pragma endregion
- #pragma region CUtilsPause
- void ConsoleUtils::pause()
- {
- pause(10, 1);
- }
- void ConsoleUtils::pause(int fg, int bg)
- {
- outputCentered(con.getHeight()-2, "Press any key to continue . . . ", fg, bg);
- _getch();
- }
- #pragma endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement