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 2 - Setting Up The Environment


Welcome back! Last time we set up SGDK and made sure everything worked. This time we’ll set up a proper development environment, which will make things a lot easier and more fun for us. Believe me, it’ll be worth it!

Why an environment at all?

A development environment offers a variety of things that makes coding stuff a more convenient experience. The main advantages in our case will be code completion and easy compiling/testing of our code. Instead of having to type our compilation command in a console and then having to load up the exported rom in an emulator, we’ll simply press a keyboard shortcut and have everything done automatically. Spiffy!

Development Environments

Code for SGDK is written in C, so you can use basically any IDE that supports C. Some popular choices are Code::Blocks or Eclipse. However, the one I use and the one I would definitely recommend is Visual Studio Code. It’s free, open source, extremely customizable and extendable, and it’s cross-platform. All explanations and steps given here will be for VSCode, so if you want to follow along I’d definitely recommend downloading it. However, if you’re already familiar with another C environment, feel free to stick with it. You shouldn’t have much trouble getting SGDK to comfortably work in it. Now, let’s get cracking!

Installing VSCode

  1. The first step to installing VSCode is to download VSCode. You can do so here.
  2. The next step is to actually install it. If you want more information you can check the official website but simply double-clicking and picking “next” a bunch of times should work just fine.
  3. Finally, we’ll need to install some additional things to get SGDK working properly. First we’ll need support for the C language, which is done via the C/C++ extension. Open up the Extensions menu on the left-hand side of VSCode, type the extension name into the search field and install it. (If you need further info, check the official website.)

Adding Support for SGDK

Now we have a VSCode installation that knows what C is. Next we’ll have to teach it about SGDK! Luckily, a chap named pleft has made this very easy for us by providing all the files we need.

  1. Clone this git repository or simply download is as a .zip-file and extract it somewhere.
  2. Open the folder titled .vscode.
  3. In it you’ll find the file c_cpp_properties.json. Open it with a text editor.
  4. Change the value of includePath to point to the inc folder of your SGDK installation. For example, if you’ve installed SGDK under C:\SGDK, then the line would say "includePath": ["c:/sgdk/inc"]
  5. Copy the .vscode folder into the root of your project directory.

And that should do it! Open (or restart) VSCode, open your project and start typing VDP_drawText. Autocomplete should pop up to help you out!

images/autocomplete.png

Compiling

Autocomplete will already save us a lot of time, but what about compiling? As you’ll recall from part 1, compiling is done by opening a command line and entering the line:

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

Not exactly convenient, especially since we then also have to manually load the generated rom in our emulator. But I wouldn’t be whining about it if there wasn’t a way to fix this, so let’s go make things easier for ourselves!

First, compilation is now as easy as pressing Ctrl-Shift-B. This is thanks to the magic .vscode folder courtesy of pleft. Go on, try it out with your Hello World project!

Opening the Rom

That’s already a lot easier than typing in stuff manually. And now, to be extra lazy, we’ll tell VSCode to automatically open our rom in an emulator once it’s compiled!

First, create a .bat file. (If you’re not sure what that is: Create a new .txt file, then change the extension to .bat). You can call it whatever you want, I’d suggest something along the lines of aftercompile.bat since the file will be run after compilation has finished. Save this file in your project folder.

Open the .bat file in a text editor of your choice (feel free to use VSCode). Now you’ll have to add two lines to it. For now, simply copy-paste these two into your file:

%GDK_WIN%\bin\make -f %GDK_WIN%\makefile.gen
%GDK_WIN%\gensk\gens.exe %cd%/out/rom.bin

The first line should look familiar; it’s the command that tells SGDK to compile our source code. If you’ve followed the SGDK setup instructions and defined your environment variable, you can leave the first line as it is. If not, you’ll need to replace %GDK_WIN% with the path to your SGDK installation folder (like C:\SGDK).

You’ll probably have to modify the second line, as it depends on your emulator and general setup. Basically, the first part %GDK_WIN%\gensk\gens.exe should point to the executable file of your emulator. As mentioned last time I’ve put mine into the SGDK folder, but you might have to enter C:\Fusion364\Fusion.exe or whatever. You can leave the second part %cd%/out/rom.bin as it is.

Hang in there, we’re almost done! Next, open up the .vscode folder inside of your project. This time, open the tasks.json file in a text editor. You’ll see that two tasks are already defined; we’re gonna add a third one:

{
    "isBuildCommand": true,
    "suppressTaskName": true,
    "echoCommand": true,
    "taskName": "makeAndRun",
    "args": [
        "aftercompile.bat"
    ]
}

And finally, change the value of isBuildCommand in the make task to false.

Since the tasks.json file can be a bit confusing, here’s what the whole thing should look like in the end:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "command": "cmd",
    "isShellCommand": true,
    "args": ["/C"],
    "showOutput": "always",
    "tasks": [
        {
            "isBuildCommand": false,
            "suppressTaskName": true,
            "echoCommand": true,
            "taskName": "make",
            "args": [
                "${env:GDK}\\bin\\make", "-f", "${env:GDK}\\makefile.gen"
            ]

        },
        {
            "suppressTaskName": true,
            "echoCommand": true,
            "taskName": "clean",
            "args": [
                "${env:GDK}\\bin\\make", "clean", "-f", "${env:GDK}\\makefile.gen"
            ]

        },
        {
            "isBuildCommand": true,
            "suppressTaskName": true,
            "echoCommand": true,
            "taskName": "build",
            "args": [
                "aftercompile.bat"
            ]

        },
    ]
}

And we are finally done! Now, when we press Ctrl-Shift-B our new build task should be called. This will compile the code as usual, then automatically open the generated rom in our emulator. Give it a try!

Alright, that was a lot of setting up and fiddling with things. Congratulations on sticking with it! I’d recommend creating a copy of your project folder and using that as a base for all new projects you start, so you won’t have to set up everything you just did again.

Next time we’ll finally get to some actual coding so we can get this Megapong show on the road! We will learn about:

  1. Importing Resources
  2. Drawing tiles

All of these things are very important in Mega Drive coding, so stay tuned! And don’t worry, it’s not really all that hard.

Thanks 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 Patreon

    New SGDK Tutorial: Megarunner!

    November 4, 2019
    Mega Drive Ramblings