Life in the Shade

Okay.  An update.  Yeah.  I haven’t been working as diligently on this project in the last week or so.  Life, as they say, intervened.  Indeed, this past weekend, I was involved in a 24 hour play production.  Five writers, five directors, 15-18 actors, 24 hours.  As a writer, I was tasked with drawing an object, a location, and a first line from three hats and then writing a fifteen-ish minute play between 8pm and 7am.  The next morning, the casts and directors took my script and made art out of it.  It was all immensely gratifying and fun.  Indeed, it was the first time I’d ever seen anything I’ve written performed by actual human beings.  I have performed sketches that I’ve written, but never watched it from the audience.  So it was fun.  Jolly, great fun.  Perhaps I’ll post the script to the play here at some point in the future.

Anyway.  I’ve had a bit of a breakthrough.  I have been wracking my brain to come up with ways to get more than 30 frames per second.  I have a really nice computer; this is no reason that I should be getting any less than 60 frames per second on a rendered 2D world.  Well, it turns out that the Lightweight Java Games Library (LWJGL) is a pretty curious and odd API and not only was I using only the tiniest fraction of its functionality, I was using literally the oldest commands OpenGL had to offer.  I have found a far more efficient way of rendering the map.  The problem is, I have to rewrite the whole thing.  This is fine, of course.  The first test program was just a way of seeing whether the heightmap software worked and whether I could make a map that I could scroll through.  That worked, so, great.  But then I hit the wall.

I have a spritesheet for an avatar (I’ll probably have a post about overcoming the fact that I’m not a pixel artist, or indeed, any artist), but I had no idea how to make it render and animate properly.  Well, yesterday I learned about Vertex Buffer Objects and Vertex Object Arrays.  And my entire life changed.  instead of using just the OpenGL 1.1 commands, I’ve expanded far further, well into OpenGL 3.0.  It has been terrifying.  I had not realized just how much I have to learn yet.  Shaders are a pretty key concept.  If I’m going to be shoving this stuff off to the graphics card to process, I have to use OpenGL shaders.  If I’m going to use OpenGL shaders, I have to learn about…OpenGL shaders.  And so I’m learning GLSL.  And it’s utterly confusing.  But I’m making progress and that’s good and my current goal is to have a world with an avatar by the end of November.  Fingers crossed.

Cirque du fouture de merde.

Breakthrough

Not only was today my birthday, but I got the map demo to function properly. The current code can generate a random heightmap from a seed integer, render it to the screen, and–and–I can scroll around map with arrow keys or by grabbing it with the mouse. For a 96×96 tile map, I get an even 30 frames per second. This is not ideal. I’m not sure how to make it render faster, but honestly, I feel pretty damned good about this. It looks good, it slides well, and I really think it’s a success.

Next task, I think I’m going to try my hand at spriting. I’m going to create a player avatar and attempt to animate him/her. Maybe once he/she is ready, and I have a few other game assets (and a new ground tileset), I’ll tell you all what the core concept of the game will be.

I’m starting to feel hopeful about this.

Alles Gute zum Geburtstag fur mich.

So close, yet so far away.

Would it be trite to say that this is a Sisyphean task? Am I breaking some sort of horrible cliche rule? I don’t care.

Anyway, I’m so close I can taste it. Some problem with my textures mapping to the tile in the world. I’m grabbing too much texture and I want to grab just enough. Sprite sheets be damned.

I’m off to game night.

In the meantime, if someone would be so kind as to explain how to use the following code:

GL11.glMatrixMode(GL11.GL_TEXTURE);
GL11.glLoadIdentity();
GL11.glScalef(1f/10f, 1f/8f, 1f);
GL11.glMatrixMode(GL11.GL_MODELVIEW);

Just leave complete and concise explanations of how the glScalef() function works and you will be my best friend.

arrivederci!

Success!

I once went on a European tour through the university that I went to. The same university where I studied computer science for two years before finishing with a degree in philosophy. The same university that, for whatever reason, gave me a masters degree in English.

On this trip to Europe, we had a lot of downtime on a bus, traveling from one fascinating country to another. On those bus rides, we were allowed to drink beer and be irresponsible. It was the most liberated I have ever been, despite the fact that I was really just riding a rail to its terminus (an airport in Frankfurt that brought us back to America). I spent that time, of course, drinking Swiss beer (the bus driver sold us beer on the bus; he was an Australian expat living in Switzerland). I also spent that time reading.

I read several books that had been on my reading list for quite some time. One of those books was Zen and the Art of Motorcycle Maintenance. It’s actually a deeply flawed book in several respects, but about the first two-thirds of it are very, very good. There’s a lot of very interesting discussion about the scientific method. In it, Robert Pirsig discusses some of the general techniques that a person can use to solve a problem when a motorcycle is not working. One of these is to employ the scientific method. The scientific method is very akin to the process of elimination. Form a hypothesis for what might be causing the problem, and then, test that hypothesis. If that doesn’t work, you know one thing that isn’t the problem.

It also hearkens to Sherlock Holmes: “Once you eliminate the impossible, whatever is left, however improbable, must be the truth.” Not sure if that’s the exact quote.

So, with the code I was using to generate this noise, there were several function calls. One would generate the base noise. One would “perturb” it, creating a wobbly effect. One creates an erosion-like effect. The last one smooths it all out.

I commented all of these out, leaving just the noise function call. The output looked great. It was just cloudy noise (although I still have the problem that my returned values are in the float range of -1 to 1, and I want them to be from 0 to 1; this is why there are dark “rivers” running through it because to work around it, any number that was negative was raised to 0). The point is, there was no line. I brought back the perturb function call. No line. Just a nice, perturbed version of the original output. The bug was not in the perturb function.

Finally, I added the erosion function back in. And there it was. A fine, one-pixel-thick line from the top left to the bottom right corner. The smooth function just widened it and gave it that peculiar bar-like quality. Success! I went back to erode function in the heightmap class and went through it line by line (it’s only about 15 lines or so). And I found that, where I was using nested for() loops, using i and j as my iterators, there was an i that should have been a j. Fixed it. Re-ran the code, outputted it to a bmp, and there was my heightmap. Nothing weird, just nice, coherent noise.

What’s weird is that I didn’t need to know how the code worked. And I still don’t. I saw the error, not fully understanding why it was causing that bar, but fixing it anyway, and eliminating the bug.

It took longer to write this post than it did to fix the bug.

Fantastich!

A New Project

Above is the results of several weeks of work. It has one fatal flaw, however. Allow me to explain.

I am a gamer.  I play video games, board games, role-playing games, and some sports.  My taste in games is very eclectic, but also somewhat mercurial.  I will love a game–obsess even–for a week or two, and then move on to something else.  I have always had a problem with attention span when it comes to games.  I do not believe this is a character defect.  There are simply some absolutely brilliant games that have managed to suck me in for more than 40-60 hours (the flexible benchmark in my mind for a brilliant game), and some that don’t.

The upshot of this is that I have played many, many games.  My Steam library is only about three years old, and yet it’s about 130 games long (some of which I’ve never played, of course).  Through it all, there has always been this little thing in the back of my mind.  This notion that if I had been working on this game, I might have considered doing something differently.  I do not have the slightest notion how difficult it is to make a game.  I can only understand intellectually, by watching the credits roll by at the end of a game, how many people and how much time is involved.  I understand that a big budget game takes many people many, many, many person-hours over several years to complete.  But I don’t actually understand this any more than I understand that the sun is 98 million miles away.

And so, it is with a sort of naive enthusiasm and confidence that I embark on a new personal project.  I have been calling it my “self-education project” with my wife and friends.  For the past month, I have been reading, in true obsessive fashion, everything I can find on Java programming (more on my choice of Java in a moment).  I have been reading articles on game design, AI, terrain generation, pathfinding, data structures, isometric perspectives, sprites, and more.  I’ve written stupid little programs in Java and OpenGL. A blackjack game, a little-ship-that-shoots-at-aliens game, and a few others.

Let me back up. I quit my job last spring. I was teaching at a community college in rural North Dakota. Both my wife and I did. The reason for this is convoluted. The job was fun and rewarding. But the pay was pretty abysmal and we had few friends there and no family. Not to mention, there is nothing to do out there. In the end, the reason we took the chance and quit was simply the fact that the thought of staying for another year (after two years already) was a deeply depressing thought. So we quit.

And we didn’t manage to find brilliant replacement jobs before our summertime paychecks ran out.

This means that I’m back to substitute teaching and my wife (who is far more aggressive than I at job searching) is back to bartending. This is fine for now. However, I need something to work on. And one day, two months ago maybe, I was reading a book in a hammock (this was my indolent summer after quitting a teaching job which pro-rated paychecks throughout the summer–yay!), I got an idea. It was nebulous then, and it continues to be nebulous, but it is an idea.

I haven’t been able to write fiction all summer. There has just been a part of me that refused to do it. This is purely my own lack of self-control, but it is also the fact that I simply do not have a good idea to run with (excuses, excuses). I now have an idea that I want to run with. However, I do not know much about computer programming (or at least I didn’t). I took a few computer science classes in college and learned some basic C++ and even a little Lisp, but that’s it. I knew some syntax, but I did not know “how to program.”

And so I got it in my head that I wanted to do a feasibility study. I wanted to discover just how difficult it would be for an expert-level user (someone who understands at an intuitive level the current conventions about how to use a computer, and currently operates his own, self-constructed Windows box) with almost no real knowledge of actual computer programming to make a video game of real quality. How long would such a project take? What language should I learn? What books/articles should I read? Will I, as I often do, get bored and give up?

So that’s the experiment, and so far so good. It has been two months, give or take, and from here on out, I will make an attempt to regularly post about it here on the Spin.

And so I’ll share a few of my thoughts here. First: Java.

I chose Java because it’s portable. There is some concern that it’s not as fast a language as it could be. This is true. The JRE, from what I understand, makes it a very high level language. Not as high as, say, a scripting language, but certainly not as close to the machine as C++. But machines are faster and stronger these days. Some very sophisticated software is programmed in Java, especially in gaming. Minecraft, for instance, is programmed in Java using the Lightweight Java Games Library (LWJGL), an OpenGL interface for graphics acceleration. I’m not looking to make Minecraft or even come close to its success (Marcus Persson was a professional programmer before he made Minecraft), but I’m interested in using some of the same technology. Knowing how robust and tight Minecraft is, gives me hope that a simple 2D isolinear game will also run well using the same language.

So I started reading a text on Java. This one, to be exact. And it’s…just truly marvelous. And I had some great success with it. I did get impatient before finishing the text (c’est la vie), but I got through the essential stuff. I have a good idea of how to navigate the Java API, I understand variables and pointers, I have a working understanding of how Java handles exceptions, I think I know enough about data structures, recursion, searching, sorting, and all that other good stuff, that I just started looking up some stuff that would be useful for my project.

I’m starting with something really hard. Perlin noise. I want to make a heightmap so I can generate the terrain of an alien world so that everyone who plays the game will play on a different world. This has been a nightmare. I’ve read many articles about Perlin noise and I’ve read lots of forum posts by people who are doing or have done exactly what I am trying to do. People who are all like, “I be learning how 2 program for 2 weeks now, and I ned a good tut on perlin noise.” And that was me, too. I wanted a good tut. I needed it. In my brain. But there isn’t one. At least, not one for someone with my incomplete knowledge base.

Oh sure, there’s a lot of truly great literature. This is something I have struggled with all my career when it comes to mathematics. I was able to construct a pretty solid intuitive understanding of how the math works, but there was a barrier between that intuitive understanding of what it was and how to actually…just…do maths. Same here. I get how Perlin noise works, I just don’t understand how to implement it in java and just make it fucking happen on my monitor.

What I ended up doing was reading every article I could and realizing that, as a math enthusiast, but not a math expert, I was never going to completely understand the algorithms involved in generating any kind of coherent noise. But I persevered. I looked for algorithms Read some pseudocode, found it very difficult to translate to Java, gave up, looked for something else, finally found complete source code in C# for a Perlin noise algorithm with lots of options. This was last night. I stayed up way too late. I am so tired today at work. But I am satisfied that I have finally found it. I generated my first, very rough heightmap last night. And boy…did it feel good. I even managed to write some of the code myself.

And so, the image at the top of the post is the first output that my algorithm pumped out. But what in the hell is up with that slash through it? I have no idea what in the code caused it and I can see the next few weeks will be spent trying to figure out how to get rid of it. Wish me luck.

Ciao.