gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > n00b Sprite Corruption

#44967 - AkumaATR - Mon Jun 06, 2005 11:51 pm

My problematic code is below -- it's a variation of the one of the tutorials from Jonathan Harbour's free materials.

My problems is as follows: I've been having an issue getting sprites to work in 4bpp (16 color) mode all afternoon. i was able to get it to work in 256 color mode, so i'm completely confused now (since i thought i had figured it out). i know this code is ugly but i just wanted to get it working first.

i have tried using pcx2sprite (pcx2sprite h:16 ?w:16 -16) and gfx2gba (gfx2gba -t8 -c16 -fsrc star_64x64.bmp). the source image was def. 16 color, correct palette entries.

original image: http://home.insightbb.com/~cecil.jason/files/star_64x64.bmp

corrupt image displayed by this code: http://home.insightbb.com/~cecil.jason/files/corrupt_star.bmp

Thanks for any help everyone.

Code Follows:

//typedefs
typedef unsigned short u16;

#include "star_64x64.h"
#include "star_64x64.c"

//macros
#define SetMode(mode) REG_DISPCNT = (mode)
#define REG_DISPCNT *(volatile unsigned short *)0x4000000
#define BGPaletteMem ((unsigned short *)0x5000000)
#define REG_VCOUNT *(volatile unsigned short *)0x4000006
#define REG_DISPSTAT *(volatile unsigned short *)0x4000004

//OAM mem state addy
#define SpriteMem ((unsigned short *)0x7000000)

//OAM image addy
#define SpriteData ((unsigned short *)0x6010000)

//OAM palette addy
#define SpritePal ((unsigned short *)0x5000200)

//misc constants
#define OBJ_MAP_2D 0x0
#define OBJ_MAP_1D 0x40
#define OBJ_ENABLE 0x1000

//attr0 stuff
#define ROTATION_FLAG 0x100
#define SIZE_DOUBLE 0x200
#define MODE_NORMAL 0x0
#define MODE_TRANSPARENT 0x400
#define MODE_WINDOWED 0x800
#define MOSAIC 0x1000
#define COLOR_16 0x0000
#define COLOR_256 0x2000
#define SQUARE 0x0
#define TALL 0x4000
#define WIDE 0x8000

//attr1 stuff
#define ROTDATA(n) ((n) << 9)
#define HORIZONTAL_FLIP 0x1000
#define VERTICAL_FLIP 0x2000
#define SIZE_8 0x0
#define SIZE_16 0x4000
#define SIZE_32 0x8000
#define SIZE_64 0xC000

//attr2 stuff
#define PRIORITY(n) ((n << 10)
#define PALETTE(n) ((n << 12)

//sprite structs
typedef struct tagSprite
{
unsigned short attribute0;
unsigned short attribute1;
unsigned short attribute2;
unsigned short attribute3;
} Sprite, *pSprite;

//array of 128 sprites
Sprite sprites[128];

void WaitForVSync(void);
void UpdateSpriteMemory(void);

int main(void)
{
signed short x = 20, y = 20;
int char_number = 0;
int n;
SetMode(2 | OBJ_ENABLE | OBJ_MAP_1D);

//set sprite palette
for (n = 0; n < 8; n++)
{
SpritePal[n] = star_64x64Palette[n];
}

//load sprite image data
for (n = 0; n < 1024; n++)
{
SpriteData[n] = star_64x64Data[n];
}

sprites[0].attribute0 = COLOR_16 | y;
sprites[0].attribute1 = SIZE_64 | x;
sprites[0].attribute2 = char_number;

while (1)
{
WaitForVSync();
UpdateSpriteMemory();
}
}

void WaitForVSync(void)
{
while ((REG_DISPSTAT & 1));
}

void UpdateSpriteMemory(void)
{
int n;
unsigned short* temp;
temp = (unsigned short *)sprites;
for (n = 0; n < 128 * 4; n++)
SpriteMem[n] = temp[n];
}

#45035 - AkumaATR - Tue Jun 07, 2005 5:24 am

damn zero replies!!! and i was all excited to have come home to some assistance... hehe

#45036 - tepples - Tue Jun 07, 2005 5:31 am

It looks like you're getting 256-color tile data. No, I don't know the specific arguments to pass to those programs to make them output 16-color tile data.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#45088 - AkumaATR - Tue Jun 07, 2005 4:49 pm

The problem ended up being that gfx2gba spits out unpacked values, rather than packed ones, even though I told I specified 16 color mode as a parameter.

For example, four red pixels in a row (where red is palette index 1) should look like this from the tool.

0x1111, ...

gfx2gba spits out

0x0101, 0x0101 in an array that is twice as big as it should be. I was using the -fsrc to get source code instead of raw output, so their may just be a big with that flag. Of course it's possible I'm still doing something wrong but I did notice many posts about strangeness with 16 color mode w/ gfx2gba that makes me think the tool may have a bug. I tried using gif2sprite and it worked, spitting out a proper data array representing a 16 color sprite.

- Jason

#45092 - poslundc - Tue Jun 07, 2005 5:26 pm

In the long run, if you are serious about GBA programming, you will probably wind up having to write your own tools anyway.

Dan.