Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********************************************************************
- *
- * file: Console.h
- *
- * brief: Console interface library
- *
- * version: 1.0.2
- *
- * details: This library provides an extended functionality for
- * console interface for ASCII-based applications and games
- * on the windows platform. It is wraped around windows.h
- * and cstdio.
- *
- * author: w2ch Anon
- *
- * date: May 2014
- *
- * copyright: None
- *
- ********************************************************************/
- /************************** BEGIN SOURCE ***************************/
- #ifndef CONSOLE_H
- #define CONSOLE_H
- /********************************************************************
- * System Includes
- ********************************************************************/
- #define _WIN32_WINNT 0x0500
- #define WIN32_LEAN_AND_MEAN
- #include <Windows.h>
- #include <cstdio> // fprintf()
- /********************************************************************
- * Symbolic Constants
- ********************************************************************/
- // NULL
- #ifndef NULL
- #define NULL (void *)0
- #endif
- /* Win32Console::WaitForConsoleInput() Values */
- #define KEYBOARD_AND_MOUSE_INPUT (0x0000)
- #define ONLY_MOUSE_INPUT (KEY_EVENT) // (0x0001)
- #define ONLY_KEYBOARD_INPUT (MOUSE_EVENT) // (0x0002)
- /* (all in bitflag!) */
- #define CONSOLE_KEYFLAG_CTRL (0x0001) // CTRL key + another key
- #define CONSOLE_KEYFLAG_ALT (0x0002) // ALT key + another key
- #define CONSOLE_KEYFLAG_SHIFT (0x0010) // SHIFT key + another key
- #define CONSOLE_KEYFLAG_CAPSLOCK (0x0004) // CAPSLOCK key is on
- #define CONSOLE_KEYFLAG_NUMLOCK (0x0008) // NUMLOCK key is on
- #define CONSOLE_KEYFLAG_NONE (0x8000) // key found and instance is empty
- /* Console key codes */
- typedef enum {
- CONSOLE_KEY_NONE = 0,
- CONSOLE_KEY_A = 'A', /* Alphanumeric Keys */
- CONSOLE_KEY_B = 'B',
- CONSOLE_KEY_C = 'C',
- CONSOLE_KEY_D = 'D',
- CONSOLE_KEY_E = 'E',
- CONSOLE_KEY_F = 'F',
- CONSOLE_KEY_G = 'G',
- CONSOLE_KEY_H = 'H',
- CONSOLE_KEY_I = 'I',
- CONSOLE_KEY_J = 'J',
- CONSOLE_KEY_K = 'K',
- CONSOLE_KEY_L = 'L',
- CONSOLE_KEY_M = 'M',
- CONSOLE_KEY_N = 'N',
- CONSOLE_KEY_O = 'O',
- CONSOLE_KEY_P = 'P',
- CONSOLE_KEY_Q = 'Q',
- CONSOLE_KEY_R = 'R',
- CONSOLE_KEY_S = 'S',
- CONSOLE_KEY_T = 'T',
- CONSOLE_KEY_U = 'U',
- CONSOLE_KEY_V = 'V',
- CONSOLE_KEY_W = 'W',
- CONSOLE_KEY_X = 'X',
- CONSOLE_KEY_Y = 'Y',
- CONSOLE_KEY_Z = 'Z',
- CONSOLE_KEY_0 = '0',
- CONSOLE_KEY_1 = '1',
- CONSOLE_KEY_2 = '2',
- CONSOLE_KEY_3 = '3',
- CONSOLE_KEY_4 = '4',
- CONSOLE_KEY_5 = '5',
- CONSOLE_KEY_6 = '6',
- CONSOLE_KEY_7 = '7',
- CONSOLE_KEY_8 = '8',
- CONSOLE_KEY_9 = '9',
- CONSOLE_KEY_SPACE = ' ', /* Misc Keys */
- CONSOLE_KEY_TAB = '\t',
- CONSOLE_KEY_ESCAPE = 27,
- CONSOLE_KEY_RETURN = '\r',
- CONSOLE_KEY_BACKSPACE = '\b',
- CONSOLE_KEY_PGUP = 0xc000,
- CONSOLE_KEY_PGDN = 0xc001,
- CONSOLE_KEY_HOME = 0xc002,
- CONSOLE_KEY_END = 0xc003,
- CONSOLE_KEY_INSERT = 0xc004,
- CONSOLE_KEY_DELETE = 0xc005,
- CONSOLE_KEY_UP = 0xc200, /* Arrow keys */
- CONSOLE_KEY_DOWN = 0xc201,
- CONSOLE_KEY_LEFT = 0xc202,
- CONSOLE_KEY_RIGHT = 0xc203,
- CONSOLE_KEY_F1 = 0xc101, /* Function Keys */
- CONSOLE_KEY_F2 = 0xc102,
- CONSOLE_KEY_F3 = 0xc103,
- CONSOLE_KEY_F4 = 0xc104,
- CONSOLE_KEY_F5 = 0xc105,
- CONSOLE_KEY_F6 = 0xc106,
- CONSOLE_KEY_F7 = 0xc107,
- CONSOLE_KEY_F8 = 0xc108,
- CONSOLE_KEY_F9 = 0xc109,
- CONSOLE_KEY_F10 = 0xc10a,
- CONSOLE_KEY_F11 = 0xc10b,
- CONSOLE_KEY_F12 = 0xc10c,
- CONSOLE_MOUSE_LEFT_BUTTON = 0xc10d, /* Mouse Keys */
- CONSOLE_MOUSE_RIGHT_BUTTON = 0xc10e
- } KEYCODE;
- typedef int ATTRIBUTE;
- const ATTRIBUTE BLACK = 0x00;
- const ATTRIBUTE BROWN = 0x03;
- const ATTRIBUTE GREY = 0x07;
- const ATTRIBUTE RED = 0x09;
- const ATTRIBUTE GREEN = 0x0a;
- const ATTRIBUTE YELLOW = 0x0b;
- const ATTRIBUTE BLUE = 0x0c;
- const ATTRIBUTE MANGENTA = 0x0d;
- const ATTRIBUTE CYAN = 0x0e;
- const ATTRIBUTE WHITE = 0x0f;
- struct KEYPRESS {
- KEYCODE eCode; // Code for the key pressed
- BYTE bErrorCode; // If error ecoured
- COORD coordMousePosition; // If moouse event occured, posison is returned here
- };
- /********************************************************************
- * Forward declaration
- ********************************************************************/
- class Win32Console;
- void ErrorExit(const char *, Win32Console *); // Error exit handeling
- /********************************************************************
- *
- * class: Win32Console(singletone class)
- *
- * brief: Windows target console.
- *
- ********************************************************************/
- class Win32Console
- {
- public:
- // Singleton Invoke Of Class
- static Win32Console& Instance() // Invoke the singletone object of the class.
- {
- static Win32Console InstanceConsole;
- return InstanceConsole;
- }
- // Public Methods:
- // I/O Routines:
- Win32Console & operator <<(const char *); // Writes to the console a zero themanating char string.
- template <class T> Win32Console & operator <<(const T); // Writes to the console a single type.
- //KEYPRESS get( void ); // Retrieves a single, ASCII encoded character from the console.
- KEYPRESS WaitForConsoleInput(int nValue); // Waits for console input that accepts cValue flag value; NOTE:Key input precedes mouse input.
- // Control Routines:
- int Clear(void); // Clears the console.
- void Position(int nX, int nY); // Sets the cursor position to the specified coordinates.
- void Color(ATTRIBUTE foreground, ATTRIBUTE background); // color attributes of outgoing characters.
- int SetTitle(const char *); // Changes the title of the window to the specified text.
- void SetConsoleWindowSize(int nWidth, int nHeight); // Resizes the window to the desired width and height.
- void DisableResisingOfConsole(void); // Disable the resising of the console boders.
- void EnableResisingOfConsole(void); // Enable the resising of the console boders.
- // Information Routines:
- int GetWidth(void); // Returns the width of the console, in characters.
- int GetHeight(void); // Returns the height of the console, in characters.
- friend void ErrorExit(const char *, Win32Console *);
- private:
- // Private constructors and Destructors:
- Win32Console(); // Default constructor.
- ~Win32Console(); // Destructor.
- //Win32Console(Win32Console const&); // Copy constructor is private. (Not Implemented).
- //Win32Console& operator=(Win32Console const&); // Assignment operator is private. (Not Implemented).
- // Private Properties:
- WORD wAttributes; // Specifies the current outgoing text attributes.
- COORD coordPlayerPosition; // Position to wirte output in console.
- HANDLE hOutput; // Handle to the output stream for the console.
- HANDLE hInput; // Handle to the input stream for the console.
- DWORD fdwSaveOldConsoleMode; // Old console mode
- TCHAR szOldConsoleTitle[MAX_PATH]; // The console orginal titel.
- DWORD dwOldConsoleStyle; // The orginal console style of the window.
- };
- #include "Console.tpp" // Template declaration.
- static Win32Console& console = Win32Console::Instance();
- #endif // CONSOLE_H
- /************************** END OF SOURCE **************************/
- /********************************************************************
- *
- * file: Console.tpp
- *
- * brief: Console interface library
- *
- * version: 1.0.1
- *
- * details: This file is used with Console.h
- *
- * author: w2ch Anon
- *
- * date: May 2014
- *
- * copyright: None
- *
- ********************************************************************/
- template<class T>
- Win32Console & Win32Console::operator << (const T tOutput)
- {
- if (coordPlayerPosition.X > GetWidth()) return *this; // Cehck if the write cordinates(x only) are inside the bounds of the console window
- DWORD cWritten;
- // Write a string of colors to a screen buffer.
- if (!FillConsoleOutputAttribute(hOutput, // screen buffer handle
- wAttributes, // color to fill with
- 1, // number of cells to fill
- coordPlayerPosition, // first cell to write to
- &cWritten)) // actual number written
- ExitProcess(0);
- // Write a string of characters to a screen buffer.
- if (!FillConsoleOutputCharacter(hOutput, // screen buffer handle
- tOutput, // charecter to be writen
- 1, // number of times the charecter should be writen
- coordPlayerPosition, // first cell to write to
- &cWritten)) // actual number written
- ExitProcess(0);
- coordPlayerPosition.X += (short)cWritten;
- return *this;
- }
- /************************** END OF SOURCE **************************/
- /********************************************************************
- *
- * file: Console.cpp
- *
- * brief: Console interface library
- *
- * version: 1.0.2
- *
- * details: This file is used with Console.h
- *
- * author: w2ch Anon
- *
- * date: May 2014
- *
- * copyright: None
- *
- ********************************************************************/
- #include "Console.h"
- // Function declaring
- void ErrorExit(const char *szErrorMessage, Win32Console *w32cpObj)
- {
- // 1. Print the error message to stderr
- fprintf(stderr, "%s\n", szErrorMessage);
- // 2. Call the console destructor
- w32cpObj->~Win32Console(); // NOTE:Can create error loop?
- // 3. Exit the program
- ExitProcess(0);
- }
- // Constructors and Destructors
- Win32Console::Win32Console() // Default constructor
- {
- // 1. No new console title set
- szOldConsoleTitle[0] = '\0';
- // 2. Set player position
- coordPlayerPosition.X = 0;
- coordPlayerPosition.Y = 0;
- // 3. Get console input and output handels and clear the input buffer
- hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
- hInput = GetStdHandle(STD_INPUT_HANDLE);
- FlushConsoleInputBuffer(hInput); //just need one time as ReadConsoleInput dose it automaticly
- if (hInput == INVALID_HANDLE_VALUE) ErrorExit("GetStdHandle", this);
- // 4. Save the current input mode(restored on exit)
- if (!GetConsoleMode(hInput, &fdwSaveOldConsoleMode)) ErrorExit("GetConsoleMode", this);
- // 5. Enable the window input events
- DWORD fdwMode = ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT | ENABLE_EXTENDED_FLAGS; //0x0080 == ENABLE_EXTENDED_FLAGS
- if (!SetConsoleMode(hInput, fdwMode)) ErrorExit("SetConsoleMode", this);
- // 6. Get the console style(for border set to static)
- HWND hwndConsole;
- hwndConsole = GetConsoleWindow();
- if (hwndConsole == NULL) ErrorExit("GetConsoleWindow", this);
- dwOldConsoleStyle = GetWindowLong(hwndConsole, GWL_STYLE);
- if (!dwOldConsoleStyle) ErrorExit("GetWindowLong", this);
- // 7. Hide the console curser
- CONSOLE_CURSOR_INFO CCI;
- GetConsoleCursorInfo(hOutput, &CCI);
- CCI.bVisible = false;
- SetConsoleCursorInfo(hOutput, &CCI);
- // 8. Set the default text attribute
- Color(GREY, BLACK);
- }
- Win32Console::~Win32Console()
- {
- // 1. Restore the old console mode
- SetConsoleMode(hInput, fdwSaveOldConsoleMode);
- // 2. Show the console curser
- CONSOLE_CURSOR_INFO CCI;
- GetConsoleCursorInfo(hOutput, &CCI);
- CCI.bVisible = true;
- SetConsoleCursorInfo(hOutput, &CCI);
- // 3. reset the console title if the title has been changed
- if (szOldConsoleTitle[0] != '\0') SetTitle(szOldConsoleTitle);
- }
- // Get console information
- int Win32Console::GetWidth(void)
- {
- CONSOLE_SCREEN_BUFFER_INFO csbiWin32Console;
- GetConsoleScreenBufferInfo(hOutput, &csbiWin32Console);
- return csbiWin32Console.srWindow.Right - csbiWin32Console.srWindow.Left + 1;
- }
- int Win32Console::GetHeight(void)
- {
- CONSOLE_SCREEN_BUFFER_INFO csbiWin32Console;
- GetConsoleScreenBufferInfo(hOutput, &csbiWin32Console);
- return csbiWin32Console.srWindow.Bottom - csbiWin32Console.srWindow.Top + 1;
- }
- // Cosnole I/O
- Win32Console& Win32Console::operator <<(const char *szWrite)
- {
- if (coordPlayerPosition.X > GetWidth()) return *this; // Cehck if the write cordinates(x only) are inside the bounds of the console window
- DWORD cWritten;
- // Write a string of colors to a screen buffer.
- if (!FillConsoleOutputAttribute(hOutput, // screen buffer handle
- wAttributes, // color to fill with
- lstrlen(szWrite) - ( (coordPlayerPosition.X + lstrlen(szWrite) > GetWidth() ) * ( coordPlayerPosition.X + lstrlen(szWrite) - GetWidth() ) ), // number of cells to fill
- coordPlayerPosition, // first cell to write to
- &cWritten)) // actual number written
- ErrorExit("FillConsoleOutputAttribute", this);
- // Write a string of characters to a screen buffer.
- if (!WriteConsoleOutputCharacter(hOutput, // screen buffer handle
- szWrite, // pointer to source string
- lstrlen(szWrite) - ( (coordPlayerPosition.X + lstrlen(szWrite) > GetWidth() ) * ( coordPlayerPosition.X + lstrlen(szWrite) - GetWidth() ) ), // get the length of the string
- coordPlayerPosition, // first cell to write to
- &cWritten)) // actual number written
- ErrorExit("WriteConsoleOutputCharacter", this);
- coordPlayerPosition.X += (short)cWritten;
- return *this;
- }
- // int Win32Console::get(void)
- /* There is no need for this function right now
- int Win32Console::get(void) // Retrieves a single, ASCII encoded character from the console
- {
- DWORD cNumRead;
- INPUT_RECORD irInBuf[128];
- char cQuit = 1;
- unsigned int i;
- // Wait for the events
- PeekConsoleInput(hInput, // input buffer handle
- irInBuf, // buffer to read into
- 128, // size of read buffer
- &cNumRead); // number of records read
- for (i = 0; i < cNumRead && cQuit; i++) if (irInBuf[i].Event.KeyEvent.wVirtualKeyCode) --cQuit;
- if(cNumRead) FlushConsoleInputBuffer(hInput);
- return irInBuf[i].Event.KeyEvent.wVirtualKeyCode;
- } */
- KEYPRESS Win32Console::WaitForConsoleInput(int nValue) // Waits for console input that accepts cValue flag value
- {
- DWORD cNumRead;
- INPUT_RECORD irInBuf[128];
- KEYPRESS ReturnKeypress = { CONSOLE_KEY_NONE, 0, { 0, 0 } }; // 2nd, 0 is for ReturnKeypress.bErrorCode; 3rd is for mouse posision
- Label1:
- // Wait for the events.
- do {
- if (!ReadConsoleInput(hInput, // input buffer handle
- irInBuf, // buffer to read into
- 128, // size of read buffer
- &cNumRead)) // number of records read
- ErrorExit("ReadConsoleInput", this);
- } while (irInBuf[0].EventType & nValue); // only get the right input by AND operator with the result on the nValue
- if ((irInBuf[0].EventType == KEY_EVENT && irInBuf[0].Event.KeyEvent.bKeyDown) || irInBuf[0].EventType == FOCUS_EVENT) goto Label1; // Dont accept keyboard input when key pressed down only when released and focus event types
- // NOTE:I use goto here because I think it is most readable code produce
- switch (irInBuf[0].EventType)
- {
- case KEY_EVENT:
- switch (irInBuf[0].Event.KeyEvent.wVirtualKeyCode)
- {
- case 0x41:
- ReturnKeypress.eCode = CONSOLE_KEY_A;
- break;
- case 0x42:
- ReturnKeypress.eCode = CONSOLE_KEY_B;
- break;
- case 0x43:
- ReturnKeypress.eCode = CONSOLE_KEY_C;
- break;
- case 0x44:
- ReturnKeypress.eCode = CONSOLE_KEY_D;
- break;
- case 0x45:
- ReturnKeypress.eCode = CONSOLE_KEY_E;
- break;
- case 0x46:
- ReturnKeypress.eCode = CONSOLE_KEY_F;
- break;
- case 0x47:
- ReturnKeypress.eCode = CONSOLE_KEY_G;
- break;
- case 0x48:
- ReturnKeypress.eCode = CONSOLE_KEY_H;
- break;
- case 0x49:
- ReturnKeypress.eCode = CONSOLE_KEY_I;
- break;
- case 0x4A:
- ReturnKeypress.eCode = CONSOLE_KEY_J;
- break;
- case 0x4B:
- ReturnKeypress.eCode = CONSOLE_KEY_K;
- break;
- case 0x4C:
- ReturnKeypress.eCode = CONSOLE_KEY_L;
- break;
- case 0x4D:
- ReturnKeypress.eCode = CONSOLE_KEY_M;
- break;
- case 0x4E:
- ReturnKeypress.eCode = CONSOLE_KEY_N;
- break;
- case 0x4F:
- ReturnKeypress.eCode = CONSOLE_KEY_O;
- break;
- case 0x50:
- ReturnKeypress.eCode = CONSOLE_KEY_P;
- break;
- case 0x51:
- ReturnKeypress.eCode = CONSOLE_KEY_Q;
- break;
- case 0x52:
- ReturnKeypress.eCode = CONSOLE_KEY_R;
- break;
- case 0x53:
- ReturnKeypress.eCode = CONSOLE_KEY_S;
- break;
- case 0x54:
- ReturnKeypress.eCode = CONSOLE_KEY_T;
- break;
- case 0x55:
- ReturnKeypress.eCode = CONSOLE_KEY_U;
- break;
- case 0x56:
- ReturnKeypress.eCode = CONSOLE_KEY_V;
- break;
- case 0x57:
- ReturnKeypress.eCode = CONSOLE_KEY_W;
- break;
- case 0x58:
- ReturnKeypress.eCode = CONSOLE_KEY_X;
- break;
- case 0x59:
- ReturnKeypress.eCode = CONSOLE_KEY_Y;
- break;
- case 0x5A:
- ReturnKeypress.eCode = CONSOLE_KEY_Z;
- break;
- case 0x30:
- ReturnKeypress.eCode = CONSOLE_KEY_0;
- break;
- case 0x31:
- ReturnKeypress.eCode = CONSOLE_KEY_1;
- break;
- case 0x32:
- ReturnKeypress.eCode = CONSOLE_KEY_2;
- break;
- case 0x33:
- ReturnKeypress.eCode = CONSOLE_KEY_3;
- break;
- case 0x34:
- ReturnKeypress.eCode = CONSOLE_KEY_4;
- break;
- case 0x35:
- ReturnKeypress.eCode = CONSOLE_KEY_5;
- break;
- case 0x36:
- ReturnKeypress.eCode = CONSOLE_KEY_6;
- break;
- case 0x37:
- ReturnKeypress.eCode = CONSOLE_KEY_7;
- break;
- case 0x38:
- ReturnKeypress.eCode = CONSOLE_KEY_8;
- break;
- case 0x39:
- ReturnKeypress.eCode = CONSOLE_KEY_9;
- break;
- case VK_SPACE:
- ReturnKeypress.eCode = CONSOLE_KEY_SPACE;
- break;
- case VK_TAB:
- ReturnKeypress.eCode = CONSOLE_KEY_TAB;
- break;
- case VK_ESCAPE:
- ReturnKeypress.eCode = CONSOLE_KEY_ESCAPE;
- break;
- case VK_RETURN:
- ReturnKeypress.eCode = CONSOLE_KEY_RETURN;
- break;
- case VK_BACK:
- ReturnKeypress.eCode = CONSOLE_KEY_BACKSPACE;
- break;
- case VK_PRIOR:
- ReturnKeypress.eCode = CONSOLE_KEY_PGUP;
- break;
- case VK_NEXT:
- ReturnKeypress.eCode = CONSOLE_KEY_PGDN;
- break;
- case VK_HOME:
- ReturnKeypress.eCode = CONSOLE_KEY_HOME;
- break;
- case VK_END:
- ReturnKeypress.eCode = CONSOLE_KEY_END;
- break;
- case VK_INSERT:
- ReturnKeypress.eCode = CONSOLE_KEY_INSERT;
- break;
- case VK_DELETE:
- ReturnKeypress.eCode = CONSOLE_KEY_DELETE;
- break;
- case VK_UP:
- ReturnKeypress.eCode = CONSOLE_KEY_UP;
- break;
- case VK_DOWN:
- ReturnKeypress.eCode = CONSOLE_KEY_DOWN;
- break;
- case VK_LEFT:
- ReturnKeypress.eCode = CONSOLE_KEY_LEFT;
- break;
- case VK_RIGHT:
- ReturnKeypress.eCode = CONSOLE_KEY_RIGHT;
- break;
- case VK_F1:
- ReturnKeypress.eCode = CONSOLE_KEY_F1;
- break;
- case VK_F2:
- ReturnKeypress.eCode = CONSOLE_KEY_F2;
- break;
- case VK_F3:
- ReturnKeypress.eCode = CONSOLE_KEY_F3;
- break;
- case VK_F4:
- ReturnKeypress.eCode = CONSOLE_KEY_F4;
- break;
- case VK_F5:
- ReturnKeypress.eCode = CONSOLE_KEY_F5;
- break;
- case VK_F6:
- ReturnKeypress.eCode = CONSOLE_KEY_F6;
- break;
- case VK_F7:
- ReturnKeypress.eCode = CONSOLE_KEY_F7;
- break;
- case VK_F8:
- ReturnKeypress.eCode = CONSOLE_KEY_F8;
- break;
- case VK_F9:
- ReturnKeypress.eCode = CONSOLE_KEY_F9;
- break;
- case VK_F10:
- ReturnKeypress.eCode = CONSOLE_KEY_F10;
- break;
- case VK_F11:
- ReturnKeypress.eCode = CONSOLE_KEY_F11;
- break;
- case VK_F12:
- ReturnKeypress.eCode = CONSOLE_KEY_F12;
- break;
- default:
- ReturnKeypress.bErrorCode = 1;
- break;
- }
- break;
- case MOUSE_EVENT:
- ReturnKeypress.coordMousePosition.X = irInBuf[0].Event.MouseEvent.dwMousePosition.X;
- ReturnKeypress.coordMousePosition.Y = irInBuf[0].Event.MouseEvent.dwMousePosition.Y;
- switch (irInBuf[0].Event.MouseEvent.dwButtonState)
- {
- case FROM_LEFT_1ST_BUTTON_PRESSED:
- ReturnKeypress.eCode = CONSOLE_MOUSE_LEFT_BUTTON;
- break;
- case RIGHTMOST_BUTTON_PRESSED:
- ReturnKeypress.eCode = CONSOLE_MOUSE_RIGHT_BUTTON;
- break;
- default:
- ReturnKeypress.bErrorCode = 1;
- break;
- }
- break;
- default:
- ReturnKeypress.bErrorCode = 1;
- break;
- }
- return ReturnKeypress;
- }
- // Console manipulation
- int Win32Console::Clear(void)
- {
- CONSOLE_SCREEN_BUFFER_INFO csbiWin32Console;
- COORD coordScreen = { 0, 0 };
- DWORD cCharsWritten;
- DWORD dwConSize;
- // Get the number of character cells in the current buffer.
- if (!GetConsoleScreenBufferInfo(hOutput, &csbiWin32Console))
- {
- return 1;
- }
- dwConSize = csbiWin32Console.dwSize.X * csbiWin32Console.dwSize.Y;
- // Fill the entire screen with blanks.
- if (!FillConsoleOutputCharacter(hOutput, // Handle to console screen buffer
- (TCHAR) ' ', // Character to write to the buffer
- dwConSize, // Number of cells to write
- coordScreen, // Coordinates of first cell
- &cCharsWritten)) // Receive number of characters written
- {
- return 1;
- }
- // Get the current text attribute.
- if (!GetConsoleScreenBufferInfo(hOutput, &csbiWin32Console))
- {
- return 1;
- }
- // Set the buffer's attributes accordingly.
- if (!FillConsoleOutputAttribute(hOutput, // Handle to console screen buffer
- csbiWin32Console.wAttributes, // Character attributes to use
- dwConSize, // Number of cells to set attribute
- coordScreen, // Coordinates of first cell
- &cCharsWritten)) // Receive number of characters written
- {
- return 1;
- }
- return 0;
- }
- void Win32Console::Position(int nX, int nY)
- {
- coordPlayerPosition.X = nX;
- coordPlayerPosition.Y = nY;
- }
- void Win32Console::Color(ATTRIBUTE foreground, ATTRIBUTE background)
- {
- wAttributes = 0;
- switch (foreground)
- {
- case BLACK:
- wAttributes += 0;
- break;
- /* case BROWN:
- wAttributes += ;
- break; */
- /* case GREY:
- wAttributes += ;
- break; */
- case RED:
- wAttributes += FOREGROUND_RED;
- break;
- case GREEN:
- wAttributes += FOREGROUND_GREEN;
- break;
- case YELLOW:
- wAttributes += FOREGROUND_RED | FOREGROUND_GREEN;
- break;
- case BLUE:
- wAttributes += FOREGROUND_BLUE;
- break;
- case MANGENTA:
- wAttributes += FOREGROUND_RED | FOREGROUND_BLUE;
- break;
- case CYAN:
- wAttributes += FOREGROUND_BLUE | FOREGROUND_GREEN;
- break;
- case WHITE:
- case GREY:
- default:
- wAttributes += FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN;
- break;
- }
- switch (background)
- {
- case BLACK:
- wAttributes += 0;
- break;
- /* case BROWN:
- wAttributes += ;
- break; */
- /* case GREY:
- wAttributes += ;
- break; */
- case RED:
- wAttributes += BACKGROUND_RED;
- break;
- case GREEN:
- wAttributes += BACKGROUND_GREEN;
- break;
- case YELLOW:
- wAttributes += BACKGROUND_RED | BACKGROUND_GREEN;
- break;
- case BLUE:
- wAttributes += BACKGROUND_BLUE;
- break;
- case MANGENTA:
- wAttributes += BACKGROUND_RED | BACKGROUND_BLUE;
- break;
- case CYAN:
- wAttributes += BACKGROUND_BLUE | BACKGROUND_GREEN;
- break;
- case WHITE:
- case GREY:
- default:
- wAttributes += BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN;
- break;
- }
- }
- int Win32Console::SetTitle(LPCTSTR szNewConsoleTitle)
- {
- // save orginal console title for later to reset
- if (szOldConsoleTitle[0] == '\0') GetConsoleTitle(szOldConsoleTitle, MAX_PATH);
- // set new console title
- if (!SetConsoleTitle(szNewConsoleTitle)) return 1;
- return 0;
- }
- void Win32Console::SetConsoleWindowSize(int nWidth, int nHeight)
- {
- // If either dimension is greater than the largest console window we can have,
- // there is no point in attempting the change.
- {
- COORD largestSize = GetLargestConsoleWindowSize(hOutput);
- if (nWidth > largestSize.X) ErrorExit("GetLargestConsoleWindowSize, The x dimension is too large.", this);
- if (nHeight > largestSize.Y) ErrorExit("GetLargestConsoleWindowSize, The y dimension is too large.", this);
- }
- CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
- if (!GetConsoleScreenBufferInfo(hOutput, &bufferInfo)) ErrorExit("bufferInfo, Unable to retrieve screen buffer info.", this);
- SMALL_RECT& winInfo = bufferInfo.srWindow;
- COORD windowSize = { winInfo.Right - winInfo.Left + 1, winInfo.Bottom - winInfo.Top + 1 };
- if (windowSize.X > nWidth || windowSize.Y > nHeight)
- {
- // window size needs to be adjusted before the buffer size can be reduced.
- SMALL_RECT info =
- {
- 0,
- 0,
- nWidth < windowSize.X ? nWidth - 1 : windowSize.X - 1,
- nHeight < windowSize.Y ? nHeight - 1 : windowSize.Y - 1
- };
- if (!SetConsoleWindowInfo(hOutput, TRUE, &info)) ErrorExit("SetConsoleWindowInfo, Unable to resize window before resizing buffer.", this);
- }
- COORD size = { nWidth, nHeight };
- if (!SetConsoleScreenBufferSize(hOutput, size)) ErrorExit("SetConsoleScreenBufferSize, Unable to resize screen buffer.", this);
- SMALL_RECT info = { 0, 0, nWidth - 1, nHeight - 1 };
- if (!SetConsoleWindowInfo(hOutput, TRUE, &info)) ErrorExit("SetConsoleWindowInfo, Unable to resize window after resizing buffer.", this);
- }
- void Win32Console::DisableResisingOfConsole(void)
- {
- HWND hwndConsole;
- hwndConsole = GetConsoleWindow();
- if (hwndConsole == NULL) ErrorExit("GetConsoleWindow", this);
- SetWindowLong(hwndConsole, GWL_STYLE, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX);
- ShowWindow(hwndConsole, SW_SHOW);
- }
- void Win32Console::EnableResisingOfConsole(void)
- {
- HWND hwndConsole;
- hwndConsole = GetConsoleWindow();
- if (hwndConsole == NULL) ErrorExit("GetConsoleWindow", this);
- SetWindowLong(hwndConsole, GWL_STYLE, dwOldConsoleStyle);
- ShowWindow(hwndConsole, SW_SHOW);
- }
- /************************** END OF SOURCE **************************/
Advertisement
Add Comment
Please, Sign In to add comment