Guest User

ConsoleAPI1-0-2

a guest
Jun 3rd, 2014
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 25.60 KB | None | 0 0
  1. /********************************************************************
  2. *
  3. * file:     Console.h
  4. *
  5. * brief:    Console interface library
  6. *
  7. * version:  1.0.2
  8. *
  9. * details:  This library provides an extended functionality for
  10. *           console interface for ASCII-based applications and games
  11. *           on the windows platform. It is wraped around windows.h
  12. *           and cstdio.
  13. *
  14. * author:   w2ch Anon
  15. *
  16. * date: May 2014
  17. *
  18. * copyright: None
  19. *
  20. ********************************************************************/
  21.  
  22.  
  23.  
  24. /************************** BEGIN SOURCE ***************************/
  25.  
  26. #ifndef CONSOLE_H
  27. #define CONSOLE_H
  28.  
  29. /********************************************************************
  30. * System Includes
  31. ********************************************************************/
  32.  
  33.  
  34. #define _WIN32_WINNT 0x0500
  35. #define WIN32_LEAN_AND_MEAN
  36. #include <Windows.h>
  37. #include <cstdio>               // fprintf()
  38.  
  39.  
  40. /********************************************************************
  41. * Symbolic Constants
  42. ********************************************************************/
  43.  
  44.  
  45. // NULL
  46. #ifndef NULL
  47. #define NULL    (void *)0
  48. #endif
  49.  
  50.  
  51. /* Win32Console::WaitForConsoleInput() Values */
  52. #define KEYBOARD_AND_MOUSE_INPUT    (0x0000)
  53. #define ONLY_MOUSE_INPUT            (KEY_EVENT)     // (0x0001)
  54. #define ONLY_KEYBOARD_INPUT         (MOUSE_EVENT)   // (0x0002)
  55.  
  56. /* (all in bitflag!) */
  57. #define CONSOLE_KEYFLAG_CTRL        (0x0001)    // CTRL key + another key
  58. #define CONSOLE_KEYFLAG_ALT         (0x0002)    // ALT key + another key
  59. #define CONSOLE_KEYFLAG_SHIFT       (0x0010)    // SHIFT key + another key
  60. #define CONSOLE_KEYFLAG_CAPSLOCK    (0x0004)    // CAPSLOCK key is on
  61. #define CONSOLE_KEYFLAG_NUMLOCK     (0x0008)    // NUMLOCK key is on
  62. #define CONSOLE_KEYFLAG_NONE        (0x8000)    // key found and instance is empty
  63.  
  64.  
  65. /* Console key codes */
  66. typedef enum {
  67.     CONSOLE_KEY_NONE = 0,
  68.     CONSOLE_KEY_A = 'A',    /* Alphanumeric Keys */
  69.     CONSOLE_KEY_B = 'B',
  70.     CONSOLE_KEY_C = 'C',
  71.     CONSOLE_KEY_D = 'D',
  72.     CONSOLE_KEY_E = 'E',
  73.     CONSOLE_KEY_F = 'F',
  74.     CONSOLE_KEY_G = 'G',
  75.     CONSOLE_KEY_H = 'H',
  76.     CONSOLE_KEY_I = 'I',
  77.     CONSOLE_KEY_J = 'J',
  78.     CONSOLE_KEY_K = 'K',
  79.     CONSOLE_KEY_L = 'L',
  80.     CONSOLE_KEY_M = 'M',
  81.     CONSOLE_KEY_N = 'N',
  82.     CONSOLE_KEY_O = 'O',
  83.     CONSOLE_KEY_P = 'P',
  84.     CONSOLE_KEY_Q = 'Q',
  85.     CONSOLE_KEY_R = 'R',
  86.     CONSOLE_KEY_S = 'S',
  87.     CONSOLE_KEY_T = 'T',
  88.     CONSOLE_KEY_U = 'U',
  89.     CONSOLE_KEY_V = 'V',
  90.     CONSOLE_KEY_W = 'W',
  91.     CONSOLE_KEY_X = 'X',
  92.     CONSOLE_KEY_Y = 'Y',
  93.     CONSOLE_KEY_Z = 'Z',
  94.     CONSOLE_KEY_0 = '0',
  95.     CONSOLE_KEY_1 = '1',
  96.     CONSOLE_KEY_2 = '2',
  97.     CONSOLE_KEY_3 = '3',
  98.     CONSOLE_KEY_4 = '4',
  99.     CONSOLE_KEY_5 = '5',
  100.     CONSOLE_KEY_6 = '6',
  101.     CONSOLE_KEY_7 = '7',
  102.     CONSOLE_KEY_8 = '8',
  103.     CONSOLE_KEY_9 = '9',
  104.     CONSOLE_KEY_SPACE = ' ',    /* Misc Keys */
  105.     CONSOLE_KEY_TAB = '\t',
  106.     CONSOLE_KEY_ESCAPE = 27,
  107.     CONSOLE_KEY_RETURN = '\r',
  108.     CONSOLE_KEY_BACKSPACE = '\b',
  109.     CONSOLE_KEY_PGUP = 0xc000,
  110.     CONSOLE_KEY_PGDN = 0xc001,
  111.     CONSOLE_KEY_HOME = 0xc002,
  112.     CONSOLE_KEY_END = 0xc003,
  113.     CONSOLE_KEY_INSERT = 0xc004,
  114.     CONSOLE_KEY_DELETE = 0xc005,
  115.     CONSOLE_KEY_UP = 0xc200,    /* Arrow keys */
  116.     CONSOLE_KEY_DOWN = 0xc201,
  117.     CONSOLE_KEY_LEFT = 0xc202,
  118.     CONSOLE_KEY_RIGHT = 0xc203,
  119.     CONSOLE_KEY_F1 = 0xc101,    /* Function Keys */
  120.     CONSOLE_KEY_F2 = 0xc102,
  121.     CONSOLE_KEY_F3 = 0xc103,
  122.     CONSOLE_KEY_F4 = 0xc104,
  123.     CONSOLE_KEY_F5 = 0xc105,
  124.     CONSOLE_KEY_F6 = 0xc106,
  125.     CONSOLE_KEY_F7 = 0xc107,
  126.     CONSOLE_KEY_F8 = 0xc108,
  127.     CONSOLE_KEY_F9 = 0xc109,
  128.     CONSOLE_KEY_F10 = 0xc10a,
  129.     CONSOLE_KEY_F11 = 0xc10b,
  130.     CONSOLE_KEY_F12 = 0xc10c,
  131.     CONSOLE_MOUSE_LEFT_BUTTON = 0xc10d,     /* Mouse Keys */
  132.     CONSOLE_MOUSE_RIGHT_BUTTON = 0xc10e
  133. } KEYCODE;
  134.  
  135.  
  136. typedef int ATTRIBUTE;
  137.  
  138. const ATTRIBUTE BLACK = 0x00;
  139. const ATTRIBUTE BROWN = 0x03;
  140. const ATTRIBUTE GREY = 0x07;
  141. const ATTRIBUTE RED = 0x09;
  142. const ATTRIBUTE GREEN = 0x0a;
  143. const ATTRIBUTE YELLOW = 0x0b;
  144. const ATTRIBUTE BLUE = 0x0c;
  145. const ATTRIBUTE MANGENTA = 0x0d;
  146. const ATTRIBUTE CYAN = 0x0e;
  147. const ATTRIBUTE WHITE = 0x0f;
  148.  
  149.  
  150. struct KEYPRESS {
  151.  
  152.     KEYCODE eCode;              // Code for the key pressed
  153.     BYTE    bErrorCode;         // If error ecoured
  154.     COORD   coordMousePosition; // If moouse event occured, posison is returned here
  155. };
  156.  
  157.  
  158. /********************************************************************
  159. * Forward declaration
  160. ********************************************************************/
  161.  
  162. class Win32Console;
  163. void ErrorExit(const char *, Win32Console *); // Error exit handeling
  164.  
  165.  
  166. /********************************************************************
  167. *
  168. * class:    Win32Console(singletone class)
  169. *
  170. * brief:    Windows target console.
  171. *
  172. ********************************************************************/
  173. class Win32Console
  174. {
  175.  
  176. public:
  177.  
  178.     // Singleton Invoke Of Class
  179.     static Win32Console& Instance()                         // Invoke the singletone object of the class.
  180.     {
  181.         static Win32Console InstanceConsole;
  182.         return InstanceConsole;
  183.     }
  184.  
  185.     // Public Methods:
  186.     // I/O Routines:
  187.     Win32Console & operator <<(const char *);               // Writes to the console a zero themanating char string.
  188.     template <class T> Win32Console & operator <<(const T); // Writes to the console a single type.
  189.     //KEYPRESS get( void );                                 // Retrieves a single, ASCII encoded character from the console.
  190.     KEYPRESS WaitForConsoleInput(int nValue);               // Waits for console input that accepts cValue flag value; NOTE:Key input precedes mouse input.
  191.  
  192.     // Control Routines:
  193.     int  Clear(void);                                       // Clears the console.
  194.     void Position(int nX, int nY);                          // Sets the cursor position to the specified coordinates.
  195.     void Color(ATTRIBUTE foreground, ATTRIBUTE background); // color attributes of outgoing characters.
  196.     int  SetTitle(const char *);                            // Changes the title of the window to the specified text.
  197.  
  198.     void SetConsoleWindowSize(int nWidth, int nHeight);     // Resizes the window to the desired width and height.
  199.     void DisableResisingOfConsole(void);                    // Disable the resising of the console boders.
  200.     void EnableResisingOfConsole(void);                     // Enable the resising of the console boders.
  201.  
  202.     // Information Routines:
  203.     int  GetWidth(void);                                    // Returns the width of the console, in characters.
  204.     int  GetHeight(void);                                   // Returns the height of the console, in characters.
  205.    
  206.     friend void ErrorExit(const char *, Win32Console *);
  207.  
  208. private:
  209.  
  210.     // Private constructors and Destructors:
  211.     Win32Console();                                         // Default constructor.
  212.     ~Win32Console();                                        // Destructor.
  213.     //Win32Console(Win32Console const&);                        // Copy constructor is private. (Not Implemented).
  214.     //Win32Console& operator=(Win32Console const&);         // Assignment operator is private. (Not Implemented).
  215.  
  216.  
  217.     // Private Properties:
  218.     WORD   wAttributes;                 // Specifies the current outgoing text attributes.
  219.     COORD  coordPlayerPosition;         // Position to wirte output in console.
  220.  
  221.     HANDLE hOutput;                     // Handle to the output stream for the console.
  222.     HANDLE hInput;                      // Handle to the input stream for the console.
  223.     DWORD  fdwSaveOldConsoleMode;       // Old console mode
  224.  
  225.     TCHAR  szOldConsoleTitle[MAX_PATH]; // The console orginal titel.
  226.  
  227.     DWORD  dwOldConsoleStyle;           // The orginal console style of the window.
  228.  
  229. };
  230.  
  231. #include "Console.tpp"                  // Template declaration.
  232.  
  233. static Win32Console& console = Win32Console::Instance();
  234.  
  235. #endif  // CONSOLE_H
  236.  
  237. /************************** END OF SOURCE **************************/
  238.  
  239. /********************************************************************
  240. *
  241. * file:     Console.tpp
  242. *
  243. * brief:    Console interface library
  244. *
  245. * version:  1.0.1
  246. *
  247. * details:  This file is used with Console.h
  248. *
  249. * author:   w2ch Anon
  250. *
  251. * date: May 2014
  252. *
  253. * copyright: None
  254. *
  255. ********************************************************************/
  256.  
  257. template<class T>
  258. Win32Console & Win32Console::operator << (const T tOutput)
  259. {
  260.     if (coordPlayerPosition.X > GetWidth()) return *this;   // Cehck if the write cordinates(x only) are inside the bounds of the console window
  261.  
  262.     DWORD cWritten;
  263.  
  264.     // Write a string of colors to a screen buffer.
  265.     if (!FillConsoleOutputAttribute(hOutput,               // screen buffer handle
  266.                                     wAttributes,           // color to fill with
  267.                                     1,                     // number of cells to fill
  268.                                     coordPlayerPosition,   // first cell to write to
  269.                                     &cWritten))            // actual number written
  270.         ExitProcess(0);
  271.  
  272.     // Write a string of characters to a screen buffer.
  273.     if (!FillConsoleOutputCharacter(hOutput,              // screen buffer handle
  274.                                     tOutput,              // charecter to be writen
  275.                                     1,     // number of times the charecter should be writen
  276.                                     coordPlayerPosition,  // first cell to write to
  277.                                     &cWritten))           // actual number written
  278.         ExitProcess(0);
  279.  
  280.     coordPlayerPosition.X += (short)cWritten;
  281.  
  282.     return *this;
  283. }
  284.  
  285. /************************** END OF SOURCE **************************/
  286.  
  287. /********************************************************************
  288. *
  289. * file:     Console.cpp
  290. *
  291. * brief:    Console interface library
  292. *
  293. * version:  1.0.2
  294. *
  295. * details:  This file is used with Console.h
  296. *
  297. * author:   w2ch Anon
  298. *
  299. * date: May 2014
  300. *
  301. * copyright: None
  302. *
  303. ********************************************************************/
  304.  
  305.  
  306. #include "Console.h"
  307.  
  308.  
  309. // Function declaring
  310. void ErrorExit(const char *szErrorMessage, Win32Console *w32cpObj)
  311. {
  312.     // 1. Print the error message to stderr
  313.     fprintf(stderr, "%s\n", szErrorMessage);
  314.  
  315.     // 2. Call the console destructor
  316.     w32cpObj->~Win32Console();          // NOTE:Can create error loop?
  317.  
  318.     // 3. Exit the program
  319.     ExitProcess(0);
  320. }
  321.  
  322.  
  323. // Constructors and Destructors
  324. Win32Console::Win32Console()                                        // Default constructor
  325. {
  326.     // 1. No new console title set
  327.     szOldConsoleTitle[0] = '\0';
  328.  
  329.     // 2. Set player position
  330.     coordPlayerPosition.X = 0;
  331.     coordPlayerPosition.Y = 0;
  332.  
  333.     // 3. Get console input and output handels and clear the input buffer
  334.     hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  335.     hInput  = GetStdHandle(STD_INPUT_HANDLE);
  336.  
  337.     FlushConsoleInputBuffer(hInput); //just need one time as ReadConsoleInput dose it automaticly
  338.  
  339.     if (hInput == INVALID_HANDLE_VALUE) ErrorExit("GetStdHandle", this);
  340.  
  341.     // 4. Save the current input mode(restored on exit)
  342.     if (!GetConsoleMode(hInput, &fdwSaveOldConsoleMode)) ErrorExit("GetConsoleMode", this);
  343.  
  344.     // 5. Enable the window input events
  345.     DWORD fdwMode = ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT | ENABLE_EXTENDED_FLAGS;   //0x0080 == ENABLE_EXTENDED_FLAGS
  346.     if (!SetConsoleMode(hInput, fdwMode)) ErrorExit("SetConsoleMode", this);
  347.  
  348.     // 6. Get the console style(for border set to static)
  349.     HWND  hwndConsole;
  350.  
  351.     hwndConsole = GetConsoleWindow();
  352.     if (hwndConsole == NULL) ErrorExit("GetConsoleWindow", this);
  353.  
  354.     dwOldConsoleStyle = GetWindowLong(hwndConsole, GWL_STYLE);
  355.         if (!dwOldConsoleStyle) ErrorExit("GetWindowLong", this);
  356.  
  357.     // 7. Hide the console curser
  358.     CONSOLE_CURSOR_INFO CCI;
  359.     GetConsoleCursorInfo(hOutput, &CCI);
  360.     CCI.bVisible = false;
  361.     SetConsoleCursorInfo(hOutput, &CCI);
  362.  
  363.     // 8. Set the default text attribute
  364.     Color(GREY, BLACK);
  365. }
  366.  
  367. Win32Console::~Win32Console()
  368. {
  369.     // 1. Restore the old console mode
  370.     SetConsoleMode(hInput, fdwSaveOldConsoleMode);
  371.  
  372.     // 2. Show the console curser
  373.     CONSOLE_CURSOR_INFO CCI;
  374.     GetConsoleCursorInfo(hOutput, &CCI);
  375.     CCI.bVisible = true;
  376.     SetConsoleCursorInfo(hOutput, &CCI);
  377.  
  378.     // 3. reset the console title if the title has been changed
  379.     if (szOldConsoleTitle[0] != '\0') SetTitle(szOldConsoleTitle);
  380. }
  381.  
  382.  
  383. // Get console information
  384. int Win32Console::GetWidth(void)
  385. {
  386.     CONSOLE_SCREEN_BUFFER_INFO csbiWin32Console;
  387.     GetConsoleScreenBufferInfo(hOutput, &csbiWin32Console);
  388.     return csbiWin32Console.srWindow.Right - csbiWin32Console.srWindow.Left + 1;
  389. }
  390.  
  391. int Win32Console::GetHeight(void)
  392. {
  393.     CONSOLE_SCREEN_BUFFER_INFO csbiWin32Console;
  394.     GetConsoleScreenBufferInfo(hOutput, &csbiWin32Console);
  395.     return csbiWin32Console.srWindow.Bottom - csbiWin32Console.srWindow.Top + 1;
  396. }
  397.  
  398.  
  399. // Cosnole I/O
  400. Win32Console& Win32Console::operator <<(const char *szWrite)
  401. {
  402.     if (coordPlayerPosition.X > GetWidth()) return *this;   // Cehck if the write cordinates(x only) are inside the bounds of the console window
  403.  
  404.     DWORD cWritten;
  405.  
  406.     // Write a string of colors to a screen buffer.
  407.     if (!FillConsoleOutputAttribute(hOutput,               // screen buffer handle
  408.                                     wAttributes,           // color to fill with
  409.                                     lstrlen(szWrite) - ( (coordPlayerPosition.X + lstrlen(szWrite) > GetWidth() ) * ( coordPlayerPosition.X + lstrlen(szWrite) - GetWidth() ) ),    // number of cells to fill
  410.                                     coordPlayerPosition,   // first cell to write to
  411.                                     &cWritten))            // actual number written
  412.         ErrorExit("FillConsoleOutputAttribute", this);
  413.  
  414.     // Write a string of characters to a screen buffer.
  415.     if (!WriteConsoleOutputCharacter(hOutput,              // screen buffer handle
  416.                                      szWrite,              // pointer to source string
  417.                                      lstrlen(szWrite) - ( (coordPlayerPosition.X + lstrlen(szWrite) > GetWidth() ) * ( coordPlayerPosition.X + lstrlen(szWrite) - GetWidth() ) ),   // get the length of the string
  418.                                      coordPlayerPosition,  // first cell to write to
  419.                                      &cWritten))           // actual number written
  420.         ErrorExit("WriteConsoleOutputCharacter", this);
  421.    
  422.     coordPlayerPosition.X += (short)cWritten;
  423.  
  424.     return *this;
  425. }
  426.  
  427. // int  Win32Console::get(void)
  428. /* There is no need for this function right now
  429. int Win32Console::get(void)                                                 // Retrieves a single, ASCII encoded character from the console
  430. {
  431. DWORD cNumRead;
  432. INPUT_RECORD irInBuf[128];
  433. char cQuit = 1;
  434. unsigned int i;
  435.  
  436. // Wait for the events
  437. PeekConsoleInput(hInput,     // input buffer handle
  438. irInBuf,     // buffer to read into
  439. 128,         // size of read buffer
  440. &cNumRead);  // number of records read
  441.  
  442. for (i = 0; i < cNumRead && cQuit; i++) if (irInBuf[i].Event.KeyEvent.wVirtualKeyCode) --cQuit;
  443.  
  444. if(cNumRead) FlushConsoleInputBuffer(hInput);
  445.  
  446. return irInBuf[i].Event.KeyEvent.wVirtualKeyCode;
  447. } */
  448. KEYPRESS Win32Console::WaitForConsoleInput(int nValue)      // Waits for console input that accepts cValue flag value
  449. {
  450.     DWORD cNumRead;
  451.     INPUT_RECORD irInBuf[128];
  452.     KEYPRESS ReturnKeypress = { CONSOLE_KEY_NONE, 0, { 0, 0 } }; // 2nd, 0 is for ReturnKeypress.bErrorCode; 3rd is for mouse posision
  453.    
  454.  
  455.     Label1:
  456.  
  457.     // Wait for the events.
  458.     do {
  459.         if (!ReadConsoleInput(hInput,      // input buffer handle
  460.                               irInBuf,     // buffer to read into
  461.                               128,         // size of read buffer
  462.                               &cNumRead))  // number of records read
  463.             ErrorExit("ReadConsoleInput", this);
  464.     } while (irInBuf[0].EventType & nValue);    // only get the right input by AND operator with the result on the nValue
  465.  
  466.     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
  467.     // NOTE:I use goto here because I think it is most readable code produce
  468.  
  469.  
  470.     switch (irInBuf[0].EventType)
  471.     {
  472.         case KEY_EVENT:
  473.             switch (irInBuf[0].Event.KeyEvent.wVirtualKeyCode)
  474.             {
  475.                 case 0x41:
  476.                     ReturnKeypress.eCode = CONSOLE_KEY_A;
  477.                     break;
  478.                 case 0x42:
  479.                     ReturnKeypress.eCode = CONSOLE_KEY_B;
  480.                     break;
  481.                 case 0x43:
  482.                     ReturnKeypress.eCode = CONSOLE_KEY_C;
  483.                     break;
  484.                 case 0x44:
  485.                     ReturnKeypress.eCode = CONSOLE_KEY_D;
  486.                     break;
  487.                 case 0x45:
  488.                     ReturnKeypress.eCode = CONSOLE_KEY_E;
  489.                     break;
  490.                 case 0x46:
  491.                     ReturnKeypress.eCode = CONSOLE_KEY_F;
  492.                     break;
  493.                 case 0x47:
  494.                     ReturnKeypress.eCode = CONSOLE_KEY_G;
  495.                     break;
  496.                 case 0x48:
  497.                     ReturnKeypress.eCode = CONSOLE_KEY_H;
  498.                     break;
  499.                 case 0x49:
  500.                     ReturnKeypress.eCode = CONSOLE_KEY_I;
  501.                     break;
  502.                 case 0x4A:
  503.                     ReturnKeypress.eCode = CONSOLE_KEY_J;
  504.                     break;
  505.                 case 0x4B:
  506.                     ReturnKeypress.eCode = CONSOLE_KEY_K;
  507.                     break;
  508.                 case 0x4C:
  509.                     ReturnKeypress.eCode = CONSOLE_KEY_L;
  510.                     break;
  511.                 case 0x4D:
  512.                     ReturnKeypress.eCode = CONSOLE_KEY_M;
  513.                     break;
  514.                 case 0x4E:
  515.                     ReturnKeypress.eCode = CONSOLE_KEY_N;
  516.                     break;
  517.                 case 0x4F:
  518.                     ReturnKeypress.eCode = CONSOLE_KEY_O;
  519.                     break;
  520.                 case 0x50:
  521.                     ReturnKeypress.eCode = CONSOLE_KEY_P;
  522.                     break;
  523.                 case 0x51:
  524.                     ReturnKeypress.eCode = CONSOLE_KEY_Q;
  525.                     break;
  526.                 case 0x52:
  527.                     ReturnKeypress.eCode = CONSOLE_KEY_R;
  528.                     break;
  529.                 case 0x53:
  530.                     ReturnKeypress.eCode = CONSOLE_KEY_S;
  531.                     break;
  532.                 case 0x54:
  533.                     ReturnKeypress.eCode = CONSOLE_KEY_T;
  534.                     break;
  535.                 case 0x55:
  536.                     ReturnKeypress.eCode = CONSOLE_KEY_U;
  537.                     break;
  538.                 case 0x56:
  539.                     ReturnKeypress.eCode = CONSOLE_KEY_V;
  540.                     break;
  541.                 case 0x57:
  542.                     ReturnKeypress.eCode = CONSOLE_KEY_W;
  543.                     break;
  544.                 case 0x58:
  545.                     ReturnKeypress.eCode = CONSOLE_KEY_X;
  546.                     break;
  547.                 case 0x59:
  548.                     ReturnKeypress.eCode = CONSOLE_KEY_Y;
  549.                     break;
  550.                 case 0x5A:
  551.                     ReturnKeypress.eCode = CONSOLE_KEY_Z;
  552.                     break;
  553.                 case 0x30:
  554.                     ReturnKeypress.eCode = CONSOLE_KEY_0;
  555.                     break;
  556.                 case 0x31:
  557.                     ReturnKeypress.eCode = CONSOLE_KEY_1;
  558.                     break;
  559.                 case 0x32:
  560.                     ReturnKeypress.eCode = CONSOLE_KEY_2;
  561.                     break;
  562.                 case 0x33:
  563.                     ReturnKeypress.eCode = CONSOLE_KEY_3;
  564.                     break;
  565.                 case 0x34:
  566.                     ReturnKeypress.eCode = CONSOLE_KEY_4;
  567.                     break;
  568.                 case 0x35:
  569.                     ReturnKeypress.eCode = CONSOLE_KEY_5;
  570.                     break;
  571.                 case 0x36:
  572.                     ReturnKeypress.eCode = CONSOLE_KEY_6;
  573.                     break;
  574.                 case 0x37:
  575.                     ReturnKeypress.eCode = CONSOLE_KEY_7;
  576.                     break;
  577.                 case 0x38:
  578.                     ReturnKeypress.eCode = CONSOLE_KEY_8;
  579.                     break;
  580.                 case 0x39:
  581.                     ReturnKeypress.eCode = CONSOLE_KEY_9;
  582.                     break;
  583.                 case VK_SPACE:
  584.                     ReturnKeypress.eCode = CONSOLE_KEY_SPACE;
  585.                     break;
  586.                 case VK_TAB:
  587.                     ReturnKeypress.eCode = CONSOLE_KEY_TAB;
  588.                     break;
  589.                 case VK_ESCAPE:
  590.                     ReturnKeypress.eCode = CONSOLE_KEY_ESCAPE;
  591.                     break;
  592.                 case VK_RETURN:
  593.                     ReturnKeypress.eCode = CONSOLE_KEY_RETURN;
  594.                     break;
  595.                 case VK_BACK:
  596.                     ReturnKeypress.eCode = CONSOLE_KEY_BACKSPACE;
  597.                     break;
  598.                 case VK_PRIOR:
  599.                     ReturnKeypress.eCode = CONSOLE_KEY_PGUP;
  600.                     break;
  601.                 case VK_NEXT:
  602.                     ReturnKeypress.eCode = CONSOLE_KEY_PGDN;
  603.                     break;
  604.                 case VK_HOME:
  605.                     ReturnKeypress.eCode = CONSOLE_KEY_HOME;
  606.                     break;
  607.                 case VK_END:
  608.                     ReturnKeypress.eCode = CONSOLE_KEY_END;
  609.                     break;
  610.                 case VK_INSERT:
  611.                     ReturnKeypress.eCode = CONSOLE_KEY_INSERT;
  612.                     break;
  613.                 case VK_DELETE:
  614.                     ReturnKeypress.eCode = CONSOLE_KEY_DELETE;
  615.                     break;
  616.                 case VK_UP:
  617.                     ReturnKeypress.eCode = CONSOLE_KEY_UP;
  618.                     break;
  619.                 case VK_DOWN:
  620.                     ReturnKeypress.eCode = CONSOLE_KEY_DOWN;
  621.                     break;
  622.                 case VK_LEFT:
  623.                     ReturnKeypress.eCode = CONSOLE_KEY_LEFT;
  624.                     break;
  625.                 case VK_RIGHT:
  626.                     ReturnKeypress.eCode = CONSOLE_KEY_RIGHT;
  627.                     break;
  628.                 case VK_F1:
  629.                     ReturnKeypress.eCode = CONSOLE_KEY_F1;
  630.                     break;
  631.                 case VK_F2:
  632.                     ReturnKeypress.eCode = CONSOLE_KEY_F2;
  633.                     break;
  634.                 case VK_F3:
  635.                     ReturnKeypress.eCode = CONSOLE_KEY_F3;
  636.                     break;
  637.                 case VK_F4:
  638.                     ReturnKeypress.eCode = CONSOLE_KEY_F4;
  639.                     break;
  640.                 case VK_F5:
  641.                     ReturnKeypress.eCode = CONSOLE_KEY_F5;
  642.                     break;
  643.                 case VK_F6:
  644.                     ReturnKeypress.eCode = CONSOLE_KEY_F6;
  645.                     break;
  646.                 case VK_F7:
  647.                     ReturnKeypress.eCode = CONSOLE_KEY_F7;
  648.                     break;
  649.                 case VK_F8:
  650.                     ReturnKeypress.eCode = CONSOLE_KEY_F8;
  651.                     break;
  652.                 case VK_F9:
  653.                     ReturnKeypress.eCode = CONSOLE_KEY_F9;
  654.                     break;
  655.                 case VK_F10:
  656.                     ReturnKeypress.eCode = CONSOLE_KEY_F10;
  657.                     break;
  658.                 case VK_F11:
  659.                     ReturnKeypress.eCode = CONSOLE_KEY_F11;
  660.                     break;
  661.                 case VK_F12:
  662.                     ReturnKeypress.eCode = CONSOLE_KEY_F12;
  663.                     break;
  664.                 default:
  665.                     ReturnKeypress.bErrorCode = 1;
  666.                     break;
  667.             }
  668.             break;
  669.  
  670.         case MOUSE_EVENT:
  671.  
  672.             ReturnKeypress.coordMousePosition.X = irInBuf[0].Event.MouseEvent.dwMousePosition.X;
  673.             ReturnKeypress.coordMousePosition.Y = irInBuf[0].Event.MouseEvent.dwMousePosition.Y;
  674.            
  675.             switch (irInBuf[0].Event.MouseEvent.dwButtonState)
  676.             {
  677.                 case FROM_LEFT_1ST_BUTTON_PRESSED:
  678.                     ReturnKeypress.eCode = CONSOLE_MOUSE_LEFT_BUTTON;
  679.                     break;
  680.                 case RIGHTMOST_BUTTON_PRESSED:
  681.                     ReturnKeypress.eCode = CONSOLE_MOUSE_RIGHT_BUTTON;
  682.                     break;
  683.                 default:
  684.                     ReturnKeypress.bErrorCode = 1;
  685.                     break;
  686.             }
  687.  
  688.             break;
  689.  
  690.         default:
  691.             ReturnKeypress.bErrorCode = 1;
  692.             break;
  693.     }
  694.  
  695.     return ReturnKeypress;
  696. }
  697.  
  698.  
  699. // Console manipulation
  700. int Win32Console::Clear(void)
  701. {
  702.     CONSOLE_SCREEN_BUFFER_INFO csbiWin32Console;
  703.     COORD coordScreen = { 0, 0 };
  704.     DWORD cCharsWritten;
  705.     DWORD dwConSize;
  706.  
  707.  
  708.     // Get the number of character cells in the current buffer.
  709.     if (!GetConsoleScreenBufferInfo(hOutput, &csbiWin32Console))
  710.     {
  711.         return 1;
  712.     }
  713.  
  714.     dwConSize = csbiWin32Console.dwSize.X * csbiWin32Console.dwSize.Y;
  715.  
  716.     // Fill the entire screen with blanks.
  717.     if (!FillConsoleOutputCharacter(hOutput,         // Handle to console screen buffer
  718.         (TCHAR) ' ',     // Character to write to the buffer
  719.         dwConSize,       // Number of cells to write
  720.         coordScreen,     // Coordinates of first cell
  721.         &cCharsWritten)) // Receive number of characters written
  722.     {
  723.         return 1;
  724.     }
  725.  
  726.     // Get the current text attribute.
  727.     if (!GetConsoleScreenBufferInfo(hOutput, &csbiWin32Console))
  728.     {
  729.         return 1;
  730.     }
  731.  
  732.     // Set the buffer's attributes accordingly.
  733.     if (!FillConsoleOutputAttribute(hOutput,                      // Handle to console screen buffer
  734.         csbiWin32Console.wAttributes, // Character attributes to use
  735.         dwConSize,                    // Number of cells to set attribute
  736.         coordScreen,                  // Coordinates of first cell
  737.         &cCharsWritten))              // Receive number of characters written
  738.     {
  739.         return 1;
  740.     }
  741.  
  742.     return 0;
  743. }
  744.  
  745. void Win32Console::Position(int nX, int nY)
  746. {
  747.     coordPlayerPosition.X = nX;
  748.     coordPlayerPosition.Y = nY;
  749. }
  750.  
  751. void Win32Console::Color(ATTRIBUTE foreground, ATTRIBUTE background)
  752. {
  753.     wAttributes = 0;
  754.  
  755.     switch (foreground)
  756.     {
  757.     case BLACK:
  758.         wAttributes += 0;
  759.         break;
  760.         /*  case BROWN:
  761.         wAttributes += ;
  762.         break; */
  763.         /*  case GREY:
  764.         wAttributes += ;
  765.         break; */
  766.     case RED:
  767.         wAttributes += FOREGROUND_RED;
  768.         break;
  769.     case GREEN:
  770.         wAttributes += FOREGROUND_GREEN;
  771.         break;
  772.     case YELLOW:
  773.         wAttributes += FOREGROUND_RED | FOREGROUND_GREEN;
  774.         break;
  775.     case BLUE:
  776.         wAttributes += FOREGROUND_BLUE;
  777.         break;
  778.     case MANGENTA:
  779.         wAttributes += FOREGROUND_RED | FOREGROUND_BLUE;
  780.         break;
  781.     case CYAN:
  782.         wAttributes += FOREGROUND_BLUE | FOREGROUND_GREEN;
  783.         break;
  784.     case WHITE:
  785.     case GREY:
  786.     default:
  787.         wAttributes += FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN;
  788.         break;
  789.     }
  790.  
  791.     switch (background)
  792.     {
  793.     case BLACK:
  794.         wAttributes += 0;
  795.         break;
  796.         /*  case BROWN:
  797.         wAttributes += ;
  798.         break; */
  799.         /*  case GREY:
  800.         wAttributes += ;
  801.         break; */
  802.     case RED:
  803.         wAttributes += BACKGROUND_RED;
  804.         break;
  805.     case GREEN:
  806.         wAttributes += BACKGROUND_GREEN;
  807.         break;
  808.     case YELLOW:
  809.         wAttributes += BACKGROUND_RED | BACKGROUND_GREEN;
  810.         break;
  811.     case BLUE:
  812.         wAttributes += BACKGROUND_BLUE;
  813.         break;
  814.     case MANGENTA:
  815.         wAttributes += BACKGROUND_RED | BACKGROUND_BLUE;
  816.         break;
  817.     case CYAN:
  818.         wAttributes += BACKGROUND_BLUE | BACKGROUND_GREEN;
  819.         break;
  820.     case WHITE:
  821.     case GREY:
  822.     default:
  823.         wAttributes += BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN;
  824.         break;
  825.     }
  826. }
  827.  
  828. int Win32Console::SetTitle(LPCTSTR szNewConsoleTitle)
  829. {
  830.     // save orginal console title for later to reset
  831.     if (szOldConsoleTitle[0] == '\0') GetConsoleTitle(szOldConsoleTitle, MAX_PATH);
  832.  
  833.     // set new console title
  834.     if (!SetConsoleTitle(szNewConsoleTitle)) return 1;
  835.  
  836.     return 0;
  837. }
  838.  
  839.  
  840. void Win32Console::SetConsoleWindowSize(int nWidth, int nHeight)
  841. {
  842.     // If either dimension is greater than the largest console window we can have,
  843.     // there is no point in attempting the change.
  844.     {
  845.         COORD largestSize = GetLargestConsoleWindowSize(hOutput);
  846.         if (nWidth  > largestSize.X) ErrorExit("GetLargestConsoleWindowSize, The x dimension is too large.", this);
  847.         if (nHeight > largestSize.Y) ErrorExit("GetLargestConsoleWindowSize, The y dimension is too large.", this);
  848.     }
  849.  
  850.     CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
  851.     if (!GetConsoleScreenBufferInfo(hOutput, &bufferInfo)) ErrorExit("bufferInfo, Unable to retrieve screen buffer info.", this);
  852.  
  853.     SMALL_RECT& winInfo = bufferInfo.srWindow;
  854.     COORD windowSize = { winInfo.Right - winInfo.Left + 1, winInfo.Bottom - winInfo.Top + 1 };
  855.  
  856.  
  857.  
  858.     if (windowSize.X > nWidth || windowSize.Y > nHeight)
  859.     {
  860.         // window size needs to be adjusted before the buffer size can be reduced.
  861.         SMALL_RECT info =
  862.         {
  863.             0,
  864.             0,
  865.             nWidth  < windowSize.X ?  nWidth - 1 : windowSize.X - 1,
  866.             nHeight < windowSize.Y ? nHeight - 1 : windowSize.Y - 1
  867.         };
  868.  
  869.         if (!SetConsoleWindowInfo(hOutput, TRUE, &info)) ErrorExit("SetConsoleWindowInfo, Unable to resize window before resizing buffer.", this);
  870.     }
  871.  
  872.  
  873.  
  874.     COORD size = { nWidth, nHeight };
  875.     if (!SetConsoleScreenBufferSize(hOutput, size)) ErrorExit("SetConsoleScreenBufferSize, Unable to resize screen buffer.", this);
  876.  
  877.  
  878.     SMALL_RECT info = { 0, 0, nWidth - 1, nHeight - 1 };
  879.     if (!SetConsoleWindowInfo(hOutput, TRUE, &info)) ErrorExit("SetConsoleWindowInfo, Unable to resize window after resizing buffer.", this);
  880. }
  881.  
  882. void Win32Console::DisableResisingOfConsole(void)
  883. {
  884.     HWND  hwndConsole;
  885.  
  886.     hwndConsole = GetConsoleWindow();
  887.         if (hwndConsole == NULL) ErrorExit("GetConsoleWindow", this);
  888.  
  889.     SetWindowLong(hwndConsole, GWL_STYLE, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX);
  890.     ShowWindow(hwndConsole, SW_SHOW);
  891. }
  892.  
  893. void Win32Console::EnableResisingOfConsole(void)
  894. {
  895.     HWND  hwndConsole;
  896.  
  897.     hwndConsole = GetConsoleWindow();
  898.     if (hwndConsole == NULL) ErrorExit("GetConsoleWindow", this);
  899.  
  900.     SetWindowLong(hwndConsole, GWL_STYLE, dwOldConsoleStyle);
  901.     ShowWindow(hwndConsole, SW_SHOW);
  902. }
  903.  
  904. /************************** END OF SOURCE **************************/
Advertisement
Add Comment
Please, Sign In to add comment