FontAsset *Assets::RequestFont(const std::string &filename, int size) { FontAsset *asset = NULL; std::string name = filename; // GetContentPath() + filename; asset = (FontAsset *)GetAssetByFilename(name); if (!asset) { asset = new FontAsset(); if (asset->Load(name, size)) { StoreAsset(asset); std::cout << asset->m_filename << ": Load" << std::endl; } else { SDELETE(asset); } } if (asset) { asset->AddReference(); std::cout << asset->m_filename << ": RefCount (" << asset->m_iRefCount << ")" << std::endl; } return asset; }
FontAsset* AssetManager::RequestFont(const std::string &filename, int size) { assert(filename.c_str()); FontAsset* asset = static_cast<FontAsset*>(GetAssetByFilename(filename)); if (!asset) { asset = new FontAsset(); bool result = false; switch (mMethod) { case AssetLoadingMethod::LoadFromFile: result = asset->Load(filename, size); break; case AssetLoadingMethod::LoadFromBinary: result = asset->Load(filename, size); break; } if (result) { StoreAsset(asset); } else { printIn("%s: Failed to load\n", asset->mName.c_str()); SAFE_DELETE(asset); } } if (asset) { asset->AddReference(); printIn("%s: RefCount (%d)\n", asset->mName.c_str(), asset->mRefCount); } return asset; }
void Graphics::RenderText(const FontAsset& font, const std::string& text, float x, float y) { Rect verts, texCoords; glBegin(GL_QUADS); for (int i = 0; i < text.size(); i++) { char c = text[i]; if ((c >= 32) && (c < 128)) { font.GetGlyphData(c, &x, &y, verts, texCoords); glTexCoord2f(texCoords.topLeft.x, texCoords.topLeft.y); glVertex2f(verts.topLeft.x, verts.topLeft.y); glTexCoord2f(texCoords.bottomRight.x, texCoords.topLeft.y); glVertex2f(verts.bottomRight.x, verts.topLeft.y); glTexCoord2f(texCoords.bottomRight.x, texCoords.bottomRight.y); glVertex2f(verts.bottomRight.x, verts.bottomRight.y); glTexCoord2f(texCoords.topLeft.x, texCoords.bottomRight.y); glVertex2f(verts.topLeft.x, verts.bottomRight.y); } } glEnd(); }
// Render the given string, updating x and y to the point where the next character // after this string should be placed. void Graphics::RenderTextUpdatePos(const FontAsset& font, const std::wstring& text, float *x, float *y) { Rect verts, texCoords; BindFont(&font); glBegin(GL_QUADS); for (int i = 0; i < text.size(); i++) { // Render any non-control characters int unicodeCodepoint = text[i]; if (unicodeCodepoint >= 32) { font.GetGlyphData(unicodeCodepoint, x, y, verts, texCoords); glTexCoord2f(texCoords.topLeft.x, texCoords.topLeft.y); glVertex2f(verts.topLeft.x, verts.topLeft.y); glTexCoord2f(texCoords.bottomRight.x, texCoords.topLeft.y); glVertex2f(verts.bottomRight.x, verts.topLeft.y); glTexCoord2f(texCoords.bottomRight.x, texCoords.bottomRight.y); glVertex2f(verts.bottomRight.x, verts.bottomRight.y); glTexCoord2f(texCoords.topLeft.x, texCoords.bottomRight.y); glVertex2f(verts.topLeft.x, verts.bottomRight.y); } } glEnd(); }