• SF Central Subway vs. California High Speed Rail

    A while back, San Francisco’s MTA got a big bundle of federal cash to build Muni’s “Central Subway” project. This new subway line will create an underground connection between CalTrain’s 4th and King station, Union Square, and Chinatown.

    This area is horribly congested with traffic so the idea has merit. It would allow folks from the peninsula and South Bay to hop off Caltrain and take a quick subway ride to Union Square and Chinatown.

    Meanwhile, there’s another plan being developed separately — a plan that would build a high speed railway from Los Angeles to San Francisco. Between SF and Gilroy, the new railway would follow the existing CalTrain route.

    But instead of terminating at 4th and King, the new High Speed Rail line — as well as CalTrain — would be extended via subway and terminate at 1st and Mission. Construction on the new station and subway train box is already underway.

    Now, any reasonable person looking at a map can see that 4th and King is awfully close to 1st and Mission. Especially for a train. So close together, in fact, that keeping the existing 4th and King station seems excessive.

    Which leads me to wonder: aren’t these two plans mutually exclusive? Either the 4th and King station will be gone, and the Central Subway will effectively go nowhere, or CalTrain will have to keep an awkwardly placed station just to connect to the Central Subway.

    Overall, I think the High Speed Rail makes a lot of sense. It’s fast, electric, can be powered by renewable resources, and trips will take less time than an airplane. But there’s something being lost in the planning process, and it needs to be fixed if they’re serious about building it.

  • Human bodies: the missing features

    There’s a lot of things I’d change about the way our bodies work if I could. We could be so much better. So what would I change?

    I’m going to forgo the obvious here. There’s no need to discuss superpowers, infinite lifespans, Matrix-style brain plugs, regenerative limbs, the ability to run 100 MPH, etc. My new features are more unique and immediately practical. Sort of.

    Here’s my new features.

    Detachable teeth

    Even a dentist would agree that going to the dentist sucks. But why? It’s not like anyone wants to have dirty, rotting teeth. No, the problem is the dental experience. Even the best dentist can’t magically make you feel great about having various painful implements stuffed into your mouth.

    So if your jaw had a lever that removed your teeth, wouldn’t that be perfect? The dentist could just work on your teeth and you’d pick them up in an hour.

    Now I know what you’re thinking — how is this different from dentures? Well first of all they’d be your natural teeth, so they’d fit much better and would come free with the rest of your body. But second, there would be no glues or weird odors or special tablets. The important thing here is you don’t smell like a grandfather.

    Sleep timer

    Picture this: you go to bed with eight hours to spare before you have to get up the next morning. But once you crawl into bed, you can’t get to sleep! Dammit!

    What if instead of having an alarm, we had a way of setting our brains to automatically wake up at a certain time? This way you’d never have to worry about being late for something, plus you’d never have to worry about waking up too soon either because you’d sleep right up until the timer went off.

    Of course there might be some issues, like you’d need a way to override the timer if a bear was attacking your children or your boss called, and a way to change your clock for daylight savings. But these are minor details… I guess.

    Earlids

    You can close your eyes. Why can’t you close your ears?

    Think about how useful this would be — you could close your ears for sirens, at a loud concert, or even to prevent things from getting in your ear. Hearing loss and ear infections would be far less common.

    Plus if someone was bitching at you, you could close your ears and think about how much you hate them.

    Pain switch

    Sometimes, pain is a good thing. For example, if you break your leg, pain lets you know that you need to go to the hospital.

    But around puberty, we all start experiencing pain that doesn’t help us. Growing pains, headaches, backaches… and as you get older, it only gets worse. Wouldn’t it be great if we could just switch it off when we didn’t need it?

    Ideally we could just switch off specific pain signals. So if you had a headache, you could turn that off, but you’d still know if you had a scrape and needed a bandage.

    (Credit for the pain switch goes to Robert Anton Wilson.)

    Eye focus knob

    Eyes get out of focus? You would never need glasses if you had a knob to adjust the focus setting.

    Sure, your eyes would still focus automatically even with the knob. But it would change the default in case it got out of whack.

    Now you might say, “okay dude, but why not just have 20/20 vision?” Ah, well here’s the cool thing about a focus knob: you could use it to tune out. Bored in class? Just unfocus your eyes until the world is a gray blob, sit back, and pretend to be paying attention.

    Baby door

    Painful vaginal birth or a surgical cesarean section? How about neither? Women would just have a door on their bellies that unlocked and opened so the baby could be taken out.

    Before anyone e-mails me and says this one is obvious, I sort of agree. If you’re a woman who’s given birth, I’m sure you’ve thought of this one. But I’m a guy, so this is original for me. So let’s give credit where credit is due, k?

    Large diameter pee pipe

    I’ve saved the best for last. Sure, it’s similar to the last one, but hear me out.

    When you urinate, you’re basically squeezing out a balloon of liquid through a very, very small pipe. Due to the small diameter of the pipe, it takes quite a while to go pee. But why should it be this way?

    So let’s have a bigger pipe for peeing. Or maybe a zipper or lid that we could open so all the pee could exit at once. Zip, splash, flush, and you’re done in 2 seconds.

    Best part is, the whole issue with… um… *crosses legs* stones (ouch) would no longer be a threat to mankind.

  • SF Weekly SF Weekly

    I had the strangest feeling of deja vu after opening a copy of the latest SF Weekly.

  • Wraps coming off Mission National Bank

    In case you haven’t been by recently, the wraps are finally coming off Mission National Bank’s flagship 16th St. location!

    Mission National Bank

    Restoration on the building’s exterior has been going on for roughly a year now, but the result is totally worth it. The restored facade adds some prestige to the often sketchy part of 16th between Mission and Valencia.

  • Inanimate Racer

    Today I was surprised to find a (semi-serious) review of a game I created back in high school, “Inanimate Racer.” The point of the game was that you could only race objects that didn’t move.

    Here’s the review:

    If you’re interested in playing(?) Inanimate Racer, there’s also a Flash remake of the game available here.

  • Obnoxious tourists

    Don’t get me wrong, when people get lost and need help I enjoy giving directions. I know the city pretty well and have a good sense of direction, and failing that I have an iPhone with Google Maps.

    But sometimes, tourists in the city are just completely obnoxious. You know it’s always a bad sign when they ask how to get to Fisherman’s Wharf.

  • Fire Truck

    This may be the greatest paint job ever on a fire truck. Click the image and zoom for more detail.

    Fire Truck

    Spotted at Townsend and 2nd.

  • Tuesday

    This was someone’s Tuesday night. Looks like they had a great time!

    Tuesday

    Spotted just outside of 330 Ritch.

  • No Smoking, No Spitting, No Exit

    These are the signs on the noisy Italian streetcars on the F Market line. A rough translation (thanks to Google) is in the title of this post.

    Vietato Fumare
    Vietato Sputare
    Vietato Scendere

  • Accelerated 2D graphics with OpenGL and SDL: part 1

    As some of you know, I’m working on an old-school puzzle game with Hryx. This game makes use of OpenGL’s graphics capabilities, but not for 3D graphics. Nope, the game is entirely two dimensional.

    So why OpenGL? Because it’s really, really fast on most systems. Much faster than the slowpoke graphics in SDL.

    This is going to be part one of a two part series on doing 2D graphics using OpenGL. And step one? Getting some 2D sprites loaded.

    Loading the sprite with SDL_image
    Now since we’re using SDL for our game, we decided on the excellent SDL_image to do the image loading.

    Using SDL_image to load a sprite into memory is absurdly easy.

    string filename = "myImage.png";
    SDL_Surface* surface = IMG_Load( filename.c_str() );

    That’s it!

    But wait… that loads the graphic into an SDL surface. We need it to be an OpenGL texture! Damn.

    OpenGL textures
    In OpenGL, there’s no such thing as a “sprite.” You just have “textures.” So we’ll need to take our sprite and map it to a texture. The texture will be referenced with an integer (a GLuint) rather than a pointer.

    But there’s an interesting restriction we have to work around that SDL simply doesn’t have. On most systems, OpenGL textures must have dimensions are are powers of two: 2, 4, 8, 16, and so on.

    So once we load our SDL_Surface, we’ll make a new surface where we’ve rounded up the dimensions to the next highest power of two. The new surface will be filled with the alpha “color” and then we can blit (copy) over the existing image to the top left corner of our new image.

    SDL_PixelFormat *format = surface->format;
    Uint32 width = surface->w;
    Uint32 height = surface->h;
    Uint32 widthPow = (unsigned) pow( 2, ceil( log( width ) / log( 2 ) ) );
    Uint32 heightPow = (unsigned) pow( 2, ceil( log( height ) / log( 2 ) ) );
    
    // Create new empty surface.
    SDL_Surface* newSurface = SDL_CreateRGBSurface( SDL_SRCALPHA,
    	   widthPow, heightPow, 32,
    	   rmask, bmask, gmask, amask );
    
    // Fill sprite with alpha.
    Uint32 alpha = 0;
    alpha = SDL_MapRGBA( format, 0, 0, 0, amask );
    SDL_Rect rect;
    rect.x = 0;
    rect.y = 0;
    rect.h = heightPow;
    rect.w = widthPow;
    int ret = SDL_FillRect( newSurface, &rect, alpha);
    surface->flags &= !SDL_SRCALPHA;
    
    SDL_SetAlpha( newSurface, SDL_SRCALPHA, SDL_ALPHA_TRANSPARENT );
    
    // Copy image data to our new surface.
    ret = SDL_BlitSurface( surface, 0, newSurface, 0 );

    Great, we got our surface loaded. Now it’s time to create an OpenGL texture, then take our SDL surface and copy it to the texture.

    // This will be our OpenGL texture.
    GLuint texture = 0;
    
    // Bind the texture.
    glPixelStorei(GL_UNPACK_ALIGNMENT,4);
    glGenTextures(1, &texture );
    glBindTexture(GL_TEXTURE_2D, texture);
    
    // Convert surface to Open GL format.
    gluBuild2DMipmaps(GL_TEXTURE_2D, 4,
    	widthPow, heightPow, GL_RGBA,GL_UNSIGNED_BYTE, newSurface->pixels);
    
    // Free our temporary SDL buffers.
    SDL_FreeSurface( surface );
    SDL_FreeSurface( newSurface );

    Easy, huh?

    Dealing with 24-bit surfaces
    Up until now we’ve only dealt with is 32-bit images. One thing I hadn’t dealt with until now is 24-bit color-keyed images. A color-keyed image means you have RGB but no alpha or “A” channel to tell you what should be invisible. Instead, you just take one color (often black) and just say that it’s 100% transparent.

    What we’re going to have to do is make the SDL_surface color keyed before we copy it to the OpenGL texture.

    First, we’re going to need some functions for plotting individual pixels on an SDL texture. Unfortunately there’s no built in functions for this, so I stole some from this site.

    Uint32 GetPixel( SDL_Surface *surface, int x, int y )
    {
    	Uint32 color = 0;
    	char* pPosition = ( char* ) surface->pixels;
    	pPosition += ( surface->pitch * y );
    	pPosition += ( surface->format->BytesPerPixel * x );
    	memcpy ( &color , pPosition , surface->format->BytesPerPixel );
    	return color;
    }
    
    void SetPixel( SDL_Surface *surface, int x, int y, Uint32 color )
    {
    	char* pPosition = ( char* ) surface->pixels;
    	pPosition += ( surface->pitch * y );
    	pPosition += ( surface->format->BytesPerPixel * x );
    	memcpy ( pPosition , &color , surface->format->BytesPerPixel );
    }

    Cool, now we can plot pixels! Feel free to go crazy and write a routine to draw circles or something!

    But all we really need to do here is to take pixels that correspond with our color key and set the alpha channel to the alpha mask channel color.

    if ( 24 == format->BitsPerPixel )
    {
       Uint32 colorKey = format->colorkey;
    
       for ( int y = 0; y < surface->h; ++y )
       {
    	   for (int x = 0; x < surface->w; x++)
    	   {
    		   Uint32 color = GetPixel( surface, x, y );
    		   if ( color == colorKey )
    		   {
    			   SetPixel( newSurface, x, y, 0 );
    		   }
    	   }
       }
    }

    Putting it all together
    Here is the final code to load an image file to a texture using SDL.

    #include 
    #include 
    #include 
    #include 
    #include 
    
    #if SDL_BYTEORDER == SDL_LIL_ENDIAN
       static const Uint32 rmask = 0x000000FF;
       static const Uint32 bmask = 0x0000FF00;
       static const Uint32 gmask = 0x00FF0000;
       static const Uint32 amask = 0xFF000000;
    #else
       static const Uint32 rmask = 0xFF000000;
       static const Uint32 bmask = 0x00FF0000;
       static const Uint32 gmask = 0x0000FF00;
       static const Uint32 amask = 0x000000FF;
    #endif
    
    
    
    Uint32 GetPixel( SDL_Surface *surface, int x, int y )
    {
    	Uint32 color = 0;
    	char* pPosition = ( char* ) surface->pixels;
    	pPosition += ( surface->pitch * y );
    	pPosition += ( surface->format->BytesPerPixel * x );
    	memcpy ( &color , pPosition , surface->format->BytesPerPixel );
    	return color;
    }
    
    
    void SetPixel( SDL_Surface *surface, int x, int y, Uint32 color )
    {
    	char* pPosition = ( char* ) surface->pixels;
    	pPosition += ( surface->pitch * y );
    	pPosition += ( surface->format->BytesPerPixel * x );
    	memcpy ( pPosition , &color , surface->format->BytesPerPixel );
    }
    
    
    GLuint loadTexture( std::string filename )
    {
    	SDL_Surface* surface = IMG_Load( filename.c_str() );
    
    	if ( NULL == surface )
    	{
    	   // Error!
    	   return 0;
    	}
    
    
    	SDL_PixelFormat *format = surface->format;
    	Uint32 width = surface->w;
    	Uint32 height = surface->h;
    	Uint32 widthPow = (unsigned) pow( 2, ceil( log( width ) / log( 2 ) ) );
    	Uint32 heightPow = (unsigned) pow( 2, ceil( log( height ) / log( 2 ) ) );
    
    	// Create new empty surface.
    	SDL_Surface* newSurface = SDL_CreateRGBSurface( SDL_SRCALPHA,
    		   widthPow, heightPow, 32,
    		   rmask, bmask, gmask, amask );
    
    	// Fill sprite with alpha.
    	Uint32 alpha = 0;
    	alpha = SDL_MapRGBA( format, 0, 0, 0, amask );
    	SDL_Rect rect;
    	rect.x = 0;
    	rect.y = 0;
    	rect.h = heightPow;
    	rect.w = widthPow;
    	int ret = SDL_FillRect( newSurface, &rect, alpha);
    	surface->flags &= !SDL_SRCALPHA;
    
    	SDL_SetAlpha( newSurface, SDL_SRCALPHA, SDL_ALPHA_TRANSPARENT );
    
    	// Copy image data to our new surface.
    	ret = SDL_BlitSurface( surface, 0, newSurface, 0 );
    
    	// Change color key to alpha transparency.
    	// Used for 24-bit images.
    	if ( 24 == format->BitsPerPixel )
    	{
    	   Uint32 colorKey = format->colorkey;
    
    	   for ( int y = 0; y < surface->h; ++y )
    	   {
    		   for (int x = 0; x < surface->w; x++)
    		   {
    			   Uint32 color = GetPixel( surface, x, y );
    			   if ( color == colorKey )
    			   {
    				   SetPixel( newSurface, x, y, 0 );
    			   }
    		   }
    	   }
    	}
    
    	// This will be our OpenGL texture.
    	GLuint texture = 0;
    
    	// Bind the texture.
    	glPixelStorei(GL_UNPACK_ALIGNMENT,4);
    	glGenTextures(1, &texture );
    	glBindTexture(GL_TEXTURE_2D, texture);
    
    	// Convert surface to Open GL format.
    	gluBuild2DMipmaps(GL_TEXTURE_2D, 4,
    		widthPow, heightPow, GL_RGBA,GL_UNSIGNED_BYTE, 
                    newSurface->pixels);
    
    	// Free our temporary SDL buffers.
    	SDL_FreeSurface( surface );
    	SDL_FreeSurface( newSurface );
    
    	return texture;
    }

    That’s it! Next time: displaying our new OpenGL texture on the screen as a 2d sprite.