Mega Drive SGDK Megapong

In this tutorial series you will learn how to make a simple single-player Pong-like game for the Sega Mega Drive using SGDK! If you just want to see the full source code, you can get it from Github.

Megapong 1 - Hello Mega Drive World!


Update: I added a note saying that Java is required for SGDK to work. Thanks to Diego for the suggestion!

Welcome to my new tutorial series on Mega Drive development! In it we will be creating a Mega Drive game from scratch using SGDK, the Sega Genesis Development Kit. I will show you how to set up your development environment, how to import graphics, how to put things on the screen, how to control them and have them collide…the whole shebang. And this is what we will be making:

images/screenshotmegapong.png

I call it Megapong. It is a very simple single-player Ponglike where the goal is to keep the ball in play for as long as possible. Each time you hit it you’ll receive points and when you’ve hit the ball a certain number of times, it’ll get faster. The game is over when the ball leaves the screen at the bottom. No it’s not gonna knock any blue hedgehogs off the shelf, but it’s a very good game to learn the basics of Mega Drive programming. And that’s the whole point!

This tutorial will lead you through every step of making this game. However, if you’re impatient and ready to dive into the deep end, you can find the whole project on Github right now! I tried to comment most of the code so if you’re feeling adventurous (and maybe already have some basic knowledge of SGDK) feel free to clone the repo and poke around!

And one last thing before we start: If you like what I’m doing, please consider supporting me on Patreon. I’d love to do more of these tutorials on a regular basis, also expanding to other genres like platformers. Becoming a patron lets me make more time for these, and it gets you some cool perks too!

Become a Patron!

Alright, without further ado: Let’s get this show on the road!

Setting up SGDK

First we have to install and set up SGDK. You can download the latest version here. Please note that out of the box, SGDK only works on Windows. Linux users can give the Gendev Project a try, of which there also exists a Mac OS version here. Keep in mind that I have used neither of these and thus can’t really help with setting them up. The actual coding will work the same across all platforms, since all environments use SGDK, but the stuff around it (setting up folders, toolchains etc.) will most likely be different.

Note: SGDK requires Java, so you will have to install that as well if you haven’t already. The latest version of SGDK uses Java to compile resources, which lets it do some pretty powerful and cool stuff.

Then follow the installation instructions, which you can find here. If you need some help setting the environment variables, check out this site. And remember that you might have to restart your PC after setting the variables. Note: You only have to follow the instructions up until you’ve created the libmd.a file. We will compile a project in a second!

Setting up a project

The easiest way to set up a project is to copy an empty template project. I’ve prepared one right here, so either clone it or download the .zip file and you’re good to go!

Emulators

In order to test your games, you’ll need an emulator to load the roms. If you already have a favorite one, feel free to use it! Any reasonably up-to-date emulator should work. However, if you only want to use an emulator for Mega Drive development, you might want to use Gens KMod. It’s based on the famous Gens emulator codebase but adds a few debugging tools that are very useful for developers. Just download the archive and unpack it anywhere you want; it makes sense to keep it close to the SGDK folder. I actually keep it inside my SGDK folder so that I always have everything I need in one spot.

Compiling and running Hello World

Now that we have everything set up, let’s see if we can actually get things running! We’ll do that by creating the old programmer standby, the Hello World program. Open up your project folder and you should see the file main.c. Open it up in your text editor of choice and type in the following lines of code. Note that if you’re using my template project, you’ll only have to add one line!

#include <genesis.h>
#include <resources.h>

int main() //The main function and entry point
{
    VDP_drawText("Hello Mega Drive World!", 8, 12); //Draw a text on the screen

    while(1) //The main game loop
    {
        VDP_waitVSync(); //wait for screen refresh
    }
    return (0); //End the program
}

I’ll explain what these lines do in a second, but first let’s try to compile and run our program! To do this, open the command line and navigate to your project folder. The magic line that compiles your code into a rom is this one:

%GDK_WIN%\bin\make -f %GDK_WIN%\makefile.gen

%GDK_WIN% indicates the path where you have saved SGDK. If you’ve set up the environment variables as per the instructions, the command should work fine as is. But if for some reason it doesn’t work, you’ll have to replace %GDK_WIN% with the path to your SGDK folder. So for example, if you’ve put it under C:\SGDK you would type the following:

C:\SGDK\bin\make -f C:\SGDK\makefile.gen

This should create a rom.bin file in the out directory of your project folder. This is your program! Open it in your emulator and you should see the following:

images/hellomdworld.png

Success! You’ve made your first Mega Drive program. Give yourself a pat on the back! It may not look like much yet, but it is a big step on a brand new road. I mean, you just made a Mega Drive program. You wouldn’t have been able to do that yesterday!

But Wait, What Did I Just Write?

But now for a quick explanation of what you’ve actually typed. Most lines are simple boilerplate stuff that you’ll need for the program to even run. But two lines are of special interest for now. Let’s take a look:

VDP_drawText("Hello Mega Drive World!", 8, 12);

As you can probably tell, this function draws text on the screen. To be more precise, in this case, it draws the text “Hello Mega Drive World!” on the screen, at the coordinates (8,12). We’ll learn about coordinates in part 3, but feel free to change these numbers around and see what happens!

By the way: This function uses the VDP of the Mega Drive, or the Video Display Processor, which explains the VDP_ prefix. Basically every function starting with VDP_ in SGDK does something visual, in this case drawing text on the screen.

The next interesting line also uses the VDP:

VDP_waitVSync();

This is a very important function. Basically, everything you put into the while(1){ ... } loop is executed as quickly as possible, which might or might not sync up with your display. In order to avoid display issues, you have to tell your Mega Drive to wait until the screen display has been fully updated before it starts processing more stuff. And that’s what VDP_waitVSync() does!

If that sounds confusing to you, don’t worry - you don’t really need to know the details for now. Just remember to always put VDP_waitVSync(); at the end of your game loop and things should be fine!

That was a lot of stuff at once, but you’ve made it through and ended up with a working rom. Congratulations! Here’s what we will be taking a look at next:

It might not sound exciting, but it will make everything a lot easier and more pleasant. And once you’ve set everything up you can use it for any future Mega Drive project you might try your hands on.

Thank you for reading and until next time!

If you have any questions, comments or criticism, post them in the comments below or reach out to me on Twitter @ohsat_games! Special thanks to Stephane Dallongeville for creating SGDK and everyone in the SGDK Discord for their help and keeping the dream alive!

Download the source code

All patrons on Patreon get the complete source code for this tutorial, as well as other perks such as early access! Become a Patron!
Just Want to Buy Me a Coffee?

Check out the rest of this tutorial series!

  • Megapong 1 - Hello Mega Drive World!
  • Megapong 2 - Setting Up The Environment
  • Megapong 3 - Importing Resources
  • Megapong 4 - Palettes
  • Megapong 5 - Sprites
  • Megapong 6 - Input and Paddles
  • Megapong 7 - Collisions
  • Megapong 8 - Score and HUD
  • Megapong 9 - Game Over
  • Megapong BONUS - Flashing!
  • Get Words in Your Inbox!

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



    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.

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

    comments powered by Disqus

    Make a Space Shooter for the Mega Drive!

    February 24, 2020
    Mega Drive Ramblings

    Patreon Revamps

    January 28, 2020
    Mega Drive Ramblings

    New SGDK Tutorial: Megarunner!

    November 4, 2019
    Mega Drive Ramblings