Sign in to follow this  
Darklighter

[C#] Console backbuffering and asynchronous input?

Recommended Posts

I'm in the process of hacking together a small game based on Project 1 from the C# Workshop. I thought I'd post this topic here as the issue at hand isn't related to the requirements of the project. Basically, my game uses a combination of System.Console.Write() and System.Console.Clear() for state-of-the-art real-time rendering. On to my problem... Initially, the rendering process was tied to user input - whenever the player pressed a key, the screen would be cleared and the updated graphics would be printed. However, I now have a timer that needs to be displayed every second (and preferably every millisecond), so I can't rely on that anymore. My current implementation involves an event handler which updates the timer and draws the graphics every ¼ seconds. Needless to say, it flickers to death. I have tried dumping the output into a System.IO.Stream and then displaying it as a string, but with minimal improvement. Also, I still need to loop through the characters to toggle colors and alignment. The flickering decreases when I reduce the rendering rate, but at the expense of choppy animation and a less precise timer. Are there any other alternatives for a backbuffer in the console? CartRunner by ApochPiQ seems to handle it well. The timer even displays milliseconds. Another issue is that System.Console.ReadKey() freezes the process while waiting for user input. This means that even though the timer stops, the game won't end until the player presses a key. Essentially, what I need is something similar to GetAsyncKeyState() from the Win32 API. Apart from using DirectInput, is there any other such thing in the .NET Framework?

Share this post


Link to post
Share on other sites
I wrapped up some of the native (Win32) functions for fast console drawing into this monstrosity. It's probably overkill for what you're looking for, but you could probably get away with just nicking the P/Invoke signatures for console access.

As for your second question: you can use the Console.KeyAvailable property to try and avoid the blocking problem. For better control you could use the console's native keyboard/mouse events, which would required further Win32 hackery.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this