Thursday, April 28, 2005

Latest SDK means more updates gah!

So I've installed the latest SDK now and it's kind of fixed my problem. I still believe that C++ has a better interface to the CALCRECT stuff.

API Rant That Probably Won't Mean Much If You Haven't Been Playing With The Same Areas Of The API Recently. Yes Lots Of Captial Letters!

With C++ you pass in a reference to a rectangle. According to the docs on my system the API call will take into account the width of this rectangle (or height) and then tell you what sort of shape your big lump of text will be. Where as in C# you give it the text and it tells you the dimensions of the text as it's formatted. So if you've written a really long sentence - it gives you the width of that really long sentence you cannot (as far as I can tell) tell it measure the text dimensions if the width is constrained :(

Also the syntax has changed in places - so I need to go through the first few tutorials again and update the source code :D I think this teaches me an important lesson about downloading the latest SDKs before updating tutorials!

Some very nice code

Don't you hate it when you have an awful feeling. The feeling you are coding something that has probably been done already by the API but you just can't find it! Especially when you write code that's as inefficent as mine. Still I'm rather proud of it as I programming defensively from the start and once it compiled it worked! First time yay!

  /// <summary>
/// Removes a substring measured in pixels from
/// a string that will that will be rendered in a
/// given font.
/// </summary>
/// <param name="s">The string that will be reduced.
/// It's width in pixels in font f must be greater than
/// the Width parameter!</param>
/// <param name="f">The Font that the text will be rendered in</param>
/// <param name="Width">The width's-worth to be removed</param>
/// <param name="splitWords">Should the program splits lines in the middles of words?</param>
/// <returns>A string reprenting a widths worths removal</returns>
private string CutWidthFromString(ref string s, dxFont f,
float Width, bool splitWords)
#region Pre Conditions
Debug.Assert(Width > 0f,
"The Width to subtrack must be greater than 0!");
Debug.Assert(f != null, "The font object must not be null!");
Debug.Assert(s.Length > 0,
"The length of the string s must be greater than 0!");
Debug.Assert(Width <
f.MeasureString(null, s,
"The pixel-width to subtract must be less than the pixel-width of the string");
int DEBUG_startLength = s.Length;

//Take off a widths worth of substring
//!Super inefficent way!
string substring = "";
float substringWidth = 0f;
int stringIndex = 1;

while(Width > substringWidth)
substring = s.Substring(0,stringIndex);

//I'm guessing this call is expensive.
substringWidth =
f.MeasureString(null, substring,

#region Debug
Debug.Assert(stringIndex <= DEBUG_startLength,
"Substring is attempting to read from outside the parent string");

//Substring is actually bigger now so knock one character off
substring = substring.Remove(substring.Length-1, 1);

int splitPoint = substring.LastIndexOfAny(
new char[3] {' ', '\n', '\t'});

if(!(splitPoint== -1))
substring = substring.Substring(0,splitPoint);

s = s.Remove(0, substring.Length);

#region Post Conditions
Debug.Assert(s.Length < DEBUG_startLength,
"Parent String was not reduced in size!");
Debug.Assert(f.MeasureString(null, substring,
DrawTextFormat.WordBreak, 0).Width
< Width);

return substring;

A word or two about the above function

So for super-fast-release mode we take out the asserts (which shouldn't be a problem as they don't actually do anything but halt the program). Very handy for writing correct code. Also has those cool #region region ... #endregion blocks that can hide away all the debug code.

Also I was looking through some directX code and I saw that param code stuff. (Which, of course, has all been eaten by html argghhh!). Fix soon!

Well it's not just eyecandy will pick if up and when you make a call to that function, as you write it, you'll be prompted with the argument descriptions that you've written! I'm sure this is old hat to a lot of people but I only just found out, it's quite spiffy.

So I continue work on Game GUI and Grammar stuff.

No comments: