#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];
}
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];
}