/** * CBitmapFont::drawStringMarkup * @date Modified Mar 29, 2006 */ void CBitmapFont::drawStringMarkup(CString str, float fX, float fY, D3DCOLOR dwColor, bool bHandleSprite) { CRenderDevice& oDev = CRenderSystem::getInstance().getRenderDevice(); LPD3DXSPRITE pSprite = oDev.getD3DXSprite(); RECT rChar; char cChar; D3DXVECTOR3 vPos, vZero; vPos.x = fX; vPos.y = fY; vPos.z = 0.0f; vZero.x = vZero.y = vZero.z = 0.0f; if(bHandleSprite) beginSprite(); // Split strings bool bFirst = false; std::vector<CString> vStrings; str.ToList(vStrings, TEXT("{}")); if(str.GetChar(0) == '{') bFirst = true; // Iterate split strings, and change color if needed. for(size_t v = 0; v < vStrings.size(); ++v) { // Get string, and read color if((bFirst && v % 2 == 0) || (!bFirst && v % 2 == 1)) { dwColor = vStrings[v].ToUlongFromHex(); continue; } // Iterate string and draw characters. size_t nSize = vStrings[v].GetLength(); for(size_t i = 0; i < nSize; ++i) { cChar = vStrings[v].GetChar(i); switch(cChar) { case '\n': vPos.y += m_cLineHeight; vPos.x = fX; break; case '\t': vPos.x += m_mCharMap['X'] * 4; break; default: getCharRect(cChar, &rChar); pSprite->Draw(m_poTexture->getD3DTexture(), &rChar, NULL, &vPos, dwColor); vPos.x += (rChar.right - rChar.left); break; } } } if(bHandleSprite) endSprite(); }
/** * CBitmapFont::drawString * @date Modified Mar 29, 2006 */ void CBitmapFont::drawString(CString str, float fX, float fY, D3DCOLOR dwColor, bool bHandleSprite) { CRenderDevice& oDev = CRenderSystem::getInstance().getRenderDevice(); LPD3DXSPRITE pSprite = oDev.getD3DXSprite(); RECT rChar; char cChar; D3DXVECTOR3 vPos, vZero; vPos.x = fX; vPos.y = fY; vPos.z = 0.0f; vZero.x = vZero.y = vZero.z = 0.0f; if(bHandleSprite) beginSprite(); // Iterate string and draw characters. size_t nSize = str.GetLength(); for(size_t i = 0; i < nSize; ++i) { cChar = str.GetChar(i); switch(cChar) { case '\n': vPos.y += m_cLineHeight; vPos.x = fX; break; case '\t': vPos.x += m_mCharMap['X'] * 4; break; default: getCharRect(str.GetChar(i), &rChar); pSprite->Draw(m_poTexture->getD3DTexture(), &rChar, NULL, &vPos, dwColor); vPos.x += (rChar.right - rChar.left); break; } } if(bHandleSprite) endSprite(); }
/** * CBitmapFont::drawStringView * @date Modified Apr 04, 2006 */ void CBitmapFont::drawStringView(CString str, RECT rView, unsigned int unOffset, D3DCOLOR dwColor, bool bHandleSprite) { std::vector<CString> vLines, vWords, vColors; unsigned int unWidth = rView.right - rView.left, unHeight = rView.bottom - rView.top, unWordWidth = 0; RECT rWord; float fX = (float)rView.left, fY = (float)rView.top; str.ToList(vLines, "\r\n"); if(bHandleSprite) beginSprite(); // Calculate max number of lines unsigned int unLineCount = unHeight / m_cLineHeight; // Iterate lines in string. size_t i = ((vLines.size() <= unLineCount) ? 0 : max(vLines.size() - unOffset - unLineCount, 0)); for(; i < ((vLines.size() <= unLineCount) ? vLines.size() : vLines.size() - unOffset); ++i) { // Iterate words in string. vLines[i].ToList(vWords, " "); for(size_t j = 0; j < vWords.size(); ++j) { bool bFirst = false; vWords[j].ToList(vColors, "{}"); if(vWords[j].GetChar(0) == '{') bFirst = true; if(vColors.size()) { for(size_t k = 0; k < vColors.size(); ++k) { if((bFirst && k % 2 == 0) || (!bFirst && k % 2 == 1)) { dwColor = vColors[k].ToUlongFromHex(); continue; } // Take line length into consideration unWordWidth = measureString(vColors[k], &rWord); if(fX + unWordWidth >= unWidth) { fX = (float)rView.left; fY += m_cLineHeight; drawString(vColors[k], fX, fY, dwColor, false); } else { drawString(vColors[k], fX, fY, dwColor, false); fX += unWordWidth + m_mCharMap[' ']; } } } else { // Take line length into consideration unWordWidth = measureString(vWords[j], &rWord); if(fX + unWordWidth >= unWidth) { fX = (float)rView.left; fY += m_cLineHeight; drawString(vWords[j], fX, fY, dwColor, false); } else { drawString(vWords[j], fX, fY, dwColor, false); fX += unWordWidth + m_mCharMap[' ']; } } } fX = (float)rView.left; fY += m_cLineHeight; } if(bHandleSprite) endSprite(); }
int CDirectXRenderLayer::drawSprite(TEXTURE_REF texture, const TSpriteDef& spriteDef) { if ((mVertexBuffer) && (texture < MAX_TEXTURES)) { if (mSpriteTextureRef != texture || mPrimitiveCount == mMaxPrimitiveCount) { endSprite(); mD3DDevice->SetTexture(0, mTexture[texture].mD3DTexture); mSpriteTextureRef = texture; beginSprite(); } UINT startVertex = mPrimitiveCount << 1; float invW = mTexture[texture].mInvWidth; float invH = mTexture[texture].mInvHeight; // Scaling + Translation float positionX, positionY; float width, height; positionX = spriteDef.mLeftTop.mX * mSpriteScale.mX; positionY = spriteDef.mLeftTop.mY * mSpriteScale.mY; width = spriteDef.mWidth * mSpriteScale.mX; height = spriteDef.mHeight * mSpriteScale.mY; if (spriteDef.mTexelToPixel) { positionX = (float)xmath_round(positionX); positionY = (float)xmath_round(positionY); width = (float)xmath_round(width); height = (float)xmath_round(height); } // Substract half-pixel for proper Texel-To-Pixel convertion positionX -= 0.5f; positionY -= 0.5f; mQuad[0].mX = positionX; mQuad[0].mY = positionY; mQuad[0].mU = spriteDef.mTextureRect.mX0 * invW; mQuad[0].mV = spriteDef.mTextureRect.mY0 * invH; mQuad[1].mX = positionX + width; mQuad[1].mY = mQuad[0].mY; mQuad[1].mU = spriteDef.mTextureRect.mX1 * invW; mQuad[1].mV = mQuad[0].mV; mQuad[2].mX = mQuad[0].mX; mQuad[2].mY = positionY + height; mQuad[2].mU = mQuad[0].mU; mQuad[2].mV = spriteDef.mTextureRect.mY1 * invH; mQuad[3].mX = mQuad[1].mX; mQuad[3].mY = mQuad[2].mY; mQuad[3].mU = mQuad[1].mU; mQuad[3].mV = mQuad[2].mV; float subX, subY; float centerX, centerY; centerX = (mQuad[0].mX + mQuad[1].mX) * 0.5f; centerY = (mQuad[0].mY + mQuad[2].mY) * 0.5f; CVector2D& dir = CVector2D::getUnitVector(spriteDef.mAngle); // Rotation for (int index = 0; index < 4; index++) { subX = mQuad[index].mX - centerX; subY = mQuad[index].mY - centerY; mQuad[index].mX = (subX * dir.mX - subY * dir.mY + centerX); mQuad[index].mY = (subX * dir.mY + subY * dir.mX + centerY); mQuad[index].mDiffuse = spriteDef.mDiffuse; } memcpy(mVertexData + startVertex, mQuad, 4 * sizeof(TVertexData)); mPrimitiveCount += 2; return E_SUCCESS; } return E_FAILED; }