21 Mar

A Tiny XML Parser

While looking for a convenient way for storing some level data for a game I’m working on (more of that later), I came across this small C++ XML parser. Originally, I wanted to stay away from XML or other formats as they are overkill for the simple game format. However, this parser is both small (the Xerces parser is over 50 megabytes!) and easy to use.

In case you can’t find a download link on the project page: you have to email the author to receive the library (the page promises a direct download link in a few weeks). I think that’s actually quite nice, considering people who write free software rarely get the praise and attention they deserve.

18 Mar

More generated game content

Shmup Pixel Craft Generator

Shmup Pixel Craft Generator is another random sprite generator that is quite similar to Richard’s Evolving Sprite Tool from the last installment. SPCG too gives a sheet of sprites that you can pick from. The sprites make me think of Xevious, they have the feel of ancient space ships.

The author mentions Dave Bollinger’s Pixel Space Ships as his inspiration. Bollinger’s Pixel Robots was featured in the last installment as well.

Explosion generators

There are very many products for generating various effects by using a particle system. However, many of those products are very expensive as they are aimed at film and game industry instead of a hobbyist. Here are some alternatives.

ExGen is a commercial product but with a much nicer price (less than your average game). It is very feature rich, has a nice GUI and even exports as AVI.

The Explosion Graphics Generator or EGG is a very customizable particle system that uses a scripting language. It is free.

Explogen is similar to ExGen only that it is not as feature rich. It’s still worth checking out as it is free.

Positech Games has a free, unnamed generator as well. The page includes sheets of sprites so you don’t even have to download the software.

This blog writes about lhfire, a tool for generating particle effects for a Quake mod. I haven’t tested this myself but it should be good and also free.

09 Mar

Dev Stories from the Past

A bit of nostalgy can never hurt anyone. To me, weird tales about old software are a source of inspiration — they are essentially war stories for nerds. Here are some cool software stories from the past told by the developers themselves.

Donkey Kong and Me

The aptly named DadHacker writes about his past at Atari Corp. and more specifically about his experiences on porting the Donkey Kong coin-op on Atari. Includes gory descriptions of 8-bit graphics modes.

… most of the game sources I saw in the consumer division were crap, just absolute garbage: almost no comments, no insight as to what was going on, just pages of LDA/STA/ADD and alphabet soup, and maybe the occasional label that was meaningful. But otherwise, totally unmaintainable code.

Sounds familiar…

Tim Follin Interview about Video Game Music

Here is a great video interview with Tim Follin, a composer of game music on the Commodore 64, Atari ST and many other platforms. Wikipedia has a surprisingly extensive biography but the below video has the same information told much better.

Be sure to watch the second part as well.

Macintosh Stories

The development of the Apple Macintosh is the source of many mythical stories. This site dedicated to Macintosh folklore contains probably most of them. I like the fact there is a lot of stories about both the people and the inner workings of the Mac.

Llamasoft

Jeff Minter is a legendary character with the distinctive style of his games. Personally, Minter is in my all time Top 5. One of my first memories about computers is not understanding Colourspace at all back when I was about 8. Below is a really interesting and funny presentation by the man itself including game footage from his old games (I think the presentation is promotion for his latest effort, Space Giraffe). He has written an extensive history of his company Llamasoft.

An Interview with Rob Northen

Rob Northen was responsible for copy protecting many games on the Amiga and Atari ST platforms. Considering publisher giants like Ocean, Microprose and US Gold used his services, I’d say a majority of games used a version of his protection. This interview gives some insight into how he came up with his software Copylock and how it worked.

09 Mar

Pointers? Pointers.

This is my attempt at demystifying pointers, a very useful concept available in C, C++ and other programming languages that do not lack balls. Java and PHP have references, a similar feature (C++ also has this) but pointers pretty much are the same thing and for some reason pointers seem ancient, complex or downright frightening to some (or, most).

Why I’m writing this is because I feel a lot of tutorials do not explain pointers as well as I’d like them to explain, considering they are useful and not some archaic curiosity from the 1970s. I originally expressed the concern over here. This tutorial, or introduction, is aimed at C and C++ people because Java doesn’t have pointers and because other languages are generally for wussies.

Note: This is a living tutorial, i.e. you can suggest things and I’ll make it better. I’m all for a good pointer tutorial.

What are pointers?

At first, let’s think about computers. A very basic computer has memory for the data and the code. In a programming language, a variable refers to a location in the memory. The location, referred by address, is something like a page number in a book. At the very least, a programming language automatically assigns these memory locations and acts as a directory for the book, allowing you to refer to the locations by the variable name instead of the page number.

In a programming language, pointer is a variable that contains the address. Simple as that. In most cases, a pointer contains an address of another variable. The pointer can then be used to read or change the contents of the referred variable.

How do I use pointers?

In C, using pointers is easy. To make a variable as a pointer to another variable of type A, you define the pointer to be of type A*.

A variable=42;
A* pointer_to_a_variable=&variable;
*pointer_to_a_variable=0;

& can be read as “address of”. * can be read as “contents of”. Try reading the above example like that and you should figure out what it does.

Here is the answer:

A variable=42;
A* pointer_to_a_variable=&variable; // a pointer to the above variable
*pointer_to_a_variable=0; // the contents of the variable referred are zeroed

When should I use pointers?

A pointer can be very useful in a variety of situations. The simplest use would be something like passing a reference to a variable as a function parameter and the function then changes the variable. This is impossible without either references or pointers. At the very far end of the same spectrum, pointers are the only way to implement polymorphism in C++, which is an important and useful concept in object oriented programming.

Using pointers is natural when in a function you need to use the contents of an object given as a parameter and change them. If you don’t pass a pointer or a reference to the object, you will only change the data in the copied object. For example, see the C++ function below.

void func(Object obj) {
  obj.data=0;
}

This bit of code would be quite useless, considering the object obj is discarded after the function exits. The data member of the object passed as a parameter will not be modified because the object is duplicated for use in the function. The below function does the same but with the exact same object that was passed as a parameter.

void func(Object& obj) {
  obj.data=0;
}

Now the object is not duplicated but obj is a direct reference to the original object. data will be zeroed. Below is yet another version of the same functionality, this time with a pointer.

void func(Object* obj) {
  obj->data=0;
}

This does the same thing, only that obj is a pointer to the object. The above examples show how pointers indeed can be avoided by using references or other more modern concepts (and in some languages you are forced do it with references).

However, in C++, the above example (the one with the pointer) would also allow you to pass an object as a parameter that was inherited from the class Object. By using C++ references this is not possible. Now, if the function called a member function, it would call different bits of code depending on the object! This can be a very, very powerful and nice feature. And you can’t do it without pointers (in C++).

Show me more examples.

In C and C++, the line between arrays and pointers is a bit blurred. For example, you can use a pointer as an array, in that you can use an index with an array and a pointer. Below is an example:

int a[2]={123,456};
int* b=a;
a[0]=0;
b[1]=0;

The array a is now full of zeros. Note that b only refers to the array, it doesn’t have its own data. Also note that specifying an index eliminates the asterisk in front of b. This is because in effect b[1] is the same as *(b+1).

Continuing from the above:

int a[2]={123,456};
int* b=a;
a[0]=0;
b=b+1;
*b=0;

This also zeros the array, because the pointer b is incremented. A pointer is simply a variable with an address and you of course can change that value. This is called pointer arithmetic. It can be very helpful and actually will often allow faster code: using an index needs a multiplication while smartly incrementing pointers only needs an addition (your mileage may vary — compilers are getting better and better). See the below example:

int a[100];
int i;
for (i=0;i<100;i++) a[i]=0;

The below modification should be marginally faster.

int a[100];
int *ptr=a;
int i;
for (i=0;i<100;i++) { *ptr=0; ++ptr; }

This is because in effect, the below example only increments the pointer while the above calculates the address using index.

Note: Many younger coders suffer of premature optimization. This is not a permanent condition and will reduce with some experience. The above is not a good place to optimize (what computer can’t do that fast enough for 100 times?) but do keep that trick in mind. At the very least, it can make code a bit shorter.

Afterword

This is it for now, I can’t think of anything more about basic pointer use. Honestly, it is a bit hard to imagine things from the perspective of an absolute beginner. Any ideas are welcome.

01 Mar

Use Macross to automate common WordPress post content

Here’s a macro plugin for WordPress I made. It features useful features for advertising, e.g. limiting the maximum amount of displayed ads.

macro view

What are macros?

A macro is a bit of text inside a post that, when displayed, will be substituted with other text. It simplifies editing of post and also provides a convenient way to insert repeating content.

Macros are useful for various uses:

  • Adding common content shared by multiple posts/pages
    • Post headers, footers and navigation between a series of posts
    • License disclaimers
    • Advertising inside post content (much more effective than leaving the ads outside the content)
  • Simplifying complex layout by hiding unnecessary formatting, e.g. adding an image with a caption using only a short macro
  • Quickly changing information on multiple pages, e.g. disclaimers, important notifications

Features

  • Configurable via the WordPress control panel system
  • Automatically limits the number of injected macros (useful for e.g. Adsense whose TOS states you can only have three instances per page)
  • Can be extensively controlled depending of page type (archive page, home page, single post etc.)

How it works

The macros are used by inserting text inside HTML comments when writing the post. Below is an example of a macro:

<!--adsense_inside_post-->

The macro looks like the above code when editing the post but when viewing the post (or page) it will be substituted with a Google Adsense advert (given that you have defined the macro).

Parameters can be used inside macros as follows. The first example is the text inside a post and the second is the macro defined on the Macross options page (with the name “image_with_caption”).

<--image_with_caption "image.jpg" "A photo I took last summer"-->
<p><img src="$1" alt="$2" /><br/>$2</p>

This macro will insert the text “A photo I took last summer” as the alt attribute for the tag and add it as a caption below the image.

You can use macros in a page template by using the expand_macro($name,$echo=true) function. This will also increment the counters. Here is the above example reinterpreted using expand_macro:

<?php expand_macro('image "image.jpg" "A photo I took last summer"'); ?>

By cleverly using macro priority (a lower priority macro gets injected first) you can use macros inside other macros. In combination with macro display options (i.e. if the macro is enabled for content, excerpts or feeds) you can use the same macro to expand into different text depending on where it is used.

Installation and configuration

Unzip the contents of macross.zip in your WordPress plugin directory and activate the plugin from Options > Plugins. Go to Options > Macross to create a macros. Counters work so that each successive macro injection increments the counter. If any of the specified counters for a macro exceeds the maximum value, the macro simply will not epand (also, none of the counters will be incremented).

The checkboxes next to a macro specify on which pages the macro will work. The first colum (Hook) tells Macross whether to expand the macro when actions the_content() or the_excerpt() is called. The third option enables the macro for the expand_macro() function. The rest of the checkboxes specify a page type, as returned by is_page(), is_single() and other conditional WordPress tags.

Download

Yes, the following is done with a macro, too. ;)

Links

  • Another macro plugin Originally, I wanted to use this, but found it cumbersome to edit macros. Also, it doesn’t have a counter/limiter system