Monday, August 03, 2009

Fixing Bad Text Encoding

I accidentally imported a UTF-8 text file into Excel as an ANSI file, then proceeded to do a lot of work on the Excel file. On closer inspection, a lot of characters were messed up. For example, apostrophe had became ’ and a lot of foreign characters were wrong. But I didn't want to re-import the file and redo the work. So, here's what I did:

1) I saved the Excel file as a text file. (Save As, Other Formats, *.csv) This saved the file as a UTF-8 text file.
2) I opened the CSV in Notepad, then saved it as "ANSI" text file. (File, Save As, Select ANSI in the combo box.) This saved the file as simple ASCII text file.
3) I then opened the text file in Notepad, but this time as UTF-8. (File, Open, Select UTF-8 in the combo box.) This opened the ASCII file as if it were UTF-8, which converted the strange two and three character runs back into the single character. I saved this file as UTF-8 with .CSV extension, then opened it in Excel.

Whew! I really didn't want to redo all that work.

Thursday, April 26, 2007

XNA: GraphicsDevice.DeviceLost Doesn't Fire

What is device loss?
Device loss occurs when Windows takes the graphics device from an application, preventing the application from rendering. The easiest way to trigger device loss is to lock your work station. To render the locked workstation screen, Windows will steal the graphics device. When the application gets the device back it must recreate the graphics content that is no longer valid.

GraphicsDevice.DeviceLost Event
This event fires when GraphicsDevice.Present() is called on a lost device. In most cases, including the "Game" class in XNA, this event will never fire because the render loop simply won't call Present()if the device is lost. The render loop might look something like this:

if (device.GraphicsDeviceStatus == GraphicDeviceStatus.Lost)
{
device.Reset();
}
if (device.GraphicsDeviceStatus == GraphicDeviceStatus.Normal)
{

device.Present();
}

I suppose the device could be lost between checking the GraphicsDeviceStatus and calling Present() then DeviceLost would fire, but that is generally unlikely.

Labels:

Wednesday, January 24, 2007

Tip: Vector2.Normalize()

Vector2.Normalize() will fail silently if both members X and Y are zero. When it fails, the method will set X and Y to NaN (which stands for Not a Number.) Most operations using NaN will yield NaN so pretty soon all of your floating point values will become NaN!

When using Normalize() use the following pattern:

Vector2 delta = new
Vector();
if (delta
!=
Vector2.Zero)
{
delta.Normalize();
}


Or:


Vector2 delta = new
Vector();
if
(delta.LengthSquared > 0)

{

delta.Normalize();

}

You may be tempted to use the following because it is most "mathematically" correct:

Vector2 delta = new Vector();
if (delta.Length > 0)
{
delta.Normalize();
}

But don't do it! The Length property causes a square root operation which is really expensive.

Labels:

New Goal: XNA Hints and Tips

From now on I'm going to post XNA hints and tips as I come across them. The XNA platform enables garage developers to create games for Windows and Xbox360 for free. You can find more information here: http://msdn.microsoft.com/directx/XNA/default.aspx

Labels:

Tuesday, October 18, 2005

Stupid Windows Tricks

Pop Quiz
You have a directory with the following files:

monkey.stu
donkey.stupid


At a DOS prompt you type:

dir /b *.stu

What does the DOS prompt display? (Answer below:)

monkey.stu
donkey.stupid


What? Why does the file called "donkey.stupid" match the wildcard string "*.stu"? It should match "*.stu*" but not "*.stu"! The reason is that in Windows all files have equivalent 8.3 filenames. It so happens that the 8.3 filename of "donkey.stupid" is "donkey.stu" so it matches. Just be glad that you didn't run "del *.stu" expecting to delete all the .stu files but not expecting to delete all of the .stupid files!

Sunday, August 28, 2005

Vanilla vs. Vanilla Extract

My life has been sanitized by writing. More specifically, I've been wiped clean by editing.

The other day, I had a chance to try some real vanilla -- not vanilla extract but the actual vanilla plant. It has a complex, strong flavor that must be served with something else to dilute it. I was surprised and dissappointed by the mushy yet distinctive taste. Where was the cleanness of vanilla extract, that dependable essence of cookie?

Taking something real and extracting only the essential qualities leads to a powerful, pure, predictable experience. This is writing and editing. But do it too much and you end up with Vanilla Coke, Dreyer's Vanilla Bean Ice Cream, and even ForPlay Vanilla flavored lubricant -- all different, but all with the same underlying familiar essence.

Expecting the extract can blind you and set your expectations wrong for what is real. Mom's home cooked meal may be too spicy or too bland. It may be lacking that theme, that potent plot, but it's mom's. It's real.

Wednesday, May 04, 2005

Play Halo 2 On PC (sort of)!

Halo 2 on XBox Live is great, competitive fun. But I've run into some problems recently:
1) My cable modem is now in a different room than my TV.
2) My TV, while nice, does not support HD and therefore doesn't support 480p.
3) The XBox controller sucks for Halo/Halo 2.

Let me expand on that final point -- with the standard XBox controller, your right thumb is forced to: Reload weapons, change weapons, melee, jump, zoome, and look. Meanwhile, your middle finger, ring finger, and pinky on either hand are doing absolutely nothing! It's about as efficient as typing with only your thumb and forefinger!

I've found a solution to all of my problems thanks to two pieces of hardware:
1) SmartJoy Frag -- Lets you use mouse and keyboard to control XBox games. With mouse and keyboard, I can finally use every finger on my left hand and use 3/5 fingers on my right hand! Additionally, the mouse allows finer control -- I can reasonably "look" using a sensitivity of 9! Sensitivity 9 is impossible to use using a standard XBox controller.

2) VDigi -- Lets you use a computer monitor as though it were an HDTV. This solves my "HDTVs are expensive" problem. Also, since my monitor is sitting on my desk, I have a place to rest the mouse and keyboard! And it lets me put my XBox closer to my cable modem.

While I love the new setup, I have to admit some minor faults:
1) Using the mouse to fire an SMG or drive a vehicle is very hard. The SMG kicks upwards as you fire -- with an XBox controller, you simply hold down the right stick. With the mouse, you have to keep lifting it and pulling it down. It makes your arm tired and throws off your aim.
2) Halo 2's resolution is very low. That's OK on a TV 10 feet away, but it looks kind of bad on a monitor 2 feet from your face.

I love PC FPSs and I love Halo 2's matchmaking. Now I have the best of both worlds... sort of.