// The Main HAM Library
#include "mygba.h"

// Graphics Includes
// gfx2gba -fsrc -m -protate.pal -rs -t8 rotate.bmp
#include "gfx/rotate.map.c"
#include "gfx/rotate.pal.c"
#include "gfx/rotate.raw.c"

// Global Variables
u32 frames = 0;
u32 zoomx = 256; // X co-ordinate of zoom center
u32 zoomy = 256; // Y co-ordinate of zoom center
u32 scrollx = 64; // X co-ordinate of scrolling center
u32 scrolly = 64; // Y co-ordinate of scrolling center
bool zoomin = 1;

// Function Prototypes
void vblFunc(); // VBL function
void query_keys(); // Query the keyboard
void redraw(); // Redraw the screen


// Main function
int main()
{	
	// Initialize HAMlib
	ham_Init();

	// Setup the background mode
	ham_SetBgMode(1);

	// Initialize the palettes
	ham_LoadBGPal(&rotate_Palette,256); // Background palette

	// Initialize the rotating background
	ham_bg[2].ti = ham_InitTileSet(&rotate_Tiles,SIZEOF_16BIT(rotate_Tiles),1,1);
	ham_bg[2].mi = ham_InitMapSet(&rotate_Map,256,0,1);

	// Display the background
	ham_InitBg(2,1,1,0);

	// Move the background to the center
	ham_RotBgEx(2,0,120,80,scrollx,scrolly,zoomx,zoomy);

	// Start the VBL interrupt handler
    ham_StartIntHandler(INT_TYPE_VBL,&vblFunc);

	while(1)
	{
		// Infinite loop to keep the program running
	}

	return EXIT_SUCCESS; 
} // End of main()


// VBL function
void vblFunc()
{
	ham_CopyObjToOAM(); // Copy plane sprite to hardware
	query_keys(); // Check for movement
	redraw(); // Redraw the plane

	// Rotate the background
	ham_RotBgEx(2,frames%360,120,80,scrollx,scrolly,zoomx,zoomy);

	// Increment the frames
	++frames;

	return;
} // End of vblFunc()


// Query the keyboard
void query_keys()
{
	// RIGHT only
	if (F_CTRLINPUT_RIGHT_PRESSED)
	{
		if (scrollx < 127) ++scrollx;
	}

	// LEFT only
	if (F_CTRLINPUT_LEFT_PRESSED)
	{
		if (scrollx > 0) --scrollx;
	}

	// START only
	if (F_CTRLINPUT_START_PRESSED) {
		scrollx = 64;
		scrolly = 64;
	}

	return;
} // End of query_keys()


// Redraw the screen
void redraw()
{
	if (zoomin) { // Zoom in
		if (zoomx != 396) {
			zoomx+=2;
			zoomy+=2;
		} else {
			zoomin = FALSE;
		}
	} else { // Zoom out
		if (zoomx != 116) {
			zoomx-=2;
			zoomy-=2;
		} else {
			zoomin = TRUE;
		}
	}

	return;
} // End of redraw()

