Example #1
0
/* 
======================================									
Listen for possible changes that the selected backdrop (the one where the cursos is over) can suffer
====================================== 
*/
void Listener::ListenHoverBackDrop ()
{
	static int mMouseClickX;
	static int mMouseClickY;

	IND_Entity2d *mBackDropOver = mBackDropNodeOver->GetEntity();

	// -------------------- Translate the selected backdrop image --------------------

	static float mInitialPosX, mInitialPosY;
	float mNewPosX, mNewPosY;

	if (!mIsRotatingBackDrop && !mIsScalingBackDrop)
	{
		if (mI->_input->onMouseButtonPress(IND_MBUTTON_LEFT))
		{
			mMouseClickX = (int) mPosBrushX;
			mMouseClickY = (int) mPosBrushY;

			mInitialPosX = mBackDropOver->getPosX();
			mInitialPosY = mBackDropOver->getPosY();
		}

		if (mI->_input->isMouseButtonPressed (IND_MBUTTON_LEFT))
		{
			mNewPosX = mInitialPosX + (mPosBrushX - mMouseClickX);
			mNewPosY = mInitialPosY + (mPosBrushY - mMouseClickY);

			mBackDropOver->setPosition (mNewPosX, 
										mNewPosY, 
										mBackDropOver->getPosZ());

			mIsTranslatingBackDrop = true;
		}


		if (mI->_input->onMouseButtonRelease(IND_MBUTTON_LEFT))
		{
			mIsTranslatingBackDrop = false;
		}
	}

	// -------------------- Translate pixel by pixel --------------------

	if (mI->_input->onKeyPress	(IND_F))
	{
		mBackDropOver->setPosition (mBackDropOver->getPosX() - 1, 
									mBackDropOver->getPosY(), 
									mBackDropOver->getPosZ());
	}

	if (mI->_input->onKeyPress	(IND_G))
	{
		mBackDropOver->setPosition (mBackDropOver->getPosX() + 1, 
									mBackDropOver->getPosY(), 
									mBackDropOver->getPosZ());
	}

	if (mI->_input->onKeyPress	(IND_C))
	{
		mBackDropOver->setPosition (mBackDropOver->getPosX(), 
									mBackDropOver->getPosY() - 1, 
									mBackDropOver->getPosZ());
	}

	if (mI->_input->onKeyPress	(IND_V))
	{
		mBackDropOver->setPosition (mBackDropOver->getPosX(), 
									mBackDropOver->getPosY() + 1, 
									mBackDropOver->getPosZ());
	}

	// -------------------- Scale the selected backdrop image --------------------

	static float mInitialScale;
	float mNewScale;
	static int mInitialRegionX, mInitialRegionY;
	int mNewRegionX, mNewRegionY;

	if (!mIsRotatingBackDrop && !mIsTranslatingBackDrop)
	{
		if (mI->_input->onMouseButtonPress (IND_MBUTTON_RIGHT) && mI->_input->isKeyPressed (IND_LSHIFT))
		{
			mMouseClickX = (int) mPosBrushX;

			if (!mBackDropOver->ifWrap())
			{
				mInitialScale = mBackDropOver->getScaleX();
			}
			else
			{
				mInitialRegionX = mBackDropOver->getRegionWidth();
				mInitialRegionY = mBackDropOver->getRegionHeight();
			}
		}

		if (mI->_input->isMouseButtonPressed (IND_MBUTTON_RIGHT) && mI->_input->isKeyPressed(IND_LSHIFT))
		{
			mIsScalingBackDrop = true;

			if (!mBackDropOver->ifWrap())
			{	
				mNewScale = mInitialScale + ((mPosBrushX - mMouseClickX) / 1000);
				if (mNewScale < 0.05f) mNewScale = 0.1f;
				mBackDropOver->setScale (mNewScale, mNewScale);
			}
			else
			{
				mNewRegionX = mInitialRegionX + ((int) mPosBrushX - mMouseClickX);
				mNewRegionY = mInitialRegionY + ((int) mPosBrushX - mMouseClickX);
				mBackDropOver->setRegion	(0, 
											0, 
											(int) mNewRegionX, 
											(int) mNewRegionY);

			}
		}

		if (mI->_input->onMouseButtonRelease (IND_MBUTTON_RIGHT))
		{
			mIsScalingBackDrop = false;
		}
	}

	// -------------------- Rotate the selected backdrop image --------------------

	static float mInitialAngle;
	float mNewAngle;

	if (!mIsTranslatingBackDrop && !mIsScalingBackDrop)
	{
		if (mI->_input->onMouseButtonPress (IND_MBUTTON_RIGHT))
		{
			mMouseClickX = (int) mPosBrushX;
			mInitialAngle = mBackDropOver->getAngleZ();
		}

		if (mI->_input->isMouseButtonPressed (IND_MBUTTON_RIGHT))
		{
			mNewAngle = mInitialAngle + (mPosBrushX - mMouseClickX);
			mBackDropOver->setAngleXYZ (0, 0, mNewAngle);
			mIsRotatingBackDrop = true;
		}

		if (mI->_input->onMouseButtonRelease (IND_MBUTTON_RIGHT))
		{
			mIsRotatingBackDrop = false;
		}
	}

	// -------------------- Flip backdrop image --------------------

	if (mI->_input->onKeyPress(IND_T))
	{
		(mBackDropOver->getMirrorX() == true) ? mBackDropOver->setMirrorX (false) : mBackDropOver->setMirrorX (true);
	}

	if (mI->_input->onKeyPress(IND_Y))
	{
		(mBackDropOver->getMirrorY() == true) ? mBackDropOver->setMirrorY (false) : mBackDropOver->setMirrorY (true);
	}

	// -------------------- Transparency --------------------

	if (mI->_input->isKeyPressed (IND_U, 5))
	{
		if (mBackDropOver->getTransparency() > 0) mBackDropOver->setTransparency (mBackDropOver->getTransparency() - 1);
	}

	if (mI->_input->isKeyPressed (IND_I, 5))
	{
		if (mBackDropOver->getTransparency() < 255) mBackDropOver->setTransparency (mBackDropOver->getTransparency() + 1);
	}

	// -------------------- Tiling --------------------

	if (mI->_input->onKeyPress(IND_L) && !mI->_input->isKeyPressed (IND_LCTRL))
	{
		(mBackDropOver->ifWrap() == true) ? mBackDropOver->toggleWrap (false) : mBackDropOver->toggleWrap (true);
		mBackDropOver->setRegion	(0, 
									0, 
									mBackDropOver->getSurface()->getWidth(), 
									mBackDropOver->getSurface()->getHeight());
	}

	// -------------------- Tinting --------------------

	if (mI->_input->isKeyPressed(IND_SPACE))
	{
		mBackDropOver->setTint ((BYTE) mMouseX, (BYTE) mMouseY, (BYTE) mMouseX);
		mIsTintingBackDrop = true;
	}

	if (mI->_input->onKeyRelease(IND_SPACE))
	{
		mIsTintingBackDrop = false;
	}

	// -------------------- Z ordering --------------------

	if (mI->_input->onKeyPress(IND_Z))
	{
		mBackDropOver->setPosition (mBackDropOver->getPosX(), mBackDropOver->getPosY(), mBackDropOver->getPosZ() - 1);
	}

	if (mI->_input->onKeyPress(IND_X))
	{
		mBackDropOver->setPosition (mBackDropOver->getPosX(), mBackDropOver->getPosY(), mBackDropOver->getPosZ() + 1);
	}

	// -------------------- Clone --------------------

	if (mI->_input->isKeyPressed(IND_LSHIFT) && mI->_input->onMouseButtonPress (IND_MBUTTON_LEFT))
	{
		mMap->CloneNode (mBackDropNodeOver);	// Clone node
	}

	// -------------------- Delete --------------------

	if (mI->_input->onKeyPress(IND_DELETE))
	{
		mMap->DeleteNode (mBackDropNodeOver);	// Erase the node from the map vector
	}
}
/*
==================
Main
==================
*/
int IndieLib()			
{
	// ----- IndieLib intialization -----

	CIndieLib *mI = CIndieLib::instance();
	if (!mI->init()) return 0;								

	// ----- Surface loading -----

	// Loading Background
	IND_Surface *mSurfaceBack = new IND_Surface();
	if (!mI->_surfaceManager->add(mSurfaceBack, "../../resources/twist.jpg", IND_OPAQUE, IND_32)) return 0;

	// Loading Beettleship
	IND_Surface *mSurfaceBeetleship = new IND_Surface();
	if (!mI->_surfaceManager->add(mSurfaceBeetleship, "../../resources/beetleship.png", IND_ALPHA, IND_32)) return 0;

	// Loading Octopus
	IND_Surface *mSurfaceOctopus = new IND_Surface();
	if (!mI->_surfaceManager->add(mSurfaceOctopus, "../../resources/octopus.png", IND_ALPHA, IND_32)) return 0;

	// Loading Planet
	IND_Surface *mSurfacePlanet = new IND_Surface();
	if (!mI->_surfaceManager->add(mSurfacePlanet, "../../resources/planet.png", IND_ALPHA, IND_32)) return 0;

	// Font
	IND_Font *mFontSmall = new IND_Font() ;
	if (!mI->_fontManager->add(mFontSmall, "../../resources/font_small.png", "../../resources/font_small.xml", IND_ALPHA, IND_32)) return 0;

	// ----- Font creation -----

	IND_Entity2d *mTextSmallWhite = new IND_Entity2d();					
	mI->_entity2dManager->add(mTextSmallWhite);			// Entity adding
	mTextSmallWhite->setFont(mFontSmall);				// Set the font into the entity
	mTextSmallWhite->setLineSpacing(18);
	mTextSmallWhite->setCharSpacing(-8);
	mTextSmallWhite->setPosition(5, 5, 1);
	mTextSmallWhite->setAlign(IND_LEFT);

	// ----- Set the surfaces into 2d entities -----

	// Creating 2d entity for the background
	IND_Entity2d *mBack = new IND_Entity2d();					
	mI->_entity2dManager->add(mBack);				// Entity adding
	mBack->setSurface(mSurfaceBack);				// Set the surface into the entity

	// Creating 2d entity for the bettleship
	IND_Entity2d *mBeetleship = new IND_Entity2d();					
	mI->_entity2dManager->add(mBeetleship);				// Entity adding
	mBeetleship->setSurface(mSurfaceBeetleship);			// Set the surface into the entity

	// Creating 2d entity for the octopus
	IND_Entity2d *mOctopus = new IND_Entity2d();					
	mI->_entity2dManager->add(mOctopus);				// Entity adding
	mOctopus->setSurface(mSurfaceOctopus);				// Set the surface into the entity

	// Creating 2d entity for the planet
	IND_Entity2d *mPlanet = new IND_Entity2d();					
	mI->_entity2dManager->add(mPlanet);				// Entity adding
	mPlanet->setSurface(mSurfacePlanet);				// Set the surface into the entity

	// ----- Changing the attributes of the 2d entities -----

	mBack->setHotSpot(0.5f, 0.5f);
	mBack->setPosition(400, 300, 0);
	mBack->setScale(1.7f, 1.7f);

	mBeetleship->setHotSpot(0.5f, 0.5f); 

	mOctopus->setMirrorX(true);
	mOctopus->setHotSpot(0.5f, 0.5f);

	mPlanet->setHotSpot(0.5f, 0.5f);

	// ----- Main Loop -----

	float mAngle = 0;
	float mPos = 400;
	int mSpeed = 200;
	float mDelta;
	char mText [2048];
	mText [0] = 0;

	while (!mI->_input->onKeyPress (IND_ESCAPE) && !mI->_input->quit())
	{
		// ----- Input update -----

		mI->_input->update();

		// ----- Text -----

		strcpy (mText, "Use the mouse for moving the planet\n");
		strcat (mText, "Use left and right arrow keys for moving the ships\n");
		strcat (mText, "Press CTRL + X or ESC key to quit");
		mTextSmallWhite->setText(mText);	

		// ----- Input -----

		mDelta = mI->_render->getFrameTime() / 1000.0f;

		// Move entities when pressing right
		if (mI->_input->isKeyPressed(IND_KEYRIGHT)){
			mPos += mSpeed * mDelta;
		}

		// Move entities when pressing left
		if (mI->_input->isKeyPressed(IND_KEYLEFT)){
			mPos -= mSpeed * mDelta;
		}

		// If CTRL + X pressed then exit
		if (mI->_input->isKeyPressed(IND_LCTRL) && mI->_input->isKeyPressed(IND_X)){
			mI->_render->endScene();
			mI->end();
			exit(0);
		}

		mAngle += (mSpeed / 4) * mDelta;

		// ----- Updating entities attributes  -----

		// Back
		mBack->setAngleXYZ(0, 0, mAngle);		

		// Beetle
		mBeetleship->setPosition((float) mPos, 140, 1);
		mBeetleship->setAngleXYZ(0, 0, (float) mPos);

		// Planet
		mPlanet->setPosition((float) mI->_input->getMouseX(), 300, 2);
		
		// Octopus
		mOctopus->setPosition(800 - (float) mPos, 480, 3);

		// ----- Render -----

		mI->_render->beginScene();
		mI->_entity2dManager->renderEntities2d();
		mI->_render->endScene();	
	}

	// ----- Free -----

	mI->end();

	return 0;
}