Game Development Community

dev|Pro Game Development Curriculum

Some C++/TS Tools

by Robert Fritzen · 12/15/2013 (4:13 pm) · 2 comments

So as many have (or haven't) noticed, I haven't been around here lately much. That on par is due to my work and also due to me starting to encounter more and more issues with the engine than I originally thought I'd find and it has actually turned from me being a few days from project completion to weeks trying to solve a engine problem only to find out that there isn't a relevant solution without me needing to gut and re-do a really huge portion of the code, basically putting it, some of the problems have just gotten too large for me to actually work through.

So, instead of trying to plow through a seemingly endless line of problems, Instead I've decided to start working on my own in-house tech solution. Sure it will take a "ton" of time for me to actually complete, but at least I'll know exactly how everything works and how to fix something that's not working. But alas, that's a discussion for another time.

Instead, since I'm slowly drifting away here, I thought I'd bring out a few of my C++/TS "gems" that may or may not provide some useful insight to help other people out with their own projects.

The first of these is a function called getNumeralValue:
S32 getNumeralValue(String in, bool reportError) {
	bool validNumeral = true;
	String charOne = "", charPrev;
	S32 cValue = 0;
	//Check for the numerals
	for(S32 i = 0; i < in.length(); i++) {
		charPrev = charOne;
		charOne = in.substr(i, 1);
		if(charOne.compare("I") != 0 && charOne.compare("V") != 0 && charOne.compare("X") != 0) {
		   validNumeral = false;
			break;
		}
		//Get the index.
		if(charOne.compare("X") == 0) {
		   cValue += 10;
			if(charPrev.compare("I") == 0) {
			   cValue -= 2; //Essentially the I is being read twice, so subduct 2.
			}
		}
		if(charOne.compare("V") == 0) {
		   cValue += 5;
			if(charPrev.compare("I") == 0) {
			   cValue -= 2; //Essentially the I is being read twice, so subduct 2.
			}
		}
		if(charOne.compare("I") == 0) {
         cValue += 1;
		}
	}

	if(!validNumeral) {
		if(reportError) {
	   	Con::errorf("getNumeralValue() - Invalid numeral sent to parser (%s)", in.c_str());
		}
	   return -1;
	}
	else {
	   return cValue;
	}
}
This little function basically converts Roman Numeral Values containing I, V, and X, into their standard integer counterparts. This one is very useful for games trying to do "challenges" with multiple tiers as you can sort them out by the similar numerals.

And the other one I'm giving away today is GenerateNRNumberString:
String generateNRNumberString(S32 min, S32 max, S32 amount, String tokenStr) {
	if(((max - min)+1) < amount) {
		Con::errorf("generateNRNumberString: Cannot generate NR number string with given max/min/amount parameters");
		return "";
	}
   String out;
	//Generate the number list.
   MRandomDeck<S32> mVals;
	for(S32 i = min; i <= max; i++) {
	   mVals.addToPile(i);
	}
	mVals.shuffle();

	//Pull 'amount' values from the list.
	S32 currentItem;
	for(S32 i = 0; i < amount; i++) {
		mVals.draw(&currentItem);
		out = addS32Data(out, currentItem);
		if(i != amount-1) {
		   out += tokenStr;
		}
	}

	return out;
}
This one generates a string of numbers of length amount, separated by tokenStr with values between min and max. This one has it's uses in a variety of places as I have found out.

To use this function, you'll need a little asset piece I have called addS32Data:
String addS32Data(String in, S32 val) {
	char NumData[32];

	dSprintf(NumData, sizeof(NumData), "%i", val);
	String out = in;
	out += NumData;

	dFree(NumData);
	return out;
}

This is a little shortcut function to skip needing to sprintf every time you need to add a number value to a "String".

Anyways, I hope these functions can find a nice home in someone's project. You're welcome to use these functions in any way you please!

#1
12/16/2013 (3:39 am)
" there isn't a relevant solution without me needing to gut and re-do a really huge portion of the code"

This is the path to the dark side.....
*chuckle*

#2
10/10/2016 (4:16 pm)
In-house solution? Sounds like a custom engine, Im interested :)