#6138 - e_mpika - Sun May 18, 2003 4:34 am
Hallo everyone, im quite new to gba porgramming so bear with me!
im trying to animat a sprite, i just want to loop it, the are 3 frame of a man walking.
ive learnt alot and think i understand most of it so far, basically this code is a copy and paste of tutorial stuff ive come accross, please could you help me animate my man, i have set up the man and loaded the sprite data and stuff, i just need to be able to loop between attribute2's, i have made a struce to hold the frame numbers in.
//my code for a man, by eddy parris.
#include "gba.h"
#include "screenmode.h"
#include "sprite.h"
#include "spred_sprite.h"
//#include "palette.h"
#include "keypad.h"
s16 x = 50;
s16 y = 60;
u16 char_number = 0;
u16 frame =0;
void GetInput(void);
void MoveSprite(OAMEntry* sp, int x, int y);
OAMEntry sprites[128];
typedef struct
{
u16 x; //x and y position on screen
u16 y;
u16 spriteFrame[3]; //animation frame
int activeFrame; //which frame is active
}Sprite;
Sprite man;
void CopyOAM()
{
u16 loop;
u16* temp;
temp = (u16*)sprites;
for(loop = 0; loop < 128*4; loop++)
{
OAMmem[loop] = temp[loop];
}
}
void GetInput(void)
{
if(!(*KEYS & KEY_UP))
{
y--;
}
if(!(*KEYS & KEY_DOWN))
{
y++;
}
if(!(*KEYS & KEY_LEFT))
{
x--;
}
if(!(*KEYS & KEY_RIGHT))
{
x++;
}
if(!(*KEYS & KEY_A))
{
x++;
y++;
}
}
void MoveSprite(OAMEntry* sp, int x, int y)
{
if(x < 0)
x = 512 + x;
if(y < 0)
y = 256 + y;
sp->attribute1 = sp->attribute1 & 0xFE00;
sp->attribute1 = sp->attribute1 | x;
sp->attribute0 = sp->attribute0 & 0xFF00;
sp->attribute0 = sp->attribute0 | y;
}
void InitializeSprites()
{
u16 loop;
for(loop = 0; loop < 128; loop++)
{
sprites[loop].attribute0 = 160;
sprites[loop].attribute1 = 240;
}
}
void WaitForVsync()
{
while((volatile u16)REG_VCOUNT != 160){}
}
int main()
{
u16 loop;
SetMode(MODE_1 | OBJ_ENABLE | OBJ_MAP_1D);
for(loop = 0; loop < 256; loop++)
OBJPaletteMem[loop] = spred_sprite_pal[loop];
InitializeSprites();
sprites[0].attribute0 = COLOR_256 | WIDE | y;
sprites[0].attribute1 = SIZE_32 | x;
sprites[0].attribute2 = 0;
for(loop = 0; loop < 768; loop++)
{
OAMdata[loop] = spred_sprite_pak[loop];
}
man.activeFrame = 0; //set the initial active frame markers
man.spriteFrame[0] = 0; //set the frame markers
man.spriteFrame[1] = 16;
man.spriteFrame[2] = 32;
while(1)
{
GetInput();
MoveSprite(&sprites[0],x,y);
WaitForVsync();
CopyOAM();
}
}
im trying to animat a sprite, i just want to loop it, the are 3 frame of a man walking.
ive learnt alot and think i understand most of it so far, basically this code is a copy and paste of tutorial stuff ive come accross, please could you help me animate my man, i have set up the man and loaded the sprite data and stuff, i just need to be able to loop between attribute2's, i have made a struce to hold the frame numbers in.
//my code for a man, by eddy parris.
#include "gba.h"
#include "screenmode.h"
#include "sprite.h"
#include "spred_sprite.h"
//#include "palette.h"
#include "keypad.h"
s16 x = 50;
s16 y = 60;
u16 char_number = 0;
u16 frame =0;
void GetInput(void);
void MoveSprite(OAMEntry* sp, int x, int y);
OAMEntry sprites[128];
typedef struct
{
u16 x; //x and y position on screen
u16 y;
u16 spriteFrame[3]; //animation frame
int activeFrame; //which frame is active
}Sprite;
Sprite man;
void CopyOAM()
{
u16 loop;
u16* temp;
temp = (u16*)sprites;
for(loop = 0; loop < 128*4; loop++)
{
OAMmem[loop] = temp[loop];
}
}
void GetInput(void)
{
if(!(*KEYS & KEY_UP))
{
y--;
}
if(!(*KEYS & KEY_DOWN))
{
y++;
}
if(!(*KEYS & KEY_LEFT))
{
x--;
}
if(!(*KEYS & KEY_RIGHT))
{
x++;
}
if(!(*KEYS & KEY_A))
{
x++;
y++;
}
}
void MoveSprite(OAMEntry* sp, int x, int y)
{
if(x < 0)
x = 512 + x;
if(y < 0)
y = 256 + y;
sp->attribute1 = sp->attribute1 & 0xFE00;
sp->attribute1 = sp->attribute1 | x;
sp->attribute0 = sp->attribute0 & 0xFF00;
sp->attribute0 = sp->attribute0 | y;
}
void InitializeSprites()
{
u16 loop;
for(loop = 0; loop < 128; loop++)
{
sprites[loop].attribute0 = 160;
sprites[loop].attribute1 = 240;
}
}
void WaitForVsync()
{
while((volatile u16)REG_VCOUNT != 160){}
}
int main()
{
u16 loop;
SetMode(MODE_1 | OBJ_ENABLE | OBJ_MAP_1D);
for(loop = 0; loop < 256; loop++)
OBJPaletteMem[loop] = spred_sprite_pal[loop];
InitializeSprites();
sprites[0].attribute0 = COLOR_256 | WIDE | y;
sprites[0].attribute1 = SIZE_32 | x;
sprites[0].attribute2 = 0;
for(loop = 0; loop < 768; loop++)
{
OAMdata[loop] = spred_sprite_pak[loop];
}
man.activeFrame = 0; //set the initial active frame markers
man.spriteFrame[0] = 0; //set the frame markers
man.spriteFrame[1] = 16;
man.spriteFrame[2] = 32;
while(1)
{
GetInput();
MoveSprite(&sprites[0],x,y);
WaitForVsync();
CopyOAM();
}
}