Intermediate C++ Game Programming Tutorial 3

From Chilipedia
Jump to: navigation, search

In this tutorial we start from the barest bone starting point. Using only two simple functions: _putch() to output a character onto the console and _getch() to read a key from the keyboard, we build up a concept and theory of string storage, basic text input and output, and parsing operations. It might seem like reinventing the wheel, but you know what they say. The squeaky wheel gets() the cheese. Seriously though, this shit is gonna give us a solid foundation of low-level understanding on which we can later build up a fortress of high-level C++ awesomeness.

IMPORTANT NOTE

In a recent update to Visual Studio 2017 Microsoft has (in their infinite wisdom) introduced a fucking annoying-ass bug (or maybe bugs) into the conio library. It basically fucks with the console input bullshit that we develop in this tutorial and I hate it. Long story short it jams null characters between every real character you read (but only in Debug configuration).

As a workaround you can do some bullshit where you ignore every other character or something like that. Or you can use Visual Studio 2015; it works fine. It also seems to work fine when you build in Release configuration. Hopefully these fuckers fix this.

For more details:

Topics Covered

  • Console I/O with <conio.h>
  • ASCII encoding
  • Single quotes char literal '#'
  • Null-terminated character arrays (c-strings)
  • Double quotes c-string literal "#"
  • Difference between string in local array vs. char* to string in read-only memory
  • String buffers and buffer overflow prevention
  • Parsing a string to convert to an integral value

Video Timestamp Index

Tutorial 3

Notes

This stuff that we're doing here with the strings, don't get the wrong idea. This is not the way forward. We're doing this stuff right now because it makes sense to learn about low-level string bullshit at this point and it gives us a chance to practice out newly-acquired pointer skills. However, using c-strings widely in your code is a terrible idea. They are inconvenient and extremely error-prone. Not sexy at all.

Once we get our practice in with basic pointer ops, strings, file I/O, and memory management, we will be ditching that caveman shit and upgrading to C++ sexy shits. That means we will be ditching c-strings for std::string sometime in the nearish future. But that doesn't mean you can skip this stuff. If you wanna be top tier programmer, you should have this background knowledge as well. And sometimes (like when you're interfacing with libraries/APIs for example) you still need to use c-strings, even if only a little.

Homework

Prompt the user to input a positive integer, find that number in the Fibonacci sequence, and then output it to the console. For example, if the user enters the number 9, you should output the 9th Fibonacci number (34) to the console. There's only one catch: you're not allowed to use any library functions except for _putch(), _getch(), and _kbhit(). You can use the functions that we have already developed in this video.

Solution Video

Bonus Assignment

The string-to-integer parsing algorithm shown in the video was written to mimic the concept of positional notation as closely as possible (i.e. the value of '689' is 6 x 100 + 8 x 10 + 9 x 1). However, this naive implementation has room for improvement. Specifically, a small adjustment can enable us to convert the number without first scanning right to the one's digit. If you want a bonus challenge, try to make this optimization on your own. Expand the section below for a possible solution.

int str2int( const char* s )
{
	int sum = 0;
	for( ; *s >= '0' && *s <= '9'; s++ )
	{
		// shift entire sum left by one decimal place
		sum *= 10;
		// add in new lowest digit
		sum += *s - '0';
	}
	return sum;
}

Source Code

GitHub Repo

See also