Help keep the free Atari 2600 programming episodes and code coming by supporting the channel on Patreon.
In this episode we learn how to creating player graphic animations and using pointers to simplify your code.
Released:
February 1, 2022
Original Link:
Animating Graphics on the ATARI 2600
GitHub Link:
s02e01 - Animating Player Graphics
Welcome to 8Blit, a channel to discover how you can make games for the 8-bit Atari Video Computer System from 1978. In this episode, we’ll explore how to do animation that can be applied to your player and missile graphics. So let’s get started and kick off season two.
Well, it’s possible to create a game that features no graphic animation at all, as we can plainly see here with the game Pong and again in the game Miniature Golf. In both games, the only movement on the screen is from the objects themselves. But in this, Pong’s execution still works because it involves risk and skill. Miniature Golf, though, really is quite boring.
Incorporating even just a couple of frames of animation can add a lot of personality and charm to a game. As we can see here in the game Bump ‘n’ Jump, the animation on the car’s tires adds a ton of movement to the screen, making it more lively and exciting. However, I think my favourite animation comes from the game Demon Attack. The flapping, along with the colours and the movement, keeps the screen exciting even when you’re down to just one demon left.
Animation on the Atari 2600 is basically no different than on TV and in movies. It’s simply a series of two or more pictures that are slightly different, shown in sequence one after the other. It could be a spaceship exploding, a man running, or a fire burning. Let’s start simple with a screen and a single non-animated ghost.
First, we’ll open up the sprite editor in Atari Dev Studio. You can learn more about that in our previous episode called “Code Faster with Atari Dev Studio.” I’ll include a link in the description.
We’ll create two frames for the ghost and export the code to use in our program. These two frames will be used in all the examples.
In example one, we’ll use the same code we used in the previous episode called “More Sprites in Colour.” Again, I’ll include the link in the description. The important part of this code is how we’re loading which line of the graphic to display on our scanline.
You can see here we’re loading a value located at the index given in the Y register, starting at the memory address represented by GhostF0. GhostF0 is actually a static, hard-coded value that the assembler will replace with the real memory address of the graphic when it’s building the binary ROM file.
If we look at the actual opcode the assembler generates, we can see that our load instruction is the hex value B9. Well, the address where the ghost graphic is located is 72 F0, as represented in little-endian form, where the least significant byte is stored before the most significant byte. A more human-readable form of this address would be F072, which is what you’ll see in a debugger. We’ll have a video on debugging coming up, so make sure you subscribe and click the notification bell so you’ll always know when the new video is up.
What we need to swap the two frames of our graphic is a way to change the memory address that’s used when loading our graphic. To accomplish this, we’ll use something called a pointer. Pointers can be a little difficult to understand when you first encounter them in programming, but the concept is simple.
Typically, you would store a value in a byte of RAM, like the number of players in a game or how many missiles you have left. A pointer doesn’t hold a value; it holds a memory address pointing to where the value is. When you use a pointer, you won’t get the number of players or remaining missiles; you’ll get the address where you can find these values.
First, we’ll need to reserve two bytes for our pointer. This will ensure that the assembler will not use those two bytes for anything else. In this particular case, our pointer will use the two bytes located at addresses 84 and 85.
Then we’ll use the assembler to get the least significant byte of the 16-bit address where the ghost graphic resides and store it in the first byte we defined for our pointer. Then we’ll pull the most significant byte of the address and store it in the second byte of the pointer in RAM. This would look like this, with address 84 containing the least significant byte value of 72 and address 85 containing the most significant byte value of F0.
Then, in our load instruction, we use brackets around our pointer. This tells the assembler that we want to use the indirect load value opcode to get the value located at the memory address found in the two bytes located at the pointer address.
The indirect load value instruction is assembled with the opcode B1, and the pointer is at address 84. The B1 opcode will use the values stored in memory addresses 84 and 85 as the 16-bit RAM address. Now we’re no longer using the static address of the ghost graphic in our load routine, and when we execute our code, our ghost is still with us.
We have our code using our pointer address to draw our graphic. Let’s use that to swap which ghost frame we’re going to draw. We could swap our graphics for every screen frame, but that would swap them so fast that you wouldn’t be able to see the animation. It would just look like the ghost is flickering, which can be used for other game mechanics, like in Ms. Pac-Man when the ghosts are returning to their non-edible form.
For our purposes, we’re going to swap the ghost frame every 8 screen frames. We’ll start by creating a variable to hold which screen frame we’re currently showing. Next, we’ll add a bit of logic to flip which frame of animation we’ll use every eighth screen frame.
The algorithm we’ll use will count up to 16 and return zero for the first eight screen frames and one for the rest. Then the screen frame counter will reset back to zero to do it all over again. We start by increasing the current value of the screen frame in memory by one and then loading it into the accumulator.
Now we’ll use the AND instruction to AND the value 15 with the screen frame we just loaded into the accumulator. This will reset the count back to zero if the screen frame is above 15; otherwise, it will return a value from 0 through 15.
Now that we know our screen frame is within 0 through 15, we’re going to shift the bits of our accumulator value to the right using the LSR instruction. We’ll use a screen counter of seven as an example. In binary, seven would be represented as 00000111. Shifting the bits to the right drops off the rightmost bit, resulting in 00000011, which is equal to three in decimal.
To make this more useful for our purposes, we’ll need to reduce it a little more by shifting the bits to the right two more times. This will leave us with a value of zero, which indicates we will use the first frame of the graphic animation. If our screen frame counter was eight, that would give us a binary equivalent of 00001000, and shifting it right three times leaves us with a value of one, which indicates that we’ll use the second frame of the graphic.
So screen frames 0 to 7 will result in 0, using the first frame of the animation, and 8 to 15 will result in 1, using the second frame. Now we can use our branch to jump to whichever graphic frame we want to assign to the pointer. This part works the same as the previous example, except for some duplicated code we used to set up our pointer. We’ll take care of that shortly.
Running the code, we have a nicely animated ghost. So now our code can flip between animation frames based on the timing we created, and we have our logic to determine which frame we’re going to display. However, this implementation is a little messy when we look at that part of the code.
The good news is that we can move that part of the code and set it up during assembly. For this, we’re going to create a couple of data tables to store the least and most significant bytes for both of our graphic animation frames.
Now, when we assign the pointer, we can use the load instruction’s index parameter to pull the graphic’s frame from the data table. Since our algorithm to calculate which graphic to use results in either a 0 or a 1, this works nicely for selecting the index from the data table. This change removes the branch and our jump from the previous example, saving us a bit of time and making the code more readable.
Running the new code, he’s still there, hovering just a bit more efficiently. Let’s tweak the code a little and give him a friend.
Once again, we’ve covered a lot of information in this episode, and all of the examples are available on our GitHub, which I’ll link to in the description. You can follow along on social media and our website. There’s not a lot at the moment, but once I figure it all out, you can be sure I’ll start posting more information, behind-the-scenes details, and a little history.
If you found the video interesting, I’d appreciate it if you hit that like button and share it to let others know. If you haven’t already done so, please consider subscribing to the channel and hitting the bell icon to make sure you’re notified when a new video comes out.
If you’d like to help support the development of the channel, please consider becoming a patron. A link to that is in the description as well.
That’s all for now. Thanks for watching, and I’ll catch you later.