I don't have any ideas to program

Started by
10 comments, last by skulldrudgery 17 years, 6 months ago
Quote:Original post by skulldrudgery
I DO recommend using ben's tutorial. I also recommend this one. It is not as sexy as benryves', but it goes over important things. They both helped me a lot when I wanted to do character mode games. I DO NOT like the tutorial you posted. Mainly because it is dated, uses horrid programming style, uses a 2D array of CHAR_INFOs instead of the proper 1D array. I'm pretty sure you can do it entirely (and much more quickly) without using any arrays at all, because the windows console supports multiple buffers. It also uses a fairly stupid method for writing a string to certain coordinates when there are much, much better ways.

A quick note: If you want your game to be played fullscreen, then you must leave the dimensions at the defualt of 80x25 OR you can change it 80x50.

If you want to see an old absolutely horrible text based pong clone from my glory days of n00b-dom, here is the link. It was my first version of text pong before I ever learned anything about the Win32 console (except the gotoxy function which I knicked from another webpage). And I am quite embarrassed and ashamed by it. It compiled fine in Dev-C++, and it flickers like hell.



I noticed you said you had problems with flickering using the console for your pong game, I had a similar problem when I created a memory game using the console. The way I overcame the problem was to create a CHAR_INFO array the same size as the screen and then draw everything to this before displaying it to the screen. It seems to work. Did you ever rectify the flickering in the console games you created and how did you do so? I would be interested in reading about any methods that people have used to overcome this.

You also mentioned something about windows console supporting multiple buffers -What do you mean by this and how would it be used in a game? Examples would be great.

How about them apples?
Advertisement
Yes, I was able to work around the flickering, the same way that you did: Writing to a CHAR_INFO buffer and then outputting the whole thing all at once. But I found that it is easier to just get Windows to create a second screen buffer. I use the second one to draw to and then copy it to the front buffer.

Windows automatically creates a screen buffer for the console. You can get a handle to that buffer using CreateFile().

HANDLE hConsoleFrontBuffer = CreateFile(    "CONOUT$",    GENERIC_READ | GENERIC_WRITE,    FILE_SHARE_READ | FILE_SHARE_WRITE,    NULL,    OPEN_EXISTING,    0,    NULL );

Then create a second buffer.
HANDLE hConsoleBackBuffer = CreateConsoleScreenBuffer(    GENERIC_READ | GENERIC_WRITE,    FILE_SHARE_READ | FILE_SHARE_WRITE,    NULL,    CONSOLE_TEXTMODE_BUFFER,    NULL );

You do all your drawing to the backbuffer. It is convenient because you can use API functions to write to the buffer and you don't have to worry about bounds checking or memory management, for example if you wanted to resize the buffer at anytime, you could just use the appropriate API function.

Then you copy the backbuffer into the front buffer.
CHAR_INFO *tempbuffer = new CHAR_INFO[width * height];COORD size = {width, height};COORD pos = {0, 0};SMALL_RECT read = {pos.x, pos.y, size.x - 1, size.y - 1};SMALL_RECT write = read; ReadConsoleOutput(hConsoleBackBuffer, tempbuffer, size, pos, read);WriteConsoleOutput(hConsoleFrontBuffer, tempbuffer, size, pos, write); delete[] tempbuffer;


But you can also use multiple buffers in another way. You draw to an offscreen buffer, then instead of copying it to the front buffer, you use SetConsoleActiveScreenBuffer(), and the contents of the buffer will be immediately displayed. It is faster, but it causes the title bar of the console window to flicker.
skulldrudgery--A tricky bit of toil

This topic is closed to new replies.

Advertisement