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
Quellcode |
|
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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
/* * GUIMgr.cpp * * Created on: 10.01.2012 * Author: Simon */ #include "GUIMgr.hpp" #include <windows.h> #include <iostream> #include <stdio.h> #include <fstream> GUIMgr::GUIMgr( int maxLayers ) : _maxLayers( maxLayers ), _width( 80 ), _height( 25 ), _outputHandle( GetStdHandle( STD_OUTPUT_HANDLE ) ) { _currentX = _currentY = 0; // allocate memory for the chararray and colorarray of each layer // dimension 1 _chars = new char**[_width]; _fgColors = new Color**[_width]; _bgColors = new Color**[_width]; // dimension 2 for( unsigned int x = 0; x < _width; x++ ) { _chars[x] = new char*[_height]; _fgColors[x] = new Color*[_height]; _bgColors[x] = new Color*[_height]; } // dimension 3 for( unsigned int x = 0; x < _width; x++ ) { for( unsigned int y = 0; y < _height; y++ ) { _chars[x][y] = new char[maxLayers]; _fgColors[x][y] = new Color[maxLayers]; _bgColors[x][y] = new Color[maxLayers]; } } // initialize for( unsigned int x = 0; x < _width; ++x ) for( unsigned int y = 0; y < _height; ++y ) for( int z = 0; z < _maxLayers; ++z ) { // if a character is zero, it won't me print to the console _chars[x][y][z] = 0; _fgColors[x][y][z] = FG_WHITE; _bgColors[x][y][z] = BG_BLACK; } } GUIMgr::~GUIMgr() { // free dimension 3 for( unsigned int x = 0; x < _width; x++ ) { for( unsigned int y = 0; y < _height; y++ ) { delete[] _chars[x][y]; delete[] _fgColors[x][y]; delete[] _bgColors[x][y]; } } // free dimension 2 for( unsigned int x = 0; x < _width; x++ ) { delete[] _chars[x]; delete[] _fgColors[x]; delete[] _bgColors[x]; } // free dimension 1 delete[] _chars; delete[] _fgColors; delete[] _bgColors; } // prints a .txt file on the specific layer and stores the characters // of the .txt file in the layer's char array void GUIMgr::printFile( const std::string & txtFilePath, int layer ) { std::string line; std::ifstream myfile( txtFilePath ); _currentX = 0; _currentY = 0; if( myfile.is_open() ) { while( myfile.good() ) { getline( myfile, line ); unsigned int tmp = _currentY; printString( line, _currentX, _currentY, layer ); if( tmp == _currentY ) { // no line break because input line was to short (<80 characters) // make the breakline "per hand" _currentY++; } _currentX = 0; } myfile.close(); } } void GUIMgr::printString( const std::string & text, int x, int y, int layer, Color foreground, Color background ) { if( x >= 80 || x < -1 || y >= 25 || y < -1 ) return; if( x != -1 && y != -1 ) { // coordinates specified -> set new cursor position _currentX = x; _currentY = y; } _COORD c; c.X = _currentX; c.Y = _currentY; SetConsoleCursorPosition( _outputHandle, c ); SetConsoleTextAttribute( _outputHandle, foreground | background ); // print the text char by char size_t pos = 0; while( pos < text.size() ) { const char c = text.at( pos ); _chars[_currentX][_currentY][layer] = c; _fgColors[_currentX][_currentY][layer] = foreground; _bgColors[_currentX][_currentY][layer] = background; std::cout << c; _currentX++; if( _currentX >= _width ) { // string is longer then console width -> stop printing the string _currentX = 0; // go to the next line _currentY++; if( _currentY >= _height ) { // if we reach the end of the console -> return to line zero _currentY = 0; _COORD coord; coord.X = _currentX; coord.Y = _currentY; SetConsoleCursorPosition( _outputHandle, coord ); } break; } ++pos; } } // resets a layer to its default values void GUIMgr::clearLayer( int layer ) { if( layer < 0 || layer >= _maxLayers ) return; for( unsigned int x = 0; x < _width; ++x ) for( unsigned int y = 0; y < _height; ++y ) { _chars[x][y][layer] = 0; _fgColors[x][y][layer] = FG_WHITE; _bgColors[x][y][layer] = BG_BLACK; } } // reprints all characters except the specified layer's characters void GUIMgr::hideLayer( int layer ) { if( layer < 0 || layer >= _maxLayers ) return; _COORD c; c.X = 0; c.Y = 0; // clear the console clear(); // reprint all except the specified layer for( unsigned int x = 0; x < _width; ++x ) for( unsigned int y = 0; y < _height; ++y ) for( int z = 0; z < _maxLayers; ++z ) { if( z == layer || _chars[x][y][z] == 0 ) continue; c.X = x; c.Y = y; SetConsoleCursorPosition( _outputHandle, c ); SetConsoleTextAttribute( _outputHandle, _fgColors[x][y][z] | _bgColors[x][y][z] ); std::cout << _chars[x][y][z]; } } void GUIMgr::printAll() { _COORD c; c.X = 0; c.Y = 0; for( unsigned int x = 0; x < _width; ++x ) for( unsigned int y = 0; y < _height; ++y ) for( int z = 0; z < _maxLayers; ++z ) { // ingore "ZERO" character if( _chars[x][y][z] == 0 ) continue; c.X = x; c.Y = y; SetConsoleCursorPosition( _outputHandle, c ); SetConsoleTextAttribute( _outputHandle, _fgColors[x][y][z] | _bgColors[x][y][z] ); std::cout << _chars[x][y][z]; } } // clears the entire console but doesn't clear the chars and colors stored // in each layer's char/color array void GUIMgr::clear() { _COORD c; c.X = 0; c.Y = 0; SetConsoleTextAttribute( _outputHandle, FG_BLACK | BG_BLACK ); SetConsoleCursorPosition( _outputHandle, c ); for( unsigned int x = 0; x < _width; ++x ) for( unsigned int y = 0; y < _height; ++y ) std::cout << ' '; } |
Community-Fossil
Ich habe für mein Konsolenprojekt einen kleinen "GUIMgr" geschrieben (unfertig), der mehrere layers an characters abspeichern kann und pro character eine farbe für den text und hintergrund mitspeichert.
dafür hab ich ein dynamisches 3dimensionales array verwendet, bei dem ich mir jedoch nicht 100% sicher bin, ob ich es richtig lösche (also es funktioniert alles und wirft keine fehler, aber das muss ja nichts bedeuten).
Zitat
Wieso hat jedes Zeichen eine Vorder- und eine Hintergrundfarbe?
Zitat
Ich würde auch viel eher eine Klasse für ein Zeichen machen, dass dann Zeichen, Vordergrundfarbe und Hintergrundfarbe speichert
Zitat
Wenn du schon weißt, was new und delete macht, aber trotzdem nicht beurteilen kannst, was dein Programm hier eigentlich macht,
Zitat
Was spricht gegen einen einfachen std::vector?
Du musst dir nur eine eigene at-Methode schreiben, die die Position im Vector berechnet und schon hast du den ganzen Stress mit dem Freigeben und Reservieren nicht.
Community-Fossil
Werbeanzeige