Asynchronous Input in Dark GDK

For a while I was puzzled as to how to get input asynchronously in Dark GDK - that is, without interrupting the regular flow of things, mostly drawing functions. It turns out there is a function for that, dbGetInput(). By the way, DarkGDK’s documentation isn’t the greatest - dbGetInput is listed as dbInput$ in the docs.

Anyway, as soon as I used it, I ran into another problem; dbGetInput didn’t support backspace. After a bit of poking around on the forums, it turns out you need to do that manually. Someone on the forums made a little function to do just that, which I modified to support multisync and C++ strings. Here it is:

//Function to get input asychronously; returns true if input is still being fed, false if the input has been sent to the server
//Credit goes here: http://forum.thegamecreators.com/?m=forum_view&t=160546&b=22
bool GetInput(std::string& Buffer, int MaxChars)
{
	std::string NewEntry = dbGetEntry();

	// dbGetEntry returns zero before the first character is typed.
	if (NewEntry.length() > 0)
	{
		// Afterwards, the returned pointer is not zero but the string can be empty.
		if (NewEntry[0] != 0)
		{
			dbClearEntryBuffer();

			// There can be more than one characters in the returned string
			// if the user types fast. Add the characters one by one.
			int NewLength = NewEntry.length();
			for (int i = 0; i < NewLength; i++)
			{
				// Backspace: remove the last character from the string
				// if there is still any left.
				if (NewEntry[i] == 8 && !Buffer.empty())
					Buffer.pop_back();
				// Enter: Send the message to the server if there is a message.
				else if (dbReturnKey())
				{
					if(!Buffer.empty())
					{
						//Send the message to the server so it can distribute the message
						NetPutString("PlayerMessage");
						NetPutString((char*)Buffer.c_str());
						NetSend(0);
					}member
					return false;
				}
				// Otherwise: add the character if the buffer is not full yet.
				else if (Buffer.size() < MaxChars - 1)
					Buffer.push_back(NewEntry[i]);
			}
		}
	}
	return true;
}

Here’s the function in use:

// ============ TYPING HANDLING =============
//If the player presses T, start the typing prompt
if(dbKeyState(DIK_T) || typing)
{
	//The player is now typing, set the typing flag to true
	if(!typing)
	{
		dbClearEntryBuffer();
		chatBuffer.clear();
		typing = true;
	}
	//Get the input asynchrounously
	if(!GetInput(chatBuffer, CHATBUFFER_MAX_SIZE))
		typing = false;
	dbText(0, dbScreenHeight() - dbTextHeight("a"), "> ");
	dbText(dbTextWidth(" > "), dbScreenHeight() - dbTextHeight("a"), (char*)chatBuffer.c_str());
}

Edit: Made the code a little cleaner, using strings more.

Dark GDK

As I was working on my Computer Science final project, I really started hating Dark GDK. It was slow. It was rarely updated. It relied on the directx SDK from 2007. It required a bunch of external modifications to work to any appreciable degree. Eventually, I swore to myself that if I ever continued the shooter, I’d switch to a different graphics API, probably SDL, or maybe Allegro.

Then I actually took a look at the libraries’ documentation.

I was absolutely horrified by the depth of complexity that the two libraries needed. I immediately went back to the comfort of Dark GDK. It turns out that my professor had a reason (as most humans do for the things they do) that he recommended it to me. Sure, it’s slow, and relies on old SDKs and all that. But it is extremely easy for aspiring game programmers to get into. For example, in order to play a sound in SDL or Allegro, from what I could tell (it could be different, I haven’t actually tried the two libraries out), you needed to load some audio drivers, then specify the actual length of the audio stream. It takes about two or three commands to actually load the sound, and then I couldn’t even tell how to play the dang sound without looking up some tutorials. In Dark GDK, though, it takes two lines. One to load the sound from a file, and one to play it. The commands are simple - dbLoadSound(char* soundName, int ID) and dbPlaySound(int ID) - and they use a easy-to-learn number-indexed system to identify sounds.

As I looked around the SDL/Allegro documentations, I grew more and more certain that I could not use them. I may return and look at those libraries in a year or two, when I really need a robust library and deal with that kind of complexity, but for now, I’m sticking with DarkGDK. It has it’s shortcomings, but hey, it’s easy, and I’m a teenager.