2018-06-17

Professor Sinister And His Marauding Mechanicals

I’ve given up on Chuckie Egg for the GBA. Not because it got too complicated; the opposite in fact. I wanted to play with things the GBA will allow me to try out, like parallax scrolling, chiptunes and pixel art, and remaking a four color Dragon 32 game just wasn’t cutting it. I still adore the game though. I’m keeping the engine but I’m going to do a full reskin with new graphics, music, sound, and possibly new level layouts.

The remake was drifting further and further from the original anyway. The first change I made was to eliminate the flicker. Then I added scrolling for the GBA, and then I replaced the music with XM versions. Why not just modernize the whole thing?

The new game is called “Professor Sinister and his Marauding Mechanicals”. You play Professor Sinister, an evil scientist who created robots to help him destroy the world. Unfortunately his robots have gone wrong and he’s lost control of them. The Chuckie Egg hens have become robots, the eggs have become vacuum tubes, and the chicken feed has become little power packs.

I’ve added parallax scrolling and a little chiptune. As per usual there’s a large list of things left to do, but here’s a video of the state of things so far:

Though the graphics are all original, they draw from a number of different influences. I wanted the robot to look something like a cross between the robot from “A Grand Day Out with Wallace and Gromit” and the robot from “Forbidden Planet”. The palette and background was inspired by Rick Dangerous 2. I referenced the Master System version of “Wonder Boy in Monster Land” when drawing the life indicator. The original sketches for Professor Sinister looked like something from “The Far Side”, but as a 16x16 sprite that doesn’t really come across.

2018-06-06

Chuckie Egg GBA

Here’s a video of something old-but-new I’ve been working on:

It’s Chuckie Egg, but for the GBA.

How did I manage to squeeze the bitmap-mode UI library built for a dual-CPU 66/16MHz DS into a single-CPU 16MHz GBA? With full-screen scrolling?

I didn’t even try.

Extracting A Library

I mentioned before that the games I’ve been writing with SZLib adhere reasonably strictly to the MVC architecture. The game logic forms the “model” portion of that architecture. If done correctly, the game logic could live in an entirely separate library from the code that handles graphics, sound and user input. That’s what I’ve done to get this working: I moved the model to a new “SZChuckie” library and created a new front-end project that uses the GBA’s tile-and-sprite graphics modes.

Splitting out the game library was mostly trivial, but somehow took far longer than I expected. References to bitmaps, sounds and user input handling had all found their way into the game logic. For example, each of the sprite-esque things in the game (eggs, chicken feed, hens, ducks, Harry) stored an array of bitmaps that represented their animations. The view controller could ask for the current bitmap in order to display the correct image in a view. I had to replace the bitmap reference with a more abstract “animation frame” object, which ditched the bitmap data and stored just a size and a frame number. The view controller now stores the bitmaps and uses SZLib’s type introspection to figure out which bitmap to display for any incoming didChangeFrame events emitted by the game object. The class that represents a level had a function for generating a bitmap from its level data which the view controller used as the game’s background; I moved that into a separate file.

Pulling the sounds into a higher layer meant adding more callbacks to the game object’s delegate. The delegate - in this case, the view controller - can respond appropriately and play the correct sounds.

Input was similarly straightforward. I added a new d-pad/button class specific to Chuckie Egg (so there’s a “jump” button instead of “A” and “B” buttons) and pass its state to the game whenever I call its update() function. As a side-effect, this fixed a few places where the game wasn’t responding correctly to some button presses or the analog stick.

The most long-winded changes were purely administrative: renaming all of the files/classes/etc; creating makefiles and build scripts; and testing that nothing was broken.

With all that done I could move onto the GBA port, which of course started with more administrative work. I needed to get three libraries from SZLib building for the GBA: the core library, which includes reference-counted objects, boxed primitives and collections; the geometry library, which includes points, sizes and rectangles; and the hardware abstraction library, which includes abstractions for initializing the hardware, sound, vwaits and hardware division. Those all needed makefiles. I used the makefile from libgba as a template. With the makefiles in place I could add GBA support to the hardware abstraction library, which involved digging through libgba’s headers to find the appropriate function calls (where available) or disabling the functionality (where not). The core and geometry libraries just worked.

That all put the project in a very weird place. Typically when I start a game I get enough of the logic working to start pushing pixels around on the screen, and from there the logic and graphics code evolve in parallel. Somewhere near the end of the project I’ll add sound. However, in this project I just had to link the game library and hook up a few callbacks to get a fully-working and audible, but invisible, version of Chuckie Egg.

The GBA

Programming the GBA isn’t particularly difficult. Well, it shouldn’t be difficult because it’s a very simple platform. It’s a little harder than it needs to be because libgba has no documentation and the canonical tonc guide references constants that don’t match libgba, so I find myself trying to interpret memory addresses rather than looking for the names of those addresses. The command line graphics conversion utility that ships with devKitARM (grit) has a tendency to either get things wrong (it refuses to generate a correct palette for me) or be unhelpfully helpful (some combination of settings causes it to insert a blank tile at the start of a tile map, causing me to spend far too long trying to figure out why I couldn’t fill the screen with the tile I wanted).

Once I really get the hang of the GBA I’ll hide all of its features behind an abstraction library that I can make cross-platform, which will let me debug my code on my development machine. With neither a debugger nor the ability to printf() on the GBA I’m wasting far too much time running C in my head.

The Port

The GBA’s screen is 240x160 pixels, which is about 20% smaller than the Dragon 32’s resolution of 256x192 (this is a remake of the Dragon 32 version). The DS port was very fortunate in that the DS’ resolution is also 256x192, so there was no need to scroll or scale the game to get it to fit. For the GBA port I decided to opt for scrolling as it’s so simple to achieve (just change a couple of registers). It refused to work for a while until I dug up a comment in one of the examples that noted that the registers are write-only. Duh.

I think that the scrolling works really well. The levels aren’t much larger than the GBA’s screen so it’s impossible to get disoriented or lost. It is possible to get into a “leap of faith” situation where a player might jump from the top of the level hoping that there’s something at the bottom to catch them, but it doesn’t make the game less enjoyable (at least, not for me, but then I know these levels backwards so I’m not a great test subject).

The unmodifiable parts of the levels - the floors and ladders - are implemented in a tiled background. Everything else - the hens, duck, eggs, chicken feed and Harry himself - are all sprites. That gives me around 30 sprites per level, which the GBA throws around without breaking a sweat. I’m considering changing the eggs and feed to become tiles within the background if performance becomes a problem.

Sound is powered by MaxMod. The API is slightly different to the DS’ version, but they’re similar enough that making sound work was simple.

I wasn’t particularly worried about performance. From the considerable amount of perf work I’ve done on the DS version I know that the overwhelming amount of work the game does is in the UI libraries that I’m not using.

Disaster Strikes

Everything was progressing well until I hit a massive problem. The game worked perfectly in mGBA and VBA-M, but it refused to do anything but display a green screen on a real GBA. Every time I’ve encountered something like this on the DS it was caused by a memory problem. The DS emulators I use don’t accurately model the behavior of memory on a real device, so problems like allocating the wrong amount of memory, forgetting to initialize variables, and using memory after it’s freed (etc) can crash real hardware but cause no problems under emulation. I set about investigating.

First step: Code inspection. Was I allocating the wrong amount of memory for my objects? That’s a mistake I’ve made repeatedly in SZLib: allocating enough memory for a pointer to a struct rather than the struct itself. Nope. Forgetting to initialize variables? Nope. Leaking memory? Nope.

Next: Run the SDL version through a static analyzer. No problems.

Next: Run with the address sanitizer enabled. No problems.

Maybe the problem is in the GBA front-end code. I #ifdef-ed out enough of the GBA-specific code to get it compiling on my dev machine and ran that through all of the available code/memory checking tools. No problems.

Let’s dig back through the Git history to find a version that did work on the GBA. Rolling back a few days gave me a version that worked. It seemed that I’d subsequently made a change where I stopped writing to an intermediate buffer representing the OAM (object attribute memory; for sprites) and wrote directly to it. Undoing that change didn’t fix the bug.

I started to remove arbitrary pieces of code that caused memory allocation to occur. Sometimes a change would allow the game to run; sometimes a different bug would manifest. It made no sense.

I enabled libgba’s console output and wrapped malloc() so that it would print the address of the allocated memory. No clues there: all of the addresses were safely contained within the correct region of RAM. Then I started printing out the addresses of stack variables. It seemed that I was getting close to the end of the stack, which perhaps made sense. Stack variables are stored in a 32K block of memory (IWRAM) which is immediately adjacent to a 1K block of memory that controls I/O. Overflowing the stack would cause weird behavior. Immediately following the 1K I/O block is a 1K palette block. One of the changes I made to the game caused the palette to start cycling. Aha, that seems likely. But how am I overflowing the stack? And why doesn’t that bug show up in the emulators?

I started up mGBA and took a look at its memory viewer. According to that the game uses a tiny fraction of the 256K and 32K RAM blocks. There’s no way it should be overflowing the stack or encountering problems related to memory shortages.

Perhaps there’s something wrong with the GBA itself. I recently got hold of another backlit GBA which is the device I’ve been testing on. I haven’t used it for playing games; maybe it’s defective? Nope, same problem on another device.

At this point I’d wasted three evenings on this ridiculous problem and, although I’d learned a significant amount about how the GBA’s memory is laid out, I’d made no progress in figuring out the cause of the issue.

I put everything away and thought about the problem some more.

There’s nothing wrong with the majority of the code. It’s running on 4 other platforms without issue. I’ve tested it with every analysis tool at my disposal and can’t find any problems. The code that’s specific to the GBA amounts to less than 1,000 simple lines, and I analyzed most of that too. The fact that the game runs perfectly under emulation suggests it isn’t a library or compiler problem. It isn’t a problem with the GBA hardware because it exhibits the same behavior on two different devices. It isn’t a memory exhaustion problem. So what’s the difference between running the game in an emulator and running it on my GBA?

The flash cart.

Could it be the flash cart?

I’ve got an EZ-Flash IV (one of the older ones that uses mini-SD cards). It runs its own code and patches ROM files in order for them to work correctly. Could the flash cart be breaking the ROM? Fortunately I’ve got a couple of older flash carts lying around. Copy the ROM to my Supercard SD and…

It works. Perfectly.

The EZ-Flash is doing something bad to the ROM, causing it to write to bad memory addresses, causing bad things to happen. There’s nothing wrong with my code. And there was no way I was ever going to debug that problem.

To Do

There’s plenty of work left to do before the game is finished:

  • Add a “Simian Zombie” screen.
  • Add a title/high score screen.
  • Add level/life/score/time/bonus indicators.
  • Show the “Game over” screen when the player dies.
  • Show the score increasing when the player completes a level.
  • Show the “Get ready” screen before each level.

I also want to add an attract mode showing little demos of gameplay that appear after a few seconds idling on the title screen. I’m thinking of using that system to show how to perform various techniques in the game (like grabbing ladders easily, bouncing between ladders, etc). Oh, and then there’s the level editor that I didn’t finish, too, but that makes less sense on the GBA as it doesn’t have a writable file system.

2017-10-15

SZLib 3DS

Just in time for it to be killed off by Nintendo in favour of the Switch (if you could ever find them in the shops; grumble grumble), Hanky Alien and Chuckie Egg now run on the 3DS. Getting the new platform up and running was pretty easy. Input was straightforward: the d-pad required switching to a couple of new functions for reading the button state but otherwise the constants are the same in libctru as those in libnds. Touch somehow just worked, which was surprising.

The hardest part was getting the graphics up and running. Not only does the 3DS introduce yet another set of RGB encodings, but the output of the LCDs is rotated 90 degrees from the VRAM. What’s that? You have a bunch of 2D graphics functions that are all optimized around VRAM being laid out in rows, from left to right? Bwahaha! Now VRAM is organized into columns, from bottom to top.

Though graphics were the trickiest part, the most laborious exercise was checking that the projects continued to build correctly for all 5 of the currently supported platforms (DS, 3DS, PSP, Dreamcast and SDL). I really need to come up with a better build system for the PSP and Dreamcast, and it’d be neat if devKitARM supported makefile names other than “makefile”.

There are a few things still to do:

  • libctru introduces yet another sound API, so there’s no sound yet;
  • SZLib doesn’t cater for the possibility that a device has multiple screens with different dimensions, so the bottom screen is off-center.

Anyhow, here are early 3DS builds, which I’ve tested out on my old 3DS XL and Citra (which is shaping up to be a great homebrew tool):

2017-05-24

Fake DS Lite

I had some problems with devKitARM recently that broke Chuckie Egg and Hanky Alien. Thinking that the problem might be related to some DSi-specific code that got added to libnds, I decided to try out the games on my DS Lite. Unfortunately, the console no longer recognized DS carts. No amount of fiddling would get it to read either of the two carts I tested.

Nuts.

I’d heard that AliExpress - purveyor of fine knock-off Chinese electronics like the GB Boy Colour - had a selection of fake DS Lites on offer, so I went digging. I eventually settled on a cobalt blue DS Lite for the very reasonable price of $40. Given that an original Nintendo console would have cost more than double that back in the day, and given that it’s difficult to find an original, new DS Lite for less than ten times that, it seemed like a chance worth taking.

Here’s what arrived:

Fake DS Lite

Let’s start with the bad first. Side by side with a real DS, the shell is obviously not original. The top case is slightly larger than it should be and bulges outwards slightly along the edge where the clamshell opens. The square black rubber pads that are dotted around the upper screen are fitted imprecisely. The plastic feels cheap, especially when compared to the beautifully glossy Nintendo device. The A, B, X and Y buttons are horribly mushy, and the Start and Select buttons are too small for the holes that they sit in. The cover over the top screen is fitted incorrectly, allowing light from the screen to bleed through its top edge. The screens themselves are set deeper into the casing than on the original. The DS cartridge slot makes a series of nasty griding sounds when a cartridge is inserted, and the cover over the GBA slot is looser than on the original handheld.

In short, the casing is cheap and poorly assembled. But it’s close enough.

Now the good. My original DS Lite came from the factory with a dead pixel; a common problem. Cracked hinges were another, and the hinges on mine cracked not long after I bought it. The clone device doesn’t exhibit either problem (yet). Both screens are perfect and indistinguishable from the original. We’ll see how long the hinge lasts.

Functionally it appears identical to the Nintendo console. When you consider that the K1-GBA still isn’t a perfect clone of the GBA, and that this thing has a GBA embedded within it, that’s stunning. The only explanations that make any sense are that this clone is from a DS factory secretly making extra devices on the side, or that someone stole and replicated the PCBs like the Everdrive clones, or that it’s assembled from spare OEM parts and a third-party case.

It even comes with a box (missing the serial number, of course), manual, soft case and screen protectors.

I haven’t really tried the stylus yet. It’s possible that the touchscreen is terrible. The screen’s texture feels different to the original, though that could just be because it’s new. I haven’t tried the charger yet as the battery is fully charged. The combination of a knock-off charger and a knock-off battery is a little concerning. Other than that, it looks to be a good alternative to either buying a second-hand DS Lite (and possibly ending up with a device that was mistreated) or paying a crazy amount for a new official device.

Oh, and now that I have a second DS Lite, my original console has started recognizing carts again.

2017-04-10

Chuckie Egg 20170410

With egg hunting season almost upon us, it seems appropriate to release a little update for my favorite egg hunting game. Grab it now:

I’d hoped to get a level editor into the next release, but it’s not finished. This version fixes a few bugs and adds a couple of minor new features.

Changelog:

  • Scores are persisted on DS and PSP.
  • PSP analog stick can control Harry.
  • Fixed crash when completing a level if a cheat label is showing.
  • Fixed crash when switching to high score table.
  • Harry dismounts ladders correctly when climbing and jump is pressed.
  • Player does not get points when hens eat grain.
  • Bonus cannot be negative.

I haven’t looked into working with the Dreamcast’s VMU yet.

2016-12-29

Transparency in SZLib

One of the neat features in early Woopsi was screen dimming. The class responsible read each overlapped pixel from the framebuffer, adjusted its brightness, and wrote it back. I eventually had to kill the feature when I changed how the rendering system worked and broke any support for transparency.

Woopsi’s drawing algorithm (essentially a reversed painter’s algorithm) looks like this:

  • Pass in a rect that describes the area to be redrawn;
  • For each layer in the layer hierarchy, from the topmost down:
    • Draw the parts of the layer that intersect with the rect;
    • Subtract the drawn parts from the rect.

Each layer is expected to own all pixels within its rectangular region. To support transparency, there would need to be a way for layers to opt out of subtracting themselves from the redraw rect so that lower layers could draw into the same area. Even if there were a way to achieve that, we’re traversing the hierarchy the wrong way. Transparent views need to render over the top of existing framebuffer data. We need to draw from the bottom up.

The CanvasLayers JavaScript library has a workaround but it’s really inefficient. When setting the library up, it includes the option to support transparency. With that enabled, this is the algorithhm:

  • Pass in a rect that describes the area to be redrawn;
  • For each layer in the layer hierarchy, from the bottommost up:
    • Draw the parts of the layer that intersect the rect.

It’s just the painter’s algorithm. Its awfulness is slightly mitigated by all of the dirty rectangle work that the library does, but we’re still pointlessly drawing a bunch of stuff below opaque layers. We’re also forced to maintain two algorithms to do the same work.

SZLib needed to support transparency in order for overlapping objects in Hanky Alien and Chuckie Egg to work, so I put some thought into the problem and arrived at the obvious (with hindsight) solution: a two-pass algorithm. In pass 1, we iterate over the layers from top-down to figure out which layer will be responsible for drawing what. In pass 2, we iterate over pass 1’s results from bottom-up and perform the drawing.

Pass 1 looks like this:

  • Pass in a rect that describes the area to be redrawn;
  • Create an array of layer/rect tuples;
  • For each layer in the layer hierarchy, from the topmost down:
    • Find the intersection of the layer with the rect;
    • Create a tuple containing the intersection and the layer;
    • If the layer is opaque, remove the intersection from the rect.

Note that we don’t consider an intersection to be “used” if the layer is transparent, which allows lower layers to create their own tuple in subsequent iterations.

In pass 2, we iterate backwards over the tuple array and redraw each tuple. Pass 1 gave us an array ordered by z-index from high to low; by iterating backwards we therefore redraw from the bottom up. Lower opaque layers are drawn before higher transparent layers.

This new algorithm supports transparency with the minimum amount of redundant drawing.

2016-11-19

Chuckie Egg Releases

Just in time for the holidays, here are final versions of Chuckie Egg for DS and PSP, and an almost-final version for the Dreamcast:

Chuckie Egg DS

Post Mortem

PSP

The PSP has a huge screen. Well, relative to the DS; maybe not so huge if you compare it to a modern phone. What do you do with all that space if you’re remaking a game that fits in 256x192 pixels? The obvious thing to do is double up the pixels and make the graphics twice the size. But wait; at 272 pixels tall the PSP’s screen is only 70% larger vertically than the Dragon 32, which means we can’t double up the resolution without losing 30% of the game’s display. I imagine it’s possible to wrap the game’s bitmap onto a quad and get the GPU to stretch it, but I eventually decided just to centre the game at its native resolution.

The PSP’s screen is 480 pixels wide but its default framebuffer is 512 pixels wide. Trying to change the width of the framebuffer to match the screen does not work (it results in the display being smeared horizontally, which always means “the dimensions of your bitmap do not match the layout of memory”). I had to add in the concept of “usable” vs “available” framebuffer space (“physical” vs “logical”, I suppose) and base the layout of the various screens on that.

It’s possible to push the PSP’s “home” button and quit from a game back to the launcher. Making this work entails creating a background thread and wiring up a callback that the OS can use to signal to the game that it needs to quit. It’s very simplistic; if the game doesn’t implement the callback then the system just hangs forever. The OS doesn’t appear to forcibly quit rogue processes. I’m not even sure that it’s really a multi-process system. I imagine Sony handled errors like this in their QA process so they didn’t bother with anything more elaborate to catch badly-implemented games.

Different geographic regions have different PSP button layouts. I don’t quite know what Sony thought the advantage of swapping the O and X buttons was, but it means well-behaved homebrew has to work around it. Games typically either allow the buttons to be remapped or come in different builds for different layouts. Official games were region-locked so that wasn’t an issue; it was probably fixed during localization. I’m too lazy to deal with it.

Getting sound working was troublesome. The basic PSPSDK libraries offer a way - I think - to craft PCM data and play it back, which really doesn’t help if you just want to play a WAV and not put any thought into it. I ended up using libmikmod, which has some peculiarities. Its MikMod_Update() function must be called regularly in order for sounds to work, but that can be stuffed into another background thread and forgotten about. It offers two ways to play back samples: Sample_Play(), which will automatically choose a channel to play back on, and Voice_Play() which lets you choose the channel. I found that Sample_Play() always chose exactly the wrong channel and ended up stopping sounds that I wanted to continue playing, so I switched to the alternative function. Voice_Play() doesn’t have any useful documentation. Inspecting the library’s code reveals that the trick is to set the volume, frequency and panning of the voice each time a new sample begins:

Voice_Play(channel, sound, 0);
Voice_SetVolume(channel, sound->volume << 2);
Voice_SetPanning(channel, PAN_CENTER);
Voice_SetFrequency(channel, sound->speed);

DS

The DS has a second screen. What do you do with two screens when remaking a game designed for one? I had a few ideas:

  • Level editor;
  • Chuckie Egg title screen;
  • Simian Zombie logo;
  • Dragon 32 image.

In the end the SZ logo was simplest, so I went with that. Otherwise, the DS is my default platform, so there aren’t any surprises here.

Dreamcast

It appears that calling snd_sfx_stop() on a channel playing a stereo WAV file will only stop one of the channels, which explains why sounds weren’t behaving themselves in the beta. Switching to mono files fixed it. Another problem was caused by limitations of the audio hardware: the Dreamcast can only play back the first 65534 samples in a WAV file, which is far shorter than several of the sounds in the game. To work around this I halved the sample rates of the longer samples. That fixed most of them, but the title song and Spectrum loading sound were still too long. I converted those to MP3 and used the streaming API, but that created problems of its own. Sounds still cut out or don’t play, and the emulator I’ve been using to help development crashes whenever I call mp3_stop(). There’s still some work to do there.

The DC has a similar problem to the PSP: I’m using the 320x240 NTSC graphics mode, but a good portion of that is off-screen on my TV. The “usable” framebuffer space concept helped out here.

The DC feels noticeably faster than the other two platforms, which is odd, as I’d expect them to run at identical framerates. Perhaps it’s just because it runs on such a massive display.

The Dreamcast’s reversed ARGB ordering means I’ve had to convert bitmaps into both DS and DC sourcecode and use #ifdefs to choose the right version of the data for the current target platform at compile time.

Cross Platform

What was most surprising was how little effort I had to put into Chuckie Egg in order to make it performant on these different platforms. The effort that went into Hanky Alien presumably helped a lot. I was testing HankyAlien with up to ~140 views on screen at once and up to ~45 of those updating simultaneously. In contrast, despite superficially being a more complex game, Chuckie Egg has ~45 views on screen at once and only ~10 updating simultaneously. (It’s interesting to note that, while both the DS and DC struggled with the HankyAlien test scenario and their framerates suffered, the PSP sauntered through it without a hitch. I really must write something specifically for the PSP in future.)

Writing object-oriented C is fun, but sometimes I look at the amount of boilerplate necessary to create a new “class” and decide to do things another way, particularly if I’m creating a base class. The most simple class requires:

  • A static metaclass struct, which forms a linked list back through the chain of superclasses (it enables the program to determine the type of an object at runtime).
  • A static callback struct that includes pointers to equality/comparison/hash/copy/dealloc functions.
  • Declarations of those functions.
  • Implementations of those functions.
  • A struct with the member variables of the class.
  • A typedef that defines a pointer to the struct, allowing the struct to be hidden.
  • A constructor that calls the allocator and initializer methods of the class.
  • An initializer function that sets up the members of class instances and calls back to the superclass’ initializer.
  • A convenience release method that wraps the basic release method.

If the class is intended to be subclassed it must also:

  • Provide an additional private header including:
    • A callback struct definition that nests the superclass’ callback struct and enables “methods” to be “overridden” (really it’s the template pattern with function pointers).
    • The struct itself, so subclasses can directly access properties of the struct and include the struct as the first item in their own structs.
  • Call the functions in the callback struct when appropriate (including those for the superclass).

Every time I create a new class I do so by copying-and-pasting an existing class and modifying it rather than start it from scratch and have to retype all of the boilerplate. Sometimes I’ll think about the work involved in setting one of these things up, pine for C++ or Objective-C, and then use a different structure that’s less “correct” but will still get the job done without smelling too badly. The grain and eggs, for example, should really inherit from a single “collectible item” class, but the correctness of that design doesn’t in any way compensate for the amount of work involved in setting it all up.

Keeping a strict separation between the model (game logic), the views (the stuff you see on screen) and the view controller (delegate of the model; updates the views to match incoming events) results in some beautifully clean code but makes prototyping difficult, especially early on when none of the infrastructure is in place. I did most of the early prototyping in the view controller before moving it into a separate model.

Each level renders its static content (ladders, platforms and bird cage) as a bitmap when it begins. It passes that up through the model to the view controller, which uses the bitmap as the background view for the game. That eliminated the need to create dozens of views to represent tiles that never change during the course of a level.

There are now 134,000 lines of code in the project, but a sizable number of those are included in bitmaps that have been converted to C.

2016-11-14

PSP Surprise

I’ve mentioned before that SZLib presents challenges:

The downside to completely ignoring the capabilities of the DS hardware and doing all of the graphics work with just the CPU and a framebuffer is that it is insanely expensive.

However, its bare-bones approach - it just needs a framebuffer and a way to read a joystick, pretty much - made getting it working on the Dreamcast fairly trivial. Getting it working on other platforms should be simple too, but when choosing a new platform for the library I eliminated everything but the Dreamcast as a potential target.

I was too hasty when I crossed the PSP off that list. The handheld does offer the ability to write directly to the framebuffer. It took about an hour or so to get to this point:

ChuckieEggPSP

By far the hardest part was getting MikMod set up to play sound effects, which took a couple of hours on its own.

There’s a little bit of work to do. The PSP has a physical screen size of 480x272, but for some reason it seems that the frame buffer is 512 pixels wide. The disconnect between the physical and logical sizes means everything is slightly off-center. Also, the “home” button isn’t wired up yet. Overall, though, the game works just as well on the PSP as it doesn on the Dreamcast and NDS.

Excitingly, because they’re based on the same libraries, HankyAlien also now runs on the PSP.

2016-11-10

Chuckie Egg

Back in the early eighties, when the home computer boom was booming, my parents decided to buy themselves a computer. Following the advice of various computing magazines of the era they ended up with what was supposedly the best of the bunch: the Dragon 32. It featured 32K of RAM, a real keyboard and a Centronics parallel port. It was powered by Bill Gates’ favourite 8-bit CPU, the Motorola 6809. Unfortunately for my parents and their new Dragon, the manufacturer ran into financial troubles and collapsed not long after.

The unusual choice of the 6809 CPU meant very little software was written for or ported to the platform. One of the few games that made it was the BBC and Spectrum classic “Chuckie Egg”. This obscure port (the World of Spectrum doesn’t list it) is by far my favourite version, and it’s the game I spent the most time playing on our Dragon. There has never been a remake of it. Worse, neither the Dreamcast nor the Nintendo DS has a native version of Chuckie Egg. Let’s fix that.

Here’s a beta version of my remake of the Dragon 32 version of Chucky Egg for Nintendo DS and Dreamcast:

A couple of screenshots:

Chuckie Egg Logo Chuckie Egg Game

The game itself is fully playable, but there’s still some polishing to do:

  • Sound on the Dreamcast is misbehaving;
  • The Dreamcast version runs faster than the DS, so some timings are off;
  • Later levels do not have increased numbers of hens or faster moving hens;
  • Hens don’t eat grain;
  • The high score table is not functional (it’s just a bitmap);
  • There’s no “game over” screen;
  • The score/timer/etc display layout and font is all wrong;
  • Remaining lives aren’t displayed;
  • The font in the title screen scroller is wrong.

This is an inexhaustive list of bugs I spotted in the original game while making this:

  • On the first level, climb the largest ladder to the third platform on the right (the start of the staircase). Stand on the ladder just below the top of the floor and jump right. Instead of bouncing off the first step in the staircase, Harry lands inside it. Now press right and Harry will fall through the platform on the right. (The remake fixes this.)

  • On level 7, stand next to the two blocks immediately on the right when the level starts. Face them, hold “right”, and jump. Harry stands in the middle of the upper block. (The remake reproduces this.)

  • Animation frame doesn’t reset when Harry stands on an elevator. (The remake fixes this.)

  • When a new level rolls in the remaining time is set to 999; it immediately jumps to 898 when the level starts. After dying it resets to 899.

  • Harry gets killed if the elevator he is standing on hits the top of the screen, but if he is jumping then he falls through the elevator. (The remake doesn’t kill Harry; he just drops down when the elevator disappears.)

  • There are a bunch of inconsistencies with ladder behavior. Sometimes it is possible to walk from a platform all the way across a ladder; sometimes Harry falls through half-way along the ladder. This looks more like a bug than desired behavior.

  • One of the extra hens on level 3 (at the bottom of the twin ladders) starts one block below where it should do.

Here are some of the differences between the remake and the original:

  • Jump isn’t quite the same.
  • Frame rate is much better.
  • No flickering.
  • Hen patterns aren’t the same (but the Dragon doesn’t match the BBC or ZX, and they don’t match each other, so that’s OK).
  • Duck moves smoothly.
  • Hens move more smoothly.

I think this will be the last remake for a while. This is the list of remakes I’ve managed to complete (more or less) so far:

  • Pong (Blitz Basic/C/C++/Java/Flash; Amiga/Cybiko/GBA/DS)
  • Asteroids (Flash)
  • Mario Bros (Flash)
  • Tetris (Flash)
  • IK+ (Flash)
  • PacMan (Java; Linux)
  • Space War (Java; web applet)
  • Earth Shaker (C++; DS/SDL)
  • Super Foul Egg (C++/Objective-C; DS/SDL/OSX/iOS)
  • BioShock sub game (C++; DS/SDL)
  • Space Invaders (C; DS/SDL/DC)
  • Chuckie Egg (C; DS/SDL/DC)

This is the list of remakes I started but abandoned:

  • World Grand Prix (Flash)
  • Trick or Treat (Java; web applet)
  • Atic Atac (C/C++; Cybiko/DS/SDL)
  • Defender (C/C++; DS)
  • Dan Dare (C; DS/SDL)