Extracting Layers from Inkscape (Pulling Teeth)

Inkscape is probably the greatest thing since baloney on bread, and it’s free. That’s a real deal!

Basically, it is a powerful and feature-filled vector graphics editor, and I have an easier time using it than I do with Photoshop. That’s because I am an art noob. But armed with Dmitry Kirsanov’s The Book of Inkscape and things like these awesome tutorials from Chris Hildenbrand, you can get some really nice results.

One thing that I use Inkscape for is animating sprites. Paint.net is very nice for this as well (just tabbing between frames is awesome), but I had to cobble together a batch file and the obscure pdn2png to get a good workflow for saving frames into a usable form. I basically wanted to be able to edit the frames of a sprite and hit a button to export everything out to PNG files. There are other programs that are specific for animation that make the process of animating much easier, but they generally cost money or are not very nice to edit art with

Inkscape has some exporting to PNG capabilities from the command line, but you need to know the SVG ID of the object you want to export. In my case each frame is a group within the drawing. Inkscape lets you name each group (groups are SVG objects as well), but this is an extension to the SVG specification. The actual ID that you need is obscured at the Inkscape level. So it is possible to export the group from the UI using the Inkscape label, but you must know the SVG ID to do this from the command line.

Needless to say I was bummed!

Even worse, I want to have each frame of animation have discrete parts. So if I have a character, I want each frame to have a sub-layer of the arms, the legs, the eyes, etc. That way I can grab that sub-frame PNG and animate it separately, allowing me to articulate different frame parts as an aggregate sprite.

So, I dropped using scripting and went full on code! I messed about with the SVG file format and basically just brute force wrote a parser in Java that will traverse the XML for the SVG format and extract layer groups by using the Inkscape label. The Java matches the Inkscape label to the SVG ID, and then calls the Inkscape program with command line arguments to make the PNG. This allows me to have an SVG drawing, make several layers and sub-layers of animation for a sprite, and run a batch file that calls my extractor to organize the frames in nice ways.

You can get ExtractInkscapeLayers for Windows here. I will get around to posting the source at some point if anyone cares!

The Video Age

I made a quick YouTube video of my graphics test program. Whee, it’s like the space age.

The neat thing (at least to me) is that the cutter beam from the game is actually a texture that is stretched and rotated to the location of the mouse. That solution seems to work really well.

Java Enumerations, or Hey, You Aren’t Just an En-number Anymore

Java has some cool features, but one thing I really like are enumerations. For my project I need a damage type, so I made a base class for it:

public abstract class Damage
{
    public enum Type
    {
        NORMAL("NORMAL"),
        FIRE("FIRE"),
        COLD("COLD"),
        AIR("AIR"),
        EARTH("EARTH"),
        POISON("POISON"),
        CORROSION("CORROSION"),
        ELECTRICAL("ELECTRICAL"),
        BLUNT("BLUNT"),
        PIERCING("PIERCING");

        private final String desc;
        private final String user;

        /**
         * Construct the enumeration with the description given
         * @param desc The description of the type
         */
        private Type(String desc)
        {
            // save the description in all uppercase
            this.desc=desc.toUpperCase();

            // the user first character is upper case
            // make the characters after the first lowercase for the user description
            this.user=Character.toString((Character.toUpperCase(this.desc.charAt(0))))+(this.desc.substring(1).toLowerCase());
        }

        /**
         * Get the description of the type
         * @return A string describing the type
         */
        public String getDesc()
        {
            return desc;
        }

        /**
         * Get the user-readable description of the type
         * @return A string for users describing the type
         */
        public String getUser()
        {
            return user;
        }

        @Override
        public String toString()
        {
            return user;
        }

    }

    /**
     * Get the damage for the damage type
     * @return The damage caused.
     */
    public abstract int getDamage();

    /**
     * Get the damage type for the damage
     * @return The damage type
     */
    public abstract Type getType();

    /**
     * Get the type of the damage given a string describing the type
     * @param desc The description of the damage type
     * @return The type of the damage
     * @throws IllegalArgumentException if the description has no match to a damage type
     */
    public Type getType(String desc) throws IllegalArgumentException
    {
        // if the description is empty, then make this normal
        if(desc.isEmpty())
        {
            return Type.NORMAL;
        }

        // loop through the values in the damage type enumeration
        // using the enhanced for loop after changing the enumeration to an array
        for(Type damageType : Type.values())
        {
            if(desc.equalsIgnoreCase(damageType.getDesc()))
            {
                // matched the damage type, return this type
                return damageType;
            }

        }        

        // no match
        throw new IllegalArgumentException("Cannot match damage type to description "+ desc);

    }

}

This base class is really just two things:  an interface to get the damage amount and type, and a container for the enumeration I use for the damage types. The neat thing I find is how I can match the damage type easily to a string by using Java’s enhanced for loop form and converting the enumeration to an array with the values() call.

That enhanced for loop may be old hat to Java programmers, but coming from a C++ world it is very cool. I am sure it boils down to the same-old-thing I would write in C once it goes through the byte code, but it is much more readable. Why not be able to iterate through the values in an enumeration? It is just intuitive and very usable.

But the really neat thing is how enumerations aren’t just for numbers anymore. I like being able to attach multiple data to the values in the enumeration. So it makes the enum type name a misnomer, but it is really useful. Just about every beginner Java book starts with an example of this a few sections in, and I believe that this is so they can show off this powerful capability to any C programmers delving into the language.

Well, I’m convinced. Programmer jealousy is a wonderful thing…maybe this will show up in C++ sometime? Strongly typed enumerations are not enough!

Cooking with Grease

I always thought that using the cookbook method was an odd approach to programming. The idea is to abstract some of the difficult things to do at the low level so you can get on with the making instead of reinventing string stuff for the tenth time or whatever. This is done on grand scale with things like DirectX and OpenGL or even more intensely with something like BlitzBasic. Who wants to deal with low-level graphics programming when you could be making your games?

The main drawback of programming like that to me was that you can end up not understanding anything about the topic you used the cookbook for. It makes weird problems you inevitably encounter nearly impossible to debug. But for some things, you can’t avoid that; I certainly don’t understand how OpenGL triangle strips end up making nice objects on screen, and I never will. But now I find myself with an entire language apparently devoted to cookbook programming for nearly everything. That is, Java.

At least to me it seems like cookbook programming. You could (and people do) approach C++ the same way, for instance, by using a thing like Boost or wxWidgets (especially cross-platform). But I always found the documentation to be either so hard to understand (in the case of Boost) or so chaotic (in the case of wxWidgets) that I ended up having to really look at the examples and classes I was using and really understand them to use them anyway. Don’t believe me? Take a look at one of the coolest things in Boost:  the multi-index container. Awesome piece of code, but it is hard to get the hang of. The examples don’t cover the things it is really useful for; the examples are mostly for things that other lists already do, or too simple. So you have to dive deep in the tests and the source code to find out what is going on. That’s not really cookbook-style to me; it just saves me the trouble of actually writing the code, but I still have to know what’s going on.

Well, Java may not have a multi-index going for it, but it has a ton of stuff. And the kicker is, it is basically all built in, especially if you use Eclipse (see red squiggly, hover over, import the missing bits). One of the big hurdles to Boost or any other C++ library is shoehorning it into your project and build workflows. It can be a real pain to do. Java is just…there. And Boost/wxWidgets/Poco/etc. are all missing big chunks because trying to do academic things mixed with GUI seems to be a no-no. This is compared to Java, which is a pretty complete solution.

So by having a ton of stuff, you can Google even weird problems and the solution is there. Just cut and paste, and you are cooking up a new program. This is definitely a good thing, just like Boost is; no need to reinvent the wheel. But I feel like I am missing out on the nuts and bolts of Java, which I guess is the idea. It is worrying, because I feel a bit like I am working without a net.

That is the big drawback to me so far: when something doesn’t function as expected or is just too limited. I always had the opportunity to dive into the other open source libraries if I needed to see exactly what weirdness is going on or see the lack of functionality, and often I could fix the problem and submit the bug or modification to the project for a change. Bureaucracy aside, the fix or enhancement would eventually get in, but I could patch things on my side for our next release (I only ever used licenses that allowed that for obvious reasons) and everything was good to go.

I don’t know of a parallel in Java. If something doesn’t do what I need, then I don’t have a way to add in a modification that will do it, and bugs mean you just have to wait. Today’s issue for me is how the java.awt.Color object works.

Basically, it lacks the ability to multiply the color contained therein by either a scale factor or another color. To do that you have to release the previous instance and make a new one, which in my case could mean a lot of references tossed to the garbage collector, and it is slow way to do such a simple thing for thousands of color nodes anyway. That means the object is useless for things like color shifted bitmaps or lightening/darkening the color. There is a function to brighten or darken the bitmap, but it is by a fixed factor; you can’t specify the scaling yourself.

This is a nuisance, because what I would like to do is declare my own bitmaps that are arrays of Color objects, and then write their RGBA values directly to a BufferedImage and then blit said image to the screen. Instead, I either have to use arrays of integers for my bitmaps or make my own flavor of Color and use that.

Well, I chose the second path, and for my own purposes named it FastColor. It didn’t take long to write it, but it seems inelegant. Java gives so much, but when you are out of luck the improvisation feels tacked on. You don’t have a net with Java, and I don’t know where I will go if something doesn’t exist, and I can’t find a way around it. I always had the option to rewrite things at low levels if I needed to. It makes me want to rein in my ambitions when working with Java. I suppose that working low level in C/C++ is what the JNI is for…but I usually only need to tweak something, not rewrite the whole thing!

Oh, and I made good chili this weekend. The analog type. From a real cookbook.

My Old Friend fdebug

I suppose all programmers have code that they have carried with them for a long time. I have a bit of code that I have been developing for decades, from nearly the first programs I wrote.

I don’t remember where I got the idea to make this kind of library, but I remember when things started to look like the current version. I ordered a program called OpenDoors from a programmer named Brian Pirie in Canada. I hope that’s his name…this was a long time ago.  To wit, the purpose of OpenDoors was to abstract the modem/BBS interface so you could write software to work with old school bulletin boards. Definitely pre-everyone-has-it Internet.

Anyway, to work with OpenDoors I couldn’t really have much debugging output on screen, since I had it filled up with all of my awesome ASCII art.  So I took some code that I had been using while learning Pascal and C that wrote printf-formatted text to files for debugging and adapted it to OpenDoors.  Now I had code that would write to a file given print statements with a macro called dpr(), and it would even transmit that over the modem! An even nicer touch I had was that this macro was only a print statement if I had a #define DEBUG in the file it appeared in.  Otherwise, it was a C-style line comment. This meant that I could completely remove the printing from the runtime version of the code by taking out #define DEBUG in whatever files I wanted.

Over the years, this facility evolved, mostly in my professional applications in my old job. I added capitalized versions that would print regardless of the #defines around, versions that would print out the line number, file, and time, and a version that had logging levels (plus bunches of other neat features). The log levels were nice to have too; you could take out the printing at only the cost of an if comparison to the current log level.

But all of these previous versions started as Pascal, then evolved to C, and eventually evolved to use C++.  I spent a lot of today making a Java version.  Unfortunately Java doesn’t really have conditional compiling as far as I can tell, but I want to find a way to easily take the printing out (I have some ideas).  I always thought of that as the nicest feature of the fdebug library.  For Java I think I will have to be satisfied with log levels.

I went ahead and named the package project Logbook, but I didn’t really want to. I always wanted to keep the name of the library fdebug (that’s file debugger, btw) but for various reasons we changed it to logbook later. What a boring name…no abbreviations or hidden meanings or anything.

Java has a lot of conventions about capitalization/camel case, and I thought Fdebug looked silly.  So Logbook it is. Oh, fdebug, we hardly knew ye.

I should mention that I basically recreated Logbook from scratch in Java in about 2 hours. I was a little ashamed, or scared maybe? Eclipse can write a lot of code for you, which seems to work a lot better than the last time I did anything significant with Eclipse. It is really awesome to have the compiler warn you that you forgot to handle a particular exception, but to make the Twinkie completely fried in luscious batter the editor will let you surround the offending area with a try/catch in one action.  That’s neat, and it works better than something like Visual Assist X.  That’s a great program, but C++ keeps it from doing the awesome things that Eclipse can do when you are using it for Java.

Completely off topic:  I have never loaded up a game that is so playable, and yet so rife with bugs as Dead Island. Generally when a game has this many problems there are intrinsic flaws in the design that make it all around a stinky affair. But Dead Island is fun, and it is double, triple, or quadruple fun in multiplayer. If you can get the program working…and then if it is working, there are lots of little weirdnesses and problems that can be frustrating.  I am glad people have kept with it, because the game is a really cool thing and the more people keep playing it the more fixes the developers will upload. I just hope that the rage out there keeps simmering until they can fix the game. But there are too many good things coming out in the next few months for me to keep my hope alive.

Whitespace, Shmitespace

I finally have my Sirius radio installed! I have an XM already installed here, and it is aimed perfectly; I get tons of satellite signal.  However, aiming the Sirius was a little problematic. The instructions say the satellite is in the northern midwest, so if you aim at North Dakota from wherever you are, your antenna has tons of aspect with the satellite transmitter. For whatever reason that method is not working. I wonder if since Sirius and XM merged things are working a bit differently?  I would think that I would know about any new satellites, but I can’t explain why my current aiming gets any signal at all.  My guess is that all of the houses around are causing problems, but I get a good view with the XM. Radio engineering is complicated.

Most of today was spent learning how Java handles XML.  It’s like every other XML parser out there (it is basically just Xerces, I think), and at this point it is old hat.  But I still need to write my little playground tests to see just how things work. Inevitably, there is a wrinkle!

For some reason the Java (I am using Java SE 1.7) XML Text class does not correctly report when a node is only whitespace.  I don’t understand the purists who insist that including all of the tabs, newlines, returns, and spaces should be there for all cases.  They are pretty much useless if you are just trying to grab data.  I understand needing them if you want to replicate a document or something, but every parser should be able to discard those nodes during parsing so that it reduces the amount of checking for these useless pieces when you are actually doing something with the XML.

I may be doing something wrong, but from looking at the docs I think everything is correct. In fact, for the DocumentBuilderFactory I set setIgnoringElementContentWhitespace to true and everything still had the various whitespace thingies.  From looking around the web it seems that this is a regression in Java SE 1.6, but I figured it would be fixed in 1.7? Guess not.

So I added a method to my parser class (as many people said had to be done):

/**
* Return whether a text node is whitespace only or not
* @param tn The text node to check
* @return true if the node is whitespace only, false if it has content other than whitespace
*/
public boolean IsNodeWhitespaceOnly(Text tn)
{
    // when it starts working again, return isElementContentWhitespace
    // now, have to iterate through the content

    String value=tn.getTextContent();

    for(int i=0;i<value.length();++i)
        {
            switch(value.charAt(i))
            {
            case '\n':
            case '\r':
            case '\t':
            case ' ':
                break;

            default:
                // non-whitespace found
                return false;
            }
        }

    // only whitespace found

    return true;
}

This works fine for testing Text nodes, but it is a bummer to have to incur this overhead after it could be easily set when initially parsing the data.  I hate to stay focused on this much more, but I have to rule out my Java ignorance. I need to make myself believe that Oracle is lazy and didn’t fix the bug in 1.7.  Maybe I can make a tape repeating that while I sleep.

Anyway, with the basics of XML figured out, next is writing the parser that stores the maze XML data in Maze class data structures.  Things are heating up!

Hierarchy of Dunces

XML is a powerful tool.  Having the ability to store data in a text file using a method that resembles the way you would do the same thing with data structures and containers in code is a big deal.

Or, at least I see it that way.  Before I heard of XML, every implementation of data storage or retrieval involved me writing up some code (hopefully cutting and pasting from some other similar program) and writing binary to the data files.  This inevitably includes writing some sort of viewer program, which takes even more time.  I am not one of those people who can look at a hex dump and see the blondes in the Matrix.

With XML technology, your text editor is your viewer.  And it’s quite simple to think in terms of XML.  I can visualize the database I need and the XML layout and support code is nearly already written in my mind, since the technique lends itself to hierarchies of data. Hierarchical databases are nearly all I use anymore, and that makes me wonder.

I tend to fall in that group of people who finds a new toy and proceeds to try to solve every problem with it.  Just like the old Maslow’s Hammer: “if all you have is a hammer, everything looks like a nail.”  I usually have lots of tools in addition to that hammer, but I use the hammer anyway.  Because it’s new, and it rules.

I think I am falling into that trap with XML.  I can’t really remember what I did for database design before I got the hang of XML.  Now everything I come up with is a big top node that contains a series of nodes that themselves have a bunch of nodes, ad nauseam. So the software that parses that collection of nodes looks just like the XML:  a map of the first tier, and then that tier is whatever container fits it best until I get to the bottom of the DOM tree and find that nugget of data I am looking for.

Are relational databases so far behind me? I think that I may need to step back sometime and think if XML is a good solution for everything (or perhaps anything).  I am probably permanently infected; as soon as I typed relational database I tried to think how I would write XML that could describe a relational database.  But those tend to be better stored in flat files, and I know a relational database can be described in XML, but relational databases are flat.  Describing a relational database with XML is like putting tighty whities on a horse.  Horses don’t need underwear, don’t like underwear, and they really stretch out the leg holes. How about a nice saddle?

The problem I am tackling is like this:

A Maze consists of Floors that each contain one or more Pieces, each of which consists of one or more Chunks, where a Chunk has various numbers of Rooms, Corridors, and Vaults.  So I lay this out mentally in XML like:

<maze>
    <floor>
         <piece>
             <chunk>
                 <corridor/>
                 <room/>
                 <vault/>
             </chunk>
         </piece>
     </floor>
</maze>

It seems logical, and I know what attributes I need for each type of element, what data structures I would parse those XML elements and attributes into, and what containers would give me the fastest search times for each element type given how I will be using the data.

But I worry when I see a hierarchy in my mind these days.  Am I just whacking the problem with my hammer?  Maybe there is a crowbar around here somewhere…

Global Ignore All The Files!

One thing about source control is that you can get a lot of cruft in your repository (sounds naughty).  Since I am (at least as far as using it for something important in a professional capacity) new to Subversion, I had to deal with that problem today.

A couple of deleted repositories later and I think I have it all tuned.  I found a nice page on StackOverflow that had a good list of file globs to add to Subversion’s global-ignore configuration item.  I had to add a couple of extensions that I have been using for years in my various endeavors (*.out as an example) and now I think my configuration covers C, C++, C# and Java’s worthless effluvia.

Well, the Java case is a bit sketchy.  It seems that ignoring the bin directory pretty much leaves Subversion to get only meaningful Java stuff during an import or a commit. Subversion still indulged in some weirdness though (probably because I did something weird).  I will have to keep an eye out for junkfiles.

I have experience with Visual SourceSafe and ClearCase in a work environment, and luckily I didn’t have to do much with their actual administration parts.  After a while you kind of get how versioning systems are supposed to work, and I think that starting with something as dead simple as SourceSafe and then moving to ClearCase was a good thing.  ClearCase is high octane and would have been too much for us as a team to deal with on day one in the blessed land of source control.  But having been in the ring with ClearCase (and lost many metaphorical battles) going to Subversion now has been relatively painless.

My favorite thing about Subversion is that it has encouraged me to use branches as they are intended, something that is a little obscured in ClearCase (at least the way we used it). I hope I have the comparisons correct:  a VOB is like a repository, and a view is like a working copy.  So working in a branch in Subversion is like having a view that you will eventually discard or merge into the main path.  We actively did not keep a lot of views around because of server storage space and the pain it took to make them (even though I pretty much used snapshots myself rather than dynamic views, which is nearly the same as a working copy with Subversion, except for the annoying read-only files).  With Subversion all of this is extremely streamlined and seemingly more integrated into Subversion’s design.  Getting a branch is a checkout away.

I guess ClearCase is meant for different (perhaps insane) things developers attempt, which is why dynamic views are the real shiny nubbins for that product.

So even though it is just me developing things for now, I am going to get into the habit of working in branches and merging to trunk like a good boy.  I always just worked in trunk during previous forays into Subversion, but now it is best practices, best practices, definitely best practices.  A little luck and I may need those practices.

I am rustier in Java than I thought, so I spent a little time going over some of the weirder parts of the language.  One of my favorite occasions this year was a lazy Saturday and Sunday playing Crawl with a video stream of Notch writing his Ludum Dare entry Prelude of the Chambered on the other monitor.  Yes, that is what passes for fun for programmers.

It was (nerdily) cool watching an expert rush through the development of a pretty impressive game for the 48 hours (or so) it took to write.  The most interesting thing was some of the weird stuff I saw him do, like this little bit of magic:

public static Sound loadSound(String fileName) {
    Sound sound = new Sound();
    try {
        AudioInputStream ais = AudioSystem.getAudioInputStream(Sound.class.getResource(fileName));
        Clip clip = AudioSystem.getClip();
        clip.open(ais);
        sound.clip = clip;
    } catch (Exception e) {
        System.out.println(e);
    }
    return sound;
}

private Clip clip;

public void play() {
    try {
        if (clip != null) {
            new Thread() {
                public void run() {
                    synchronized (clip) {
                        clip.stop();
                        clip.setFramePosition(0);
                        clip.start();
                        }
                    }
                }.start();
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

This is part of his Sound object that plays the various effects for in-game events.  I hadn’t done anything with sound in Java before, and while this is simple, doing the same in C++ sure takes a lot more lines.  At least in Win32; creating a bunch of events and mutexes to synchronize a thread (and writing the thread code, not to mention the actual sound library stuff) makes for a lot more code.  The bit that Notch wrote here is pretty compact.  I watched him write this in live detail and the first few tries didn’t work, so I don’t know if he had the general idea in his mind (e.g., the sound routines from Minecraft!) and he hacked away at it or if he peeked at some old code to get a good method.  Is this real code from somewhere else? If this is all Java production-level code needs to do at-least-usable sound effects, I think that’s neat.

By the way, the most painful thing about watching Notch work was how there are no comments.  I know he was on a nuttily tight schedule, but he went fast enough that it was a little too easy for him to leave commenting behind.  I shouldn’t count this as a good example of his work, even though I can’t help it; I hope Minecraft is well commented and maintainable.  I have spent the last five years beating commenting habits into myself, so it is compulsive at this point (compulsive is right…I am still terrible at it, and I make some of the most worthless comments you can imagine, but I can’t help it.  It’s either this way or no way!).  I wanted to at least write something about what he was doing for the above routines.  Best practices, definitely best practices.

Another aside:  I am very happy with Eclipse’s current incarnations nowadays.  It’s been a while for me and Eclipse, and the maintainers have made it into an impressive development environment.  As is obvious, it really shines when writing Java.  Most of my previous Java work has been a DevStudio→javac→java-type workflow, so it was clunky with no real debugging (just messages to the console, really).  Now that I am taking on a much larger scale Java project, I am really grateful that something like Eclipse exists.  But I am sure I will find more to complain about with Eclipse in the coming months (I sure miss virtual space!).