SEGASGDK

SGDK Basic Sprite Example

2024/02/16

SGDK is a fantastic library for developing games for the Sega Genesis/Mega Drive. There are plenty of resources for learning how to use SGDK. Such as the the wiki, SpritesMind.net and the tutorials by ohsat games and danibus games. So I don’t plan on writing a bunch of tutorials for it. My blog will cover things that I had trouble with or find interesting.

Sprite Image Creation

The Sega Genesis is a fun system to program, but has some strict limitations. One of which is the limited color palette. Sega Genesis sprites can only be 16 colors.

GIMP

  1. Create an image using “File|New…”. Set the image width and height to a size supported by SGDK.

  2. Convert the image to an Indexed Image using “Image|Mode|Indexed…”. Choose a palette with 16 colors or fewer.

Convert Image
  1. You can use the “Colormap” Dockable Dialog to edit colors if needed.
Color Map
  1. Draw your sprite. Take care to use only 15 color or fewer. The first color is the background color and will be transparent when rendered by SGDK.

  2. Once you’re doing editing your image, export it as a PNG with “File|Export As…”

Pro Motion NG

  1. Create a new project using “File|New Project|Create …”. Set the image width and height to a size supported by SGDK.
  2. In the “Create a new Project” dialog box set the size of your image, the Type to “Sega Megadrive/Genesis (Generic)” and the Color Palette to “rainbow333.pal”

New Project  

  1. Draw your sprite. Take care to use only 15 colors or fewer. The first color is the background color and will be transparent when rendered by SGDK.

  2. Reduce the colors in your image to 16. Use “Colors|Reduce Colors…”

Reduce Colors  

  1. Once you’re doing editing your image, save it as a PNG with “File|Save Image As…”

Save as PNG  

Basic Sprite Example

Sprite Specific Code

SGDK makes it easy to load and display sprites from images. At a minimum you want to 1. Define a sprite resource in res/resources.res

SPRITE ship "sprites/ship.png" 2 2 NONE
  1. Load the color palette of the sprite resource
    VDP_setPalette( PAL1, ship.palette->data );
  1. Initialize the sprite engine
  SPR_init()
  1. Create a Sprite with SPR_addSprite()
    Sprite *shipSprite = NULL;
    shipSprite = SPR_addSprite( &ship,      // Sprite name from resources.res
                152,        // starting X position
                102,        // starting Y position
                TILE_ATTR( 
                    PAL1,   // Palette index
                    0,      // Palette index
                    FALSE,  // Vertical Flip
                    FALSE   // Horizontal Flip
                    )); 
  1. Update the sprite engine in the main game loop
  SPR_update();

The full source code can be found at github

About Me

Greg Gallardo

I'm a software developer and sys-admin in Iowa. I use C++, C#, Java, Swift, Python, JavaScript and TypeScript in various projects. I also maintain Windows and Linux systems on-premise and in the cloud ( Linode, AWS, and Azure )

Github

Mastodon

YouTube

About you

IP Address: 18.119.158.110

User Agent: Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)

Language:

Latest Posts

Iowa City Weather

Overnight

42 ˚F / 46 ˚F

Friday Night

41 ˚F / 50 ˚F

Saturday Night

35 ˚F / 48 ˚F

Sunday Night

32 ˚F / 44 ˚F

Monday Night

30 ˚F / 35 ˚F

Tuesday Night

24 ˚F / 31 ˚F

Wednesday Night

19 ˚F / 28 ˚F