Sunday, July 12, 2009

texture sprites in second life

seasoned web hands will probably be familiar with the concept of CSS Sprites. the idea behind sprites is that with modern networks tuned for downloading large images, why not improve the performance of a web site by smashing all your visual elements in one image (JPG, GIF, PNG, whatever.) and then use the position features of CSS2 to display just a bit of that big image in a visual element. here's a visual to help grok it:

this image contains 12 distinct visual elements, arrows, circles, checkmarks, etc. (i nabbed these images from the wikimedia commons where they are released under a GPL license, more info at the gnome-look website.) note the box around one of the elements? that's there to remind you that you probably only want to display a portion of this image at any given time. the elements are stuffed into a single image for download to your browser to take advantage of the performance boost you may get by downloading single images instead of multiple individual images.

users of second life might have seen textures seem to not rez properly or rez half way and hang before completing download. there are several reasons why this might occur, some errors, others ramifications of the way progressive downloads on SL textures are handled. progressive downloads are intended to give users a context for the virtual world in as short a time as possible. in other words, it was thought that a world where all/most the textures are filled in but fuzzy and gradually got less fuzzy was better than a grey or transparent world where you had to wait forever to render the whole, non-fuzzy scene.

but this policy is problematic for some objects in the virtual world. objects like signs intended to display arbitrary text will sometimes rez the images representing different letters on a different schedule. this gives a weird appearance where some letters are clearly defined while others are fuzzy. for my money, this is a touch unsettling.

you can avoid this with the use of "texture sprites". SL builders are probably familiar with the fact that textures have a "repeats per face" and "offset" options. you can use these options to create the SL equivalent of CSS sprites. that is, you can apply a large image that contains multiple visual elements and use the repeats per face and offset parameters on a primitive face to only show the visual element you want to see. the benefits here are that all the visual elements will be in the same state of "rezzedness".

here's an example. i rezzed a cube with the dimensions <0.01,0.1,0.1> and applied the blank texture. i then put the following script in it:

// Copyright (c) 2009, Meadhbh Hamrick
// All rights reserved.
// Redistribution and use in source and binary forms,
// with or without modification, are permitted provided
// that the following conditions are met:
// * Redistributions of source code must retain the
//   above copyright notice, this list of conditions and
//   the following disclaimer.
// * Redistributions in binary form must reproduce the
//   above copyright notice, this list of conditions and
//   the following disclaimer in the documentation
//   and/or other materials provided with the
//   distribution.
// * Neither the name of the Project Meadhbh nor the
//   names of its contributors may be used to endorse
//   or promote productsderived from this software
//   without specific prior written permission.

integer face;

stroke_face ( ) {
    vector offset;
    if ( 0 == face ) {
        offset = <0.0,0.333,0.0>;
    } else if ( 1 == face ) {
        offset = <0.0,0.111,0.0>;
    } else if ( 2 == face ) {
        offset = <0.0,-0.111,0.0>;
    } else {
        offset = <0.0,-.333,0.0>;

    llSetPrimitiveParams( [ PRIM_TEXTURE, 2, "4b2b744a-7203-dcc7-3d8e-db8aa195f103", <1.0,0.111,0.0>, offset, 0  ] );

        face = 0;

    touch_start(integer total_number)
        face = face + 1;
        if( face > 3 ) { 
            face = 0;

people allergic to copy and paste can download the script from project meadhbh at sprite_test.lsl

the texture "4b2b744a-7203-dcc7-3d8e-db8aa195f103" was generated by combining several small images with the css sprite generator from project fondue. a copy of the png the texture was generated from is available at texturesprite.png. and in the interest of full disclosure, i should mention that the images from which this texture was generated were downloaded from the wikimedia commons site, were apparently made by John Erling Blad and released under a GNU Free Documentation License. Ergo, the PNG linked to above and the "4b2b744a-7203-dcc7-3d8e-db8aa195f103" texture should both be considered to be released under the same license.

Saturday, July 11, 2009

when nostalgia strikes

so the other day as i was trying to figure out why my macbook pro was giving me a command line interface at bootup instead of a screenful of aqua goodness, i realized i longed for the delightful mush of my old commodore 64. readers who weren't around for the heady days of the early home computer explosion probably won't get it, but the old 8-bit systems had some great features. i bought my commodore 64 new for $200 and, after hitting the power switch, you were ready to start typing in about 5 seconds. two features you will not find on a PC class machine.

but in today's world, the 8-bit machines are virtually useless for what i do normally: surf the web, write software and use second life. so even though twittering and rendering second life scene graphs are not out of the question for old 6502 based machines; were i to switch back to the old 8 bit machine i cut my teeth on, i would surely die of youtube withdrawal.

so i reckoned the next best thing was to relive the glory days on my macbook pro. if you're like me, one of the apps you keep open constantly is the i'm an old software developer and NeXT system administrator; i took the time to learn many useful command line tools, and dang-nabbit i'm going to use them.

instead of pitching my mac for an old 8-bit system, i simply made it look a little more like the old c64. and here's how i did it:

  • first, download the Commodore 64 Pixelated typeface. it's a free download. yay! nostalgia doesn't have to hurt! After downloading the typeface, you should see a TrueType File called commodore.ttf. double click on it to launch the font installer.
  • next you'll want to download the small program that spits out the commodore welcome message. you can extract it from the project meadhbh repository file trunk/nostalgia/c64. once it's downloaded, you'll want to move it to your home directory, add a dot to the beginning of it's name and make it executable with the chmod command. so open a terminal, cd to the directory containing the file (probably ~/Downloads unless you've changed it) and execute the commands:

    mv ./c64 ~/.c64
    chmod 755 ~/.c64

  • now download the terminal settings file from trunk/nostalgia/nostalgia.terminal. you'll want to move this file to your home directory for reasons that will be clear soon.
  • finally, open the preferences and click on the settings tab. you should see something like this. note the location of the settings tools widget (it's the thing circled by the red oval.)
  • click the tools widget. this will give you a menu, select the item "Import..." this will bring up a standard file dialog. for reasons i don't understand, my version of doesn't allow you to navigate through your directories in search of nostalgia.terminal file. fortunately, you moved it to your home directory.
  • finally.. you need to insert a line into your .profile file. using your favorite text editor, add the following line to the file ~/.profile

    test -r ~/.c64 && . ~/.c64

    i put mine right above the other test line near the top.
and that's it. you should now be enjoying the glory of a commodore-64 like experience.

Wednesday, April 1, 2009

hands on with the tv computer

so a couple days ago i see a post on boingboing about the TV Computer. it's basically a $12 6502 nintendo famicom clone being sold for the "developing middle class." sure, 12 US Pesos is nothing to latte swilling software professionals like you and me. but in the developing world where the local currency is constantly defending itself from IMF hit men, buying laptops from the US supply chain can be a bit cost prohibitive. even a $100 laptop can give pause to the most well meaning parent or school system (or so the reasoning goes.) and that's where the $12 TV-Computer comes in, 'cause if $100 is too much, $12 might not be a budget killer.

so lotsa kids overseas are getting to experience the joy of 8 bits, just like i did when i was young. this would be the end of the story were it not for the folks from it just so happened, so the story goes, that a USCD grad student visiting his overseas home happened upon a store selling the $12 machines. inspiration struck and he wondered if it would be possible to get g33ks in the developed (?) world to code an educational game or two. so they have a wiki and an idea and are ready to start harvesting spare g33k cycles.

i am a total sucker for all things 8-bit. i generally eschew first person shooters for a nice game of galaga. and i have 8-bit operators on heavy rotation around chez meadhbh. so it was nearly impossible to resist. this is the story of my 8 bit fantasy (so far.)

buying the machine has coordinated with the maker shed to resell the TV-Computer in the US. click on over to to order. one thing you should note, however. while the TV-Computer costs the equivalent of $12 overseas, it's selling for $50 here. i suspect this is a secret plot to preserve the inflated price of commodore PET's on ebay, though i think i read somewhere some of the money is going to the 501(c)(3) the playpower people are putting together. in california, you're going to pay state sales tax and unless you want to drive over to the o'reilly offices, you're going to pay shipping. (though kudos to the maker shed for offering this as an option.)

my total came to something like US$63.56 (or since i spend most of my day buying things in linden dollars, L$16,525)

unpacking the machine

for the benefit of those readers who will be living vicariously through my experiences, let me provide a play by play description of unboxing the TV-Computer.

when i opened the shipping container (which was surprisingly light) i saw this. if you read chinese, you might be thinking to yourself, "oh. okay, it's a computer." sadly, i spent my youth learning dutch, english, esperanto and greek. i never quite made it to chinese. seeing things like this makes me mildly nervous, since i worry i'll be completely lost. the box the tv computer came in
but the true horror came when i realized the contents of the box included no instructions. note to self; when i make the $12 computer i've been thinking about for the last several years, remember to include a manual. so ack, no docs.

here's what i was looking at when i realized i'll be reverse engineering the instructions on this device.
looking at the box with the cover removed
to recap what's in the box:
  • the keyboard / processor
  • a power supply
  • a yellow cartridge with lots of chinese characters on it
  • a mouse
  • an A/V cable
  • and two (2) game controllers
the stuff in the box

operating the machine

after hooking the thing up to my ancient 32" sony studio monitor i powered it up and got this beautiful red screen. what did i do wrong? the red screen of stupidity. put the cartridge in!
i forgot to put the cartridge in, of course. put the cartridge in and things get better. (if you compare this photo with the one immediately previous, you can see that the cartridge goes in a slot in the above of the keyboard.)

i don't understand anything on that screen, though i'm guessing 2007 is the year the ROMs were burned. after playing with the buttons on the controller, i finally figured that pressing the left-most button of the controller moves you to the next screen, and on to 8 bit fun.
the splash screen
here's a shot of the main screen. OMG! it looks like windows! and look at the DOS icon! if you look carefully, you'll see it says "SB-DOS." at this point i sat and marveled for a moment. for years we tried to escape the horrors of the command line. sure, i love the Unix(tm) command line as much as the next chic, but DOS? but i think i figured it out. the windows interface (including the DOS box) is pretty ubiquitous. having a system that gives the "look and feel" of that same interface may have some benefits for kids starting their computing careers. i would (of course) have preferred they replicate the interface of the Canon Cat, but that's just me.

anyway. here's the main screen. it reminds me a LOT of Windows 95/98.
the main screen
down in the lower-left corner of the screen, there's something like a start menu. fortunately for me, i have some friends visiting soon who put their son in a mandarin immersion program. maybe they can help me translate. i'm not sure why the start menu renders below the bottom of the screen. it could be the computer sending funky settings to the video registers or it could just be that the old sony monitor i bought for $35 is not the TV in the world. the start menu
and yes, that was the icon for solitaire. the parallels with windows are striking. playing solitaire

about the hardware

at some point in the future, i'm going to void the warranty. but not tonight; it's a touch late. but here are a few notes about the hardware.
my favorite quirk on the system is that the power LED is labeled "LED" and not something like "power". if you look close, you can make out the "LED" label below the LED.

the keyboard is complete garbage. seriously. there's no debounce routine so pressing a key will occasionally get you two or three characters on the screen. and the enter key on my unit is somehow misaligned so i have to press it four or five times to register an enter key press.

your mileage may vary. i'm guessing its the software in the cartridge that's responsible for the missing / substandard debounce routine and i'm inclined to guess that the enter key problem on my unit is a manufacturing defect and not a design defect.
the keyboard
but the controllers didn't disappoint at all. i tried both the game controllers and the mouse. both worked perfectly. it's sort of a weird experience using a mouse on an 8-bit computer (i didn't spend much time with the Apple IIgs, obviously.) but i was happy to find the software "did the right thing" when i tried to move the mouse pointer with the game controller. the controllers
here's where the controllers / mouse plug in. the ports are labeled "com1" and "com2" so i'm wondering if they're regular db-9 serial ports. if so, bonus! i might be able to rig something up for development so i can download software via the serial port. the only nit i have to pick with the com ports is there are only two of them. i would have liked to have both the mouse and both controllers plugged in at the same time, but i'll live. the com ports the controllers plug into
here are the video outputs. you've got the traditional yellow/white audio/video thing and the red plug is labeled "RF" so i'm guessing it modulates the signal on channel 3. the video outputs


it was great fun to go back and relive the glory days of 8 bit, but the keyboard was a major problem and it looked like my unit put some scan lines above the screen and some below.

the unit came with zero documentation, so i'm left to guess what the commands are for the cartridge DOS clone and GBASIC. i haven't done the google search yet, so i'll post here if i find anything.

despite the unit's flaws, the 6502 has a magical otherworldly feeling about it, and i for one can't wait to start playing around with it. i'm motivated half by my nostalgia for 8-bit systems and half for wanting to have a cheap platform to develop educational games for my son. ultimately i would LOVE to buy a room full of these things and some refurbished CRT TVs for a computer lab for my local school. but i'd also feel obligated to pay for at least one local teacher to play at seymour papert's feet like i did.

what's next?

next week i'm gonna void the warranty to see what's inside it. maybe i can fix the keyboard while i'm in there. ultimately i would like to develop / port something like Apple LOGO to the box. and i'm probably gonna try to do a FORTH interpreter; they're pretty simple, and honestly, i like FORTH waaaay more than i like BASIC.

if you're interested in hacking these things with me, i'm hosting a weekly virtual get-together. thursday nights at 7PM, Second Life Time at meadhbh is full of stars.

Wednesday, October 29, 2008

NetFlix New API

So a couple things happened in the last month: I got my Android Phone and I discovered the NetFlix API. Can you guess what's coming next? Yup. A NetFlix queue manager for Android.