/////////////////////////////////////////////////////////////////////////////// // Console.cpp // // // // Copyright (c) Jan Knepper, 1998. All Rights Reserved. // // Written by Jan Knepper // // // // Redistribution and use in source and binary forms, with or without // // modification, are permitted provided that the following conditions // // are met: // // 1. Redistributions of source code must retain the above copyright // // notice, this list of conditions and the following disclaimer. // // 2. Redistributions in binary form must reproduce the above copyright // // notice, this list of conditions and the following disclaimer in the // // documentation and/or other materials provided with the distribution. // // 3. The name of the author may not be used to endorse or promote products // // derived from this software without specific prior written permission. // // // // THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND // // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR // // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE // // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR // // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF // // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS // // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN // // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) // // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF // // THE POSSIBILITY OF SUCH DAMAGE. // /////////////////////////////////////////////////////////////////////////////// #include #define WIN32_LEAN_AND_MEAN #include #include #include "Console.h" #define USE_STD_HANDLE 1 const int COLS = 80; const int ROWS = 25; static HANDLE output; static COORD cursor; static CHAR_INFO *char_info; static short screen [ 25 ][ 80 ]; static COORD size; static COORD from; static SMALL_RECT to; static int col; static int row; static int x; static int y; static short attr = 0x0000; static int _ConsoleCheckPosition ( int, int ); void ConsoleOpen () { SECURITY_ATTRIBUTES sec = { sizeof ( SECURITY_ATTRIBUTES ), NULL, TRUE }; // Message ( "ConsoleOpen" ); ConsoleReset (); AllocConsole (); size.X = COLS; size.Y = ROWS; col = COLS; row = ROWS; y = 0; x = 0; // char_info = new CHAR_INFO [ size.Y * size.X ]; char_info = ( CHAR_INFO * ) calloc ( size.Y * size.X, sizeof ( CHAR_INFO ) ); memset ( screen, 0, sizeof ( screen ) ); #if defined(USE_STD_HANDLE) output = GetStdHandle ( STD_OUTPUT_HANDLE ); #else output = CreateConsoleScreenBuffer ( GENERIC_WRITE | GENERIC_READ, 0, &sec, CONSOLE_TEXTMODE_BUFFER, NULL ); #endif SetConsoleActiveScreenBuffer ( output ); SetConsoleMode ( output, 0L ); // Message ( "ConsoleOpen (2)" ); } void ConsoleClose () { // delete [] char_info; free ( char_info ); #if defined(USE_STD_HANDLE) #else CloseHandle ( output ); #endif FreeConsole (); } void ConsoleAttr ( short a ) { attr = a; } void ConsolePutC ( short c ) { c |= attr; ConsolePutCA ( c ); } void ConsoleMove ( int _y, int _x ) { if ( ! _ConsoleCheckPosition ( _y, _x ) ) return; y = _y; x = _x; } void ConsoleMvPutC ( int _y, int _x, short c ) { if ( ! _ConsoleCheckPosition ( _y, _x ) ) return; y = _y; x = _x; ConsolePutC ( c ); } void ConsolePutCA ( short c ) { if ( screen [ y ][ x ] != c ) { screen [ y ][ x ] = c; ConsoleChange ( y, x ); char_info [ y * size.X + x ].Char.AsciiChar = ( char ) ( c & 0x00FF ); char_info [ y * size.X + x ].Attributes = ( char ) ( ( c & 0xFF00 ) >> 0x0008 ); } if ( ++x >= col ) { x = 0; if ( ++y >= row ) y = 0; } } void ConsoleMvPutCA ( int _y, int _x, short c ) { if ( ! _ConsoleCheckPosition ( _y, _x ) ) return; y = _y; x = _x; ConsolePutCA ( c ); } void ConsolePaint () { if ( to.Left > to.Right ) // Only paint if there is some thing to paint! return; if ( to.Top > to.Bottom ) return; from.X = to.Left; from.Y = to.Top; WriteConsoleOutput ( output, char_info, size, from, &to ); } void ConsoleFlush () { // COORD coord; // { ( SHORT ) x, ( SHORT ) y }; ConsolePaint (); ConsoleCursorMove ( y, x ); ConsoleReset (); // coord.X = x; // coord.Y = y; // // SetConsoleCursorPosition ( output, coord ); } void ConsoleCursorShow () { CONSOLE_CURSOR_INFO cursorinfo; GetConsoleCursorInfo ( output, &cursorinfo ); if ( ! cursorinfo.bVisible ) { cursorinfo.bVisible = TRUE; SetConsoleCursorInfo ( output, &cursorinfo ); } } void ConsoleCursorHide () { CONSOLE_CURSOR_INFO cursorinfo; GetConsoleCursorInfo ( output, &cursorinfo ); if ( cursorinfo.bVisible ) { cursorinfo.bVisible = FALSE; SetConsoleCursorInfo ( output, &cursorinfo ); } } void ConsoleCursorMove ( int y, int x ) { // COORD coord; if ( ( cursor.X == x ) && ( cursor.Y == y ) ) return; cursor.X = x; cursor.Y = y; SetConsoleCursorPosition ( output, cursor ); } void ConsoleChange ( int y, int x ) { if ( x < to.Left ) to.Left = ( SHORT ) x; if ( x > to.Right ) to.Right = ( SHORT ) x; if ( y < to.Top ) to.Top = ( SHORT ) y; if ( y > to.Bottom ) to.Bottom = ( SHORT ) y; } void ConsoleTouch () { to.Left = 0; to.Right = col; to.Top = 0; to.Bottom = row; } void ConsoleReset () { to.Left = col; to.Right = 0; to.Top = row; to.Bottom = 0; } static int _ConsoleCheckPosition ( int y, int x ) { if ( y < 0 ) return ( FALSE ); if ( y >= row ) return ( FALSE ); if ( x < 0 ) return ( FALSE ); if ( x >= col ) return ( FALSE ); return ( TRUE ); }