September 24th, 2008

Objective fun

This would make an excellent recruitment-test for an Objective-C programmer.
Both of these snippets are from the Apple developer example code collection. One of them is correct, and the other will likely make your application burst into flames*.

Code snippet 1:

- (void)setArguments:(NSDictionary *)arguments
{
    [arguments copy];
    [_arguments release];
    _arguments = arguments;
}

Code snippet 2:

- (void)setURLToLoad:(NSURL *)URL
{
    [URL retain];
    [URLToLoad release];
    URLToLoad = URL;
}

Quiz questions (select text for answer):
Q: Which one is broken? (1p)
A: Snippet 1. It throws away the copy, and instead assigns a non-retained variable.
Q: Why is the other one not broken? (2p)
A: Because retain modifies the object, so assigning the retained object is fine.
Q: Why is the author using copy instead of retain? (5p)
A: Because the object could be a NSMutableDictionary which could unexpectedly change later.
Q: How should they both be written instead so that this will never happen? (20p)
A: Like ‘[_v release];_v = [v retain]’ or ‘[_v release];_v = [v copy]’

Thanks Apple, made my day here (hey, it’s raining).
*) The computer supplies ‘BusError’ and you’ll have to provide the pyrotechnics. I usually go with illustrative hand-waving and onomatopoetic sound effects.

Posted by Roine at September 24th, 2008 15:09 | Permalink

April 14th, 2008

Songkran

Water splashing festivities started late today. I was up at 6am and the festivities didn’t kick off until ten.
I used the same camera this year too, in a plastic bag. This year I didn’t bother making a glass lens cover as I knew it would just fog up. I managed to snap 300 pictures before the camera permanently fogged up at around 4pm. The splashing had gotten more subdued the last hour so I decided to call it a day. I had been out for nine hours in blistering sun, and despite double layer spf30 sunblock I’m now burnt to a crisp. My arms are so red they’re actually blinking.
I thought I got some nice shots, but checking them later I barely got a few ‘OK’ ones. Many were ruined by fogging but I guess most were ruined by me.

Songkran picture Songkran picture Songkran picture Songkran picture

While at it I also added more albums to both 2006 and 2007 which should have been up long time ago.

Posted by Roine at April 14th, 2008 21:04 | Permalink

August 9th, 2007

The Dark is coming to a theater near you

Today I happened to walk past a movie poster with the title The Dark is Rising. Something clicked inside my head, and I had to go up to it to read the poster’s small print. “Story by Susan Cooper”.

No. Way.

There are two books that defined my childhood: The Dark is Rising by Susan Cooper, and Programming the Z80 by Rodnay Zaks.

The Dark is Rising is a fantasy novel (actually a sequence of five shorter books) about a boy, Will Stanton, who is made aware that he is no ordinary boy. The evil forces of the world, The Dark, is rising, and only with the help of the special powers Will will inherit on his eleventh birthday, the forest spirit Herne the Hunter, the power of the six Signs, the six awakened Riders, the Grail coaxed from the Greenwitch in the sea, and the birthright of the strange little boy with the raven eyes can the Old Ones from The Light win the final battle by the Silver tree.

When light from the lost land shall return,
Six Sleepers shall ride, six Signs shall burn,
And where the midsummer tree grows tall
By Pendragon’s sword the Dark shall fall.

The similarities to Lord of the Rings is apparent (each book begins with a prophetic verse, much like in Lord of the Rings) and not so strange; Susan Cooper was a student of JRR Tolkien.

The books have won multiple awards and many, like me, cherish them and re-read them over and over.
I’ve always hoped that someone would make a movie out of Susan Cooper’s books. The movie adaptation of Lord of the Rings shows that it could be done.

And now Cooper’s epic has been turned into a movie. There’s a trailer available.

Watching the trailer, it’s painfully obvious that the production company has completely raped it.

They’ve changed the story to present, changed most of the characters (Will is now American!), and in an effort to appeal “to a new audience” turned it into an action movie (explosions!) that resembles those Hollywood crap magician kid movies. Likely extremely intended.

Why do they keep picking these masterpieces and turning them into manure?

I think I’ll go cry myself to sleep now.

Posted by Roine at August 9th, 2007 21:08 | Permalink

July 22nd, 2007

Elusive Ellipses

Today I received this letter.

Dear Roine,
I’m trying to do ellipses in OpenGL. The ellipses turn out fine, but I can’t seem to position them correctly. All my tries have turned into giant hairballs. Please advice.
Confused

Dear Confused. I know just what you are going through. Just the other day (well, today actually) I needed to do the same thing. I started out with a bunch of glRotatef(..) to position the ellipses with angles computed from the vectors with arcsine, but the cyclic issues and rounding issues with e.g. sin(asin(x)) made the ellipses go bonkers. Google didn’t turn up more than hints, so I had to write this beautiful piece of code myself:

// Draw an ellipse with focus points r1 and r2, with radius r
// 'quad' is a previously allocated GLUquadric object
// The code avoids using inverse trigonometric functions and the cyclic issues with them.
void
ellipse(float r1x, float r1y, float r1z, float r2x, float r2y, float r2z, float r, GLUquadric* quad )
{
  glPushMatrix();

  // As with all OpenGL code, this is done 'backwards', the first transformation being applied last:

  // Finally, create a coordinate system transformation for the ellipse,
  // where the X axis transforms to the desired vector (i.e. r2-r1).
  // 'by' and 'bz' are created by cross products, which is perfectly
  // valid since an ellipse is rotationally symmetric about (in this case)
  // the X-axis and so any orthonormal vectors to 'r2-r1' are valid choices.

  const float d = sqrt((r2x-r1x)*(r2x-r1x)+(r2y-r1y)*(r2y-r1y)+(r2z-r1z)*(r2z-r1z));  // d = |r2-r1|

  const float bx[] = { (r2x-r1x)/d, (r2y-r1y)/d, (r2z-r1z)/d };  // normalized r2-r1

  float by[3];
  const float dx2 = bx[2]*bx[2]+bx[1]*bx[1];          // |[ex x bx]|^2
  // Unfotunately, there is no vector guaranteed to be orthogonal to r2-r1;
  // we have to test for it.
  // If bx happens to be (nearly) parallel to ex then use ey instead to avoid degenerate case.
  if (dx2 > 0.01) {
    // normalized [ex x bx]
    const float dx = sqrt(dx2);                       // |[ex x bx]|
    by[0] = 0;
    by[1] = -bx[2]/dx;
    by[2] = bx[1]/dx;
  } else {
    // normalized [ey x bx]
    const float dy = sqrt(bx[0]*bx[0]+bx[2]*bx[2]);   // |[ey x bx]|
    by[0] = bx[2]/dy;
    by[1] = 0;
    by[2] = -bx[0]/dy;
  }

  // Now make one last orthonormal vector to both bx and by crossing them
  const float bz[] = { bx[1]*by[2]-bx[2]*by[1],
                       bx[2]*by[0]-bx[0]*by[2],
                       bx[0]*by[1]-bx[1]*by[0] };  // [bx x by]

  // This matrix both rotates the ellipse to the desired vector r2-r1
  // and translates it to r1 (last column)
  // 'm' is column major as required by OpenGL.
  const float m[16] = { bx[0], bx[1], bx[2], 0.0,
                        by[0], by[1], by[2], 0.0,
                        bz[0], bz[1], bz[2], 0.0,
                        r1x, r1y, r1z, 1.0};
  glMultMatrixf(m);

  // Reposition ellipse with left focus in origo, and right focus at 'd' on X axis
  glTranslatef(d/2.0,0.0,0.0);

  // Stretch sphere to an ellipse with semimajor radius 'r' and distance 'd' between foci
  glScalef((d+r)/2.0,r,r);    

  // Rotate sphere so the rotationally symmetric axis is in X direction (looks better)
  glRotatef(90,0.0f,1.0f,0.0f);  

  // Create unit sphere
  gluSphere(quad,1.0,12,8);    

  glPopMatrix();
}
Posted by Roine at July 22nd, 2007 10:07 | Permalink

April 21st, 2007

Gecko galore

I have a gecko invasion in my apartment. I counted three the other night. Geckos are tiny lizards that can crawl on walls and ceilings and eat bugz.

One of them are missing it’s tail. They can shed tails if they’re for example coming under heavy avian attack. Or being stepped on late at night in a dark bathroom. I’m so sorry mr Gecko! I expect to wake up one of these days and find one of my legs has been gnawed off during the night.

I love geckos. Not only are they cute, their primary reason of existance is to eat things I hate, like ants and mosquitos.

I hate mosquitos. I’ve spent months of my life hunting and swatting mosquitos that have managed to sneak into my bedroom. I’ve actually become quite apt. I have a fierce backhand, and I can take them out in mid-air. I’m thinking of publishing a how-to book on the topic.

When I get filthy rich I will set aside a specially tagged fund for eradicating mosquitos from this planet (and any other planet should they manage interstellar space travel first. At the rate I’m making money, it’s not a given which will come first. The best-selling how-to book should help here).

As I side bonus I will have cured Malaria. You’re welcome.

Posted by Roine at April 21st, 2007 18:04 | Permalink

April 14th, 2007

Songkran pictures

As promised, new folder (and new year! Ugh, need to take the camera out more) in the photo section.

This Songkran it rained and I managed not to get sunburned, but my entire body aches today.

My camera is now quite possibly toast. I used a wrapping bag at first, but it caused the camera to fog up. I ditched the bag and the camera got completely soaked, but worked! - for thirty minutes. When it dries out, next week or so, I’ll try it out, but if it’s gone I’ll let it rest in peace. It’s an eight year old digicam which has more stuck pixels than a 50 cent webcam.

Then again, that’s exactly what happened on the first Songkran (yes, it’s the same camera) and after it dried out it’s been working fine for five years.

Just for kicks, I’ve also added four more images to the 2006 Songkran pictures.

Songkran picture Songkran picture Songkran picture
Posted by Roine at April 14th, 2007 23:04 | Permalink

April 13th, 2007

Songkran 2007

Songkran is already over (technically the festival is three days, but there is only splashing on day 1). After a slow start it turned out to be the best one so far. It is getting dark now and only the most devoted are still splashing away.

I took almost 300 pictures, but most of them came out hazy because of lens fog (I really need to kill someone to get a 1Ds with enviromental sealings. Killing is required, no?) However I do think I got a few good. Will update with pix tomorrow.

Need. Sleep. Now.

Posted by Roine at April 13th, 2007 17:04 | Permalink

April 12th, 2007

Anticipation

The situation is getting critical now. The anticipation is so tense it’s smothering. Any street market vendor will sell you a gun - they even hang them in the windows. I’ve already seen cars cruising around, people standing in the back, showing off their guns, harassing innocent people. They’re just young punks that can’t wait. People are already avoiding the streets.

I’ve heard fighting will start at midnight tonight, not too far from here. Of course hell won’t be unleashed until sometime later tomorrow. Ordinary people will stay indoors with cars full of hooligans with war paint on their faces cruising around.

I’ve checked my gear. I think I’m good. Technically, I’m a non-combattant, but experience tells me most don’t care, especially girls. These people don’t even care about sides, they’ll just take out anyone who crosses their path. And with a smile on their face.

I’ve actually gotten a small piece for myself. For protection, you know. This time they won’t take me dry.

Posted by Roine at April 12th, 2007 20:04 | Permalink

April 10th, 2007

Tap’d

I donated blood yesterday. A local hospital apparently had a drive, and were present in a mall. Since I used to go regularly I decided to donate. Forgive me doctor, it has been several years since my last donation.

They had a medical form, which was of course in Thai. Since I don’t read Thai they had to read it out to me. A cute nurse had to ask me all the personal questions and check them down. I swear she blushed when she had to ask me if I see prostitutes.

One kind of bizarre question was if I had visited England. “England?”. “Yes”. “Um, yes?”. “How long?”. “Um, about two weeks?”. “Oh, fine”, check. Imperial blood is bad? Must be all the vinegar crisps they eat. I knew that can’t be good for you.

The setup was very conventional and everything was as sterile as anywhere. The only difference in the setup was they used what looked like regular kitchen scales to measure the blood bags, instead of fancy schmanzy specialized automatic scales you see elsewhere.

I felt fine afterwards, but slept like a log last night.

Posted by Roine at April 10th, 2007 10:04 | Permalink

February 4th, 2007

cobain onebritney nose reindeer

OK, so an update. Not dead. The bunnymess sending was recently fixed, and he’s been online pretty much constantly lately so sending a bunnymess should work now.
People always ask me what I’m doing. I guess if I updated this a bit more people wouldn’t need to ask. It’s just that my days are mostly ordinary and boring anyway; I’m doing much the same thing as everyone else, just 9000km away. And it’s a little better weather here I guess.
While other of my friends do cool and spontaneous things like delivering babies on their kitchen floor, I mostly go through my daily routine. Lately I’ve been working my ass off to get a release done, so I think I’ll be in for a little vacation afterwards. After all, I am living in paradise.

The title? Oh, it’s from a spam mail. I don’t know what a onebritney is either.

Posted by Roine at February 4th, 2007 13:02 | Permalink

September 8th, 2006

Not dead

Uh, is this thing on? Hello?

I decided to do something about the site today. There were hundreds of spam comments to be weeded out, and I fixed an old style bug that has probably been here since day one. The rodent is still here.

What’s happened since last? Well, I’ve moved to another town, found a girl, lost her, got an apartment, watched too much TV, played with bugs (the computer kind), slept late, ate funny stuff, shopped pillows and bedcovers, met some new people, got some freckles.

I’ve had a nice summer vacation. How was yours?

Posted by Roine at September 8th, 2006 05:09 | Permalink

June 21st, 2006

MacBook

Already back from Singapore, with a brand spanking new MacBook. I’ve migrated over most of the stuff from my old iBook (thank you Firewire Target Disk mode!) and played around a little. It’s amazing how transparent the x86 transition is - I’m not even aware of which apps I run are native x86 and which are emulated PowerPC. But this thing is fast:)

Some initial observations:

  • Size

    At 13.3″, the screen is huge. I’d be happy with a quarter size. It’s about an inch wider than the old iBook and quite a bit thinner, and about the same weight.

  • Glossy screen

    The new MacBooks have a glossy screen (as do all new laptops). This can be quite annoying; e.g. you see the ceiling lights in the screen, but it’s not as bad as I thought.

  • Superbright screen

    I’m not kidding, this screen will make you a nice tan. I’m using the absolutely lowest brightness setting, and it’s too bright for my eyes. I have to muck with the gamma settings to lower the brightness to a usable level. It’s stupid, really really stupid.

  • Screaming fast

    I have the Dual 2GHz model, since the slightly cheaper one was out of stock everywhere. Compared to the old 600MHz iBook G3, it screams. It plays HD without batting an eye. I’d say for video, this thing is 10x faster than the iBook. Coool.

  • Heat

    There’s been lots of heat issues with the MacBooks, and I would have held out buying a MacBook for this reason if my iBook hadn’t died. I’m happy to say it seems my new ‘book doesn’t have any such issues.

  • Fans

    There are fans? No really, they are either broken or so silent I can’t hear them (it?). Big plus. Huge.

  • x86

    Allegedly, this thing runs Windows XP. I haven’t had a chance to try this, but I’ve heard people buy MacBooks to exclusively to run XP on. The new CoreDual CPU also sports Intel’s VT instructions, so running several OS (eg Mac OS X and XP at the same time) is easy and runs at native speed. You only need something like Parallels Desktop to manage it.

  • OpenGL sucks

    The MacBook has an onboard Intel graphics chip. It only does a “sort of” emulated OpenGL , and it really sucks bigtime. Most OpenGL apps like Blender has issues. There are also other occasional video display errors. Apple really needs to update the drivers.

  • Mac OS X 10.4

    I’ve only used 10.3 so far, and 10.4 feels like a slight upgrade to 10.3. Except for stupid Spotlight, the “automatic index” feature. It doesn’t turn off. I’ve really really tried. The stupid thing spent an hour wearing down my old iBook’s poor harddisk for absolutely no reason. I hate it already.

All in all, I’m very happy with the MacBook. I hope this keeps up, because I’m likely to still use this computer several years from now.

Posted by Roine at June 21st, 2006 20:06 (1 Comment) | Permalink

June 15th, 2006

iBook RIP (2002-2006)

My beloved ‘book just died <sob>. Her trackpad’s been wonky for several months now, and today the screen went. Very sudden, but still expected. When I bought her I expected a lifetime of three years. She’s pulled four and some. I’ve revived her from various ailments before, but I’ll let her sleep this time.

So tomorrow I’m going to S’pore and buy a MacBook (with intel. yech). Yes, it was actually planned.

Posted by Roine at June 15th, 2006 19:06 | Permalink

May 7th, 2006

Yellow frames

I was invited to the opening of a small restaurant yesterday. I didn’t know what to expect but as always I accept if there is no real reason not to; it’s almost always worth it. This certainly was.

When everyone had arrived, there would be a Buddhist ceremony to bless the restaurant. Five monks in orange robes and one nun in white presided. Everyone sat on the concrete floor except the monks who had ceremonial mats. I happened to sit directly across from the row of monks.

Just before the ceremony would begin, the host passed out drinks to the monks; they were given Fantas and Sprites in glass bottles. Sitting across a whole line of elderly monks in orange robes sipping orange Fanta from straws was just too good to be true. Calling it a Kodak moment would be an understatement. It was literally a National Geographic cover picture.

Of course a) I didn’t bring a camera and b) I would have been ceremonially killed if I had taken a picture. The latter is sort of a consolidation, even if it would have been a ‘to die for’ picture.

Oh, and the food was terrific.

Posted by Roine at May 7th, 2006 10:05 | Permalink

April 21st, 2006

Site update

It seems some of you actually use my RSS feeds. Well, as you might have noticed they broke a few days ago, when I switched from MovableType to Wordpress. Anyway, the old MT RSS feed was version 0.92, while wordpress supplies one with version 1.0 and another with version 2.0. Try the one you like.

  • RSS version 1.0
  • RSS version 2.0

It seems I got a bit of a rodent problem in the switch. Don’t worry, he’s been neutered and won’t bite. You can pet him if you like.

Posted by Roine at April 21st, 2006 14:04 | Permalink

April 15th, 2006

Songkran

Pictures from the Thai newyear April 13.

This year it actually rained for a while here on Songkran; everybody seemed surprised. Even so, I was outside in blistering sun for several hours and got a pretty decent sunburn to show for it. You don’t tend to notice the sun when you’re constantly dodging being soaked in water and rubbed with talcum.

Beats the office drudgery any day.

Also I added some photos I took a few days ago at a food market

Posted by Roine at April 15th, 2006 11:04 | Permalink

March 25th, 2006

Power pains

The birthday party got a little wild and my iBook managed to fray a power cord and had to have emergency bypass surgery. It was close there for a while, but everything turned out all right. The patient returned to active duty merely hours after the procedure, and is fine except her power plug is now all wrapped up in electrical tape.

Posted by Roine at March 25th, 2006 12:03 | Permalink

March 21st, 2006

Happy birthday iBook!

My iBook is four years today. Amazing how time flies. I remember going to Singapore and buying her like it was yesterday. Sure, she’s a little loose in the hinges and some keys have started to fall out, but she’s in remarkable shape. Well, she had some works done: new LCD, DVD and she’s well into her second HD now, and she’s seen three major OS versions.

Now, what do you buy for a computer that has everything??

Posted by Roine at March 21st, 2006 14:03 | Permalink

February 26th, 2006

Rest and Recreation

My project is finally released; phew. Time for some R&R. Ola is MIA as usual. I might go over to Krabi and see if there’s anyone I know there. Then maybe over to Phuket for awhile. Maybe some scubadiving.

Posted by Roine at February 26th, 2006 16:02 | Permalink

February 10th, 2006

Red Mars

I managed to save a copy of Kim Stanley Robinson’s Red Mars from the trash today. I’ve started on it before, but I never finished it. It tended to get a little bit technical sometimes (people who know me can stop laughing now). But I know it’s supposed to be good I’m willing to give it another try. My life does not need a seven hundred page book right now…

In related news (not), my pet project hit 3333 builds yesterday, is feature complete and is just waiting for the stupid website to be finished. I want to get this out already; I’m already like two months over deadline.

Posted by Roine at February 10th, 2006 14:02 | Permalink