Wednesday, January 18, 2012

WebROWS: An Old Friend Reborn

"Luke, you will find that many of the truths we cling to depend greatly on our own point of view." —Ben Kenobi

Once upon a time, back in the last century, I was working with a programming language called C and attempting to learn a little bit about making computer graphics. The first little graphics program I wrote was a three-layer false parallax star field. I revisited that idea a couple of months ago when learning Javascript and the HTML canvas API. After doing a few small exercises to get some familiarity with the language and API, it was time to do something a bit more ambitious. I decided to try my hand at recreating something else I did back in the 90s in C: a simplistic Columns knock-off that I called ROWS.

Re-implementing something I had not looked at in a decade was an interesting experience. The browser model is quite different than the old C direct-to-graphics-memory method. In fact, you basically can't do the old way anymore thanks to the evolution of operating systems. Still, re-solving a known problem gives you the advantage of knowing at least one way it will work. That does not mean I did not make any mistakes along the way.

I wrote a few years ago about a defect in one of my programs at work. The solution involved looking at the problem I a new way. I ran into a different kind of issue when implementing the program in the browser. After working through each piece of the application, finding errors along the way and removing them, everything looked like it was working. And then I tested embedding it in this blog. It still worked, but I had made one rather egregious oversight. I had used the cursor keys to control the game.

Perhaps this sounds reasonable to you; I know it did to me. After all, the old version used the cursor keys, why shouldn't the new version. Well, you see, the cursor keys already have defined uses in the browser environment, namely to scroll web pages around. That didn't matter when the game fit on the page with no scrolling needed, but as soon as I put it in a page larger than the browser window those cursor keys moved the page again, shifting the game around with it. All in all a rather unacceptable situation when trying to actually play. Off I went to search for people's solutions to this problem, and I found a few that purported to fix the cursor key behavior. I even tried out a couple, to no avail. But I also found other people saying that using the cursor keys was not the way to behave. The browser, and more importantly the browser's user, already expects those keys to perform a specific function. Breaking it is bad form, they said, perhaps you should consider another way. And of course they were quite correct.

I moved the controls for the game to a different set of keys and the thing I perceived as a problem was revealed for what it was. My program didn't have an issue, I did. In attempting to override the browser's desired behavior I had created difficulty for myself that shouldn't have existed in the first place. And isn't life just like that sometimes? How often do we cause problems for ourselves by projecting our expectations onto others without really stopping to consider them? Admitting you are wrong can be hard, but sometimes it can help too.

Now I am not going to finish this post without putting in the link to the WebROWS page. The link is also over in the sidebar. I hope you enjoy playing around with it, and I would be happy to answer any questions about how it works or how I built it.

No comments: