#include "Game.h""

Game::Game()
{
	running = false;
}

Game::~Game()
{
}

void Game::updateMouse()
{
	sf::Vector2i position = sf::Mouse::getPosition(window);

	// convert it to world coordinates
	Vector2i mousePosition = (sf::Vector2i) window.mapPixelToCoords(position);

	mouse.iX             = mousePosition.x;
	mouse.iY             = mousePosition.y;

	mouse.bDownBtnLeft   = false;
	mouse.bUpBtnLeft     = false;
	mouse.bDownBtnRight  = false;
	mouse.bUpBtnRight    = false;

	if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
	{
		if (!mouse.bPressBtnLeft)
		{
			mouse.bPressBtnLeft = true;
			mouse.bDownBtnLeft  = true;
		}
	}
	else
	{
		if (mouse.bPressBtnLeft)
			mouse.bUpBtnLeft = true;

		mouse.bPressBtnLeft = false;		
	}

	if (sf::Mouse::isButtonPressed(sf::Mouse::Right))
	{
		if( !mouse.bPressBtnRight )
		{
			mouse.bPressBtnRight = true;
			mouse.bDownBtnRight  = true;
		}
	}
	else
	{
		if(mouse.bPressBtnRight)
			mouse.bUpBtnRight = true;

		mouse.bPressBtnRight = false;		
	}
}

void Game::updateKeyboard()
{	
	keyboard.keyDownUp    = false;
	keyboard.keyDownRight = false;
	keyboard.keyDownDown  = false;
	keyboard.keyDownLeft  = false;

	keyboard.keyUpUp    = false;
	keyboard.keyUpRight = false;
	keyboard.keyUpDown  = false;
	keyboard.keyUpLeft  = false;

	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
	{
		if( !keyboard.keyPressUp )
		{
			keyboard.keyPressUp = true;
			keyboard.keyDownUp  = true;
		}
	}
	else
	{
		if (keyboard.keyPressUp)
			keyboard.keyUpUp = true;

		keyboard.keyPressUp = false;
	}



	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
	{
		if( !keyboard.keyPressRight )
		{
			keyboard.keyPressRight = true;
			keyboard.keyDownRight  = true;
		}
	}
	else
	{
		if (keyboard.keyPressRight)
			keyboard.keyUpRight = true;

		keyboard.keyPressRight = false;
	}



	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
	{
		if( !keyboard.keyPressDown )
		{
			keyboard.keyPressDown = true;
			keyboard.keyDownDown  = true;
		}
	}
	else
	{
		if (keyboard.keyPressDown)
			keyboard.keyUpDown = true;

		keyboard.keyPressDown = false;
	}



	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
	{
		if( !keyboard.keyPressLeft )
		{
			keyboard.keyPressLeft = true;
			keyboard.keyDownLeft  = true;
		}
	}
	else
	{
		if (keyboard.keyPressLeft)
			keyboard.keyUpLeft = true;

		keyboard.keyPressLeft = false;
	}
}

void Game::update()
{
	updateMouse();
	cursorSpr.setPosition(mouse.iX - 2, mouse.iY);

	switch (eCurrentScene)
	{
		case SCENE_LOGOS    : updateSceneLogos();    break;
		case SCENE_MENU     : updateSceneMenu();     break;
		case SCENE_HOWPLAY  : updateSceneHowPlay(); break;
		case SCENE_LEVELS   : updateSceneLevels();   break;
		case SCENE_GAME     : updateSceneGame();     break;
	}

	switch (eSplashState)
	{
		case SPLASH_IN  : 
			iSplashFrame -= 1;
			
			if (iSplashFrame <= 0)
			{
				iSplashFrame = 0;
				eSplashState = SPLASH_OUT;
			}

			splashSpr.setTextureRect(IntRect(iSplashFrame * 16, 0, 16, 16));
			break;

		case SPLASH_OUT :
			iSplashFrame += 1;

			if (iSplashFrame >= 8)
			{
				iSplashFrame = 8;
				eSplashState = SPLASH_NONE;
			}

			splashSpr.setTextureRect(IntRect(iSplashFrame * 16, 0, 16, 16));
			break;
		default : break;
	}
}

void Game::render()
{
	switch (eCurrentScene)
	{
		case SCENE_LOGOS    : drawSceneLogos();    break;
		case SCENE_MENU     : drawSceneMenu();     break;
		case SCENE_HOWPLAY  : drawSceneHowPlay(); break;
		case SCENE_LEVELS   : drawSceneLevels();   break;
		case SCENE_GAME     : drawSceneGame();     break;
	}

	if (eSplashState != SPLASH_NONE)
	{
		for (int y = 0; y < 12; y++)
		for (int x = 0; x < 16; x++)
		{
			splashSpr.setPosition(x * 16, y * 16);
			window.draw(splashSpr);
		}
	}

	window.display();
}

void Game::drawText(int iX, int iY, std::string sText)
{
	IntRect rect;
	bool bSkip;
	int iCharWidth;
	int iCharHeight;
	int iPosX = iX;

	int iCount = sText.length();

	if (iCount > 0)
	for(int i = 0; i < iCount; ++i)
	{
		bSkip = false;

		iCharWidth  = 3;
		iCharHeight = 5;

		switch(sText[i])
		{
			case ' ' : rect = IntRect(0, 2, 3, 5); break;
			case 'а' : rect = IntRect(4, 2, 3, 5); break;
			case 'б' : rect = IntRect(8, 2, 3, 5); break;
			case 'в' : rect = IntRect(12, 2, 3, 5); break;
			case 'г' : rect = IntRect(16, 2, 3, 5); break;
			case 'д' : rect = IntRect(20, 2, 5, 5); iCharWidth = 5; break;
			case 'е' : rect = IntRect(26, 2, 3, 5); break;
			case 'ё' : rect = IntRect(30, 2, 3, 5); break;
			case 'ж' : rect = IntRect(34, 2, 7, 5); iCharWidth = 7; break;
			case 'з' : rect = IntRect(42, 2, 3, 5); break;
			case 'и' : rect = IntRect(46, 2, 4, 5); iCharWidth = 4; break;
			case 'й' : rect = IntRect(51, 2, 4, 5); iCharWidth = 4; break;
			case 'к' : rect = IntRect(56, 2, 3, 5); break;
			case 'л' : rect = IntRect(60, 2, 3, 5); break;
			case 'м' : rect = IntRect(64, 2, 5, 5); iCharWidth = 5; break;
			case 'н' : rect = IntRect(70, 2, 3, 5); break;
			case 'о' : rect = IntRect(74, 2, 3, 5); break;
			case 'п' : rect = IntRect(78, 2, 3, 5); break;
			case 'р' : rect = IntRect(82, 2, 3, 5); break;
			case 'с' : rect = IntRect(86, 2, 3, 5); break;
			case 'т' : rect = IntRect(90, 2, 3, 5); break;
			default  : bSkip = true; break;
		}
		
		if (!bSkip)
		{
			fontSpr.setTextureRect(rect);

			
			fontSpr.setPosition(iPosX, iY);
			iPosX = iPosX + iCharWidth + 1;

			window.draw(fontSpr);
		}
	}
}

void Game::drawNumber(int iX, int iY, int iNumber)
{
	char res[16];
	sprintf_s(res, "%d", iNumber);
	std::string sNumber = std::string(res);

	IntRect rect;
	bool bSkip;
	int iCharWidth;
	int iPosX = iX;
	int iCount = sNumber.length();

	if (iCount > 0)
	for(int i = 0; i < iCount; ++i)
	{
		bSkip = false;
		iCharWidth  = 3;

		switch(sNumber[i])
		{
			case '0' : rect = IntRect(0, 8, 3, 5); break;
			case '1' : rect = IntRect(4, 8, 3, 5); break;
			case '2' : rect = IntRect(8, 8, 3, 5); break;
			case '3' : rect = IntRect(12, 8, 3, 5); break;
			case '4' : rect = IntRect(16, 8, 3, 5); break;
			case '5' : rect = IntRect(20, 8, 3, 5); break;
			case '6' : rect = IntRect(24, 8, 3, 5); break;
			case '7' : rect = IntRect(28, 8, 3, 5); break;
			case '8' : rect = IntRect(32, 8, 3, 5); break;
			case '9' : rect = IntRect(36, 8, 3, 5); break;
			default  : bSkip = true; break;
		}

		if (!bSkip)
		{
			fontSpr.setTextureRect(rect);

			fontSpr.setPosition(iPosX, iY);
			iPosX = iPosX + iCharWidth + 1;

			window.draw(fontSpr);
		}
	}
}

void Game::clearLevel()
{
	for (int y = 0; y < MAP_SIZE_Y; y++)
	for (int x = 0; x < MAP_SIZE_X; x++)
	{
		stLevel[x][y].eCell = CELL_EMPTY;
	}

	listBoxes.clear();

	respawnLevel = Vector2i(0, 0);
	iCountMarkers = 0;
	bWin = false;
}

bool Game::loadLevel(std::string sFilename)
{
	TiXmlDocument levelFile(sFilename.c_str());

	// Загружаем XML-карту
    if (!levelFile.LoadFile())
		return false;

	// Работаем с контейнером map
    TiXmlElement *map;
    map = levelFile.FirstChildElement("map");

	TiXmlElement *tilesetElement;
    tilesetElement = map->FirstChildElement("tileset");

	// Работа со слоями
    TiXmlElement *layerElement;
    layerElement = map->FirstChildElement("layer");

	listBoxes.clear();
	respawnLevel = Vector2i(0, 0);
	iCountMarkers = 0;
	bWin = false;

    while(layerElement)
    {
		// Контейнер <data>
        TiXmlElement *layerDataElement;
        layerDataElement = layerElement->FirstChildElement("data");

		// Контейнер <tile> - описание тайлов каждого слоя
        TiXmlElement *tileElement;
        tileElement = layerDataElement->FirstChildElement("tile");

        if(tileElement == NULL)
        {
            return false;
        }

        int x = 0;
        int y = 0;

        while(tileElement)
        {
            int tileGID = atoi(tileElement->Attribute("gid"));

			stLevel[x][y].eCell = CELL_EMPTY;

			Box box;
			box.setTexture(tilesTex);
			box.setTextureRect(IntRect(36, 0, CELL_SIZE, CELL_SIZE));

			switch (tileGID)
			{
				case 1 :
					respawnLevel = Vector2i(x, y);
					player.setCellPosition(respawnLevel);
					player.setPosition((x * CELL_SIZE) + levelPosition.x, (y * CELL_SIZE) + levelPosition.y);
					break;

				case 2 :
					respawnLevel = Vector2i(x, y);
					player.setCellPosition(respawnLevel);
					player.setPosition((x * CELL_SIZE) + levelPosition.x, (y * CELL_SIZE) + levelPosition.y);

					stLevel[x][y].eCell = CELL_MARKER;
					iCountMarkers++;
					break;

				case 3 :
					stLevel[x][y].eCell = CELL_MARKER;
					iCountMarkers++;
					break;

				case 4 :
					box.setCellPosition(Vector2i(x, y));
					box.setPosition((x * CELL_SIZE) + levelPosition.x, (y * CELL_SIZE) + levelPosition.y);
					listBoxes.push_back(box);
					break; // box

				case 5 :
					stLevel[x][y].eCell = CELL_MARKER;
					iCountMarkers++;
					box.setCellPosition(Vector2i(x, y));
					box.setPosition((x * CELL_SIZE) + levelPosition.x, (y * CELL_SIZE) + levelPosition.y);
					listBoxes.push_back(box);
					break; // +box

				case 6 : stLevel[x][y].eCell = CELL_BLOCK; break;
			}

            tileElement = tileElement->NextSiblingElement("tile");

            x++;
            if (x >= MAP_SIZE_X)
            {
                x = 0;
                y++;

				if (y >= MAP_SIZE_Y)
                    y = 0;
            }			
        }

        layerElement = layerElement->NextSiblingElement("layer");
    }
}

void  Game::nextLevel()
{
	if(iCurrentLevel==0)
	{
		bool res = loadLevel("data/levels/1.tmx");
		clearActions();
	}

	if(iCurrentLevel==1)
	{
		bool res = loadLevel("data/levels/2.tmx");
		clearActions();
	}

	if(iCurrentLevel==2)
	{
		bool res = loadLevel("data/levels/3.tmx");
		clearActions();
	}

	if(iCurrentLevel==3)
	{
		bool res = loadLevel("data/levels/4.tmx");
		clearActions();
	}

	if(iCurrentLevel==4)
	{
		bool res = loadLevel("data/levels/5.tmx");
		clearActions();
	}

	if(iCurrentLevel==5)
	{
		bool res = loadLevel("data/levels/6.tmx");
		clearActions();
	}

	if(iCurrentLevel==6)
	{
		bool res = loadLevel("data/levels/7.tmx");
		clearActions();
	}

	if(iCurrentLevel==7)
	{
		bool res = loadLevel("data/levels/8.tmx");
		clearActions();
	}

	if(iCurrentLevel==8)
	{
		bool res = loadLevel("data/levels/9.tmx");
		clearActions();
	}

	if(iCurrentLevel==9)
	{
		bool res = loadLevel("data/levels/10.tmx");
		clearActions();
	}

	iCountSteps = 0;

	if(iCurrentLevel==10)
	{
		startSplash(true);
		startScene(SCENE_MENU);
	}
}

void  Game::resetLevel()
{
	if(iCurrentLevel==0)
	{
		bool res = loadLevel("data/levels/1.tmx");
	}

	if(iCurrentLevel==1)
	{
		bool res = loadLevel("data/levels/2.tmx");
	}

	if(iCurrentLevel==2)
	{
		bool res = loadLevel("data/levels/3.tmx");
	}

	if(iCurrentLevel==3)
	{
		bool res = loadLevel("data/levels/4.tmx");
	}

	if(iCurrentLevel==4)
	{
		bool res = loadLevel("data/levels/5.tmx");
	}

	if(iCurrentLevel==5)
	{
		bool res = loadLevel("data/levels/6.tmx");
	}

	if(iCurrentLevel==6)
	{
		bool res = loadLevel("data/levels/7.tmx");
	}

	if(iCurrentLevel==7)
	{
		bool res = loadLevel("data/levels/8.tmx");
	}

	if(iCurrentLevel==8)
	{
		bool res = loadLevel("data/levels/9.tmx");
	}

	if(iCurrentLevel==9)
	{
		bool res = loadLevel("data/levels/10.tmx");
	}

	iCountSteps = 0;
		
	if(iCurrentLevel==10)
	{
		startSplash(false);
		startScene(SCENE_MENU);
	}
}

bool Game::isBlocked(Vector2i cellPosition, bool testBoxes)
{
	bool blocked = true;

	if (cellPosition.x >= 0 && cellPosition.x <= (MAP_SIZE_X - 1) &&
		cellPosition.y >= 0 && cellPosition.y <= (MAP_SIZE_Y - 1))
	{
		if (stLevel[cellPosition.x][cellPosition.y].eCell != CELL_BLOCK)
			blocked = false;
	}

	if (!blocked && testBoxes)
	{
		int countBoxes = listBoxes.size();

		if (countBoxes > 0)
		{
			for (size_t i = 0; i < countBoxes; ++i)
			{
				Vector2i cellPositionBox = listBoxes[i].getCellPosition();
				if (cellPositionBox == cellPosition)
				{
					blocked = true;
					break;
				}
			}
		}
	}

	return blocked;
}

bool Game::hasBox(Vector2i cellPosition, int &indexBox)
{
	bool result = false;

	int countBoxes = listBoxes.size();

	if (countBoxes > 0)
	{
		for (size_t i = 0; i < countBoxes; ++i)
		{
			Vector2i cellPositionBox = listBoxes[i].getCellPosition();
			if (cellPositionBox == cellPosition)
			{
				result = true;
				indexBox = i;
				break;
			}
		}
	}

	return result;
}

void Game::move(E_DIRECTION eDirection)
{
	int indexBox;
	Vector2i cellPosition = player.getCellPosition();

	switch (eDirection)
	{
		case DIRECTION_UP :
			{
				cellPosition.y--;
				if (!isBlocked(cellPosition))
				{
					if (hasBox(cellPosition, indexBox))
					{
						cellPosition.y--;
						if (!isBlocked(cellPosition, true))
						{
							player.moveUp();
							listBoxes[indexBox].moveUp();

							if (stLevel[cellPosition.x][cellPosition.y].eCell == CELL_MARKER)
								listBoxes[indexBox].mark();
						}
					}
					else player.moveUp();
				}
			}
			break;

		case DIRECTION_RIGHT :
			{
				cellPosition.x++;
				if (!isBlocked(cellPosition))
				{
					if (hasBox(cellPosition, indexBox))
					{
						cellPosition.x++;
						if (!isBlocked(cellPosition, true))
						{
							player.moveRight();
							listBoxes[indexBox].moveRight();

							if (stLevel[cellPosition.x][cellPosition.y].eCell == CELL_MARKER)
								listBoxes[indexBox].mark();
						}
					}
					else player.moveRight();
				}
			}
			break;

		case DIRECTION_DOWN :
			{
				cellPosition.y++;
				if (!isBlocked(cellPosition))
				{
					if (hasBox(cellPosition, indexBox))
					{
						cellPosition.y++;
						if (!isBlocked(cellPosition, true))
						{
							player.moveDown();
							listBoxes[indexBox].moveDown();

							if (stLevel[cellPosition.x][cellPosition.y].eCell == CELL_MARKER)
								listBoxes[indexBox].mark();
						}
					}
					else player.moveDown();
				}
			}
			break;

		case DIRECTION_LEFT :
			{
				cellPosition.x--;
				if (!isBlocked(cellPosition))
				{
					if (hasBox(cellPosition, indexBox))
					{
						cellPosition.x--;
						if (!isBlocked(cellPosition, true))
						{
							player.moveLeft();
							listBoxes[indexBox].moveLeft();

							if (stLevel[cellPosition.x][cellPosition.y].eCell == CELL_MARKER)
								listBoxes[indexBox].mark();
						}
					}
					else player.moveLeft();
				}
			}
			break;
	}
}

void Game::runProgramm()
{
	iCurrentIndexAction = 0;
	iCurrentCountAction = 0;
	iCurrentSleepAction = 0;
	iCountSteps = 0;
	bRunProgramm = true;
}

void Game::clearActions()
{
	listActions.clear();
	indexArrow = 0;
	addAction(ACTION_NONE, 0); // старт
}

void Game::addAction(E_ACTION eAction, int iCount, int iIndex)
{
	ActionBlock actionBlock;
	actionBlock.setAction(eAction);
	actionBlock.setFrameSize(75, 11);

	if (eAction != ACTION_NONE)
		 actionBlock.setCount(iCount);
	else actionBlock.setFrameSize(61, 11);

	switch (eAction)
	{
		case ACTION_NONE       : actionBlock.setTexture(actionStartTex); break;
		case ACTION_MOVE_UP    : actionBlock.setTexture(actionUpTex); break;
		case ACTION_MOVE_RIGHT : actionBlock.setTexture(actionRightTex); break;
		case ACTION_MOVE_DOWN  : actionBlock.setTexture(actionDownTex); break;
		case ACTION_MOVE_LEFT  : actionBlock.setTexture(actionLeftTex); break;
	}

	if (iIndex != 0)
	{
		listActions.insert(listActions.begin() + iIndex, actionBlock);
	}
	else listActions.push_back(actionBlock);
}

void Game::clearCursorBlock()
{
	getBlockAction = false;
	getBlockNumber = false;
}

void Game::setCursorBlockAction(E_ACTION eAction)
{
	getBlockAction = true;
	getBlockNumber = false;
	getAction = eAction;

	switch (eAction)
	{
		case ACTION_NONE : getBlockAction = false; break;
		case ACTION_MOVE_UP :
			cursorBlockSpr.setTexture(btnTplUpTex);
			cursorBlockSpr.setTextureRect(IntRect(0, 16, 22, 8));
			break;

		case ACTION_MOVE_RIGHT :
			cursorBlockSpr.setTexture(btnTplRightTex);
			cursorBlockSpr.setTextureRect(IntRect(0, 16, 26, 8));
			break;

		case ACTION_MOVE_DOWN : 
			cursorBlockSpr.setTexture(btnTplDownTex);
			cursorBlockSpr.setTextureRect(IntRect(0, 16, 19, 8));
			break;

		case ACTION_MOVE_LEFT : 
			cursorBlockSpr.setTexture(btnTplLeftTex);
			cursorBlockSpr.setTextureRect(IntRect(0, 16, 22, 8));
			break;
	}
}

void Game::setCursorBlockNumber(int iNumber)
{
	getBlockAction = false;
	getBlockNumber = true;
	getNumber = iNumber;
	cursorBlockSpr.setTextureRect(IntRect(0, 16, 6, 8));

	switch (iNumber)
	{
		case 0 : cursorBlockSpr.setTexture(btnTpl0Tex); break;
		case 1 : cursorBlockSpr.setTexture(btnTpl1Tex); break;
		case 2 : cursorBlockSpr.setTexture(btnTpl2Tex); break;
		case 3 : cursorBlockSpr.setTexture(btnTpl3Tex); break;
		case 4 : cursorBlockSpr.setTexture(btnTpl4Tex); break;
		case 5 : cursorBlockSpr.setTexture(btnTpl5Tex); break;
		case 6 : cursorBlockSpr.setTexture(btnTpl6Tex); break;
		case 7 : cursorBlockSpr.setTexture(btnTpl7Tex); break;
		case 8 : cursorBlockSpr.setTexture(btnTpl8Tex); break;
		case 9 : cursorBlockSpr.setTexture(btnTpl9Tex); break;
		default : getBlockNumber = false; break;
	}
}

void Game::startSplash(bool bOut)
{
	if (bOut)
	{
		iSplashFrame = 0;
		eSplashState = SPLASH_OUT;
	}
	else
	{
		iSplashFrame = 8;
		eSplashState = SPLASH_IN;
	}

	splashSpr.setTextureRect(IntRect(iSplashFrame * 16, 0, 16, 16));
}

void Game::startScene(E_SCENES eScene)
{
	eCurrentScene = eScene;

	switch (eCurrentScene)
	{
		case SCENE_LOGOS :
			iSleepSceneLogos = 0;
			break;
		case SCENE_MENU     : break;
		case SCENE_HOWPLAY  : break;
		case SCENE_LEVELS   : break;
		case SCENE_GAME     :
			/*
			iCurrentLevel = 0;
			bool res = loadLevel("data/levels/1.tmx");
			clearActions();

			addAction(ACTION_MOVE_RIGHT, 1);
			addAction(ACTION_MOVE_DOWN, 2);
			addAction(ACTION_MOVE_RIGHT, 2);
			addAction(ACTION_MOVE_UP, 3);
			addAction(ACTION_MOVE_RIGHT, 1);
			addAction(ACTION_MOVE_DOWN, 1);

			addAction(ACTION_MOVE_UP, 1);
			addAction(ACTION_MOVE_LEFT, 1);
			addAction(ACTION_MOVE_DOWN, 2);
			*/
			nextLevel();
			break;
	}
}

void Game::loadAccessLevels()
{
	FILE *FH = fopen("access.dat", "rb");

	if (FH != NULL)
	{
		fseek(FH, 0, 2);
		int n = ftell(FH);
		rewind(FH);
		fread(&openLevel, sizeof(openLevel), 1, FH);

		fclose(FH);
	}
}

void Game::saveAccessLevels()
{
	FILE *FH = fopen("access.dat", "wb");

	if (FH != NULL)
	{
		fwrite(&openLevel, sizeof(openLevel), 1, FH);
		fclose(FH);
	}
}

void Game::updateSceneLogos()
{
	iSleepSceneLogos++;

	if (iSleepSceneLogos >= 50)
	{
		iSleepSceneLogos = 0;
		startSplash(true);
		startScene(SCENE_MENU);
	}
}

void Game::updateSceneMenu()
{
	chooseLevelBtn.update(mouse);
	howPlayBtn.update(mouse);
	exitBtn.update(mouse);

	if (chooseLevelBtn.isClicked())
	{
		startSplash(true);
		startScene(SCENE_LEVELS);		
	}

	if (howPlayBtn.isClicked())
	{
		startSplash(true);
		startScene(SCENE_HOWPLAY);
	}

	if (exitBtn.isClicked())
		window.close();
}

void Game::updateSceneHowPlay()
{
	backBtn.update(mouse);

	if (backBtn.isClicked())
	{
		startSplash(true);
		startScene(SCENE_MENU);
	}
}

void Game::updateSceneLevels()
{
	backBtn.update(mouse);

	level1Btn.update(mouse);
	level2Btn.update(mouse);
	level3Btn.update(mouse);
	level4Btn.update(mouse);
	level5Btn.update(mouse);
	level6Btn.update(mouse);
	level7Btn.update(mouse);
	level8Btn.update(mouse);
	level9Btn.update(mouse);
	level10Btn.update(mouse);

	if (level1Btn.isClicked() && openLevel[0])
	{
		iCurrentLevel = 0;
		startSplash(true);
		startScene(SCENE_GAME);
	}

	if (level2Btn.isClicked() && openLevel[1])
	{
		iCurrentLevel = 1;
		startSplash(true);
		startScene(SCENE_GAME);
	}

	if (level3Btn.isClicked() && openLevel[2])
	{
		iCurrentLevel = 2;
		startSplash(true);
		startScene(SCENE_GAME);
	}

	if (level4Btn.isClicked() && openLevel[3])
	{
		iCurrentLevel = 3;
		startSplash(true);
		startScene(SCENE_GAME);
	}

	if (level5Btn.isClicked() && openLevel[4])
	{
		iCurrentLevel = 4;
		startSplash(true);
		startScene(SCENE_GAME);
	}

	if (level6Btn.isClicked() && openLevel[5])
	{
		iCurrentLevel = 5;
		startSplash(true);
		startScene(SCENE_GAME);
	}

	if (level7Btn.isClicked() && openLevel[6])
	{
		iCurrentLevel = 6;
		startSplash(true);
		startScene(SCENE_GAME);
	}

	if (level8Btn.isClicked() && openLevel[7])
	{
		iCurrentLevel = 7;
		startSplash(true);
		startScene(SCENE_GAME);
	}

	if (level9Btn.isClicked() && openLevel[8])
	{
		iCurrentLevel = 8;
		startSplash(true);
		startScene(SCENE_GAME);
	}

	if (level10Btn.isClicked() && openLevel[9])
	{
		iCurrentLevel = 9;
		startSplash(true);
		startScene(SCENE_GAME);
	}

	if (backBtn.isClicked())
	{
		startSplash(true);
		startScene(SCENE_MENU);
	}
}

void Game::updateSceneGame()
{
	overActionsWindow = false;

	if (mouse.iX > 150 && mouse.iX < 252 &&
	    mouse.iY > 14 && mouse.iY < 188)
		overActionsWindow = true;
	
	cursorBlockSpr.setPosition(mouse.iX, mouse.iY+13);

	int cx = (int) ((mouse.iX - 3)/CELL_SIZE);
	int cy = (int) ((mouse.iY - 14)/CELL_SIZE);
	tileSpr.setPosition((cx * CELL_SIZE) + 3, (cy * CELL_SIZE) + 14);

	closeBtn.update(mouse);
	
	if(!bRunProgramm)
		playBtn.update(mouse);

	stopBtn.update(mouse);
	clearBtn.update(mouse);

	tplUpBtn.update(mouse);
	tplRightBtn.update(mouse);
	tplDownBtn.update(mouse);
	tplLeftBtn.update(mouse);
	tpl0Btn.update(mouse);
	tpl1Btn.update(mouse);
	tpl2Btn.update(mouse);
	tpl3Btn.update(mouse);
	tpl4Btn.update(mouse);
	tpl5Btn.update(mouse);
	tpl6Btn.update(mouse);
	tpl7Btn.update(mouse);
	tpl8Btn.update(mouse);
	tpl9Btn.update(mouse);

	upBtn.update(mouse);
	downBtn.update(mouse);
	scrollBtn.update(mouse);
	nextBtn.update(mouse);

	if(nextBtn.isClicked() && bWin)
	{
		iCurrentLevel++;

		if (iCurrentLevel < 10)
		{
			openLevel[iCurrentLevel] = true;
			saveAccessLevels();
		}

		nextLevel();
	}
	
	if (clearBtn.isClicked() && !bRunProgramm)
		clearActions();

	if (closeBtn.isClicked())
	{
		startSplash(true);
		startScene(SCENE_MENU);
	}

	if (playBtn.isClicked() && !bRunProgramm)
	{
		resetLevel();
		runProgramm();
	}

	if (stopBtn.isClicked())
	{
		bRunProgramm = false;
		resetLevel();
	}

	if (tplUpBtn.isClicked())    setCursorBlockAction(ACTION_MOVE_UP);
	if (tplRightBtn.isClicked()) setCursorBlockAction(ACTION_MOVE_RIGHT);
	if (tplDownBtn.isClicked())	 setCursorBlockAction(ACTION_MOVE_DOWN);
	if (tplLeftBtn.isClicked())	 setCursorBlockAction(ACTION_MOVE_LEFT);
	if (tpl0Btn.isClicked())     setCursorBlockNumber(0);
	if (tpl1Btn.isClicked())     setCursorBlockNumber(1);
	if (tpl2Btn.isClicked())     setCursorBlockNumber(2);
	if (tpl3Btn.isClicked())     setCursorBlockNumber(3);
	if (tpl4Btn.isClicked())     setCursorBlockNumber(4);
	if (tpl5Btn.isClicked())     setCursorBlockNumber(5);
	if (tpl6Btn.isClicked())     setCursorBlockNumber(6);
	if (tpl7Btn.isClicked())     setCursorBlockNumber(7);
	if (tpl8Btn.isClicked())     setCursorBlockNumber(8);
	if (tpl9Btn.isClicked())     setCursorBlockNumber(9);

	if (mouse.bDownBtnRight)
		clearCursorBlock();
	/*
	updateKeyboard();

	if (keyboard.keyDownUp)
	{
		move(DIRECTION_UP);
		//startSplash();
	}

	if (keyboard.keyDownRight)
		move(DIRECTION_RIGHT);

	if (keyboard.keyDownDown)
	{
		move(DIRECTION_DOWN);
		//startSplash(true);
	}

	if (keyboard.keyDownLeft)
		move(DIRECTION_LEFT);
*/
	

	int countBoxes   = listBoxes.size();
	int countActions = listActions.size();

	if (bRunProgramm)
	{
		if (iCurrentSleepAction <= 0)
		{
			iCurrentSleepAction = SLEEP_ACTION;
			iCurrentCountAction--;

			if (iCurrentIndexAction == countActions-1 && iCurrentCountAction <= 0)
				bRunProgramm = false;
			else
			{
				if (iCurrentCountAction <= 0)
				{
					iCurrentIndexAction++;				

					if (iCurrentIndexAction >= countActions-1)
						iCurrentIndexAction = countActions-1;

					iCurrentCountAction = listActions[iCurrentIndexAction].getCount();				
				}
			
				E_ACTION eAction = listActions[iCurrentIndexAction].getAction();
				switch (eAction)
				{
					case ACTION_MOVE_UP    : move(DIRECTION_UP); break;
					case ACTION_MOVE_RIGHT : move(DIRECTION_RIGHT); break;
					case ACTION_MOVE_DOWN  : move(DIRECTION_DOWN); break;
					case ACTION_MOVE_LEFT  : move(DIRECTION_LEFT); break;
				}
				iCountSteps++;
				indexArrow = iCurrentIndexAction;
			}
		}

		iCurrentSleepAction--;
	}

	int countMarked = 0;

	if (countBoxes > 0)
	{
		for (int i = 0; i < countBoxes; ++i)
		{
			if (listBoxes[i].isMarked())
				countMarked++;
		}
	}

	if (iCountMarkers == countMarked)
	{
		if(!bWin)
		{
			bWin = true;
			bRunProgramm = false;
			iWinY = -60;
		}
	}

	if (bWin)
	{
		iWinY += 4;

		if (iWinY >= 50)
			iWinY = 50;


		messageSpr.setPosition(71, iWinY);
		nextBtn.setPosition(100, iWinY + 36);
	}


	
	if (countActions > 0)
	{
		int indexDelete = 0;
		bool deliteAction = false;
		bool lockedClicked = false;

		if (getBlockAction || getBlockNumber)
			lockedClicked = true;

		if (bRunProgramm)
			lockedClicked = true;

		for (int i = 0; i < countActions; ++i)
		{
			listActions[i].setPosition(programmX, (i * LIST_ITEM_HEIGHT) + programmY);
			listActions[i].update(mouse, lockedClicked);

			if (listActions[i].isDelited())
			{
				indexDelete = i;
				deliteAction = true;
			}
		}

		if (deliteAction)
			listActions.erase(listActions.begin() + indexDelete);
	}


	iHeightProgramm = (countActions * LIST_ITEM_HEIGHT);
	if (iHeightProgramm <= 172)
		iHeightProgramm = 172;

	if (mouse.iScrollDelta != 0)
		programmY = (int)(programmY + (mouse.iScrollDelta * SPEED_SCROLLING));

	if (upBtn.isClicked())
		programmY += SPEED_SCROLLING;

	if (downBtn.isClicked())
		programmY -= SPEED_SCROLLING;

	if (programmY > 16)
		programmY = 16;
	
	if (programmY < 188-iHeightProgramm)
		programmY = 188-iHeightProgramm;

	int viewProc  = (int) ((172*100)/iHeightProgramm);
	int heightScroll = (int) ((viewProc* 145)/100);
	scrollCenterSpr.setScale(1, heightScroll + 1);

	// ой наговнокодил :D
	int p = (programmY-16) * (-1);
	int pp = (int)((p*100)/iHeightProgramm);
	int ppp = (int)((pp* 145)/100);

	scrollTopSpr.setPosition(242, ppp + 30);
	scrollCenterSpr.setPosition(242, ppp + 30);
	scrollBottomSpr.setPosition(242, ppp + 30 + heightScroll );

	indexMarker = (int) ((mouse.iY - programmY) / LIST_ITEM_HEIGHT);

	if (indexMarker >= countActions)
		indexMarker = countActions - 1;
	markerListSpr.setPosition(programmX - 5, (indexMarker * LIST_ITEM_HEIGHT) + programmY + 10);
	markerNumberSpr.setPosition(programmX + 31, (indexMarker * LIST_ITEM_HEIGHT) + programmY - 1);
	arrowSpr.setPosition(programmX - 5, (indexArrow * LIST_ITEM_HEIGHT) + programmY + 2);

	if (mouse.bDownBtnLeft)
	{
		if (overActionsWindow)
		{
			if (getBlockAction)
			{
				addAction(getAction, 1, indexMarker + 1);
				clearCursorBlock();
			}

			if (getBlockNumber)
			{
				if (listActions[indexMarker].getAction() != ACTION_NONE)
				{
					listActions[indexMarker].setCount(getNumber);
					clearCursorBlock();
				}
			}
		}
	}
}

void Game::drawSceneLogos()
{
	window.clear(sf::Color(218, 255, 170, 255));
	window.draw(logoSpr);
}

void Game::drawSceneMenu()
{
	window.clear(sf::Color(218, 255, 170, 255));
	window.draw(mainSpr);
	window.draw(chooseLevelBtn);
	window.draw(howPlayBtn);
	window.draw(exitBtn);

	window.draw(cursorSpr);
}

void Game::drawSceneHowPlay()
{
	window.clear(sf::Color(218, 255, 170, 255));
	window.draw(howplaySpr);
	window.draw(backBtn);

	window.draw(cursorSpr);
}

void Game::drawSceneLevels()
{
	window.clear(sf::Color(218, 255, 170, 255));
	
	window.draw(level1Btn);
	window.draw(level2Btn);
	window.draw(level3Btn);
	window.draw(level4Btn);
	window.draw(level5Btn);
	window.draw(level6Btn);
	window.draw(level7Btn);
	window.draw(level8Btn);
	window.draw(level9Btn);
	window.draw(level10Btn);
	
	for (int i=0; i <5; i++)
	{
		drawNumber((i*39)+48, 49, i+1);
		drawNumber((i*39)+48, 95, i+6);

		if (!openLevel[i])
		{
			closeSpr.setPosition((i*39)+51, 55);
			window.draw(closeSpr);
		}

		if (!openLevel[i+5])
		{
			closeSpr.setPosition((i*39)+51, 101);
			window.draw(closeSpr);
		}
	}

	window.draw(backBtn);

	window.draw(cursorSpr);
}

void Game::drawSceneGame()
{
	window.clear(sf::Color(218, 255, 170, 255));

	drawNumber(129, 5, iCountSteps);

	if (mouse.iX > 3 && mouse.iX < 146 &&
		mouse.iY > 14 && mouse.iY < 157)
		window.draw(tileSpr);

	for (int y = 0; y < MAP_SIZE_Y; y++)
	for (int x = 0; x < MAP_SIZE_X; x++)
	{
		int cx = (x * CELL_SIZE) + levelPosition.x;
		int cy = (y * CELL_SIZE) + levelPosition.y;

		switch (stLevel[x][y].eCell)
		{
			case CELL_BLOCK :
				blockSpr.setPosition(cx, cy);
				window.draw(blockSpr);
				break;

			case CELL_MARKER :
				markerSpr.setPosition(cx, cy);
				window.draw(markerSpr);
				break;
		}
	}

	int countBoxes = listBoxes.size();
	
	if (countBoxes > 0)
	{
		for (int i = 0; i < countBoxes; ++i)
			window.draw(listBoxes[i]);
	}

	window.draw(player);

	int countActions = listActions.size();
	
	if (countActions > 0)
	{
		for (int i = 0; i < countActions; ++i)
		{
			window.draw(listActions[i]);

			E_ACTION stAction = listActions[i].getAction();

			if (stAction != ACTION_NONE)
			{
				int iCount = listActions[i].getCount();
				sf::Vector2f position = listActions[i].getPosition();
			
				drawNumber((int)position.x+35, (int)position.y+3, iCount);
			}
		}
	}

	if (bRunProgramm)
		window.draw(arrowSpr);

	if (overActionsWindow)
	{
		if (getBlockAction)
			window.draw(markerListSpr);

		if (getBlockNumber && indexMarker != 0)
			window.draw(markerNumberSpr);
	}

	window.draw(windowsSpr);
	

	if (iHeightProgramm > 172)
	{
		window.draw(scrollSpr);
		window.draw(upBtn);
		window.draw(downBtn);
		window.draw(scrollCenterSpr);
		window.draw(scrollTopSpr);
		window.draw(scrollBottomSpr);
	}

	window.draw(closeBtn);

	if (bRunProgramm)
		 window.draw(playBtnDisabledSpr);
	else window.draw(playBtn);
	
	window.draw(stopBtn);
	window.draw(clearBtn);

	window.draw(tplUpBtn);
	window.draw(tplRightBtn);
	window.draw(tplDownBtn);
	window.draw(tplLeftBtn);
	window.draw(tpl0Btn);
	window.draw(tpl1Btn);
	window.draw(tpl2Btn);
	window.draw(tpl3Btn);
	window.draw(tpl4Btn);
	window.draw(tpl5Btn);
	window.draw(tpl6Btn);
	window.draw(tpl7Btn);
	window.draw(tpl8Btn);
	window.draw(tpl9Btn);

	if (bWin)
	{
		window.draw(fillSpr);
		window.draw(messageSpr);
		window.draw(nextBtn);
		drawNumber(163, iWinY + 22, iCurrentLevel+1);
	}

	if (getBlockAction || getBlockNumber)
		window.draw(cursorBlockSpr);

	window.draw(cursorSpr);
}

void Game::run()
{
	if (!running)
	{
		running = true;
		//srand(time(NULL));
		//sf::Style::Titlebar | sf::Style::Close

		window.create(VideoMode(768, 576), APP_CAPTION, sf::Style::Titlebar | sf::Style::Close);

		window.setFramerateLimit(30);
		window.setMouseCursorVisible(false);

		tilesTex.loadFromFile("data/tiles.png");
		playerTex.loadFromFile("data/player.png");
		cursorTex.loadFromFile("data/cursor.png");

		logoTex.loadFromFile("data/logo.png");
		fontTex.loadFromFile("data/font.png");

		splashTex.loadFromFile("data/splash.png");



		windowsTex.loadFromFile("data/windows.png");
		fillTex.loadFromFile("data/fill.png");
		messageTex.loadFromFile("data/message.png");
		btnCloseTex.loadFromFile("data/btn_close.png");
		btnPlayTex.loadFromFile("data/btn_play.png");		
		btnStopTex.loadFromFile("data/btn_stop.png");
		btnClearTex.loadFromFile("data/btn_clear.png");
		btnNextTex.loadFromFile("data/btn_next.png");
		btnTplUpTex.loadFromFile("data/tpl_up.png");
		btnTplRightTex.loadFromFile("data/tpl_right.png");
		btnTplDownTex.loadFromFile("data/tpl_down.png");
		btnTplLeftTex.loadFromFile("data/tpl_left.png");
		btnTpl0Tex.loadFromFile("data/tpl_0.png");
		btnTpl1Tex.loadFromFile("data/tpl_1.png");
		btnTpl2Tex.loadFromFile("data/tpl_2.png");
		btnTpl3Tex.loadFromFile("data/tpl_3.png");
		btnTpl4Tex.loadFromFile("data/tpl_4.png");
		btnTpl5Tex.loadFromFile("data/tpl_5.png");
		btnTpl6Tex.loadFromFile("data/tpl_6.png");
		btnTpl7Tex.loadFromFile("data/tpl_7.png");
		btnTpl8Tex.loadFromFile("data/tpl_8.png");
		btnTpl9Tex.loadFromFile("data/tpl_9.png");
		btnUpTex.loadFromFile("data/btn_up.png");
		btnDownTex.loadFromFile("data/btn_down.png");
		btnScrollTex.loadFromFile("data/btn_scroll.png");
		scrollTex.loadFromFile("data/scroll.png");
		actionStartTex.loadFromFile("data/action_start.png");
		actionUpTex.loadFromFile("data/action_up.png");
		actionRightTex.loadFromFile("data/action_right.png");
		actionDownTex.loadFromFile("data/action_down.png");
		actionLeftTex.loadFromFile("data/action_left.png");
		arrowTex.loadFromFile("data/arrow.png");
		markerListTex.loadFromFile("data/marker_list.png");
		markerNumberTex.loadFromFile("data/marker_number.png");
		tileTex.loadFromFile("data/tile.png");


		mainTex.loadFromFile("data/main.png");
		howplayTex.loadFromFile("data/howplay.png");
		btnChooseLevelTex.loadFromFile("data/btn_chooselevel.png");
		btnHowPlayTex.loadFromFile("data/btn_howplay.png");
		btnExitTex.loadFromFile("data/btn_exit.png");
		btnBackTex.loadFromFile("data/btn_back.png");
		btnLevelTex.loadFromFile("data/btn_level.png");

		closeTex.loadFromFile("data/close.png");
		closeSpr.setTexture(closeTex);

		mainSpr.setTexture(mainTex);
		howplaySpr.setTexture(howplayTex);
		mainSpr.setPosition(0, 0);
		howplaySpr.setPosition(0, 0);

		
		


		chooseLevelBtn.setTexture(btnChooseLevelTex);
		chooseLevelBtn.setFrameSize(56, 12);
		chooseLevelBtn.setPosition(98, 46);

		howPlayBtn.setTexture(btnHowPlayTex);
		howPlayBtn.setFrameSize(56, 12);
		howPlayBtn.setPosition(98, 66);

		exitBtn.setTexture(btnExitTex);
		exitBtn.setFrameSize(56, 12);
		exitBtn.setPosition(98, 86);

		backBtn.setTexture(btnBackTex);
		backBtn.setFrameSize(56, 12);
		backBtn.setPosition(140, 174);



		level1Btn.setTexture(btnLevelTex);
		level1Btn.setFrameSize(24, 25);
		level1Btn.setPosition(39, 41);

		level2Btn.setTexture(btnLevelTex);
		level2Btn.setFrameSize(24, 25);
		level2Btn.setPosition(78, 41);

		level3Btn.setTexture(btnLevelTex);
		level3Btn.setFrameSize(24, 25);
		level3Btn.setPosition(117, 41);

		level4Btn.setTexture(btnLevelTex);
		level4Btn.setFrameSize(24, 25);
		level4Btn.setPosition(156, 41);

		level5Btn.setTexture(btnLevelTex);
		level5Btn.setFrameSize(24, 25);
		level5Btn.setPosition(195, 41);

		level6Btn.setTexture(btnLevelTex);
		level6Btn.setFrameSize(24, 25);
		level6Btn.setPosition(39, 87);

		level7Btn.setTexture(btnLevelTex);
		level7Btn.setFrameSize(24, 25);
		level7Btn.setPosition(78, 87);

		level8Btn.setTexture(btnLevelTex);
		level8Btn.setFrameSize(24, 25);
		level8Btn.setPosition(117, 87);

		level9Btn.setTexture(btnLevelTex);
		level9Btn.setFrameSize(24, 25);
		level9Btn.setPosition(156, 87);

		level10Btn.setTexture(btnLevelTex);
		level10Btn.setFrameSize(24, 25);
		level10Btn.setPosition(195, 87);


		windowsSpr.setTexture(windowsTex);
		fillSpr.setTexture(fillTex);
		messageSpr.setTexture(messageTex);

		arrowSpr.setTexture(arrowTex);
		markerListSpr.setTexture(markerListTex);
		markerNumberSpr.setTexture(markerNumberTex);
		scrollSpr.setTexture(scrollTex);

		windowsSpr.setPosition(0, 0);
		fillSpr.setPosition(0, 0);
		//messageSpr.setPosition(71, 32);

		scrollSpr.setPosition(244, 29);

		tileSpr.setTexture(tileTex);
		playBtnDisabledSpr.setTexture(btnPlayTex);
		playBtnDisabledSpr.setTextureRect(IntRect(0, 36, 20, 12));
		playBtnDisabledSpr.setPosition(51, 1);
		
		cursorSpr.setTexture(cursorTex);

		logoSpr.setTexture(logoTex);
		fontSpr.setTexture(fontTex);

		splashSpr.setTexture(splashTex);
		splashSpr.setTextureRect(IntRect(0, 0, 16, 16));


		blockSpr.setTexture(tilesTex);
		markerSpr.setTexture(tilesTex);
		
		player.setTexture(playerTex);

		blockSpr.setTextureRect(IntRect(60, 0, CELL_SIZE, CELL_SIZE));
		markerSpr.setTextureRect(IntRect(24, 0, CELL_SIZE, CELL_SIZE));

		logoSpr.setPosition(0, 0);

		gameView.setCenter(sf::Vector2f(128, 96));
		gameView.setSize(sf::Vector2f(256, 192));



		closeBtn.setTexture(btnCloseTex);
		closeBtn.setFrameSize(12, 12);
		closeBtn.setPosition(3, 1);

		playBtn.setTexture(btnPlayTex);
		playBtn.setFrameSize(20, 12);
		playBtn.setPosition(51, 1);

		stopBtn.setTexture(btnStopTex);
		stopBtn.setFrameSize(20, 12);
		stopBtn.setPosition(79, 1);

		clearBtn.setTexture(btnClearTex);
		clearBtn.setFrameSize(12, 12);
		clearBtn.setPosition(150, 1);
		
		nextBtn.setTexture(btnNextTex);
		nextBtn.setFrameSize(51, 12);
		//nextBtn.setPosition(100, 68);

		tplUpBtn.setTexture(btnTplUpTex);
		tplUpBtn.setFrameSize(22, 8);
		tplUpBtn.setPosition(3, 164);

		tplRightBtn.setTexture(btnTplRightTex);
		tplRightBtn.setFrameSize(26, 8);
		tplRightBtn.setPosition(31, 164);

		tplDownBtn.setTexture(btnTplDownTex);
		tplDownBtn.setFrameSize(19, 8);
		tplDownBtn.setPosition(63, 164);

		tplLeftBtn.setTexture(btnTplLeftTex);
		tplLeftBtn.setFrameSize(22, 8);
		tplLeftBtn.setPosition(88, 164);

		tpl0Btn.setTexture(btnTpl0Tex);
		tpl0Btn.setFrameSize(6, 8);
		tpl0Btn.setPosition(3, 178);

		tpl1Btn.setTexture(btnTpl1Tex);
		tpl1Btn.setFrameSize(6, 8);
		tpl1Btn.setPosition(13, 178);

		tpl2Btn.setTexture(btnTpl2Tex);
		tpl2Btn.setFrameSize(6, 8);
		tpl2Btn.setPosition(23, 178);

		tpl3Btn.setTexture(btnTpl3Tex);
		tpl3Btn.setFrameSize(6, 8);
		tpl3Btn.setPosition(33, 178);

		tpl4Btn.setTexture(btnTpl4Tex);
		tpl4Btn.setFrameSize(6, 8);
		tpl4Btn.setPosition(43, 178);

		tpl5Btn.setTexture(btnTpl5Tex);
		tpl5Btn.setFrameSize(6, 8);
		tpl5Btn.setPosition(53, 178);

		tpl6Btn.setTexture(btnTpl6Tex);
		tpl6Btn.setFrameSize(6, 8);
		tpl6Btn.setPosition(63, 178);

		tpl7Btn.setTexture(btnTpl7Tex);
		tpl7Btn.setFrameSize(6, 8);
		tpl7Btn.setPosition(73, 178);

		tpl8Btn.setTexture(btnTpl8Tex);
		tpl8Btn.setFrameSize(6, 8);
		tpl8Btn.setPosition(83, 178);

		tpl9Btn.setTexture(btnTpl9Tex);
		tpl9Btn.setFrameSize(6, 8);
		tpl9Btn.setPosition(93, 178);


		upBtn.setTexture(btnUpTex);
		upBtn.setFrameSize(12, 12);
		upBtn.setPosition(239, 16);

		downBtn.setTexture(btnDownTex);
		downBtn.setFrameSize(12, 12);
		downBtn.setPosition(239, 175);

		scrollBtn.setTexture(btnScrollTex);
		scrollBtn.setFrameSize(5, 10);
		scrollBtn.setPosition(242, 69);


		scrollTopSpr.setTexture(btnScrollTex);
		scrollCenterSpr.setTexture(btnScrollTex);
		scrollBottomSpr.setTexture(btnScrollTex);

		scrollTopSpr.setTextureRect(IntRect(0, 0, 5, 1));
		scrollCenterSpr.setTextureRect(IntRect(0, 1, 5, 1));
		scrollBottomSpr.setTextureRect(IntRect(0, 2, 5, 1));

		levelPosition = Vector2i(3, 14);

		bool res = loadLevel("data/levels/3.tmx");

		mouse.bPressBtnLeft  = false;
		mouse.bPressBtnRight = false;

		keyboard.keyPressUp    = false;
		keyboard.keyPressRight = false;
		keyboard.keyPressDown  = false;
		keyboard.keyPressLeft  = false;

		eSplashState = SPLASH_NONE;

		programmX = 158;
		programmY = 16;
		indexArrow = 0;
		indexMarker = 0;
		iCountSteps = 0;

		clearCursorBlock();

		iCurrentLevel = 0;
		iWinY = 0;

		for (int i = 0; i < COUNT_LEVELS; i++)
		{
			openLevel[i] = false;
		}

		openLevel[0] = true;
		loadAccessLevels();

		bRunProgramm = false;

		startScene(SCENE_LOGOS);

		window.setView(gameView);

		while (window.isOpen())
		{
				Event event;

				mouse.iScrollDelta = 0;

				while (window.pollEvent(event))
				{
					if (event.type == Event::Closed)
						window.close();

					if (event.type == sf::Event::MouseWheelScrolled)
					{
						if (event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel)
						{
							mouse.iScrollDelta = event.mouseWheelScroll.delta;
						}
					}
				}
 
			update();
			render();
		}
	}
}