• entries
122
121
• views
68497

# Console buffers and learner's permits

239 views

## Cripes! - The Snipes clone

After doing a little more reasearch, I tried using CreateConsoleScreenBuffer() to create a separate buffer for Cripes to draw to. It turns out that separate screen buffers keep separate state, so I don't really need to worry about saving the old state. My Game class constructor handles the set-up of the console window, and here's the source code:

Game::Game()	: hOut(CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL)),	  hIn(GetStdHandle(STD_INPUT_HANDLE)),	  hPrev(GetStdHandle(STD_OUTPUT_HANDLE)),	  world(40, 25){	if (hOut == INVALID_HANDLE_VALUE)		throw std::exception("Unable to create new console buffer.");	else if (hIn == INVALID_HANDLE_VALUE)		throw std::exception("Unable to access console input buffer.");	if (!SetConsoleActiveScreenBuffer(hOut))		throw std::exception("Unable to set the active console buffer.");	SMALL_RECT win_size = {0, 0, 39, 24};	SetConsoleWindowInfo(hOut, true, &win_size);	COORD buff_size = {40, 25};	SetConsoleScreenBufferSize(hOut, buff_size);	SetConsoleTitle("Cripes 2.0 - A Snipes Clone");	CONSOLE_CURSOR_INFO cci;	GetConsoleCursorInfo(hOut, &cci);	cci.bVisible = false;	SetConsoleCursorInfo(hOut, &cci);}

It's actually more simple than I expected. The initializer list sets up the handles and gives our World its dimensions, and the block of if-statements makes sure they're all valid. The next block of code sets the window size, then the buffer size. You actually have to do it in that order here, otherwise we'd set a buffer smaller than the window, which Win32 doesn't like. Then we set the title, and I make the cursor invisible. Take a look:

It actually looks like a normal window filled with a solid blue color. Or it would if the icon didn't scream "CONSOLE!!" I made it that color via a world.FillColor() function I'm using for testing purposes only, so I can see if this stuff is working properly. It is.

## Day-to-day Life

I just got my driver's permit. The written test I took allowed a maximum of eight mistakes. I made seven. At least I passed. I'm just a little nervous about driving for my first time... make that really nervous. :|

~Jonathan

EDIT: I don't know what happened here, but it sure looks interesting!

EDIT 2: Ah, I botched my conversion from 2D coordinates to 1D indices.

## 1 Comment

Hey, that might be really simple to you but I'd be chuffed to get a different coloured pixel on the screen! I'm not going to try and jump ahead like last time though so I'll keep it as a future goal.

## Create an account

Register a new account