void DirectXRender::blitText(IND_Font *pFo,
                             char *pText,
                             int pX,
                             int pY,
                             int pOffset,
                             int pLineSpacing,
                             float pScaleX,
                             float pScaleY,
                             unsigned char pR,
                             unsigned char pG,
                             unsigned char pB,
                             unsigned char pA,
                             unsigned char pFadeR,
                             unsigned char pFadeG,
                             unsigned char pFadeB,
                             unsigned char pFadeA,
                             IND_Filter pLinearFilter,
                             IND_BlendingType pSo,
                             IND_BlendingType pDs,
                             IND_Align pAlign) {
	// ----- Transform -----

	bool correctParams = true;
	if(!pFo->getSurface()) {
		correctParams = false;
	}

	if (correctParams) {
		//SetTransform2d (pX, pY, 0, 0, 0, 1, 1, 0, 0, 0, 0, pFo->getSurface()->getWidthBlock(), pFo->getSurface()->getHeightBlock(), 0);
		setTransform2d(pX, pY, 0, 0, 0, pScaleX, pScaleY, 0, 0, 0, 0, pFo->getSurface()->getWidthBlock(), pFo->getSurface()->getHeightBlock(), 0);
		setRainbow2d(pFo->_font._surface->getTypeInt(), 1, 0, 0, pLinearFilter, pR, pG, pB, pA, pFadeR, pFadeG, pFadeB, pFadeA, pSo, pDs);

		// ----- Drawing text   -----

		unsigned char mChar1;
		unsigned char mChar2;
		int mCont1 = 0;
		int mCont2 = 0;
		int mTranslationX = 0;
		int mTranslationY = 0;
		bool mErrorChar;    // Char that doesn't exist
		int mSentencePos;
		int mLongActualSentence;

		D3DXMATRIX mMatTraslation, mMatWorld, mMatWorldOriginal;
		_info._device->GetTransform(D3DTS_WORLD, &mMatWorld);
		_info._device->GetTransform(D3DTS_WORLD, &mMatWorldOriginal);

		mCont1 = 0;
		mChar1 = pText [mCont1++];

		while (mChar1 != 0) {
			// If it's a new line or it's the first line
			if (mChar1 == '\n' || mCont1 == 1) {
				// First line
				if (mCont1 == 1)
					mSentencePos = 0;
				else
					mSentencePos = mCont1;

				// Set the alignment
				switch (pAlign) {
				case IND_CENTER: {
					//mLongActualSentence = GetLongInPixels (pFo, pText, mSentencePos, pOffset);
					mLongActualSentence = static_cast<int>(getLongInPixels(pFo, pText, mSentencePos, pOffset) * pScaleX);
					mTranslationX = (int)(mLongActualSentence / 2);
					break;
				}

				case IND_RIGHT: {
					//mLongActualSentence = GetLongInPixels (pFo, pText, mSentencePos, pOffset);
					mLongActualSentence = static_cast<int>(getLongInPixels(pFo, pText, mSentencePos, pOffset) * pScaleX);
					mTranslationX = (int)(mLongActualSentence);

					break;
				}

				case IND_LEFT: {
					break;
				}
				}

				D3DXMatrixTranslation(&mMatTraslation, (float) - mTranslationX, (float) mTranslationY, 0);
				//mTranslationY += (pLineSpacing);
				mTranslationY += static_cast<int>((pLineSpacing * pScaleY));
				D3DXMatrixMultiply(&mMatWorld, &mMatWorldOriginal, &mMatTraslation);
				_info._device->SetTransform(D3DTS_WORLD, &mMatWorld);
			}

			// It's a normal character
			if (mChar1 != '\n') {

				mErrorChar = 0;
				mCont2 = 0;
				mChar2 = pFo->getLetters() [mCont2++]._letter;

				// Seek its location in the bitmap
				while (mChar2 != mChar1 && mCont2 < pFo->getNumChars()) mChar2 = pFo->getLetters() [mCont2++]._letter;
				if (mCont2  ==  pFo->getNumChars())
					mErrorChar = 1;

				mCont2--;
				if (!mErrorChar) {
					blitRegionSurface(pFo->getSurface(),
									  pFo->getLetters() [mCont2]._offsetX + 1,
									  pFo->getLetters() [mCont2]._offsetY + 1,
									  pFo->getLetters() [mCont2]._widthChar - 1,
									  pFo->getLetters() [mCont2]._heightChar - 1);
				}


				// Displacement of the character
				//SetTranslation ((pFo->getLetters() [mCont2]._widthChar) + pOffset, 0, &mMatWorld, &mMatTraslation);
				SetTranslation(static_cast<int>(((pFo->getLetters() [mCont2]._widthChar) + pOffset) * pScaleX), 0, &mMatWorld, &mMatTraslation);
			}

			// Advance one character
			mChar1 = pText [mCont1++];
		}
	}
}
void OpenGLRender::blitText(IND_Font *pFo,
                            char *pText,
                            int pX,
                            int pY,
                            int pOffset,
                            int pLineSpacing,
                            float pScaleX,
                            float pScaleY,
                            BYTE pR,
                            BYTE pG,
                            BYTE pB,
                            BYTE pA,
                            BYTE pFadeR,
                            BYTE pFadeG,
                            BYTE pFadeB,
                            BYTE pFadeA,
                            IND_Filter pLinearFilter,
                            IND_BlendingType pSo,
                            IND_BlendingType pDs,
                            IND_Align pAlign) {
	// ----- Transform -----
	bool correctParams = true;
	if(!pFo->getSurface()) {
		correctParams = false;
	}

	if (correctParams) {
		setTransform2d(pX, pY, 0, 0, 0, pScaleX, pScaleY, 0, 0, 0, 0, pFo->getSurface()->getWidthBlock(), pFo->getSurface()->getHeightBlock(), 0);
		setRainbow2d(pFo->_font._surface->getTypeInt(), 1, 0, 0, pLinearFilter, pR, pG, pB, pA, pFadeR, pFadeG, pFadeB, pFadeA, pSo, pDs);

		// ----- Drawing text   -----
		
		BYTE mChar1;
		BYTE mChar2;
		int mCont1 = 0;
		int mCont2 = 0;
		int mTranslationX = 0;
		int mTranslationY = 0;
		bool mErrorChar;    // Char that doesn't exist
		int mSentencePos;
		int mLongActualSentence;
		GLfloat mLineTransform[16];  //Maintains a transform inside the entity coord system
		GLfloat mEntityTransform[16]; //Maintains transform for the whole entity coord system
		mCont1 = 0;
		mChar1 = pText [mCont1++];

		//Store entity transform
		glGetFloatv(GL_MODELVIEW_MATRIX,mEntityTransform);

		//LOOP - Blit character by character
		while (mChar1 != 0) {
			// If it's a new line or it's the first line
			if (mChar1 == '\n' || mCont1 == 1) {
				// First line
				if (mCont1 == 1) {
					mSentencePos = 0;
				//Any other line
				} else {
					mSentencePos = mCont1;
				}

				// Set the alignment
				switch (pAlign) {
					case IND_CENTER: {
						mLongActualSentence = static_cast<int>(getLongInPixels(pFo, pText, mSentencePos, pOffset) * pScaleX);
						mTranslationX = (int)(mLongActualSentence / 2);
						break;
					}

					case IND_RIGHT: {
						mLongActualSentence = static_cast<int>(getLongInPixels(pFo, pText, mSentencePos, pOffset) * pScaleX);
						mTranslationX = (int)(mLongActualSentence);

						break;
					}

					case IND_LEFT: {
						break;
					}
				}

				//Load line transform matrix taking into account entity transform
				//This effectively resets transform to first character in the new line
				IND_Matrix transform;
				_math.matrix4DSetTranslation(transform, static_cast<float>(-mTranslationX), static_cast<float>(mTranslationY), 0.0f);
				transform.arrayRepresentation(mLineTransform);
				glLoadMatrixf(mEntityTransform);
				glMultMatrixf(mLineTransform);
				mTranslationY += static_cast<int>((pLineSpacing * pScaleY));
			} //Was first new line or first line

			// It's a normal character
			if (mChar1 != '\n') {

				mErrorChar = 0;
				mCont2 = 0;
				mChar2 = pFo->getLetters() [mCont2++]._letter;

				// Seek its location in the bitmap
				while (mChar2 != mChar1 && mCont2 < pFo->getNumChars()) {
					mChar2 = pFo->getLetters() [mCont2++]._letter;
				}
				if (mCont2  ==  pFo->getNumChars())
					mErrorChar = 1;

				mCont2--;
				if (!mErrorChar) {
//#warning lookout
					//mvTransformPresetState();  //Need to preset transform state, as the blit operation will reset the state!!!!
					blitRegionSurface(pFo->getSurface(),
									  pFo->getLetters() [mCont2]._offsetX + 1,
									  pFo->getLetters() [mCont2]._offsetY + 1,
									  pFo->getLetters() [mCont2]._widthChar - 1,
									  pFo->getLetters() [mCont2]._heightChar - 1);
				}

				//Displacement of the character.
				//Displacement transform accumulates for every character in the line
				float charTranslateX = ((pFo->getLetters() [mCont2]._widthChar) + pOffset) * pScaleX;
				glTranslatef(charTranslateX,0.0f,0.0f);
			}//Was normal character

			// Advance one character
			mChar1 = pText [mCont1++];
		}//LOOP END - Blit character by character

	}
}