Skip to content

Commit

Permalink
Added sprite support and improved 4MAW framework
Browse files Browse the repository at this point in the history
  • Loading branch information
Sumolari committed Jan 31, 2015
1 parent 41089f9 commit 9c870fb
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 9 deletions.
7 changes: 7 additions & 0 deletions src/FMAW.h
@@ -0,0 +1,7 @@
#ifndef FMAW_H
#define FMAW_H

#include "./sprite.h"
#include "./debug.h"

#endif
2 changes: 2 additions & 0 deletions src/debug.h
@@ -1,6 +1,8 @@
#ifndef FMAW_DEBUG_H
#define FMAW_DEBUG_H

#include "./debug.h"

namespace FMAW {

/**
Expand Down
55 changes: 46 additions & 9 deletions src/main.cpp
Expand Up @@ -4,7 +4,7 @@
#include <nds/debug.h>
#include <sstream>

#include "./debug.h"
#include "./FMAW.h" // Import our awesome framework!

//------------------------------------------------------------------------------
// Graphic references
Expand All @@ -15,7 +15,7 @@
#include "./gfx_gradient.h"

//------------------------------------------------------------------------------
// Tile entries
// Background tile entries
//------------------------------------------------------------------------------

#define TILE_EMPTY 0 // Tile 0 = empty
Expand All @@ -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).

Expand All @@ -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_cast<u16*>BG_MAP_RAM(1))
#define bg1map (reinterpret_cast<u16*>BG_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
//------------------------------------------------------------------------------
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
17 changes: 17 additions & 0 deletions src/sprite.cpp
@@ -0,0 +1,17 @@
#include <nds.h>
#include <nds/debug.h>

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

}
34 changes: 34 additions & 0 deletions 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_cast<FMAW::spriteEntry*>OAM)

#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

0 comments on commit 9c870fb

Please sign in to comment.