//ds2_main.c

#include "libBAG.h"

#define BLACK_COLOR		RGB15(0, 0, 0)
#define WHITE_COLOR		RGB15(31, 31, 31)

#define WalkSpeed 4

GFXObj_t BackGround, Sprite;
SNDObj_t Music;

void RenderScene(void){
	int ScrollX = 514,ScrollY = 319;//set background scroll
	bool Loop=1;

	//start playing the loaded music
	BAG_Audio_PlaySnd(&Music, 0);
	
	//set the sprites initial position
	BAG_Display_SetGfxBlitXY(&Sprite, (SCREEN_WIDTH>>1), (SCREEN_HEIGHT>>1));
	BAG_Display_StartAnim(&Sprite);//set the sprite for animation
	int *x, *y;//easy referencing to sprites position
	while(Loop){
		x = BAG_Display_GetGfxBlitX(&Sprite);
		y = BAG_Display_GetGfxBlitY(&Sprite);
		
		//Manipulate the sprite frame data to setup the proper animation for each specified direction
		if(Pad.Newpress.Down){
			BAG_Display_SetGfxFrameDim(&Sprite, 22, 26);//set the sprites frame width and height
			BAG_Display_SetObjOffset(&Sprite, FRAME_HOR, 0);//set off set in the graphics buffer to 0
			BAG_Display_SetGfxFlipX(&Sprite, 0);//reset the horizontal flip of the sprite
		}
		else if(Pad.Newpress.Up){
			BAG_Display_SetGfxFrameDim(&Sprite, 22, 26);
			BAG_Display_SetObjOffset(&Sprite, FRAME_HOR, 470);
			BAG_Display_SetGfxFlipX(&Sprite, 0);
		}
		else if(Pad.Newpress.Left){
			BAG_Display_SetGfxFrameDim(&Sprite, 25, 26);
			VAL(BAG_Display_GetGfxOffsetX(&Sprite)) = 220;
			BAG_Display_SetGfxFlipX(&Sprite, 0);			
		}
		else if(Pad.Newpress.Right){
			BAG_Display_SetGfxFrameDim(&Sprite, 25, 26);
			VAL(BAG_Display_GetGfxOffsetX(&Sprite)) = 220;
			BAG_Display_SetGfxFlipX(&Sprite, 1);	
		}
		//Move the sprite and the background accordingly
		if(Pad.Held.Up){
			if(VAL(y) > ((SCREEN_HEIGHT>>1)-13) && ScrollY>0 ) VAL(y)-=WalkSpeed;
			else if(VAL(y)>0 && ScrollY <= 0)VAL(y)-=WalkSpeed;
			else if(ScrollY>0)ScrollY-=WalkSpeed;
		}
		else if(Pad.Held.Down){	
			if(VAL(y) < (SCREEN_HEIGHT >>1)-13 )VAL(y) +=WalkSpeed;
			else if(VAL(y)<SCREEN_HEIGHT-26 && ScrollY >= VAL(BAG_Display_GetGfxHeight(&BackGround))-SCREEN_HEIGHT)VAL(y)+=WalkSpeed;
			else if(ScrollY<VAL(BAG_Display_GetGfxHeight(&BackGround))-SCREEN_HEIGHT)ScrollY+=WalkSpeed;
		}
		else if(Pad.Held.Left){
			if(VAL(x) >(SCREEN_WIDTH>>1)-11 )VAL(x) -=WalkSpeed;
			else if(VAL(x)>0 && ScrollX <= 0)VAL(x)-=WalkSpeed;
			else if(ScrollX>0)ScrollX-=WalkSpeed;
		}
		else if(Pad.Held.Right){
			if(VAL(x) <(SCREEN_WIDTH>>1)-11  )VAL(x) +=WalkSpeed;
			else if(VAL(x)<SCREEN_WIDTH-26 && ScrollX >= VAL(BAG_Display_GetGfxWidth(&BackGround))-SCREEN_WIDTH)VAL(x)+=WalkSpeed;
			else if(VAL(x) < VAL(BAG_Display_GetGfxWidth(&BackGround))-SCREEN_WIDTH)ScrollX+=WalkSpeed;		
		}
		
		//update the sprites animation while a direction is pressed
		if(Pad.Held.Right || Pad.Held.Left || Pad.Held.Up || Pad.Held.Down)
			BAG_Display_UpdateAnim(&Sprite, 0, 9, 2, 1, FRAME_HOR);
		
		//draw all the graphics
		ds2_clearScreen(DOWN_SCREEN, BLACK_COLOR);
		//fast drawing won't draw transparent sections, rotated, enlarged, or shrunken graphics with any coloration effects
		BAG_Display_DrawObjFast(&BackGround, down_screen_addr, -ScrollX, -ScrollY);
		//regular drawing can handle all cases
		BAG_Display_DrawObj(&Sprite, down_screen_addr, VAL(x), VAL(y));
		ds2_flipScreen(DOWN_SCREEN, 0);
		
		//update input and audio
		BAG_Update();
	}
}
	
void ds2_main(void){

	if(!BAG_Init(1))
		ds2_plug_exit();

	BAG_DBG_Init(NULL, DBG_HALT | DBG_LIB);

	if(BAG_Display_LoadObjExt("/zelda-map", &BackGround) != ERR_NONE)//loads a png, bmp or jpg with the specified name
		BAG_DBG_Msg("error loading map\n");

	if(BAG_Display_LoadObjExt("/zelda-sprite", &Sprite) != ERR_NONE)
		BAG_DBG_Msg("error loading sprite\n");	
		
	if(BAG_Audio_LoadSndExt("/zelda-song", &Music)!= ERR_NONE)//loads a mp3, ogg or wav with the specified name
		BAG_DBG_Msg("error loading audio\n");
		
	RenderScene();
	while(1);
}
