Skip to content

Commit

Permalink
Add ability to move sprite with d-pad and rotate with L/R flippers
Browse files Browse the repository at this point in the history
  • Loading branch information
Lam Bui committed Jan 29, 2016
1 parent 73f1c80 commit d9df6bc
Showing 1 changed file with 58 additions and 9 deletions.
67 changes: 58 additions & 9 deletions sprite_simple/arm9/source/template.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,27 @@
- 1D palette-based sprite on both screens using the touchpad for location
- We generate the image data(tile map/palette) on our own
- OAM is used to manage the sprite data (max of 128 sprites - 32 can be affine-transformed)
BONUS:
- Move the sprite around with the keypad
- Add some rotation with the L/R flippers - sub is double speed of the main
QUESTIONS:
- The movement spills off the screen and continues on the other side. There must be some overflow.
What size are ints on the NDS?
---------------------------------------------------------------------------------*/
#include <nds.h>

#define MOVEMENT_RATE 2
#define ROTATION_RATE 2
#define SPRITE_DIMENSION_SIZE 16

int main() {

int i;
int currentPosX = (SCREEN_WIDTH - (SPRITE_DIMENSION_SIZE/2)) / 2;
int currentPosY = (SCREEN_HEIGHT - (SPRITE_DIMENSION_SIZE/2)) / 2;
int angle = degreesToAngle(0);
touchPosition touch;
u16 *gfx;
u16 *gfxSub;
Expand Down Expand Up @@ -46,21 +60,56 @@ int main() {

// check our touch location
scanKeys();
if(keysHeld() & KEY_TOUCH)
if(keysHeld() & KEY_TOUCH) {
touchRead(&touch);
currentPosX = touch.px;
currentPosY = touch.py;
}
else if(keysHeld() & KEY_UP) {
currentPosY -= MOVEMENT_RATE;
}
else if(keysHeld() & KEY_DOWN) {
currentPosY += MOVEMENT_RATE;
}
else if(keysHeld() & KEY_LEFT) {
currentPosX -= MOVEMENT_RATE;
}
else if(keysHeld() & KEY_RIGHT) {
currentPosX += MOVEMENT_RATE;
}
else if(keysHeld() & KEY_L) {
angle -= degreesToAngle(ROTATION_RATE);
}
else if(keysHeld() & KEY_R) {
angle += degreesToAngle(ROTATION_RATE);
}

oamRotateScale(&oamMain,
0, // rotation ID (we can have up to 32 different affine matrices!)
angle, // angle of the rotation
intToFixed(1, 8), // inverse scale factor of X
intToFixed(1, 8) // inverse scale factor of Y
);

oamRotateScale(&oamSub,
0, // rotation ID (we can have up to 32 different affine matrices!)
angle * 2, // angle of the rotation (double the main screen[0])
intToFixed(1, 8), // inverse scale factor of X
intToFixed(1, 8) // inverse scale factor of Y
);

// set our new OAM data
oamSet(&oamMain,
0, // setting the OAM ID
touch.px, // X location of the sprite
touch.py, // Y location of the sprite
currentPosX, // X location of the sprite
currentPosY, // Y location of the sprite
0, // sprite priority
0, // palette index, or alpha value of using a bitmap sprite
SpriteSize_16x16, // size of the sprite
SpriteColorFormat_256Color, // color format of the sprite
gfx, // address of the sprite data
-1, // affine index of sprite (< 0 or > 31 means unrotated)
false, // double size for rotation?
0, // affine index of sprite (< 0 or > 31 means unrotated)
true, // double size for rotation? (so it doesn't get clipped)
false, // hide the sprite?
false, // V-flip the sprite?
false, // H-flip the sprite?
Expand All @@ -69,15 +118,15 @@ int main() {

oamSet(&oamSub,
0, // setting the OAM ID
touch.px, // X location of the sprite
touch.py, // Y location of the sprite
currentPosX, // X location of the sprite
currentPosY, // Y location of the sprite
0, // sprite priority
0, // palette index, or alpha value of using a bitmap sprite
SpriteSize_16x16, // size of the sprite
SpriteColorFormat_256Color, // color format of the sprite
gfxSub, // address of the sprite data
-1, // affine index of sprite (< 0 or > 31 means unrotated)
false, // double size for rotation?
0, // affine index of sprite (< 0 or > 31 means unrotated)
true, // double size for rotation? (so it doesn't get clipped)
false, // hide the sprite?
false, // V-flip the sprite?
false, // H-flip the sprite?
Expand Down

0 comments on commit d9df6bc

Please sign in to comment.