N
Nik Coughlin
System.Console is really slow. I wanted to make a retro text-only console
application game (a roguelike), but I can't get decent performance out of
it. Does anyone know how to get decent performance out of it? Is there
some way of buffering the text before outputting it? Here's my test code
(slow!):
Random r = new Random();
Console.Clear( );
while ( true ) {
for ( int y = 0; y < 20; y++ ) {
for ( int x = 0; x < 70; x++ ) {
Console.SetCursorPosition( x, y );
Console.ForegroundColor = (ConsoleColor) r.Next( 16 );
Console.Write( 'x' );
}
}
}
The only way I could get it to run at a decent speed was by using
Console.WriteLine on each row, but that means I can't set the color for
individual characters.
As an aside note, I fired up the oldest programming IDE I could find
(QuickBasic 4.5, ah the nostalgia) and tried this:
CLS
DO
FOR y% = 1 TO 20
FOR x% = 1 TO 70
LOCATE y%, x%
COLOR INT(RND * 16)
PRINT "x"
NEXT x%
NEXT y%
LOOP
It was blazingly fast. Surely a modern framework like .Net can do fast
console drawing, I must be missing something.
Please tell me I don't have to use the Win32 API I'm aiming to have it
work in Mono too, so I want to stay in managed code.
application game (a roguelike), but I can't get decent performance out of
it. Does anyone know how to get decent performance out of it? Is there
some way of buffering the text before outputting it? Here's my test code
(slow!):
Random r = new Random();
Console.Clear( );
while ( true ) {
for ( int y = 0; y < 20; y++ ) {
for ( int x = 0; x < 70; x++ ) {
Console.SetCursorPosition( x, y );
Console.ForegroundColor = (ConsoleColor) r.Next( 16 );
Console.Write( 'x' );
}
}
}
The only way I could get it to run at a decent speed was by using
Console.WriteLine on each row, but that means I can't set the color for
individual characters.
As an aside note, I fired up the oldest programming IDE I could find
(QuickBasic 4.5, ah the nostalgia) and tried this:
CLS
DO
FOR y% = 1 TO 20
FOR x% = 1 TO 70
LOCATE y%, x%
COLOR INT(RND * 16)
PRINT "x"
NEXT x%
NEXT y%
LOOP
It was blazingly fast. Surely a modern framework like .Net can do fast
console drawing, I must be missing something.
Please tell me I don't have to use the Win32 API I'm aiming to have it
work in Mono too, so I want to stay in managed code.