Game Controller Engine

I needed to add some gamepad and joystick support for the new game I am working on, and a quick Google revealed that there isn’t much out there. So I whipped one together in Java using the JNI and put it on Github here.

It’s a real mix of technologies, so I learned a lot. It uses Ant so I can work on both the Java and C code in Eclipse, and it uses regular MinGW to build the 32-bit native library while using mingw-w64 to build the 64-bit target. I couldn’t really get mingw-w64 to make the 32-bit target, so I fell back on the vanilla. I could hack away at it, but I am too lazy.

Eclipse was really snooty about getting things just right for the JDK and which JRE it was using. There is a whole mess with JAVA_HOME and Ant that inspired me to give up and just blast a solution in (e.g., define JAVA_HOME and ignore the built in java.home property in Ant, which I think points to the directory that has the JRE that Eclipse/Ant are using to run, which is useless when you need to use the JDK).

The engine library currently only supports Windows, and I have only really tested it on Windows 7 64-bit. The 32-bit should work too, but I have to find a 32-bit machine to test it on. I think I have a Windows XP box laying around somewhere.

If you get an itch and want to add Linux or Mac support, feel free to do as thou wilt with the Github repository.

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!

Ludum Dare 22

Well, Ludum Dare 22 is a little more than four days away. The anticipation is killing me!

I am not really good under pressure anymore…I tend to think a lot before I do something in my old age. I think that a game-creation contest is good therapy for this. I think that the pressure of creating a game in 48 hours will force me to get rid of my tendency for internalization and get some things done.

I will be streaming live the whole time I am working (yes, there will be sleep and eats and some guitar playing if I get stuck). I am going to try to write up a time plan too, but that presumes I finish up the helper code I need to have ready before the compo starts. It has to be in my dropbox before the Friday deadline.

So watch me here: http://www.twitch.tv/zaironengineer…it is sure to be a spellbinding video experience.  A whole two days of typos and bad doodles and hopefully a “finished” game!

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.

Back Up Your Stuff

I am ultra-paranoid about backups. I basically don’t trust them unless I make them myself, and I honestly don’t believe that they are really good even then. But luckily my silliness ends there; I let the computer test them for ZIP integrity and leave it at that.

But it is hard to stop the “silliness”. Those files are all that programmers have to show for a lot of hard work. I think that other professions have it slightly worse, though. Other creative types can see their work, but usually can’t easily make copies. Although nowadays photographers are pretty much just like programmers as far as backups go…welcome to hell (next up: film directors). At least those other products don’t disappear if a hard drive fails, or some scum walks off with your laptop. I guess it depends on how light your products end up being. Well, files are just a bunch of magnetized sequences on a fragile platter or ink burns in plastic or some other method. Pretty easy to steal and ridiculously fragile.

So, after the incident at Project Zomboid where they were ripped off by burglars, I felt terrible. This is basically one of the horror scenarios my paranoia harangues about, and I can’t imagine how those guys felt. It must have been excruciating.

Way back when I was in charge of such stuff, I made daily backups to another machine, weekly backups to tape cartridges with a monthly rotation. Milestones and releases got a tape cartridge sent to a government bunker somewhere and then way offsite. I basically covered office, local, and regional disasters for our artifacts, because I felt this is what the company would want. It wasn’t much work for something worth a lot of money, but no one told me to do this.

Of course, I was a developer; it is more logical to me for IT or infrastructure guys to take care of backups. In my experience so far it seems that backups only usually go as far as the creators of the project. Some of it is purely an ownership question:  most people don’t care how office mates take care of their stuff as long as it doesn’t impact their island of responsibility. And I also think it’s a kind of miscommunication; you warn bosses and administrators that the codebase needs that protection, but it becomes very low priority mentally because it seems incredible that someone wouldn’t take care of it. And no one really believes a disaster will happen until it does. Unless you are paranoid!

Now being on my own, I get to do all the jobs, and one thing I want to do is make sure I have a good backup system. I want to utilize a wiki and source control, and basically keep everything electronic, and to me that means:

  • Daily automated backups of the wiki and Subversion repositories on the server to a hard drive that is safely protected from fire and water damage.  The automated backup script keeps the last 14 days of backups on the drive.
  • Weekly compressed mirrors of the whole server to an alternating pair of external hard drives kept in fire- and water-proof conditions when not in use.
  • Monthly backups to a pair of DVDs of the wiki and Subversion repositories. One DVD is kept locally and the other is stored offsite in fire- and water-proof conditions.

I could probably do weekly backups with two sets of four DVDs and send one set offsite rather than doing one DVD, but that’s a bit far, even for me. Maybe if things really take off that will become the practice I follow.

Now to make something worth taking these kind of precautions.