// The Main HAM Library
#include "mygba.h"

// Graphics Includes - for each image there is a 
//   separate palette
#include "gfx/bg.pal.c"
#include "gfx/bg.raw.c"
#include "gfx/bubbles.pal.c"
#include "gfx/bubbles.raw.c"
#include "gfx/paradise.pal.c"
#include "gfx/paradise.raw.c"
#include "gfx/water.pal.c"
#include "gfx/water.raw.c"

// Function Prototypes
void show_pic();

// Global Variables
u8 which_pic = 0; // 0 through 3
u8 update_screen = 0; // Does the screen need updating
u8 key_pressed = 0; // Was a button just pressed


// Main function
void AgbMain(void)
{
	// Initialize HAMlib
	ham_Init();

	// Setup the background mode
	ham_SetBgMode(4);

	// Display the first picture before we check for input

	// DMA the background picture
	TOOL_DMA1_SET(&bg_Bitmap,
				  MEM_BG_PTR,
				  SIZEOF_32BIT(bg_Bitmap),
				  DMA_TRANSFER_32BIT,
				  DMA_STARTAT_NOW)

	// Initialize the background palette
	ham_LoadBGPal(&bg_Palette,SIZEOF_16BIT(bg_Palette));

	// Infinite loop to keep the program running
	while(1) 
	{
		// First, determine if left or right was pressed

		// If the user presses right, and the right key was not 
		// just pressed, setup the next picture to be displayed
		if((!key_pressed) && (F_CTRLINPUT_RIGHT_PRESSED)) {
			update_screen = 1;
			key_pressed = 1;
			if (which_pic < 3) {
				which_pic++;
			} else {
				which_pic = 0;
			}
		// Same here, but left and decreasing
		} else if((!key_pressed) && (F_CTRLINPUT_LEFT_PRESSED)) {
			update_screen = 1;
			key_pressed = 1;
			if (which_pic > 0) {
				which_pic--;
			} else {
				which_pic = 3;
			}
		// If the buttons are released, update key_pressed
		} else if ((!F_CTRLINPUT_RIGHT_PRESSED) && 
			(!F_CTRLINPUT_LEFT_PRESSED)) {
			key_pressed = 0;
		}

		// See if the screen needs to be updated
		if (update_screen) {
			show_pic();
			update_screen = 0;
		}
	} // End of while(1)

	return; 
} // End of AgbMain()


// Draw the Start Screen
void show_pic()
{
	if (which_pic == 0) {
		// DMA the background picture
		TOOL_DMA1_SET(&bg_Bitmap,
					  MEM_BG_PTR,
					  SIZEOF_32BIT(bg_Bitmap),
					  DMA_TRANSFER_32BIT,
					DMA_STARTAT_NOW)

		// Initialize the background palette
		ham_LoadBGPal(&bg_Palette,SIZEOF_16BIT(bg_Palette));
	} else if(which_pic == 1) {
		// DMA the background picture
		TOOL_DMA1_SET(&bubbles_Bitmap,
					  MEM_BG_PTR,
					  SIZEOF_32BIT(bubbles_Bitmap),
					  DMA_TRANSFER_32BIT,
					DMA_STARTAT_NOW)

		// Initialize the background palette
		ham_LoadBGPal(&bubbles_Palette,SIZEOF_16BIT(bubbles_Palette));
	} else if(which_pic == 2) {
		// DMA the background picture
		TOOL_DMA1_SET(&paradise_Bitmap,
					  MEM_BG_PTR,
					  SIZEOF_32BIT(paradise_Bitmap),
					  DMA_TRANSFER_32BIT,
					DMA_STARTAT_NOW)

		// Initialize the background palette
		ham_LoadBGPal(&paradise_Palette,SIZEOF_16BIT(paradise_Palette));
	} else if(which_pic == 3) {
		// DMA the background picture
		TOOL_DMA1_SET(&water_Bitmap,
					  MEM_BG_PTR,
					  SIZEOF_32BIT(water_Bitmap),
					  DMA_TRANSFER_32BIT,
					DMA_STARTAT_NOW)

		// Initialize the background palette
		ham_LoadBGPal(&water_Palette,SIZEOF_16BIT(water_Palette));
	}

	return;
} // End of show_pic()