Adventures in Mega Drive Coding Part 6: Enter Sprites

Posted July 6, 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!

Welcome back to Adventures in Mega Drive Coding, where I play around with SGDK in an attempt to get a Mega Drive game done this year!

After messing around with tiles in the last few installments, it’s finally time to move on to sprites! Sprites in SGDK work differently than tiles, so it’s gonna be a whole new round of figuring stuff out. But enough of the preamble, let’s get some things on screen!

Importing the File

First, we need a graphic for our sprite. I went with Casey from Kid Chameleon.

kidchameleon.png

Didn’t know he was called Casey? You do now. Anyway, first we need to import the sprite by adding it to our resource file:

SPRITE kid_sprite	"chameleon.png" 3 4 FAST 1

(For more details on importing assets, check part 3 of this series.)

And with that we have a Mega Drive-compatible version of our sprite. Now to actually make use of it!

Getting it Into the Game

First of all we’ll need a variable (or more precisely, a pointer) to store our sprite in, so at the top of main.c I defined one:

Sprite* kid;

But before we load Casey into the game we actually have to tell SGDK that we want to use sprites in the first place. In order to do this we call the following function:

SPR_init(0,0,0);

This will initialize the sprite engine. The parameters specify the maximum number of sprites, the VRAM size and the unpack buffer size, respectively. Since I’m not 100% clear on how to use these, I set everything to 0 which will use the default values.

With the sprite engine initialized you’d think we’re ready to load up our Chameleon Kid…but hold on, haven’t we forgotten something? Yup…as with tiles, we need to handle the palette first!

VDP_setPalette(PAL2,kid_sprite.palette->data);

PAL1 contains the colors for our tiles, so I just went ahead and saved this one in PAL2.

And now we can finally invite Casey to join our game! Well, “game”.

kid = SPR_addSprite(&kid_sprite,64,136,TILE_ATTR(PAL2, TRUE, FALSE, FALSE));
SPR_update();

The function SPR_addSprite does what it says on the tin: It adds a sprite. We specify our resource with &kid_sprite as well as the position (64,136) at which we want the sprite to appear. Note: This position is given in pixels, not tiles, so that’s something to look out for! Finally we use TILE_ATTR as usual to tell SGDK what palette to use.

Afterwards we need to call SPR_update() to update the sprite engine; without this call we wouldn’t see anything. If you have sprites that move and animate you’ll want to call this every frame, but for now it’s enough to call it once.

And after all this, let’s see what I ended up with!

kidingame.png

I ended up with Kid Chameleon in my game, which was my original goal…so mission accomplished, I’d say!

Hope you enjoyed this edition of Adventures in Mega Drive Coding! I’m sorry it’s a bit short, but I’ve been extremely busy lately so it was tough getting one of these done at all. It’s a shame too, because I love writing these posts and I do eventually want to start writing a step-by-step tutorial on creating a complete Mega Drive game, with full example code and all that good stuff. Hope I’ll get to that sooner rather than later!

On a related note, I’ve set up a Ko-Fi account in case you want to support me. I appreciate every cup of coffee and I didn’t go with Patreon for now because I don’t want people to commit when I can’t currently guarantee a steady output of the stuff they want.

And if you don’t want to (or can’t) spend money but still want to help me out: Awesome! Feel free to share my stuff, comment on it, check out my games…every little bit helps. Just don’t yell at random children in the street about how awesome my stuff is, my lawyer said I can’t have people do that anymore.

Until 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