//Adapted from BetaCairo demo : http://www.ogre3d.org/forums/viewtopic.php?t=26987 (created by betajaen)
void OgreAppLogic::updateClock2Canvas()
{
	Chrono chrono(true);

	time_t now = time(NULL);
	struct tm * timeinfo;
	timeinfo = localtime(&now);
	float sec  = (float) timeinfo->tm_sec;
	float min  = (float) timeinfo->tm_min;
	float hour = (float) timeinfo->tm_hour;

	float clockSize = 150.0f;
	float clockUpdate = 0.125f;

	Ogre::Canvas::Context* ctx = mCanvasTextureClock2->getContext();
	Ogre::ColourValue tomato     = Ogre::Canvas::ColourConverter::fromHexa("#ff6347");
	Ogre::ColourValue whitesmoke = Ogre::Canvas::ColourConverter::fromHexa("#f5f5f5");

	// Basic bits:

		// Paint the Drawing with a tomatoes, this pretty much wipes over the previous drawing.
		ctx->fillStyle(tomato);
		ctx->fillRect(0, 0, clockSize, clockSize);

		// Draw a outline around the texture, using the whitesmoke colour and a line thickness of 1 pixel
		ctx->strokeStyle(whitesmoke);
		ctx->lineWidth(1.0f);
		ctx->strokeRect(0, 0, clockSize, clockSize);

		// Path out an arc (semi or full circle)
		ctx->fillStyle(whitesmoke);
		ctx->arc(clockSize / 2.0f, clockSize / 2.0f, clockSize / 4.0f, 0.0f, 360.0f, false);
		ctx->fill();

	// The Hour hand:

		ctx->lineCap(Ogre::Canvas::LineCap_Round);
		ctx->strokeStyle(tomato);
		ctx->lineWidth(3.0f);
		ctx->moveTo(clockSize / 2.0f, clockSize / 2.0f); // Move to the center of the image

		// Using a simple bit of trig. path the hour hand from the center to the correct hour, using a length of 1/5th of the imagesize.
		ctx->lineTo((clockSize / 2.0f) + (Ogre::Math::Sin(hour * Ogre::Math::PI / 6.0f) * (clockSize / 5.0f)), (clockSize / 2.0f) + (-Ogre::Math::Cos(hour * Ogre::Math::PI / 6.0f) * (clockSize / 5.0f)));
		ctx->stroke();


	// The Minute hand:
		
		ctx->beginPath();
		ctx->moveTo(clockSize / 2.0f, clockSize / 2.0f); // Move to the center of the image

		// Now draw the hour hand, using a length of 1/6th of the imagesize.
		ctx->lineTo((clockSize / 2) + (Ogre::Math::Sin((min + (sec / 60.0f)) * Ogre::Math::PI / 30.0f) * (clockSize / 6.0f)), (clockSize / 2) + (-Ogre::Math::Cos((min + (sec / 60.0f)) * Ogre::Math::PI / 30.0f) * (clockSize / 6.0f)));
		ctx->stroke();
		ctx->lineWidth(2.0f);
		ctx->strokeStyle(whitesmoke);
		ctx->stroke();

		// The second hand, and previous seconds:

			ctx->lineWidth(1.5f);		
			Ogre::ColourValue c = whitesmoke;

			int lastSecond = (int) Ogre::Math::Ceil(sec);
			float a = 1.0f / sec;

			// For each previous second, draw it out, with a subtle fade (alpha). The alpha decreases based how close it is to the current second.
			for (int i=0; i<lastSecond; i++) 
			{
				ctx->strokeStyle(Ogre::ColourValue(c.r, c.g, c.b, a*i));
				ctx->moveTo((clockSize / 2)  + (Ogre::Math::Sin(i * Ogre::Math::PI / 30.0f) * (clockSize / 4.0f) ),  (clockSize / 2.0f)  + (-Ogre::Math::Cos(i * Ogre::Math::PI / 30.0f) * (clockSize / 4.0f)));
				ctx->lineTo((clockSize / 2)  + (Ogre::Math::Sin(i * Ogre::Math::PI / 30.0f) * (clockSize / 3.5f) ) , (clockSize / 2.0f)  + (-Ogre::Math::Cos(i * Ogre::Math::PI / 30.0f) * (clockSize / 3.5f)));
				ctx->stroke();
			}

		// Draw the main second.

			ctx->strokeStyle(whitesmoke);
			ctx->moveTo((clockSize / 2.0f)  + (Ogre::Math::Sin(sec * Ogre::Math::PI / 30.0f) * (clockSize / 4.0f) ),  (clockSize / 2.0f)  + (-Ogre::Math::Cos(sec * Ogre::Math::PI / 30.0f) * (clockSize / 4.0f)));
			ctx->lineTo((clockSize / 2.0f)  + (Ogre::Math::Sin(sec * Ogre::Math::PI / 30.0f) * (clockSize / 3.5f) ) , (clockSize / 2.0f)  + (-Ogre::Math::Cos(sec * Ogre::Math::PI / 30.0f) * (clockSize / 3.5f)));
			ctx->stroke();

		// Outer ring:

			ctx->strokeStyle(whitesmoke);		
			ctx->arc((clockSize / 2.0f),(clockSize / 2.0f), (clockSize / 3.5f), 0.0f, 360.0f, false); // Path out an arc, from the center, for a radius of 1/3.5th of the image
			ctx->stroke();

		// The 12:

			// Path and Stroke "12"
			ctx->fillText("12", (clockSize / 2.0f)-8, (clockSize / 5.0f));

	double renderTime = chrono.getTimeElapsed();
	mCanvasTextureClock2->uploadTexture();
	double uploadTime = chrono.getTimeElapsed()-renderTime;
	std::cout << "clock2 render: " << renderTime << "ms, upload: " << uploadTime << "ms" <<std::endl;
}