Page 1 of 1

[SOLVED] REG_BG0CNT assign causing error in compilation

Posted: Fri Feb 04, 2011 1:35 am
by staltux
I have learning a tutorial from site http://ekid.nintendev.com/bouncy/index.php called "How to Make a Bouncing Ball Game"
but i have a problem when using the REG_BG0CNT register...
i try to this REG_BG0CNT = BG_MAP_BASE(1);... but the compiler said: error: expected identifier or '(' before 'vu16'.

now, the code to the begin from this section

Code: Select all

#include <nds.h>
//-----------------------------------------------
// graphic references
//-----------------------------------------------
#include "gfx_ball.h"
#include "gfx_brick.h"
#include "gfx_gradient.h"
//-----------------------------------------------
// tile entries
//-----------------------------------------------
#define tile_empty     0 // tile 0 = empty
#define tile_brick     1 // tile 1 = brick
#define tile_gradient  2 // tile 2 to 9 = gradient
// macro for calculating BG VRAM memory
// address with tile index
#define tile2bgram(t) (BG_GFX + (t) * 16)
// BG PALETTES
#define pal_bricks        0    // brick palette (entry 0->15)
#define pal_gradient      1    // gradient palette (entry 16->31)
#define backdrop_colour   RGB8( 0, 0, 255 )
#define pal2bgram(p)      (BG_PALETTE + (p) * 16)

// libnds prefixes the register names with REG_
// error at this point
REG_BG0CNT = BG_MAP_BASE(1);
REG_BG1CNT = BG_MAP_BASE(2);
// and this point
#define bg0map ( (u16*) BG_MAP_RAM(1) )
#define bg1map ( (u16*) BG_MAP_RAM(2) )
and the compiler output

Code: Select all

[alex@darkstar ds]$ make
main.c
arm-eabi-gcc -MMD -MP -MF /home/alex/workspace/ds/build/main.d -g -Wall -O2 -it-frame-pointer -ffast-math -mthumb -mthumb-interwork -march=armv5te -mtune=946e-s -iquote /home/alex/workspace/ds/include -I/home/alex/apps/devkitpro//lds/include -I/home/alex/workspace/ds/build -DARM9 -c /home/alex/workspace/ds/rce/main.c -o main.o 
/home/alex/workspace/ds/source/main.c:31:1: error: expected identifier or '('fore 'vu16'
/home/alex/workspace/ds/source/main.c:31:1: error: expected ')' before numerionstant
/home/alex/workspace/ds/source/main.c:32:1: error: expected identifier or '('fore 'vu16'
/home/alex/workspace/ds/source/main.c:32:1: error: expected ')' before numerionstant
make[1]: *** [main.o] Error 1
make: *** [build] Error 2
[img][/img]
I'm confused about this part, have never seen a pointer is declared with #define (the register REG_BG0CNT), so do not make the reason for the error.

the code without the last four lines, compile and work fine.

Re: REG_BG0CNT assign causing error in compilation

Posted: Fri Feb 04, 2011 9:51 pm
by WinterMute
Admittedly the tutorial isn't quite clear at that point but those two lines belong inside a function body. Something also worth mentioning is that it seems to have been written before irq init was done by system startup so you don't need irqInit();

I ended up with something like this when following through.

Code: Select all

#include <nds.h>


//-----------------------------------------------
// graphic references
//-----------------------------------------------
#include "gfx_ball.h"
#include "gfx_brick.h"
#include "gfx_gradient.h"

//-----------------------------------------------
// tile entries
//-----------------------------------------------

#define tile_empty	   0 // tile 0 = empty
#define tile_brick	   1 // tile 1 = brick
#define tile_gradient  2 // tile 2 to 9 = gradient

// macro for calculating BG VRAM memory
// address with tile index
#define tile2bgram(t) (BG_GFX + (t) * 16)

// BG PALETTES
#define pal_bricks		  0	   // brick palette (entry 0->15)
#define pal_gradient	  1	   // gradient palette (entry 16->31)

#define backdrop_colour	  RGB8( 190, 225, 255 )
#define pal2bgram(p)	  (BG_PALETTE + (p) * 16)

#define bg0map	  ((u16*)BG_MAP_RAM(1))
#define bg1map	  ((u16*)BG_MAP_RAM(2))

void setupGraphics( void )
{
//	  vramSetBankE( VRAM_E_MAIN_BG );
//	  vramSetBankF( VRAM_F_MAIN_SPRITE );
	
	// generate the first blank tile by clearing it to zero
	int n;
	for( n = 0; n < 16; n++ )
		BG_GFX[n] = 0;
	
	// copy bg graphics
	dmaCopyHalfWords( 3, gfx_brickTiles, tile2bgram( tile_brick ), gfx_brickTilesLen );
	dmaCopyHalfWords( 3, gfx_gradientTiles, tile2bgram( tile_gradient ), gfx_gradientTilesLen );
	
	// palettes goto palette memory
	dmaCopyHalfWords( 3, gfx_brickPal, pal2bgram(pal_bricks), gfx_brickPalLen );
	dmaCopyHalfWords( 3, gfx_gradientPal, pal2bgram(pal_gradient), gfx_gradientPalLen );

	// set backdrop color
	BG_PALETTE[0] = backdrop_colour;
	REG_BG0VOFS = 112;
}

int main( void ) {

	setupGraphics();
	videoSetMode( MODE_0_2D | DISPLAY_BG0_ACTIVE | DISPLAY_BG1_ACTIVE );

	REG_BG0CNT = BG_MAP_BASE(1);
	REG_BG1CNT = BG_MAP_BASE(2);

	int n;
	for( n = 0; n < 1024; n++ ) {
		bg0map[n] = 0;
		bg1map[n] = 0;
	}

	int x, y;
	for( x = 0; x < 32; x++ )
	{
		for( y = 0; y < 6; y++ )
		{
			// magical formula to calculate if the tile needs to be flipped.
			int hflip = (x & 1) ^ (y & 1);
			
			// set the tilemap entry
			bg0map[x + y * 32] = tile_brick | (hflip << 10) | (pal_bricks << 12);
		}
		for( y = 0; y < 8; y++ )
		{
			int tile = tile_gradient + y;
			bg1map[ x + y * 32 ] = tile | (pal_gradient << 12);
		}
	}


	while(1)  // infinite loops are perfectly normal when coding for consoles
	{
		// wait for the vblank period
		swiWaitForVBlank();

	}	 
	return 0;
}

Re: REG_BG0CNT assign causing error in compilation

Posted: Sat Feb 05, 2011 12:08 am
by staltux
Thank you, worked perfectly.
As for irqInit () I'll test the rom on real hardware, my ds with a flashcard, flashcard already should have called this method, if I may call it a problem arises? how to soft reset for example?

Re: [SOLVED] REG_BG0CNT assign causing error in compilation

Posted: Sun Feb 06, 2011 12:07 am
by WinterMute
For irqInit you don't need to test anything, the libnds startup code already handles this if you call it again from your code then things will fail.

For soft reset you'll need to be using the Homebrew Menu, see http://devkitpro.org/wiki/Homebrew_Menu. When you launch your homebrew from that menu then exit(0); or simply returning from main will restart hbmenu. No other launcher I'm aware of has this feature and all of them will simply power off the DS when you do this.