From 9c870fba4f83b2095bddae0d5bcd8639382a45a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Llui=CC=81s=20Ulzurrun=20de=20Asanza=20i=20Sa=CC=80ez?= Date: Sat, 31 Jan 2015 12:06:36 +0100 Subject: [PATCH] Added sprite support and improved 4MAW framework --- src/FMAW.h | 7 +++++++ src/debug.h | 2 ++ src/main.cpp | 55 +++++++++++++++++++++++++++++++++++++++++--------- src/sprite.cpp | 17 ++++++++++++++++ src/sprite.h | 34 +++++++++++++++++++++++++++++++ 5 files changed, 106 insertions(+), 9 deletions(-) create mode 100644 src/FMAW.h create mode 100644 src/sprite.cpp create mode 100644 src/sprite.h diff --git a/src/FMAW.h b/src/FMAW.h new file mode 100644 index 0000000..8903dcf --- /dev/null +++ b/src/FMAW.h @@ -0,0 +1,7 @@ +#ifndef FMAW_H +#define FMAW_H + +#include "./sprite.h" +#include "./debug.h" + +#endif \ No newline at end of file diff --git a/src/debug.h b/src/debug.h index 052e4b6..a2d6d55 100644 --- a/src/debug.h +++ b/src/debug.h @@ -1,6 +1,8 @@ #ifndef FMAW_DEBUG_H #define FMAW_DEBUG_H +#include "./debug.h" + namespace FMAW { /** diff --git a/src/main.cpp b/src/main.cpp index 72d0b27..31deded 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,7 +4,7 @@ #include #include -#include "./debug.h" +#include "./FMAW.h" // Import our awesome framework! //------------------------------------------------------------------------------ // Graphic references @@ -15,7 +15,7 @@ #include "./gfx_gradient.h" //------------------------------------------------------------------------------ -// Tile entries +// Background tile entries //------------------------------------------------------------------------------ #define TILE_EMPTY 0 // Tile 0 = empty @@ -26,9 +26,13 @@ #define tile2bgram(t) (BG_GFX + (t) * 16) //------------------------------------------------------------------------------ -// Palette entries +// Background... //------------------------------------------------------------------------------ +//----------//------------------------------------------------------------------ +//----------// Palette entries +//----------//------------------------------------------------------------------ + #define PAL_BRICKS 0 // Brick palette (entry 0->15). #define PAL_GRADIENT 1 // Gradient palette (entry 16->31). @@ -37,12 +41,35 @@ // Macro for calculating BG VRAM memory address with palette index. #define pal2bgram(p) (BG_PALETTE + (p) * 16) -//------------------------------------------------------------------------------ -// BG Screen Base Blocks pointed -//------------------------------------------------------------------------------ +//----------//------------------------------------------------------------------ +//----------// Screen base blocks pointed +//----------//------------------------------------------------------------------ + #define bg0map (reinterpret_castBG_MAP_RAM(1)) #define bg1map (reinterpret_castBG_MAP_RAM(2)) +//------------------------------------------------------------------------------ +// Sprites... +//------------------------------------------------------------------------------ + +//----------//------------------------------------------------------------------ +//----------// Tile entries +//----------//------------------------------------------------------------------ + +#define TILES_BALL 0 // Ball tiles (16x16 tile: 0 -> 3) + +// Macro for calculating Sprite VRAM memory address with tile index. +#define tile2objram(t) (SPRITE_GFX + (t) * 16) + +//----------//------------------------------------------------------------------ +//----------// Palette entries +//----------//------------------------------------------------------------------ + +#define PAL_BALL 0 // Ball palette (entry 0 -> 15) + +// Macro for calculating Palette VRAM memorya address with palette index. +#define pal2objram(p) (SPRITE_PALETTE + (p) * 16) + //------------------------------------------------------------------------------ // Main code section //------------------------------------------------------------------------------ @@ -64,23 +91,35 @@ void setupGraphics(void) { dmaCopyHalfWords(3, gfx_gradientTiles, tile2bgram(TILE_GRADIENT), gfx_gradientTilesLen); - // Palettes go to palette memory. + // Copy Sprites graphics. + dmaCopyHalfWords(3, gfx_ballTiles, tile2objram(TILES_BALL), + gfx_ballTilesLen); + + // BG palettes go to palette memory. dmaCopyHalfWords(3, gfx_brickPal, pal2bgram(PAL_BRICKS), gfx_brickPalLen); dmaCopyHalfWords(3, gfx_gradientPal, pal2bgram(PAL_GRADIENT), gfx_gradientPalLen); + // Sprite palettes go to palette memory. + dmaCopyHalfWords(3, gfx_ballPal, pal2objram(PAL_BALL), gfx_ballPalLen); + // Set backdrop color. BG_PALETTE[0] = BACKDROP_COLOR; // libnds prefixes the register names with REG_ REG_BG0CNT = BG_MAP_BASE(1); REG_BG1CNT = BG_MAP_BASE(2); + + videoSetMode(MODE_0_2D | DISPLAY_BG0_ACTIVE | DISPLAY_BG1_ACTIVE | + DISPLAY_SPR_ACTIVE | DISPLAY_SPR_1D_LAYOUT); } void update_logic() { } void update_graphics() { + FMAW::clearAllSprites(); + // Clear entire bricks' tilemap and gradient's tilemap to zero for ( int n = 0; n < 1024; n++ ) { bg0map[n] = 0; @@ -128,8 +167,6 @@ void update_graphics() { // 0010000010000010 REG_BLDCNT = BLEND_ALPHA | BLEND_SRC_BG1 | BLEND_DST_BACKDROP; REG_BLDALPHA = (4) + (16 << 8); // This is computed at compile time. - - videoSetMode(MODE_0_2D | DISPLAY_BG0_ACTIVE | DISPLAY_BG1_ACTIVE); } int main(void) { diff --git a/src/sprite.cpp b/src/sprite.cpp new file mode 100644 index 0000000..3433595 --- /dev/null +++ b/src/sprite.cpp @@ -0,0 +1,17 @@ +#include +#include + +#include "./sprite.h" + +namespace FMAW { + +void clearAllSprites(void) { + for ( sprite_id id = 0; id < TOTAL_SPRITES; id++ ) + disableSprite( id ); +} + +void disableSprite( sprite_id id ) { + sprites[id].attr0 = ATTR0_DISABLED; +} + +} \ No newline at end of file diff --git a/src/sprite.h b/src/sprite.h new file mode 100644 index 0000000..1346801 --- /dev/null +++ b/src/sprite.h @@ -0,0 +1,34 @@ +#ifndef FMAW_SPRITE_H +#define FMAW_SPRITE_H + +namespace FMAW { + +// Access to sprites' memory, in an array-like way. +#define sprites (reinterpret_castOAM) + +#define TOTAL_SPRITES 128 // Total amount of sprites that can be rendered. + +// Low-level data structure to work with NDS libraries. +typedef struct t_spriteEntry { + uint16 attr0; + uint16 attr1; + uint16 attr2; + uint16 affine_data; +} spriteEntry; + +typedef uint8 sprite_id; + +/** + * Clears all the sprites. + */ +void clearAllSprites(void); + +/** + * Disables given sprite (it won't be rendered and no CPU cycles will be wasted + * on it). + */ +void disableSprite( sprite_id id ); + +} + +#endif \ No newline at end of file