Graphics/Sprites/fire_and_sprites/source/main.cpp
#include <stdlib.h>
#include <string.h>
#include <nds.h>
#include "ball_pcx.h"
#define NUM_SPRITES 128
SpriteEntry OAMCopySub[128];
typedef struct {
int x,y;
int dx, dy;
SpriteEntry* oam;
int gfxID;
}Sprite;
void MoveSprite(Sprite* sp) {
int x = sp->x >> 8;
int y = sp->y >> 8;
sp->oam->x = x;
sp->oam->y = y;
}
void initOAM(void) {
int i;
for(i = 0; i < 128; i++) {
OAMCopySub[i].attribute[0] = ATTR0_DISABLED;
}
}
void updateOAM(void) {
memcpy(OAM_SUB, OAMCopySub, 128 * sizeof(SpriteEntry));
}
int main(void) {
uint16* back = VRAM_A;
uint16* front = VRAM_B;
Sprite sprites[NUM_SPRITES];
int i, delta = 0;
int ix = 0;
int iy = 0;
int screen = 1;
uint16* map0 = (uint16*)SCREEN_BASE_BLOCK_SUB(1);
uint16* map1 = (uint16*)SCREEN_BASE_BLOCK_SUB(2);
uint16 red;
videoSetMode(MODE_FB1);
videoSetModeSub(MODE_0_2D |
DISPLAY_SPR_1D_LAYOUT |
DISPLAY_SPR_ACTIVE |
DISPLAY_BG0_ACTIVE |
DISPLAY_BG1_ACTIVE );
vramSetMainBanks(VRAM_A_LCD, VRAM_B_LCD, VRAM_C_SUB_BG, VRAM_D_SUB_SPRITE);
sImage ball;
loadPCX((u8*)ball_pcx, &ball);
imageTileData(&ball);
for(i = 0; i < 256; i++)
SPRITE_PALETTE_SUB[i] = ball.palette[i];
for(i = 0; i< 32*16; i++)
SPRITE_GFX_SUB[i] = ball.image.data16[i];
initOAM();
for(i = 0; i < NUM_SPRITES; i++) {
sprites[i].x = rand() & 0xFFFF;
sprites[i].y = rand() & 0x7FFF;
sprites[i].dx = (rand() & 0xFF) + 0x100;
sprites[i].dy = (rand() & 0xFF) + 0x100;
if(rand() & 1)
sprites[i].dx = -sprites[i].dx;
if(rand() & 1)
sprites[i].dy = -sprites[i].dy;
sprites[i].oam = &OAMCopySub[i];
sprites[i].gfxID = 0;
sprites[i].oam->attribute[0] = ATTR0_COLOR_256 | ATTR0_SQUARE;
sprites[i].oam->attribute[1] = ATTR1_SIZE_32;
sprites[i].oam->attribute[2] = sprites[i].gfxID;
}
REG_BG0CNT_SUB = BG_COLOR_256 | (1 << MAP_BASE_SHIFT);
REG_BG1CNT_SUB = BG_COLOR_256 | (2 << MAP_BASE_SHIFT);
BG_PALETTE_SUB[0] = RGB15(10,10,10);
BG_PALETTE_SUB[1] = RGB15(0,16,0);
BG_PALETTE_SUB[2] = RGB15(0,0,31);
for(iy = 0; iy < 32; iy++) {
for(ix = 0; ix <32; ix++) {
map0[iy * 32 + ix] = (ix ^ iy) & 1;
map1[iy * 32 + ix] = ((ix ^ iy) & 1)<<1;
}
}
for(i = 0; i < 64 / 2; i++) {
BG_GFX_SUB[i+32] = 0x0101;
BG_GFX_SUB[i+32+32] = 0x0202;
}
while (1) {
REG_BG0HOFS = delta ;
REG_BG0VOFS = delta++ ;
for(i = 0; i < NUM_SPRITES; i++) {
sprites[i].x += sprites[i].dx;
sprites[i].y += sprites[i].dy;
if(sprites[i].x < (1<<8) || sprites[i].x > (247 << 8))
sprites[i].dx = -sprites[i].dx;
if(sprites[i].y < (1<<8) || sprites[i].y > (182 << 8))
sprites[i].dy = -sprites[i].dy;
MoveSprite(&sprites[i]);
}
for(ix = 0; ix < SCREEN_WIDTH; ix++) {
back[ix + SCREEN_WIDTH * (SCREEN_HEIGHT - 1)] = rand()& 0xFFFF;
back[ix + SCREEN_WIDTH * (SCREEN_HEIGHT - 2)] = rand()& 0xFFFF;
}
back++;
for(iy = 1; iy < SCREEN_HEIGHT - 2 ; iy++) {
for(ix = 1; ix < SCREEN_WIDTH - 1; ix++) {
red = 0;
red += front[0];
red += front[2];
front += SCREEN_WIDTH;
red += front[0];
red += front[1];
red += front[2];
front += SCREEN_WIDTH;
red += front[0];
red += front[1];
red += front[2];
front -= (2 * SCREEN_WIDTH) - 1;
back[0] = (red >> 3);
back++;
}
back += 2;
front += 2;
}
swiWaitForVBlank();
updateOAM();
if(screen) {
videoSetMode(MODE_FB1);
front = VRAM_B;
back = VRAM_A;
screen = 0;
} else {
videoSetMode(MODE_FB0);
front = VRAM_A;
back = VRAM_B;
screen = 1;
}
}
return 0;
}