Adventures in Mega Drive Coding Part 2: Tiles

Posted January 19, 2018

WARNING: These are outdated and obsolete! Instad check out the new >tutorial section<. There you'll learn everything you could here but better!

Note: These are not tutorials, I'm just chronicling my progress as I figure stuff out. Some of the things I do might make an experienced Mega Drive coder cry out in anguish. Feel free to use these posts to guide yourself along, but be aware I might be leading you off a cliff!

Motivated by my first big success (getting Text on the screen) I decided to go all in and actually try to display some graphics this time. But first I set up an IDE to make things easier for myself. I decided to go with Code::Blocks as I had used that waaaay back in the day when I first learned programming. Yes, I started with C. No, I don’t remember much of it. One cool thing is that now the emulator automatically loads up the rom after I compile the project, which saves me some time and hassle.

Anyway, with the IDE and toolchain set up I got to work on getting some graphics on the screen. The Mega Drive uses 3 planes to display stuff: 2 scrolling playfields (called Plane A and B respectively) and 1 sprite plane. Each plane is made up of tiles (also called chars) with a size of 8x8 pixels. These tiles are the basic unit when it comes to loading and displaying graphics on the MD. Thanks to SGDK it’s not hard to get one of those on the screen: You load the data, then place it. Or, translated to code:

VDP_loadTileData( (const u32 *)tile, 1, 1, 0);
VDP_setTileMapXY(PLAN_A, 1, 6, 6);

Now, the first function is still a bit confusing to me, but from what I understand it loads the tile tile into position 1 of the VRAM. I’m not entirely sure what the second 1 is supposed to be; the ´0´ indicates that the DMA should not be used, which is still a bit advanced for me.

The second function is a lot easier: On Plane A it draws the tile stored at position 1 of the VRAM, at position 6,6. The position is given in tiles, not pixels.

And the result looks like this:


Thrilling! The tile I’m using isn’t actually a graphic I loaded by the way, but rather just a C array:

const u32 tile[8]=

Obviously drawing tiles in hex is slightly less convenient than using something like Photoshop, so we’ll take a look at how to load graphics made in a different software soon, but for now we’ll stick to our hex-tile.

As you might expect, drawing multiple tiles on the screen is as simple as just calling VDP_setTileMapXY() multiple times:

VDP_setTileMapXY(PLAN_A, 1, 6, 6);
VDP_setTileMapXY(PLAN_A, 1, 7, 6);
VDP_setTileMapXY(PLAN_A, 1, 9, 6);
VDP_setTileMapXY(PLAN_A, 1, 8, 7);

However! There is still a bit more you can do with a tile. First, you can add color. Each tile on a playfield can have one of 4 different color palettes applied to it. Secondly, you can actually draw tiles flipped (horizontally and/or vertically) without having to use any more memory, which is pretty cool.

So how do we do any of that? By passing these properties into VDP_setTileMapXY():

VDP_setTileMapXY(PLAN_A, TILE_ATTR_FULL(PAL2, 0, 1, 0, 1), 6, 6);

The first and last two arguments are the same as before: We’re drawing a tile on Plane A at position 6,6. However, instead of just specifying the position of the desired tile in VRAM we actually set more properties via TILE_ATTR_FULL(PAL2, 0, 1, 0, 1). These properties are as follows:

  1. Use Palette 2
  2. Draw the tile with low priority
  3. Flip the tile vertically
  4. Don’t flip the tile horizontally
  5. Use the tile at position 1 in VRAM

A little sidenote at this point: C doesn’t actually have boolean values, so instead of trueand false you use 1and 0 respectively. And here is the result of our added properties:


As you can see the first tile is now green and upside-down, a bit like me the morning after a bender. Palette 2 doesn’t have to be green of course, that’s just the way SGDK defines it by default. We’ll get into defining palettes next time.

Another thing we’ll cover next time is the tile priority: Simply put, it allows you to change the draw order of tiles. Usually plane A is drawn above plane B, but if you change the priority of a tile you can override this. This seems like a pretty powerful feature in the hands of someone who knows what they’re doing.

And that’s it for this installment, I hope you enjoyed it. As always, if there are any questions or things you are curious about, don’t hesitate to let me know! Either post in the comments below or hit me up on Twitter @ohsat_games.

See you next time!

Check out the rest of this series!

  • Adventures in Mega Drive Coding Part 1: The Journey Begins
  • Adventures in Mega Drive Coding Part 2: Tiles
  • Adventures in Mega Drive Coding Part 3: Importing Graphics
  • Adventures in Mega Drive Coding Part 4: More Tiles
  • Adventures in Mega Drive Coding Part 5: Palettes and Splitting Tiles
  • Adventures in Mega Drive Coding Part 6: Enter Sprites
  • Adventures in Mega Drive Coding Part 7: Handling Input
  • Take It to the Next Level!

    Want sneak peeks and early access to stuff? Join my Patreon!

    Become a Patron!
    Just Want to Buy Me a Coffee?
    Get Words in Your Inbox!

    Be oldschool and sign up for my newsletter to get updates! Just enter your email address, prove you're not part of Skynet and you're good to go!

    Powered by CleverReach. I will not send you spam or sell/give your email address to someone else.  You can of course unsubscribe at any time. By clicking the subscribe button above, you confirm that you have read and agreed to our privacy policy.

    Related Posts

    Streets of Rage 2 Design Docs

    A few years ago, Yuzo Koshiro posted a pile of old game design documents for Bare Knuckle 2 aka Streets of Rage 2 on the Ancient blog to commemorate the release of Streets of Rage 2 3D on the Nintendo 3DS. These documents gave a deep insight into the game’s inner workings, technical aspects, designs and even some cut content. They were an awesome resource for one of the most awesome games ever created.

    Read More

    Make a Space Shooter for the Mega Drive!

    It’s time for another SGDK tutorial series! After doing a single player Pong game and an endless runner, it’s time to reach for the stars… and make a space shooter! Apart from things like scrolling and animating sprites, this new series will show you how to deal with multiple entities and their collisions, how to randomly generate backgrounds and more! This project builds upon the previous tutorials, so if you’re new to SGDK programming and have not done those yet, I highly recommend starting with Megapong.
    Read More

    Patreon Revamps

    Hey there, what’s up? Things are continuing to evolve, as I’ve now updated my Patreon to give patrons more perks! Apart from early access to new tutorials and posts, the biggest one is probably the ability to peek behind the scenes…and there will be a lot to peek at in the coming months! This year I’m writing and submitting my MA thesis, meaning that I will have to do more stuff to make ends meet.
    Read More

    By using the Disqus service you confirm that you have read and agreed to the privacy policy.

    comments powered by Disqus