Can't Seem to Display Sprites to the Screen

SGDK only sub forum

Moderator: Stef

Post Reply
B_E_P_I_S_M_A_N
Newbie
Posts: 2
Joined: Tue Jan 09, 2018 8:03 am

Can't Seem to Display Sprites to the Screen

Post by B_E_P_I_S_M_A_N » Tue Jan 09, 2018 8:12 am

Hello,

Recently, I've begun developing for the Sega Genesis/Mega Drive using SGDK. However, one thing I seem to be having trouble with is creating tiles and sprites to use in my programs. I'm not sure if this is because of my coding, because of how I edit the image in GIMP, or a combination of both.

In this case, I'm trying to create a simple demo to flash the SEGA logo on-screen. Here's the image I'm using (a bit dirty, I know :P):
sega.png
sega.png (650 Bytes) Viewed 3324 times
The above image is indexed, and contains 15 colors, with the first color set to black.

Here's my resource.res:

Code: Select all

PALETTE PAL_Sega      "sega.png" 2
SPRITE  SPR_Sega      "sega.png" 2 2 0 10 NONE
...and here's my code (do note that VDP_init() is called in main()):

Code: Select all

void segaLogo() {
    SYS_disableInts();
    VDP_setEnable(0);
    
    VDP_setPalette(PAL2, PAL_Sega.data);
    
    SegaLetter s = {NULL, 0, 0};
    SegaLetter e = {NULL, 4, 0};
    SegaLetter g = {NULL, 8, 0};
    SegaLetter a = {NULL, 12, 0};
    
    s.spr = SPR_addSprite(&SPR_Sega, s.x, s.y, TILE_ATTR(PAL0,0,0,0));
    e.spr = SPR_addSprite(&SPR_Sega, e.x, e.y, TILE_ATTR(PAL0,0,0,1));
    g.spr = SPR_addSprite(&SPR_Sega, g.x, g.y, TILE_ATTR(PAL0,0,0,2));
    a.spr = SPR_addSprite(&SPR_Sega, a.x, a.y, TILE_ATTR(PAL0,0,0,3));
    
    VDP_setEnable(1);
    SYS_enableInts();
    
    while (1) {
        VDP_waitVSync();
    }
}
Am I loading my sprites correctly? Is my image properly formatted? What am I doing wrong?

Thanks in advance.

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: Can't Seem to Display Sprites to the Screen

Post by Stef » Tue Jan 09, 2018 9:42 am

So you want to display the logo as several sprites (and not in the background) right ?

First : you don't need to declare the palette separately, SPRITE resource already contains it ;)
Secondly : you need to use the appropriate frame of your sprite, you don't need to manually deal with tile index.

Here's the definition of TILE_ATTR :

Code: Select all

/**
 *  \brief
 *      Encode tile attributes for tilemap data.
 *
 *  \param pal
 *      Palette index
 *  \param prio
 *      Tile priority
 *  \param flipV
 *      Vertical flip
 *  \param flipH
 *      Horizontal flip
 */
#define TILE_ATTR(pal, prio, flipV, flipH) 
So basically you need to define 4 sprites using the same sprite definition (be careful to the palette to use) :

Code: Select all

s.spr = SPR_addSprite(&SPR_Sega, s.x, s.y, TILE_ATTR(PAL2,FALSE,FALSE,FALSE));
e.spr = SPR_addSprite(&SPR_Sega, e.x, e.y, TILE_ATTR(PAL2,FALSE,FALSE,FALSE));
g.spr = SPR_addSprite(&SPR_Sega, g.x, g.y, TILE_ATTR(PAL2,FALSE,FALSE,FALSE));
a.spr = SPR_addSprite(&SPR_Sega, a.x, a.y, TILE_ATTR(PAL2,FALSE,FALSE,FALSE));
Then set each sprite to the correct frame index :

Code: Select all

SPR_setFrame(s.spr, 0);
SPR_setFrame(e.spr, 1);
SPR_setFrame(g.spr, 2);
SPR_setFrame(a.spr, 3);
Finally load the palette (use the one you referenced in the addSprite(..)) :

Code: Select all

VDP_setPalette(PAL2, SPR_Sega.palette->data);
Don't forget the final call :

Code: Select all

SPR_update();
So sprites are internally updated and sent to the VDP :) That should do it !

B_E_P_I_S_M_A_N
Newbie
Posts: 2
Joined: Tue Jan 09, 2018 8:03 am

Re: Can't Seem to Display Sprites to the Screen

Post by B_E_P_I_S_M_A_N » Tue Jan 09, 2018 10:19 pm

That did get sprites on-screen, however, the arrangement of them seems all messed-up.
glitchi.PNG
glitchi.PNG (26.81 KiB) Viewed 3304 times
For reference, here's the updated code:

Code: Select all

void segaLogo() {
    //SYS_disableInts();
    //VDP_setEnable(0);
    
    SegaLetter s = {NULL, 0, 0};
    SegaLetter e = {NULL, 4, 0};
    SegaLetter g = {NULL, 8, 0};
    SegaLetter a = {NULL, 12, 0};
    
    s.spr = SPR_addSprite(&SPR_Sega, s.x, s.y, TILE_ATTR(PAL2,FALSE,FALSE,FALSE));
    e.spr = SPR_addSprite(&SPR_Sega, e.x, e.y, TILE_ATTR(PAL2,FALSE,FALSE,FALSE));
    g.spr = SPR_addSprite(&SPR_Sega, g.x, g.y, TILE_ATTR(PAL2,FALSE,FALSE,FALSE));
    a.spr = SPR_addSprite(&SPR_Sega, a.x, a.y, TILE_ATTR(PAL2,FALSE,FALSE,FALSE));
    
    SPR_setFrame(s.spr, 0);
    SPR_setFrame(e.spr, 1);
    SPR_setFrame(g.spr, 2);
    SPR_setFrame(a.spr, 3);
    
    VDP_setPalette(PAL2, SPR_Sega.palette->data);
    
    //VDP_setEnable(1);
    //SYS_enableInts();
    
    while (1) {
        VDP_waitVSync();
        SPR_update();
    }
}

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: Can't Seem to Display Sprites to the Screen

Post by Stef » Wed Jan 10, 2018 9:10 am

You need to initialize the sprite engine before using it :

Code: Select all

SPR_init(0,0,0);
That will initialize it with default parameters, trying to use it without initialization may end to unpredictable result !

Then you have to remove the animation part of your sprite definition as you don't want to animate letter :

Code: Select all

SPRITE  SPR_Sega      "sega.png" 2 2 0 0 NONE
also the sprite position is in pixels so here SEGA letters will be overlapping each other.

Post Reply