/*
 * prep.c
 *
 *  Created on: Dec 24, 2009
 *      Author: ÓHerman van der MeulenÓ
 */

#include "prep.h"

int blocksIn[54];

void wait(u8 milliSecs)
{
	u8 z;
	for (z = 0; z < milliSecs; z++)
		swiWaitForVBlank();
}

void parseBlocksArray()
{
	int b;
	for (b = 0; b < 54; b++)
		blocksIn[b] = levels[currentLevel][b];

	blocks[0][0] = -4; // TODO this needed???
	int x = 1;
	int y = 1;
	int a = 0;
	for (; a < 54; a++)
	{
		// set first position of route
		if (blocksIn[a] == 2)
		{
			currentPosition.x = x;
			currentPosition.y = y;
		}
		blocks[x][y] = blocksIn[a];
		blocksBackground[x][y] = blocksIn[a];

		if (x == placedPosition.x && y == placedPosition.y)
		{
			blocks[x][y] = 1;
			blocksBackground[x][y] = 1;
		}

		x++;
		if (x == 10)
		{
			y++;
			x = 1;
		}
	}
}

void resetField(int waitOrNot)
{
	parseBlocksArray();
	drawField(waitOrNot);
}

/* prepare the game
 * - set the screens
 * - draw background
 * - draw the play field
 * - init the sound
 */
void prep()
{
	//consoleDemoInit();
	// set main screen
	videoSetMode(MODE_FB0);
	vramSetBankA(VRAM_A_LCD);
	// set sub screen
	videoSetModeSub(MODE_5_2D);
	vramSetBankC(VRAM_C_SUB_BG);
	// draw background of sub screen
	bgInitSub(3, BgType_Bmp16, BgSize_B16_256x256, 0, 0);
	video_buffer2 = (u16*) BG_BMP_RAM_SUB(0);
	splashImage(video_buffer2, (u32 *) topscreen_img_bin);
	// swap the screens
	lcdSwap();

	drawLevel();
	resetField(1);
	soundInit();
}
