Advertisement
Volatile_Pulse

header.cpp

Jun 15th, 2012
741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.59 KB | None | 0 0
  1. /******************************************************************************
  2.  *                                                                            *
  3.  *                            Volatile Programming                            *
  4.  *                                 Header.cpp                                 *
  5.  *                                                                            *
  6.  ******************************************************************************
  7.  *                                                                            *
  8.  * By: Volatile Pulse                                                         *
  9.  * What: File that contains the header definitions                            *
  10.  * Date: 06.15.2012                                                           *
  11.  *                                                                            *
  12.  ******************************************************************************/
  13.  
  14. #include "header.h"
  15.  
  16. #ifdef _WIN32
  17.  
  18. void VP_SetConsoleCursor(bool bCursor) {
  19.     CONSOLE_CURSOR_INFO curCursorInfo;
  20.     curCursorInfo.bVisible = bCursor;
  21.     curCursorInfo.dwSize = 1;
  22.     SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &curCursorInfo);
  23. }
  24.  
  25. void VP_GoToXY(int x, int y) {
  26.     COORD CursorPosition = {x, y};
  27.     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), CursorPosition);
  28. }
  29.  
  30. void VP_SetConsoleColor(int textColor, int bgColor) {
  31.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (textColor + (bgColor * 16)));
  32. }
  33.  
  34. void VP_ClearScreen() {
  35.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  36.     HANDLE hStdOut;
  37.     DWORD count;
  38.     DWORD cellCount;
  39.     COORD homeCoords = { 0, 0 };
  40.  
  41.     hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
  42.     if (hStdOut == INVALID_HANDLE_VALUE) return;
  43.  
  44.     // Get the number of cells in the current buffer
  45.     if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
  46.     cellCount = csbi.dwSize.X *csbi.dwSize.Y;
  47.  
  48.     // Fill the entire buffer with spaces
  49.     if (!FillConsoleOutputCharacter(hStdOut,(TCHAR) ' ', cellCount, homeCoords, &count))
  50.         return;
  51.  
  52.     // Fill the entire buffer with the current colors and attributes
  53.     if (!FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, cellCount, homeCoords, &count))
  54.         return;
  55.  
  56.     // Move the cursor home
  57.     SetConsoleCursorPosition( hStdOut, homeCoords );
  58.     /* Windows */
  59. }
  60.  
  61. int VP_GetCh () {
  62.     /* Windows */
  63.     HANDLE hConsole;
  64.     INPUT_RECORD inrec;
  65.     DWORD count, mode;
  66.  
  67.     // Set the console mode to no-echo, raw input, and no window or mouse events.
  68.     hConsole = GetStdHandle(STD_INPUT_HANDLE);
  69.     if (hConsole == INVALID_HANDLE_VALUE || !GetConsoleMode(hConsole, &mode)|| !SetConsoleMode(hConsole, 0))
  70.         return 0;
  71.  
  72.     FlushConsoleInputBuffer(hConsole);
  73.  
  74.     // Get a single key RELEASE
  75.     do {
  76.         ReadConsoleInput(hConsole, &inrec, 1, &count);
  77.     } while ((inrec.EventType != KEY_EVENT) || inrec.Event.KeyEvent.bKeyDown);
  78.  
  79.     // Restore the original console mode
  80.     SetConsoleMode(hConsole, mode);
  81.  
  82.     return inrec.Event.KeyEvent.wVirtualKeyCode;
  83.     /* Windows */
  84. }
  85.  
  86. #endif
  87.  
  88. /* POSIX * /
  89. #include <unistd.h>
  90. #include <term.h>
  91.  
  92. void ClearScreen() {
  93.    if (!cur_term) {
  94.       int result;
  95.       setupterm( NULL, STDOUT_FILENO, &result );
  96.       if (result <= 0) return;
  97.    }
  98.  
  99.    putp( tigetstr( "clear" ) );
  100. }
  101. / * POSIX */
  102.  
  103. /* ---------------------------------------------------------------------------
  104.  * PressAnyKey()
  105.  * ---------------------------------------------------------------------------
  106.  * Copyright 2008 Michael Thomas Greer
  107.  * http://www.boost.org/LICENSE_1_0.txt
  108.  *
  109.  * function
  110.  *   Optionally print a message and and wait for the user to press (and
  111.  *   release) a single key.
  112.  *
  113.  * arguments
  114.  *   The message to print. If NULL, uses a default message. Specify the empty
  115.  *   string "" to not print anything.
  116.  *
  117.  * returns
  118.  *   The keycode for the key that was pressed.
  119.  *
  120.  *   Extended key codes (like arrow keys) are properly handled, but their
  121.  *   keycode is not understood; they are simply returned as the last code in
  122.  *   the sequence, negated. For example, it is likely that the arrow keys are:
  123.  *
  124.  *     UP_ARROW    = -'A' = -65
  125.  *     DOWN_ARROW  = -'B' = -66
  126.  *     RIGHT_ARROW = -'C' = -67
  127.  *     LEFT_ARROW  = -'D' = -68
  128.  *
  129.  *   Exactly identifying the values for these keys requires a foray into the
  130.  *   terminfo database, which is a subject for later. For now we'll leave it
  131.  *   at this.
  132.  */
  133.  
  134. /* POSIX * /
  135. #include <stdio.h>
  136. #include <unistd.h>
  137. #include <termios.h>
  138.  
  139. int PressAnyKey( const char* prompt )
  140. {
  141. #define MAGIC_MAX_CHARS 18
  142. struct termios initial_settings;
  143. struct termios settings;
  144. unsigned char  keycodes[ MAGIC_MAX_CHARS ];
  145. int            count;
  146.  
  147. tcgetattr( STDIN_FILENO, &initial_settings );
  148. settings = initial_settings;
  149.  
  150. / * Set the console mode to no-echo, raw input. * /
  151. / * The exact meaning of all this jazz will be discussed later. * /
  152. settings.c_cc[ VTIME ] = 1;
  153. settings.c_cc[ VMIN  ] = MAGIC_MAX_CHARS;
  154. settings.c_iflag &= ~(IXOFF);
  155. settings.c_lflag &= ~(ECHO | ICANON);
  156. tcsetattr( STDIN_FILENO, TCSANOW, &settings );
  157.  
  158. printf( "%s", prompt ? prompt : "Press a key to continue..." );
  159. count = read( stdin, (void*)keycodes, MAGIC_MAX_CHARS );
  160.  
  161. tcsetattr( STDIN_FILENO, TCSANOW, &initial_settings );
  162.  
  163. return (count == 1)
  164.      ? keycodes[ 0 ]
  165.      : -(int)(keycodes[ count -1 ]);
  166. }
  167. / * POSIX */
  168.  
  169. /* #if defined(__cplusplus)
  170. #include <string>
  171. inline int PressAnyKey( const std::string& prompt = "Press a key to continue..." )
  172.   {
  173.   return PressAnyKey( prompt.c_str() );
  174.   }
  175. #endif */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement