Added custom font support.

Check out THEMES.md for more information.
This commit is contained in:
Aloshi 2012-10-31 09:46:06 -05:00
parent 3ebb819a7c
commit fe030fb6c7
12 changed files with 116 additions and 18 deletions

View file

@ -114,6 +114,28 @@ Display tags define some "meta" display attributes about your theme. Display tag
`<boxCorner>` - path to the "top left corner" image file. It will be flipped for the top right, bottom right, and bottom left corners. ~ and . are expanded. `<boxCorner>` - path to the "top left corner" image file. It will be flipped for the top right, bottom right, and bottom left corners. ~ and . are expanded.
Fonts
=====
Fonts are defined like so:
```
<fontTag>
<path>./path/to/font</path>
<size>0.05</size>
</fontTag>
```
You can leave off any tags you don't want to use, and they'll use the default. Size is defined as a percentage of the screen height. "." and "~" are expanded for paths.
**Font tags:**
`<listFont>` - font to use for the game list.
`<descriptionFont>` - font to use for description text.
Audio Audio
===== =====

View file

@ -1,3 +1,7 @@
October 31
-Added custom font support. Check out THEMES.md.
-Happy halloween!
October 26 October 26
-Hopefully fixed the black screen bug with certain programs. -Hopefully fixed the black screen bug with certain programs.

View file

@ -10,6 +10,13 @@ bool Font::libraryInitialized = false;
int Font::getDpiX() { return 96; } int Font::getDpiX() { return 96; }
int Font::getDpiY() { return 96; } int Font::getDpiY() { return 96; }
int Font::getSize() { return mSize; }
std::string Font::getDefaultPath()
{
return "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSerif.ttf";
}
void Font::initLibrary() void Font::initLibrary()
{ {
if(FT_Init_FreeType(&sLibrary)) if(FT_Init_FreeType(&sLibrary))
@ -149,7 +156,10 @@ void Font::buildAtlas()
x += g->bitmap.width; x += g->bitmap.width;
} }
std::cout << "Final texture coords: " << x << ", " << y << "\n"; if(y >= textureHeight)
{
std::cerr << "Error - font size exceeded texture size! If you were doing something reasonable, tell Aloshi to fix it!\n";
}
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);

View file

@ -45,6 +45,9 @@ public:
void onInit(); void onInit();
void onDeinit(); void onDeinit();
int getSize();
static std::string getDefaultPath();
private: private:
static int getDpiX(); static int getDpiX();
static int getDpiY(); static int getDpiY();

View file

@ -87,8 +87,9 @@ namespace Renderer {
//make sure our font exists //make sure our font exists
if(!boost::filesystem::exists(fontPath)) if(!boost::filesystem::exists(fontPath))
{ {
std::cout << "Default font \"" << fontPath << "\" does not exist! Attempting to default to a system font..."; //std::cout << "Default font \"" << fontPath << "\" does not exist! Attempting to default to a system font...";
fontPath = "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf"; //fontPath = "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf";
fontPath = Font::getDefaultPath();
if(!boost::filesystem::exists(fontPath)) if(!boost::filesystem::exists(fontPath))
{ {
std::cerr << "System font \"" << fontPath << "\" wasn't found either! Well, you're kind of screwed. Sorry.\n"; std::cerr << "System font \"" << fontPath << "\" wasn't found either! Well, you're kind of screwed. Sorry.\n";
@ -96,7 +97,7 @@ namespace Renderer {
} }
} }
float fontSizes[] = {0.035, 0.05, 0.1}; float fontSizes[] = {0.035, 0.045, 0.1};
for(unsigned int i = 0; i < 3; i++) for(unsigned int i = 0; i < 3; i++)
{ {
fonts[i] = new Font(fontPath, (unsigned int)(fontSizes[i] * getScreenHeight())); fonts[i] = new Font(fontPath, (unsigned int)(fontSizes[i] * getScreenHeight()));

View file

@ -21,17 +21,18 @@ GuiGameList::GuiGameList(bool useDetail)
//Those with smaller displays may prefer the older view. //Those with smaller displays may prefer the older view.
if(mDetailed) if(mDetailed)
{ {
mList = new GuiList<FileData*>(Renderer::getScreenWidth() * sInfoWidth, Renderer::getDefaultFont(Renderer::LARGE)->getHeight() + 2); mList = new GuiList<FileData*>(Renderer::getScreenWidth() * sInfoWidth, Renderer::getDefaultFont(Renderer::LARGE)->getHeight() + 2, Renderer::getDefaultFont(Renderer::MEDIUM));
mScreenshot = new GuiImage(Renderer::getScreenWidth() * mTheme->getGameImageOffsetX(), Renderer::getScreenHeight() * mTheme->getGameImageOffsetY(), "", Renderer::getScreenWidth() * sInfoWidth * 0.7, 0, false); mScreenshot = new GuiImage(Renderer::getScreenWidth() * mTheme->getGameImageOffsetX(), Renderer::getScreenHeight() * mTheme->getGameImageOffsetY(), "", Renderer::getScreenWidth() * sInfoWidth * 0.7, 0, false);
mScreenshot->setOrigin(mTheme->getGameImageOriginX(), mTheme->getGameImageOriginY()); mScreenshot->setOrigin(mTheme->getGameImageOriginX(), mTheme->getGameImageOriginY());
//addChild(mScreenshot); //addChild(mScreenshot);
//the animation renders the screenshot
mImageAnimation = new GuiAnimation(); mImageAnimation = new GuiAnimation();
mImageAnimation->addChild(mScreenshot); mImageAnimation->addChild(mScreenshot);
addChild(mImageAnimation); addChild(mImageAnimation);
}else{ }else{
mList = new GuiList<FileData*>(0, Renderer::getDefaultFont(Renderer::LARGE)->getHeight() + 2); mList = new GuiList<FileData*>(0, Renderer::getDefaultFont(Renderer::LARGE)->getHeight() + 2, Renderer::getDefaultFont(Renderer::MEDIUM));
mScreenshot = NULL; mScreenshot = NULL;
mImageAnimation = NULL; mImageAnimation = NULL;
} }
@ -108,7 +109,7 @@ void GuiGameList::onRender()
std::string desc = game->getDescription(); std::string desc = game->getDescription();
if(!desc.empty()) if(!desc.empty())
Renderer::drawWrappedText(desc, Renderer::getScreenWidth() * 0.03, mScreenshot->getOffsetY() + mScreenshot->getHeight() + 12, Renderer::getScreenWidth() * (mTheme->getListOffsetX() - 0.03), mTheme->getDescColor(), Renderer::getDefaultFont(Renderer::SMALL)); Renderer::drawWrappedText(desc, Renderer::getScreenWidth() * 0.03, mScreenshot->getOffsetY() + mScreenshot->getHeight() + 12, Renderer::getScreenWidth() * (mTheme->getListOffsetX() - 0.03), mTheme->getDescColor(), mTheme->getDescriptionFont());
} }
} }
} }
@ -226,6 +227,9 @@ void GuiGameList::updateTheme()
mList->setSelectedTextColor(mTheme->getSelectedTextColor()); mList->setSelectedTextColor(mTheme->getSelectedTextColor());
mList->setScrollSound(mTheme->getMenuScrollSound()); mList->setScrollSound(mTheme->getMenuScrollSound());
//fonts
mList->setFont(mTheme->getListFont());
if(mDetailed) if(mDetailed)
{ {
mList->setCentered(mTheme->getListCentered()); mList->setCentered(mTheme->getListCentered());

View file

@ -4,7 +4,7 @@
#include <iostream> #include <iostream>
template <typename listType> template <typename listType>
GuiList<listType>::GuiList(int offsetX, int offsetY, Renderer::FontSize fontsize) GuiList<listType>::GuiList(int offsetX, int offsetY, Font* font)
{ {
mSelection = 0; mSelection = 0;
mScrollDir = 0; mScrollDir = 0;
@ -16,7 +16,7 @@ GuiList<listType>::GuiList(int offsetX, int offsetY, Renderer::FontSize fontsize
mTextOffsetX = 0; mTextOffsetX = 0;
mFont = Renderer::getDefaultFont(fontsize); mFont = font;
mSelectorColor = 0x000000FF; mSelectorColor = 0x000000FF;
mSelectedTextColorOverride = 0x0000FF; mSelectedTextColorOverride = 0x0000FF;
mScrollSound = NULL; mScrollSound = NULL;
@ -266,3 +266,9 @@ void GuiList<listType>::setScrollSound(Sound* sound)
{ {
mScrollSound = sound; mScrollSound = sound;
} }
template <typename listType>
void GuiList<listType>::setFont(Font* font)
{
mFont = font;
}

View file

@ -15,7 +15,7 @@ template <typename listType>
class GuiList : public GuiComponent class GuiList : public GuiComponent
{ {
public: public:
GuiList(int offsetX = 0, int offsetY = 0, Renderer::FontSize fontsize = Renderer::MEDIUM); GuiList(int offsetX, int offsetY, Font* font);
~GuiList(); ~GuiList();
void onRender(); void onRender();
@ -44,6 +44,7 @@ public:
listType getObject(int i); listType getObject(int i);
void setSelection(int i); void setSelection(int i);
void setFont(Font* f);
private: private:
static const int SCROLLDELAY = 507; static const int SCROLLDELAY = 507;
static const int SCROLLTIME = 200; static const int SCROLLTIME = 200;

View file

@ -6,7 +6,7 @@ GuiMenu::GuiMenu(GuiComponent* parent)
mParent = parent; mParent = parent;
parent->pause(); parent->pause();
mList = new GuiList<std::string>(0, Renderer::getDefaultFont(Renderer::LARGE)->getHeight() + 2, Renderer::LARGE); mList = new GuiList<std::string>(0, Renderer::getDefaultFont(Renderer::LARGE)->getHeight() + 2, Renderer::getDefaultFont(Renderer::LARGE));
mList->setSelectedTextColor(0x0000FFFF); mList->setSelectedTextColor(0x0000FFFF);
populateList(); populateList();
addChild(mList); addChild(mList);

View file

@ -36,8 +36,27 @@ float GuiTheme::getGameImageOriginY() { return mGameImageOriginY; }
std::string GuiTheme::getImageNotFoundPath() { return mImageNotFoundPath; } std::string GuiTheme::getImageNotFoundPath() { return mImageNotFoundPath; }
Font* GuiTheme::getListFont()
{
if(mListFont == NULL)
return Renderer::getDefaultFont(Renderer::MEDIUM);
else
return mListFont;
}
Font* GuiTheme::getDescriptionFont()
{
if(mDescFont == NULL)
return Renderer::getDefaultFont(Renderer::SMALL);
else
return mDescFont;
}
GuiTheme::GuiTheme(std::string path) GuiTheme::GuiTheme(std::string path)
{ {
mListFont = NULL;
mDescFont = NULL;
setDefaults(); setDefaults();
if(!path.empty()) if(!path.empty())
@ -85,6 +104,18 @@ void GuiTheme::setDefaults()
mMenuOpenSound.loadFile(""); mMenuOpenSound.loadFile("");
mImageNotFoundPath = ""; mImageNotFoundPath = "";
if(mListFont != NULL)
{
delete mListFont;
mListFont = NULL;
}
if(mDescFont != NULL)
{
delete mDescFont;
mDescFont = NULL;
}
} }
void GuiTheme::deleteComponents() void GuiTheme::deleteComponents()
@ -178,7 +209,8 @@ void GuiTheme::readXML(std::string path)
mMenuOpenSound.loadFile(expandPath(root.child("menuOpenSound").text().get())); mMenuOpenSound.loadFile(expandPath(root.child("menuOpenSound").text().get()));
//fonts //fonts
//mListFont = resolveFont(root.child("listFont"), NULL); mListFont = resolveFont(root.child("listFont"), Font::getDefaultPath(), Renderer::getDefaultFont(Renderer::MEDIUM)->getSize());
mDescFont = resolveFont(root.child("descriptionFont"), Font::getDefaultPath(), Renderer::getDefaultFont(Renderer::SMALL)->getSize());
//actually read the components //actually read the components
createComponentChildren(root, this); createComponentChildren(root, this);
@ -331,13 +363,23 @@ float GuiTheme::strToFloat(std::string str, float defaultVal)
return ret; return ret;
} }
Font* GuiTheme::resolveFont(pugi::xml_node node, Font* def) Font* GuiTheme::resolveFont(pugi::xml_node node, std::string defaultPath, unsigned int defaultSize)
{ {
if(!node) if(!node)
return def; return NULL;
std::string path = expandPath(node.child("path").text().get()); std::string path = expandPath(node.child("path").text().get());
int size = (int)(strToFloat(node.child("size").value()) * Renderer::getScreenHeight()); unsigned int size = (unsigned int)(strToFloat(node.child("size").text().get()) * Renderer::getScreenHeight());
if(!boost::filesystem::exists(path))
{
path = defaultPath;
}
if(size == 0)
{
size = defaultSize;
}
return new Font(path, size); return new Font(path, size);
} }

View file

@ -44,6 +44,9 @@ public:
Sound* getMenuOpenSound(); Sound* getMenuOpenSound();
std::string getImageNotFoundPath(); std::string getImageNotFoundPath();
Font* getListFont();
Font* getDescriptionFont();
private: private:
void setDefaults(); void setDefaults();
void deleteComponents(); void deleteComponents();
@ -56,7 +59,7 @@ private:
unsigned int resolveColor(std::string str, unsigned int defaultColor = 0x000000FF); unsigned int resolveColor(std::string str, unsigned int defaultColor = 0x000000FF);
void splitString(std::string str, char delim, std::string* before, std::string* after); void splitString(std::string str, char delim, std::string* before, std::string* after);
float strToFloat(std::string str, float defaultVal = 0.0f); float strToFloat(std::string str, float defaultVal = 0.0f);
Font* resolveFont(pugi::xml_node node, Font* def); Font* resolveFont(pugi::xml_node node, std::string defaultPath, unsigned int defaultSize);
std::vector<GuiComponent*> mComponentVector; std::vector<GuiComponent*> mComponentVector;
std::string mPath; std::string mPath;
@ -67,6 +70,8 @@ private:
GuiBoxData mBoxData; GuiBoxData mBoxData;
Sound mMenuScrollSound, mMenuSelectSound, mMenuBackSound, mMenuOpenSound; Sound mMenuScrollSound, mMenuSelectSound, mMenuBackSound, mMenuOpenSound;
std::string mImageNotFoundPath; std::string mImageNotFoundPath;
Font* mListFont;
Font* mDescFont;
}; };
#endif #endif

View file

@ -48,7 +48,7 @@ int main(int argc, char* argv[])
}else if(strcmp(argv[i], "--draw-framerate") == 0) }else if(strcmp(argv[i], "--draw-framerate") == 0)
{ {
DRAWFRAMERATE = true; DRAWFRAMERATE = true;
}else if(strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) }else if(strcmp(argv[i], "--help") == 0)
{ {
std::cout << "EmulationStation, a graphical front-end for ROM browsing.\n"; std::cout << "EmulationStation, a graphical front-end for ROM browsing.\n";
std::cout << "Command line arguments:\n"; std::cout << "Command line arguments:\n";
@ -166,7 +166,7 @@ int main(int argc, char* argv[])
ss << framerate; ss << framerate;
std::string fps; std::string fps;
ss >> fps; ss >> fps;
Renderer::drawText(fps, 50, 50, 0x00FF00, Renderer::getDefaultFont(Renderer::MEDIUM)); Renderer::drawText(fps, 50, 50, 0x00FF00FF, Renderer::getDefaultFont(Renderer::MEDIUM));
} }