#include <rNES.h>

byte sprite_tiles[] = { 	
	// This tile will be used with Sprite 0,	for the Sprite 0 hit
	0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	
	// this tile is used with sprite 1
	#include "graphics\ball.tile.h"
};

byte earth_tiles[] = { 
	#include "graphics\earth.tiles.h" //13 x 13 = 169 tiles
};

byte font[] = {
	#include "graphics\font.h"
};

byte charset[] = {
	#include "graphics\charset.h"
};

byte bkg_pal[] = { 
	0x0E, 0x19, 0x10, 0x01,
	0x0E, 0x28, 0x18, 0x06,
	0x0E, 0x14, 0x04, 0x00 };

byte spr_pal[] = { 
	0x0E, 0x06, 0x08, 0x01,
	0x0E, 0x01, 0x03, 0x01,
	0x0E, 0x14, 0x15, 0x01,
	0x0E, 0x09, 0x0B, 0x01
};

byte joy;
rNES_sprite *sprites;
word i,j,p;
byte sx,sy,PPU_status;

void main()
{
	const char* author = "Modified by Sebastian Mihai, 2012";
	rNES_init();
	rNES_sound_init( rNES_snd_chn_1 | rNES_snd_chn_2 );	
	rNES_disable_graphics();	
	
	rNES_copy_vram( bkg_pal, 12, rNES_back_palette );
	rNES_copy_vram( spr_pal, 16, rNES_sprite_palette );
	
	rNES_copy_tiles( font, 0, rNES_back_tiles, 64 );
	rNES_copy_tiles( earth_tiles, 0, rNES_back_tiles+64, 169 );
	rNES_copy_tiles( sprite_tiles, 0, rNES_sprite_tiles, 2 );
	
	p = 64;
	// copy background to memory
	for( j=0; j!=13; ++j )
		for( i=0; i!=13; ++i )
		{
			rNES_put_vram( rNES_name_table_0 + (j+10)*32 + i+19, p++ );
		}
	
	rNES_fill_vram( rNES_attr_table_0, 8, bin(0,1,0,1,0,1,0,1) );
	rNES_fill_vram( rNES_name_table_1-16, 16, bin(1,0,1,0,1,0,1,0) );
	
	rNES_print( "THIS PART OF THE SCREEN DOESN'T SCROLL, THANKS TO THE SPRITE 0  HIT TEST.", 
		0, 1, rNES_name_table_0, charset );

	rNES_print( "D-PAD ; SCROLL", 
		18, 27, rNES_name_table_0, charset );
	
	// clear sprite area
	rNES_fill_ram( 0x0200, 0x0100, 0 );
	// start from first sprite
	sprites = (rNES_sprite *)0x0200;
	
	// sprite 0 (showing a plain black tile)
	sprites->tile = 0;
	sprites->attr = rNES_sprite_behind_bg;
	sprites->x = 0;
	sprites->y = 30;
	
	// move to next sprite
	++sprites;
	// sprite 1 (showing ball tile)
	sprites->tile = 1; //indexed in sprite_tiles
	sprites->x = 128;
	sprites->y = 128;
	sprites->attr = 0;
	
	rNES_enable_graphics();
	rNES_waitvblank();
	rNES_sprite_dma( 2, 0 );
	
	sx=50;
	sy = 128;
	p = 1700;
	
	while(1)
	{
		joy = rNES_get_joy( rNES_JOYPAD_1 );
		
		if( rNES_RIGHT_Down( joy ) )
		{
			--sx;
		}
		else if( rNES_LEFT_Down( joy ) )
		{
			++sx;
		}
		
		if( rNES_UP_Down( joy ) )
		{
			--sy;
		}
		else if( rNES_DOWN_Down( joy ) )
		{
			++sy;
		}

		if( joy )
		{
			rNES_sound( rNES_snd_chn_1, rNES_sndlen_10, 10, p );
			//rNES_sound( rNES_snd_chn_2, rNES_sndlen_8, 8, j );
			sprites->y = sy;
		}
			
		while( true )
		{
			PPU_status = rNES_PPU_status();
			
			if( rNES_sprite0hit( PPU_status ) )
				rNES_scroll(sx,0);
			
			if( rNES_VBlank( PPU_status ) )
				break;
		}
		rNES_sprite_dma( 2, 0 );
		rNES_scroll(0,0);
		
	}
}















