Thursday, August 14, 2008

Screen size


Well, I'm getting closer! As you can see on the right, things look almost normal!

The biggest problem I ran into so far was the difference in size between the DS screen and the GBA screen. I still need to make better use of the full screen real estate (at least to center small rooms like this one), but the bigger problem was in how the graphics engine deals with the screen size.

See, most scrolling tile games on nintendo's handheld use a small tiled background which wraps around as you move. This background is just big enough to have one row and one column of tiles off the screen at a time. So as the screen scrolls, you redraw the off-screen row or off-screen column (or both), and then scroll it onto the screen. A little tricky, but no problem, really.

The problem comes in that the DS screen width is the same size as the default small wrapping background that I had been using. So I suddenly don't have an offscreen column to draw on. The answer, of course, is to make the background bigger. Which is what I did, but there are some tricks with that. For a 32x32 background, the tile indices are layed out linearly, so for any given tile, the index you want to write to is x + y * 32. But for wider backgrounds, it's different..instead of a big wide background, it's two small background stuck together. Which changes us to the slightly trickier system of:
index = x + y *32;
if (x > 31) index = index + 1024;

Not a big deal directly, but a whole lot harder to use pointer math (which I had been doing) to deal with. So I had to go through and change things to not use any optimized pointer math, and instead, compute it the hard way. (Which is probably best -- as Donald Knuth says, "premature optimization is the root of all evil"). Once that was done, things are pretty close to working!

Of course, there were some other gotchas along the way. For example, I had a horribly hackish way of keeping the foreground and background layers synced, which didn't really work the way I wanted it to for the DS, so I had to do it right this time.

Anyway, the game is actually almost playable now. There are lots of little things to do (enemies don't fade when they get hit, your selected item doesn't show up, the foreground tiles appear behind your character, the outdoor graphics are messed up, scrolling southward is glitchy, etc) and I still need to change things so that the 2nd screen is used for subscreen, minimap, enemy stats, etc. (I also just got around in this build to adding text to the 2nd screen...thus the "hi" at the bottom of the screenshot) But it's moving along faster than I expected! (at this rate, maybe I'll be able to start an iPhone port before the thing is obsolete)

No comments:

Optimization and C

Starting this next game in C, I've known there will be places where C isn't fast enough, and I'd have to drop to assembly to ...