Archive

Archive for the ‘Programming’ Category

Adventures in WPF

August 5th, 2009 Comments off

I’m very new to Microsoft’s Windows Presentation Foundation (WPF), even though the technology has been out for a few years now (since .NET Framework and Visual Studio 2008). As with many new programming paradigms, I tend not to put in the hard yards in learning them until (1) they appear bedded down and stable and (2) I see a real use for them. The latter is usually the big decider – I can’t learn everything, particularly as my aging brain finds it harder and harder to cram in new stuff.

Anyway, I did do some work in WPF earlier this year, in building a digital signage system for the National Sports Museum. Basically a display of current world records, it needed to be attractive, have interesting transitions, and be very easy to update.

In years gone by I would certainly have built this kind of thing in Flash, possibly under the control of a C# shell program. But it seemed like a good opportunity to get my feet wet with WPF, and so I gave it a go. It was a struggle, but with the aid of a good textbook (Programming WPF by Chris Sells and Ian Griffiths) I managed to achieve a pretty good result, I think. By the way, I do find that when learning something new it’s far more helpful to have a good, hardcopy, text book by my side than any amount of electronic help.

So over the weekend I got the impulse to do some more with WPF, the trigger being some astronomy articles I was reading.

A long time ago I wrote a program called Gravitorium, still available as shareware (though hardly anyone ever buys it now). The purpose was to act as a simple simulator of gravitational interactions between arbitrary stars, planets, asterioids, etc. Build your own solar system and see what happens. Throw a black hole through the middle of our own solar system. That sort of thing.

The old version of Gravitorium

Now here’s the thing: Gravitorium was written nearly 10 years ago, in Visual Basic 6. Calculating all the interactions in such a system is very slow, and updating the display is even slower. Running on the kind of computers and graphics cards we had 10 years ago, it’s surprising that I was able to get any kind of reasonable result at all. Watching a simulation the motion of the Moon around the Earth, for example, was a bit like watching grass grow, unless you cranked down the accuracy of the simulation a fair bit.

So for some time I’ve been toying with revisiting the program, using modern programming techniques and a much faster computer platform. And I figured that WPF, which gives direct access to the graphics processor, would be just the shot to give a much speedier result.

Well, so far it hasn’t worked out like that. Now do bear in mind that in what follows I’m just going to be exposing my experimentation and learning processes to you. If you already thoroughly understand WPF, this article is not for you!

I can see that there are going to be major benefits from a WPF approach. For example, in the old Gravitorium I was doing some pretty dumb things to draw planets and orbits. Everything happened on a PictureBox control. To create the illusion of a moving object I first drew a colored circle and then on the next phase, ‘erased’ it by drawing a black rectangle over it. All the objects and all of orbital trails were just bitmapped onto a single layer. The orbit trails were just literally painted dots, and each time the display is rescaled, I had to keep track of the dots and repaint them.

In my new version, I’m playing with using a WPF Canvas object, and associating Ellipse, Label and Polyline objects with each body in the system. These get moved around as the calculation proceeds, and WPF takes care of redrawing the display. The Polyline object redraws the orbital trails (admittedly as a series of straight lines rather than curves, but the segments are so small they look smooth).

It’s kind of working, but I’m not there yet! Refreshing the display regularly is a bit of a struggle, which I don’t yet understand. In the old Windows Forms days, I would just repaint things in a loop, invalidating the picture box or doing an Application.DoEvents() call. You can’t do that in WPF, which seems odd. I found a couple of ways around this limitation. The one that works best so far is a ‘fake’ DoEvents() procedure which I found here. I modified it by adding the null object check, which avoids an exception on closing the application.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System.Threading;
using System.Windows.Threading;
//....
 
/// <summary>
/// Processes all messages currently in the message queue.
/// </summary>
/// <remarks>
/// This method can potentially cause code re-entrancy problem, so use it with great care.
/// </remarks>  
public static void DoEvents()
{
     if (Application.Current !=null)
    {
       Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { }));
     }
}

I do suspect that having to use a crutch like the above means that I don’t have my head around WPF as yet.

Anyway, the revisited application (which at present is only a proof of concept) is coming along:

Gravitorium WPF POC

This is working fast enough to be able to animate the labels along with the objects (the VB6 version has to pause to show labels) and still get a pretty swift animation. At an equivalent level of accuracy the VB6 version takes 95 seconds to complete an orbit of the Moon. The WPF one, even with the overhead of redrawing labels, only takes 25 seconds. That’s about a four-fold increase in speed, and the results look much nicer.

Anyway, this is a WPF Work in Progress, so I’ll return to it as I continue my learning process.

Categories: Programming Tags: ,

Bracket Creep

July 5th, 2009 Comments off

Well, last week I was back doing some Flash programming, creating an on-line game for one of our corporate clients.

I must say that Flash’s Action Script 3 is a much better way to do this kind of work than the old days of AS2 (yeah, I know that is a long while ago now, but I’m old and I’ve been working in Flash since Flash 4, so I still think of AS3 as new).

But I do find the problem with moving between all of these C-like languages is keeping them all straight in my head, they are all so similar these days. So similar that it’s really easy to forget the differences until you are brought up short by something going wrong. With luck, this will occur at compile time. If you’re not so lucky, it will happen at run time, in certain circumstances only.

PHP code
The languages I’m thinking of are:

  • C#
  • Action Script 3
  • Javascript
  • PHP
  • Objective C

I program in these languages pretty much all the time (though mostly in C#, and with Objective C very much the newcomer to my experience).

You could also add:

  • Java
  • …and probably several other languages

They all have essentially the same structures – a switch statement, a for statement or an if-then-else is pretty much identical in them all. It’s as though the whole world had converted to speaking English – but with various regional dialects. And you have to keep straight in your head which country you are in at the moment so you get the dialect right.

So changing from C# back to AS3 requires a fair bit of concentration.

Where they don’t compare, though, is in their respective IDEs (Integrated Development Environments). I should probably be using Flex, rather than Flash, and the Eclipse IDE, which isn’t bad, as the tools inside Flash itself are absolute rubbish. I actually find myself using Dreamweaver as a better AS3 code editor than what is available inside Flash CS3.

The Objective-C IDE on the Mac (XCode) is pretty good, in comparison, but still not a patch on Microsoft’s Visual Studio, in my humble opinion.

Microsoft do a lot of things very poorly, but developer tools they still do extremely well. If only they would stop fiddling with C# and not keep issuing new versions of it, each new version requiring a yet bigger .NET Framework to be downloaded by my end-users. I have to say that I’m yet to spend much coding time using the additional features in C# version 3, such as LINQ, let alone the new features which are planned for C# version 4, out real soon now. Most of the time I still target the .NET Framework 2, which is nice and small. Well, 20 MB is small by today’s standards.

Categories: Programming Tags: , ,

Programming Journal

June 29th, 2009 Comments off

I thought I might try to start writing about what coding I’m doing, without going in to a lot of detail, but just to record my day-to-day struggles with this programming business.

Updating Chapter Master

At the moment, my main challenge is trying to get out another version of my shareware software Chapter Master.

New Chapter Master

It’s going on for a year since the last release, and at that stage the software, while working well for most people, was still in my mind a little experimental. Certainly time to consolidate what I have learned since the last release, add some requested features, and some ideas of my own.

The purpose of the software is to create or manipulate ‘chapter stops’ and optionally ‘chapter images’ in AAC files (*.m4b or *.mp4 extension). These are the files treated as audiobooks or ‘enhanced podcasts’ by Apple’s iTunes and the iPod. Chapter stops create points within the file to which the user can easily navigate. Ideally, of course, one would position these chapter stops at the exact times in the audiobook where the narrator says “Chapter Five” or “Book Three” or “Part Two”. The software will let you do that manually, but no way can I imagine what artificial intelligence would be required for the software to automatically find such points, though it has been requested.

My companion software MarkAble will allow you to merge together a bunch of source files and insert chapter stops at the beginning of each new file. Sometimes the source files are such that these points are at the logical breaks in the book. Chapter Master lets you edit these existing chapter stops to your heart’s content.

Most of my programming struggles over the last week, though, have been to do with trying to pull out – or create – the ‘cover art’ which iTunes can associate with any audio file.

There are two types of image which can be embedded in an AAC file which has been through, or is going to be used by, iTunes and the iPod. The first is the above-mentioned ‘cover art’, which is what shows up in the cover flow mode of iTunes, or in its grid view. The second type of image is embedded along with the audio data. These images can change at different times as the audio is played, and are what gives ‘enhanced podcasts’ some of their charm.

Chapter Master originally only handled the latter type of image, and some users found it puzzling that the ‘cover art’ didn’t turn up in Chapter Master. So most of my efforts over the last week or so have been to alter the program so that it can manipulate either kind of image. In the case of cover art, this meant that I had to parse and unpack the iTunes meta data, which includes the cover art images.

The AAC file structure is based on the structure Apple originally developed for QuickTime, and consists of ‘atoms’ which can be nested (that is, atoms can contain other atoms). Each atom starts with a data length (a 32-bit unsigned integer) and an atom type indicator which can be read as a 4-character ASCII string.

The iTunes meta data is stored in a ‘user data atom’, type-string ‘udta’. Now this type of atom can contain anything the encoder software desires – which means one has to be careful to check that it contains data structured in the iTunes manner. Apple structure their udta atom with yet another atom hierarchy containing the meta data and any images.

I managed to decode all this without too much trouble if the meta data atom already existed. But I also wanted to allow people to add a new cover art image in Chapter Master, and fill in any meta data, if it didn’t already exist. This meant that I had to create my own ‘fake’ meta data atom and trick iTunes into recognizing it. This proved considerably trickier than I had anticipated, but I think I have managed it. It took a lot of careful working with a hexadecimal editor (I use the excellent Hex Workshop), lots of trial and error, and lots of thinking. Anyway, it looks like I got there.

Books in iTunes

One trick I will pass on to others is that it seems that iTunes expects the udta meta data atom to be the LAST atom inside the ‘moov’ (or ‘movie’) atom. If it’s not in that location, it’s ignored.

Other features I’ve added include the ability to add titles to an image, the ability to do sophisticated search/replace on chapter text, and multi-selection ability in the chapter list.

I’ll be releasing the new version of Chapter Master (1.2.0) as a beta for a while, as so much has changed.

Little-Ease

May 31st, 2009 Comments off

Here one can neither stand nor lie nor sit…
– T.S.Elliot, The Wasteland

Mediaeval dungeons often had a cell called the “little-ease”, devilishly designed so that the prisoner was unable to find any comfortable position in which to rest.

I feel like I’m in such a place now. I’ve “done my back” – the penalty of years of sitting badly while I program, I fear. I’ve damaged something in my lower back and for the last four weeks I’ve been in some pain and often find myself in a situation where I can “neither stand nor lie nor sit”. Which makes it hard to write software or to blog, or … well, do most things. It’s a nuisance.

Anyway, my physiotherapist and doctor are both working on the problem for me. The key, it seems, is to keep moving at all costs. And regularly do targeted exercises to build up both abdominal and back muscles.

The moral of this lesson to younger programmers (I’m nearly 58) is – be very careful how you sit at the computer.

If you slouch and don’t pay attention to how straight your back is, you may not suffer now, but in years to come you may well be as broken as I am. Get a good chair and sit properly. When your teachers told you to “sit up straight” they had your best interests at heart!

Categories: Personal, Programming Tags: ,
Performance Optimization WordPress Plugins by W3 EDGE