Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
Sheddex
unregistriert
Treue Seele
Wohnort: fragt einfach per pm nix da ;)
Beruf: Ausbildung (Technischer Assistent für Software Technologie)
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
namespace Console { enum tsStyle { TS_NORMAL = 0, TS_UNDERLINE = 4, TS_BLINK = 5, TS_INVERSE = 7, }; enum tcColor { TC_BLACK = 0, TC_RED = 1, TC_GREEN = 2, TC_BROWN = 3, TC_BLUE = 4, TC_MAGENTA = 5, TC_CYAN = 6, TC_NORMAL = 7, TC_DARKGRAY = 8, TC_LIGHTRED = 9, TC_LIGHTGREEN = 10, TC_YELLOW = 11, TC_LIGHTBLUE = 12, TC_LIGHTMAGENTA = 13, TC_LIGHTCYAN = 14, TC_WHITE = 15 }; class ColorManip { private: tcColor TextColor; public: ColorManip( tcColor color = TC_NORMAL ) : TextColor( color ) {} friend std::ostream& operator<<( std::ostream& out, const ColorManip& manip ) { tcColor color = manip.TextColor; tsStyle style = TS_NORMAL; if ( color > TC_NORMAL ) { style = static_cast< tsStyle >( 1 ); color = static_cast< tcColor >( color - TC_DARKGRAY ); } return ( out << "\033[" << style << ";" << ( color+30 ) << "m" ); } }; class BackColorManip { private: tcColor BackgroundColor; public: BackColorManip( tcColor color = TC_NORMAL ) : BackgroundColor( color) {} friend std::ostream& operator<<( std::ostream& out, const BackColorManip& manip ) { return ( out << "\033[" << ( manip.BackgroundColor+30 ) << "m" ); } }; class TextStyleManip { private: tsStyle TextStyle; public: TextStyleManip( tsStyle style = TS_NORMAL ) : TextStyle( style ) {} friend std::ostream& operator<<( std::ostream& out, const TextStyleManip& manip ) { return ( out << "\033[" << manip.TextStyle << "m" ); } }; // Standard Textcolor Manipulators // static const ColorManip tc_black( TC_BLACK ); static const ColorManip tc_red( TC_RED ); static const ColorManip tc_green( TC_GREEN ); static const ColorManip tc_brown( TC_BROWN ); static const ColorManip tc_blue( TC_BLUE ); static const ColorManip tc_magenta( TC_MAGENTA ); static const ColorManip tc_cyan( TC_CYAN ); static const ColorManip tc_normal( TC_NORMAL ); static const ColorManip tc_darkgray( TC_DARKGRAY ); static const ColorManip tc_lightred( TC_LIGHTRED ); static const ColorManip tc_lightgreen( TC_LIGHTGREEN ); static const ColorManip tc_yellow( TC_YELLOW ); static const ColorManip tc_lightblue( TC_LIGHTBLUE ); static const ColorManip tc_lightmagenta( TC_LIGHTMAGENTA ); static const ColorManip tc_lightcyan( TC_LIGHTCYAN ); static const ColorManip tc_white( TC_WHITE ); // Standard Background Color Manipulators // static const BackColorManip bg_black( TC_BLACK ); static const BackColorManip bg_red( TC_RED ); static const BackColorManip bg_green( TC_GREEN ); static const BackColorManip bg_brown( TC_BROWN ); static const BackColorManip bg_blue( TC_BLUE ); static const BackColorManip bg_magenta( TC_MAGENTA ); static const BackColorManip bg_cyan( TC_CYAN ); static const BackColorManip bg_normal( TC_NORMAL ); static const BackColorManip bg_darkgray( TC_DARKGRAY ); static const BackColorManip bg_lightred( TC_LIGHTRED ); static const BackColorManip bg_lightgreen( TC_LIGHTGREEN ); static const BackColorManip bg_yellow( TC_YELLOW ); static const BackColorManip bg_lightblue( TC_LIGHTBLUE ); static const BackColorManip bg_lightmagenta( TC_LIGHTMAGENTA ); static const BackColorManip bg_lightcyan( TC_LIGHTCYAN ); static const BackColorManip bg_white( TC_WHITE ); // Standard Textstyle Manipulators // static const TextStyleManip ts_normal( TS_NORMAL ); static const TextStyleManip ts_underline( TS_UNDERLINE ); static const TextStyleManip ts_blink( TS_BLINK ); static const TextStyleManip ts_inverse( TS_INVERSE ); // Set Cursor Position // inline void gotoxy( unsigned short x, unsigned short y ) { std::cout << "\033[" << x << ";" << y << "f"; } // Clear Screen // inline void clrscr() { std::cout << "\033[2J"; } // Clear Line End // inline void clreol() { std::cout << "\033[0K"; } // Clear Line Begin // inline void clrlb() { std::cout << "\033[1K"; } // Clear Line // inline void clrln() { std::cout << "\033[2K"; } }; |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 |
int main( int argc, char* argv[] ) { using namespace Console; gotoxy( 10, 10 ); std::cout << tc_red << "Hallo " << tc_yellow << ts_underline << ts_blink << "Welt" << ts_normal << std::endl; } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
/// File: console.h #if !defined _CONSOLE_H_ #define _CONSOLE_H_ #include <windows.h> #include <iostream> #include <tchar.h> namespace Console { // Aus kompatibelitätsgründen zur alten FAQ: enum CursorType { SOLID_CURSOR = 100, NORMAL_CURSOR = 25, SMALL_CURSOR = 5 }; // Manipulatoren für die Funktionen mit 2 unsigned short Parametern bereitstellen struct _Manip2 { typedef void (*pFkt2)( unsigned short, unsigned short ); _Manip2( pFkt2 fptr, unsigned short x, unsigned short y ) :_fptr(fptr), _x(x), _y(y) {} _Manip2( const _Manip2& other ) :_fptr(other._fptr), _x(other._x), _y(other._y) {} pFkt2 _fptr; unsigned short _x, _y; }; // Operator für _Manip2 inline std::basic_ostream<TCHAR>& operator << (std::basic_ostream<TCHAR>& os, _Manip2& _manip ) { _manip._fptr( _manip._x, _manip._y ); return os; } // Manipulatoren für die Funktionen mit 1 unsigned short Parameter bereitstellen struct _Manip1 { typedef void (*pFkt1)( unsigned short ); _Manip1( pFkt1 fptr, unsigned short x ) :_fptr(fptr), _x(x) {} _Manip1( const _Manip1& other ) :_fptr(other._fptr), _x(other._x) {} pFkt1 _fptr; unsigned short _x; }; // Operator für _Manip1 inline std::basic_ostream<TCHAR>& operator << (std::basic_ostream<TCHAR>& os, _Manip1& _manip ) { _manip._fptr( _manip._x ); return os; } // Manipulatoren für die Funktionen mit 1 unsigned short Parameter bereitstellen struct _Manip0 { typedef void (*pFkt1)( void ); _Manip0( pFkt1 fptr ) :_fptr(fptr) {} _Manip0( const _Manip0& other ) :_fptr(other._fptr) {} pFkt1 _fptr; }; // Operator für _Manip0 inline std::basic_ostream<TCHAR>& operator << (std::basic_ostream<TCHAR>& os, _Manip0& _manip ) { _manip._fptr(); return os; } // zu (x, y) Koordinate gehen void gotoxy( unsigned short x, unsigned short y ); inline _Manip2 manip_gotoxy( unsigned short x, unsigned short y ) { return _Manip2( gotoxy, x, y ); } // Farben (SetConsoleTextAttribute) void color( unsigned short attr = 0 ); inline _Manip1 manip_color( unsigned short attr = 0 ) { return _Manip1( color, attr ); } // Aus kompatibelitätsgründen zur alten FAQ: inline void attribute( unsigned short attr = 0 ) { color( attr ); } // Cursortyp void cursor( unsigned short size = 0 ); inline _Manip1 manip_cursor( unsigned short size = 0 ) { return _Manip1( cursor, size ); } // Aus kompatibelitätsgründen zur alten FAQ: inline void cursor( CursorType _cursor, bool visible = true ) { cursor( (visible ? static_cast<unsigned short>(_cursor) : 0) ); } // Konsolenfenster void displaysize( unsigned short _X, unsigned short _Y ); // Ausgabebuffer void buffersize( unsigned short _X, unsigned short _Y ); // Clear Screen void cls(void); inline _Manip0 manip_cls(void) { return _Manip0( cls ); } // zur parameterfreien Nutzung template< typename Elem > std::basic_ostream< Elem >& manip_cls( std::basic_ostream< Elem >& os ) { cls(); return os; } // Titel setzen inline void title( const char* title ) { if ( ! SetConsoleTitleA( title ) ) { /*Fehlerbehandlung*/ } } inline void title( const wchar_t* title ) { if ( ! SetConsoleTitleW( title ) ) { /*Fehlerbehandlung*/ } } } #endif |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
/// File: console.cpp #include "console.h" namespace Console { void gotoxy( unsigned short x, unsigned short y ) { HANDLE hCon = GetStdHandle( STD_OUTPUT_HANDLE ); COORD pos = { x, y }; if ( ! SetConsoleCursorPosition( hCon, pos ) ) { /*Fehlerbehandlung*/ } } void color( unsigned short attr ) { HANDLE hCon = GetStdHandle( STD_OUTPUT_HANDLE ); if ( ! SetConsoleTextAttribute( hCon, attr ) ) { /*Fehlerbehandlung*/ } } // Cursorgröße, -sichtbarkeit void cursor( unsigned short size ) { HANDLE hCon = GetStdHandle( STD_OUTPUT_HANDLE ); CONSOLE_CURSOR_INFO info; if ( size == 0 ) { info.bVisible = false; size = 1; } info.dwSize = size; if ( ! SetConsoleCursorInfo( hCon, &info ) ) { /*Fehlerbehandlung*/ } } // Hier wird das Konsolenfenster bearbeitet void displaysize( unsigned short _X, unsigned short _Y ) { HANDLE hCon = GetStdHandle( STD_OUTPUT_HANDLE ); CONSOLE_SCREEN_BUFFER_INFO info; SMALL_RECT change = {0}; if ( ! GetConsoleScreenBufferInfo( hCon, &info ) ) { /*Fehlerbehandlung*/ } if ( _X >= info.dwMaximumWindowSize.X ) change.Right = info.dwMaximumWindowSize.X-1; else change.Right = _X; if ( _Y >= info.dwMaximumWindowSize.Y ) change.Bottom = info.dwMaximumWindowSize.Y-1; else change.Bottom = _Y; if ( ! SetConsoleWindowInfo( hCon, true, &change ) ) { /*Fehlerbehandlung*/ } } // Jetzt ist der Ausgabebuffer dran void buffersize( unsigned short _X, unsigned short _Y ) { HANDLE hCon = GetStdHandle( STD_OUTPUT_HANDLE ); COORD newSize; newSize.X = _X; newSize.Y = _Y; if ( ! SetConsoleScreenBufferSize( hCon, newSize ) ) { /*Fehlerbehandlung*/ } } // Schirm löschen void cls(void) { HANDLE hCon = GetStdHandle( STD_OUTPUT_HANDLE ); CONSOLE_SCREEN_BUFFER_INFO info; if ( ! GetConsoleScreenBufferInfo( hCon, &info ) ) { /*Fehlerbehandlung*/ } COORD coord={0,0}; DWORD tmp; // Farbattribute für alle Felder setzen if (! FillConsoleOutputAttribute( hCon, info.wAttributes, info.dwSize.X*info.dwSize.Y, coord, &tmp)) { /*Fehlerbehandlung*/ } // Zeichen für alle Felder setzen if (! FillConsoleOutputCharacter( hCon, _T(' '), info.dwSize.X*info.dwSize.Y, coord, &tmp)) { /*Fehlerbehandlung*/ } gotoxy( 0, 0 ); } } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
int main() { std::cout << "Test"; std::cin.get(); Console::color( FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY | BACKGROUND_BLUE ); std::cout // mit () -> void manip_cls(void) << Console::manip_cls() // ohne () -> template< typename Elem > // std::basic_ostream< Elem >& manip_cls( std::basic_ostream< Elem >& os ) << Console::manip_cls << Console::manip_cursor(0) << Console::manip_gotoxy(20,10) << "Hallo Console"; Console::gotoxy(20, 8); std::cout << Console::manip_color( FOREGROUND_RED | FOREGROUND_INTENSITY | BACKGROUND_BLUE ) << "spieleprogrammierer.de sagt"; std::cin.get(); return 0; } |
Quellcode |
|
1 2 |
SetConsoleTextAttribute( hCon, attr ) Console::attribute(FOREGROUND_BLUE); |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
void buildabox( unsigned short x, unsigned short y, unsigned short height, unsigned short width, char Zeichen, unsigned short attr = 0) { HANDLE hCon = GetStdHandle( STD_OUTPUT_HANDLE ); COORD pos = { x, y }; if ( !::SetConsoleCursorPosition( hCon, pos ) ) { /* Platz für individuelle Fehlerbehandlung ^^*/ } for(int i=0; i<height; i++) { for(int j=0; j<width; j++) { if ( ! SetConsoleTextAttribute( hCon, attr ) ) { // Platz für individuelle Fehlerbehandlung^^ } std::cout << Zeichen; } pos.Y++; if ( !::SetConsoleCursorPosition( hCon, pos ) ) { /* Platz für individuelle Fehlerbehandlung ^^*/ } } } void buildatriangle(unsigned short x, unsigned short y, unsigned short hoehe, char Zeichen, unsigned short attr) { int AnzahlZeichen = 1; int StartPosition = x + hoehe/2; HANDLE hCon = GetStdHandle( STD_OUTPUT_HANDLE ); COORD pos = { x, y }; pos.X = StartPosition; if ( !::SetConsoleCursorPosition( hCon, pos ) ) { /* Platz für individuelle Fehlerbehandlung ^^*/ } if(hoehe%2 == 0) { std::cout << "Fehler! Der Durchmesser muss eine ungerade Zahl sein." << std::endl; } for(int i=0; i<=hoehe; i++) { for (int j=1; j<=AnzahlZeichen; j++) { if ( ! SetConsoleTextAttribute( hCon, attr ) ) { // Platz für individuelle Fehlerbehandlung^^ } std::cout << Zeichen; } pos.X--; pos.Y++; AnzahlZeichen+=2; if ( !::SetConsoleCursorPosition( hCon, pos ) ) { /* Platz für individuelle Fehlerbehandlung ^^*/ } } } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 |
using namespace std; using namespace Console; int main () { buildatriangle(20,0,7,219,3); return 0; } |
Werbeanzeige