John Ratcliff's Code Suppository

A place where I insert my code into the anus of the Internet.

 

Saturday, April 25, 2009

Iphone development framework



This is a follow up to my last post. This past week I did, in fact, create a little framework that let's me develop for the Iphone primarily using Visual Studio under windows. The process went very smoothly and the only difficulty, besides learning the Macintosh and Xcode, was that it was the first time I have programmed OpenGL.

This framework is really just a starting point. It has kind of a minimal implementation I needed to get my isometric game level to show up on the screen.

Obviously I cannot make my game open source, since somebody would just steal it and upload it as their own to the App store, but I do plan to make the framework itself open source. If I can get more developers to contribute to it, then it could become something quite useful.

Even if no one else bothers to contribute to it, I am still proud to release it as a proof that this is a valid approach to developing for the Iphone.

The Google Code location is here.

If you are comfortable with SVN, you can just sync anonymously to this repository:

http://isogame-iphone.googlecode.com/svn/trunk/

If you aren't in to the whole SVN thing, you can just download the directory all zipped up from this location.

http://code.google.com/p/isogame-iphone/downloads/list

To check it out, under Windows, go to the compiler/vc8 directory and load 'WorldViewer.sln'

It should build fine, if you are using 2008, just let it autoconvert the solution and project files. Also you can disable any hooks to source control that might pop up.

Once you build and run the project, hit '1' through '4' on the keyboard to load several test isometric game levels.

Use 'W', 'A', 'S', 'D', to scroll around the map and the SPACEBAR will toggle between landscape and portrait.

Meanwhile, on your Macintosh, just go to the directory /app/WorldViewer and load the Xcode project located there.

Build and run it on your simulator and you will see the same exact application, using all of the same source code, running.

The bulk of the interface that comprises the framework is in two header files.

/include/IphoneGraphics/IphoneGraphics.h
/include/IphoneGame/IphoneGame.h

The single location that the Iphone or Windows app talks to the framework is exclusively through the pure virtual interface defined in 'IphoneGame.h'

This framework is far from complete, it is only provided here as a 'proof of concept'. If you are interested in using this framework as a starting point for a project and you intend to improve it, let me know and I will see about adding your as a member of the Google Code project.

Monday, April 20, 2009

I need a game level designer



Does anyone know of a young person who would enjoy doing some game level design for fun? I am starting work on the Iphone and I plan to specialize in 2d isometric scrolling games. I am using the level design tool called 'World Creator 2.5' to create my game levels.

If you know of a young person interested in getting a chance to work on a real game, for experience and to improve their resume, please let me know. A familiarity with World Creator 2.5 is a major, major, plus.

Please email me if you are interested in the opportunity.

Am I the only one developing their Iphone apps on the PC?



So, I have only just started programming for the Iphone. I got my MacMini and set it up. I loaded and built a bunch of sample applications and got myself familiar with the development environment.

I'm not really here to say anything all that critical about Xcode or the Iphone development system in particular. The point here is one of familiarity. I'm pretty old school, I mean really, really, old school. Even though I am on Windows XP developing graphics based applications using Visual Studio, I still spend the vast majority of my day in a DOS command prompt running an ancient text editor, called Qedit, that I have been using for over twenty years.

The bottom line is that I am set in my ways, and I see no compelling reason to switch to a new development environment that I am unfamilar with. Where are my macros, hot keys, batch files, etc. etc. ?

So, the very first thing I decided to do when I started programming the Iphone was to write the bulk of the game using C++ and OpenGL on my PC.; with just a tiny bit of conditional compilation, I can run the same code on both systems.

In addition to getting fast iteration cycles and being able to use tools I am familar with, there are some other advantages.

The Iphone game I am working on is going to take advantage of the Blue-Tooth mulitplayer game features that will come out in OS3.0. Dealing with all of the code to synchronize the game multiplayer is going to be quite complex, and I would hate to have to download a copy of the app to two different Iphone/Ipod touches just to test and debug it.

On the PC I simply fire up two copies of the game, in two copies of visual studio. I have the apps talk to each other using some kind of simple interprocess communication, and then I can debug all of the multiplayer code very rapidly.

Could I do this on a Mac? Maybe, but it would take me a long time to figure it out.

This seems such a natural way to develop code, I'm suprised more people aren't doing it as well. Isolate your OS specific code around some wrappers, write the entire application in C++ with just the absolute fewest minimal hooks from Objective C.

Needless to say, I will be making this entire framework open source as it progresses, and I'm curious to see if anyone else finds it useful.

Does this make sense to anyone else?

Saturday, April 18, 2009

Texture Packing : A code snippet to compute a texture atlas



This source is now officially located on Google Code here.

If I find time I will revise it to have a full sample application that loads a bunch of PNG/JPegs and packs them.

------------------------

I am providing a code snippet that computes a texture atlas. This code snippet does not do any image processing, it merely figures out an efficient packing of rectangles into a single larger rectangle. It is up to the user to copy the bitmaps themselves.

One feature of the algorithm is that it will rotate source rectangles by 90 degrees if it makes them pack more efficiently. Because of this you will need to be prepared to rotate your source U/V co-ordinates to account for this.

This code snippet will optionally return an enclosing rectangle that is a power of two multiple. Also it will, optionally, add a one pixel border around each texture if specified.

I wrote this code snippets so that I can combine a bunch of tiled textures into a single texture for an Iphone game I am working on. It seems to work, but I have no guarantees that it is optimal or robust. If you use it, and improve it, please let me know.

Here is how you use it. Simply include the header file 'TexturePacker.h' and add the source file 'TexturePacker.cpp' to your project. This code has no external dependencies.

TexturePacker.h
TexturePacker.cpp

First create an instance of the TexturePacker interface.

TEXTURE_PACKER::TexturePacker *tp = TEXTURE_PACKER::createTexturePacker();
//
// Next inform the system how many textures you want to pack.
//
tp->setTextureCount(10);
//
// Now, add each texture's width and height in sequence 1-10
//
tp->addTexture(wid,hit)
//
// Next you pack them .
//
int unused_area = tp->packTextures(width,height,true,true)
//
The return code is the unused surface area. It also returns the width and height of the rectangle needed to contain all textures.

If you want to force your output texture to be a power of two, set that option to true.
If you want a one pixel border around your textures set that parameter to true.

Finally, to retrieve the results, for each texture 0-(n-1) call 'getTextureLocation'.

This will return the x,y position of the texture in the texture atlas, and the width and height (should be the same as the original)

Now, this is very important, if the call to getTextureLocation returns true, it means that the texture was rotated 90 degrees to make it better fit into the available space. You should rotate your U/V co-ordinates accordingly.


The algorithm for packing textures as as follows:

Step #1 : Find the longest edge and total area of all source textures.

Step #2 : Create a single large rectangle big enough to fit all textures; round up to the nearest power of two if necessary. Add this rectangle as a node in the 'free list'.

Step #3 : Place each texture, first by finding the texture with the largest area and longest edge that has not yet been placed.

Step #4 : .. Look through the free nodes list, and if a free node is exactly the same size, then use it.
Otherwise, find a free node that is furthest down and to the left of the co-ordinate space (growing up and to the right while we stack)
Always insert the node so that the long edge lays down, preventing as much as possible the texture-atlas from growing vertically.
If a placed node has been rotated 90 degrees (width/height swapped) flag it as so.

Step #5 : If the texture we are inserting shares an edge with the free node, then simply shrink the free node down to make up the difference.

Step #6 : If the texture doesn't share any edge then split the rectangle in two, allocating a new free node and adding to the node list.

Step #7 : See if any nodes can be combined back into one; do this until no more rectangles can be collapsed.

Step #8 : Repeat until all textures have been inserted.

Step #9 : Find the maximum height we ended up using and return that as the actual height. Clamp the height to the nearest power of two if needed.

Step #10 : Iterate through the results and copy your textures to a single large texture-atlas.
This code does not do the image copying, that is done by your own application.

If you use this snippet, and especially if you improve it, let me know.

Nvidia PhysX APEX Cloth Integration for GDC 09



Here is a video clip showing the APEX cloth integration I did for GDC09. I currently work at Simutronics on the MMO game engine called HeroEngine. My friends working on the PhysX SDK did a really cool clothing module and I was the first person who got a chance to integrate it into a game engine. I think the results turned out pretty well.