Installing pygame on Max OS X 10.8.5 Mountain Lion

I first tried to follow this guide, which did install pygame but left it spitting this kind of error when I tried to actually use it:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error (1000) creating CGSWindow on line 259'

That sucked, so here is an alternative.

I assume that you already have a 64-bit version of Python 2.7.2+ along with pip and virtualenv, and the XCode Command Line Tools.

First, install XQuartz. It is an X11 server, which SDL needs, and which is no longer in OSX. The official version is here, but that site is flaky, so I downloaded it from CNET(direct download link). After running that dmg, you should log out and back in, then start the x server (e.g. cmd+space, ‘xquartz’, enter). If it shows up in your dock, you’re good.

Next, we install SDL.  Download the OSX 10.5 runtime library dmg from libsdl.org and run it. Inside, just find the SDL.framework folder and copy it into /Library/Frameworks — you’ll probably need to enter your password.

Next set the environment variables needed to perform the compilation:

export CC='/usr/bin/gcc'
export CFLAGS='-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -I/opt/X11/include -arch i386 -arch x86_64'
export LDFLAGS='-arch i386 -arch x86_64'
export ARCHFLAGS='-arch i386 -arch x86_64'

(thanks to james friend for writing the guide I linked above, which this is directly lifted from). The effects of these lines will only last as long as your terminal is open, so don’t close it until you get things working.

You can now make a virtualenv, activate it, and install pygame:

virtualenv game-env
. game-env/bin/activate
pip install pygame

It should say something like:


Downloading/unpacking pygame
  Downloading pygame-1.9.1release.tar.gz (2.1MB): 2.1MB downloaded
  Running setup.py egg_info for package pygame

    WARNING, No "Setup" File Exists, Running "config.py"
    Using Darwin configuration...

    Hunting dependencies...
    Framework SDL found
    Framework SDL_ttf not found
    Framework SDL_image not found
    Framework SDL_mixer not found
    Framework smpeg not found
    PNG     : found
    JPEG    : found
    SCRAP   : not found
    PORTMIDI: not found
    Framework CoreMidi found

    If you get compiler errors during install, doublecheck
    the compiler flags in the "Setup" file.

    Continuing With "setup.py"

As long as “SDL” was found, you’re in a good spot.

If you get an error that looks like this at the very end:

/Library/Frameworks/SDL.framework/Versions/Current/Headers/SDL_syswm.h:58:10: fatal error: 'X11/Xlib.h' file not found

#include <X11/Xlib.h>

         ^

1 error generated.

error: command 'clang' failed with exit status 1

then you’ve probably got to paste in the export lines listed above.

If you get an error that looks like this at the end:

src/_numericsurfarray.c:1100: warning: implicit declaration of function ‘SDL_FreeSurface’

lipo: can't open input file: /var/folders/5y/shcnnkg970n53k9bvdvngw8w0000gp/T//cc1KHqyH.out (No such file or directory)

error: command '/usr/bin/gcc' failed with exit status 1

with something like this near the top:

Installing collected packages: pygame
  Running setup.py install for pygame
    building 'pygame._numericsurfarray' extension
    /usr/bin/gcc -DNDEBUG -g -Os -Wall -Wstrict-prototypes -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -I/opt/X11/include -pipe -arch i386 -arch x86_64 -I/NEED_INC_PATH_FIX -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c src/_numericsurfarray.c -o build/temp.macosx-10.8-intel-2.7/src/_numericsurfarray.o
    In file included from src/_numericsurfarray.c:23:
    src/pygame.h:106:17: error: SDL.h: No such file or directory

then you’ll need to double check that you installed SDL properly. If you try this over and over and it doesn’t work, you might try to play with the export lines to find your copy of SDL, or follow the guide I linked at the start, which uses homebrew to install SDL.

If things are messed up, you can always just delete the SDL.framework folder, delete your virtual environment, and start again.

If things look good, try running a sample program like the following:

>>> import pygame
>>> pygame.init()
(4, 0)
>>> pygame.display.set_mode((640, 480))
<Surface(640x480x32 SW)>
>>> pygame.quit()
>>> ^D

A screen should appear, and execution should finish cleanly. If that’s the case, you can make games with pygame. 🙂

Ways to record thoughts

I want to write stuff down in my notebook and have it be visible from my phone and laptop. Just now I was thinking my schedule for today, which I want to write down somewhere. I have the following dilemma:

  1. Writing a schedule out on a computer feels tedious — so much typing, and I have to store it into a file somewhere or log in somewhere first.
  2. Writing a schedule out on paper feels useless — it won’t be available to me unless I bring the paper everywhere I go.

Best of both worlds

I ought to be able to write it on paper and then have it on the internet.

I could use a tablet to write things, or I could write on paper and take a picture with my phone. Either way, then I have to get it onto the internet, which constitutes logging into my blog or my server and uploading it. Maybe also “making a new page” in some way. I’d rather just “send it” using the twitter/google+ style image sender on my phone. Kind of like posterous/wordpress email blogging.

But I don’t want it to be “on my blog”, do I? I feel a strange resistance to using a blog to store and display personal information. Shouldn’t it all be stuff I want the world to see? Some deeper thinking is warranted here. I should try emailing things to wordpress and messing with visibility/categories a bit more. It could also make it easier to convert private thoughts into public posts.

Slightly more meta

While thinking about this, I thought “I’d like to keep these thoughts somewhere…” which is the same problem for ideas instead of for schedules. I’ve wanted an idea-keeping tool for a long time… I’ve tried a few things:

  • whiteboard
  • webpage with a single input form that displays everything fed into it
  • workflowy
  • evernote
  • a notepad I carried around
  • notepad files on my laptop

None of them have stuck. That’s probably due to one of the following:

  1. write friction
  2. read friction

Write friction is resistance to recording the thought. Making it easier to get my thought written down and stored somewhere reduces it. Read friction is resistance to reviewing my thought. Once it’s out there, how do I get it back? Making it easier to find past thoughts reduces read friction. A send-style interface feels aimed at reducing write friction, but that might not be the reason I’ve stopped using tools in the past.

Today, I defaulted to my blogging for recording the idea. As a prototype for now, I’ll write my schedule on paper and take a picture with my phone. It won’t be on my laptop, but it’ll follow me most places that way.

Doom’s event handling code

I decided to take a glance at the linux release of the doom source code, from back in 1997, and just above the main loop, there’s some functions to deal with events. This is the line that got my brain stuck and jammed:

eventhead = (++eventhead)&(MAXEVENTS-1);

There is an events array, and a tail and a head. Naturally, I assumed that events would be written at the head and read off the tail, and that the head and tail would just cycle around the array. So, with that in mind (and seeing the same pattern at this other point in the code, as the increment-step in a for-loop), I figured that this must somehow be incrementing the head’s position and wrapping it back to 0 when appropriate.

Except that isn’t what that code is likely to do. Let’s break it down: pre-increment eventhead. Then subtract one from MAXEVENTS. Bitwise-and those two, and put the result in eventhead. Still a little complex! Let’s break it apart more.

Add one to whatever eventhead is. Then subtract one from MAXEVENTS. Then bitwise-and the two, which is: convert them to binary, and every column that has two 1s in it stays a 1 in the resulting number. The output of that is stored in eventhead. Sounds like something we can try out manually.

Imagine that MAXEVENTS is ten. Let’s assume that eventhead starts at zero.

Adding one to eventhead, we get 1. Subtracting one from MAXEVENTS is 9. Converting these to binary and padding them to equal width, eventhead is 0001, and MAXEVENTS-1 is 1001. Going column-wise through that from the left, we get 0&1=0, 0&0=0, 0&0=0, 1&1=1. We got 0001, which is just one. So eventhead incremented the first time the code ran — great! That’s what we want. What happens next?

Adding one to eventhead at 1, we get 2. Subtracting one from MAXEVENTS is still 9. Converting these to binary and padding them to equal width, eventhead is 0010, and MAXEVENTS-1 is 1001. Going column-wise through that from the left, we get 0&1=0, 0&0=0, 1&0=0, 0&1=0. That’s 0000. Zero. So eventhead decremented the second time the code ran. Huh… did we miss something?

Starting to wonder if I’d misunderstood the code, I wrote a simple program to check it, and the output was what we just got: a stream of alternating 0s and 1s. So what’s this code supposed to do? Next I asked a friend to look and give his opinions (he ended up at the same spot), and I went googling around. I found one port where the code has been replaced with something more readable that does what I expected. So we came to the conclusion that one of the following scenarios must be true:

  1. The person who wrote this might be an idiot or a showoff. The code either doesn’t work as it seems it should, or relies on clever behaviour for no good reason to accomplish its task.
  2. There may be a valid reason for it to be clever, and despite our best attempts to grok it, we’re missing something.
  3. Maybe there have been semantic changes to bitwise-and or preincrement since this code was written.

We wrote 3 off right away. And we’re pretty sure (given the community’s adulation for John Carmack and the code he’s released) that 1 isn’t the case either. So probably, we are missing something and there’s a good reason for it to be so weird.

Near the end of my rope, I decided to search for ways to do what I think this code ought to. There, on the cplusplus forums, I found a pretty great answer that fills in the missing detail: MAXEVENTS must always be a power of two.

It didn’t even occur to me to look and see what MAXEVENTS was beyond a cursory glance — it’s not defined in d_main.c or d_main.h, so I stopped looking. Just now I’ve found it in d_event.h, defined to 64. 2**6.

So… what? Why?

Let’s try a power-of-two example MAXEVENTS: 16. MAXEVENTS minus one is then 15, in binary, 1111. Bitwise-and of 0001 (that’s eventhead starting at 0, and then preincremented before being and-ed) and 1111 is 0001. So eventhead is incremented the first time the code runs, just like before.

Next time it runs, pre-increment 1 to 2, which is 0010, and bitwise-and that with 1111, what do we get? 0010. It incremented again this time! Next time after that, it goes to 3. And then 4. Until eventhead hits 16, which is 10000. 10000 & 1111 = 00000. so this wraps back around to 0 and begins again. It finally makes sense.

Why do this? The modulus operator, using division, is the most common way to achieve this behaviour. But it’s expensive compared to bitwise operators. Carmack wanted speed and in one of the most commonly hit lines of code in the entire game, it makes sense to optimize.

Pretty sensible in the end. Two things I noted to look out for next time:

  1. Look for the actual initialization (or equivalent — find the value) when doing mental or test-runs through of the code. If none is present, consider: powers of 2, negative numbers, very large number, 0, and 1. Not just something convenient like 10.
  2. “What I think it does” is actually a good clue for where to start looking for answers.

Please, don’t shop at Budds.

I’ve never made a request of this sort before — I don’t intend to make it a regular thing — but I’d like to ask you to not shop at Budds clothing stores, and to share this message if you’re willing to show your support.

The owner of the Kitchener Budds store wrongfully accused my 57 year old mother of shoplifting, by yelling it to everyone in the store. He then proceeded to physically stop her from leaving the store by pushing on her shoulders.

She was forced to remove her coat, undo her sweater, and lift up her shirt to prove that she didn’t have any items, and finally he let her leave.

Later, after cooling down, she decided to return to explain that she’d initially headed for the door after a bad customer service experience. She wanted to give them an opportunity to apologize, but instead, they told her the experience was fine and that she was wrong. Then the owner said that they’d never found the items he’d seen her holding earlier.

So she walked with him to where she’d put down the $15 worth of clothing on a shelf. My mom told him that if the store weren’t such a mess, it would have been easier to find. She didn’t know he was the owner, so she asked to speak to a manager — that’s when he yelled at her again, that he owned the place, and that she was unwelcome there. He went so far as to shove her on her way out.

The website for Budds[1] has written things like “YOU ARE FAMILY TO US”, “Remember that your customers are the ONLY reason you and I are in business, without them, you are nothing!”, and “At Budds our customers are the real reason for our existence and they will always be like family to us.” They have placed a great deal of focus on providing good customer service, and utterly failed to deliver on that promise.

Mom has filed complaints with the Better Business Bureau and the KW Chamber of Commerce. I just want to make sure that people hear about this, because it’s unbelievable and angering. If this is how Stan Budd of Budds treats people like my mom during the holidays, then he and his store simply don’t deserve to be in business. They say so themselves.

Thanks for reading.

[1] – http://www.buddsonline.com/aboutus.htm

pro-tip:

don’t try to keep it all in your head.

I’m reading about the tortoise and hare algorithm for finding cycle detection. Just moments ago, I read the following in the (awful) comment on wikipedia:

# now the position of tortoise which is the distance between
# hare and tortoise is divisible by the length of the cycle.

So, the position of the tortoise is x, and the position of the hare is 2x, and x is divisible by L, the length of the cycle. I’m surprised that x is necessarily divisible by L, so I figured it’s time to do a thought experiment. So I sat here and thought “okay so let’s imagine a graph with a cycle in it — maybe 2 nodes going in, and 3 nodes cycling back and forth”. Next, I thought “Now let’s consider the tortoise at the point of entry into the cycle — he’s moved 2 steps, so the hare would have moved 4… so the hare would be” and I started trying to picture the point the hare would be at. Meanwhile, I’m thinking “maybe I should try fewer vertices? Or more…”, and “Maybe I should go step by step and keep track of each of their progress through the sequence.”

If that sounds confusing, good. Because it is. Keeping a ton of stuff in your head like that is silly, and error-prone, and vulnerable to distraction. Just by thinking “I should write a short blog post about this”, I lost some of the state I’d built up. Actually writing the blog has obliterated any progress I’d made.

Instead of trying to keep things in your head — whether you do it out of laziness, or because you want to prove you can do it*, or because it just doesn’t occur to you to do otherwise — just write things down. Open a text editor and plop things in there, or my personal recommendation: grab a pencil and some paper and write/draw it out. The physical world can do the job of remembering where and what everything is, and you just have to figure out the transformations. Much easier. Much smarter.

* I fall into this trap whenever I use Khan Academy’s tests. I start off on something easy enough that it makes sense to keep in my head. Then it gets a little harder, for a long time, and eventually I’m struggling to multiply 5 digit numbers in my head for no reason other than a misguided sense of pride. I’ll usually give up around then, too, which shows that it’s not a smart path.