summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2012-02-08 22:54:15 +0100
committerThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2012-02-12 19:06:28 +0100
commitbce513e08316ec01bf3384062740b4d8198cdb3e (patch)
tree7cd877925d18907765d1194792a1c4ff46436837
parentd3267f81ac036a51bbf3ee83574df1cbca0eaa85 (diff)
downloadmana-bce513e08316ec01bf3384062740b4d8198cdb3e.tar.gz
mana-bce513e08316ec01bf3384062740b4d8198cdb3e.tar.bz2
mana-bce513e08316ec01bf3384062740b4d8198cdb3e.tar.xz
mana-bce513e08316ec01bf3384062740b4d8198cdb3e.zip
Improved the wallpaper choosing algorithm
Now it searches for the smallest wallpaper that is at least as large as the screen, since downscaling is preferred to upscaling. When not found, it will go with the largest available wallpaper. Previously, the algorithm preferred upscaling, but didn't bother searching for the largest available one, but would pick a random image. It would also revert to the default Mana wallpaper when no image was small enough to fit on the screen. Reviewed-by: Erik Schilling Reviewed-by: Yohann Ferreira
-rw-r--r--src/resources/wallpaper.cpp46
1 files changed, 22 insertions, 24 deletions
diff --git a/src/resources/wallpaper.cpp b/src/resources/wallpaper.cpp
index f48f954f..306e4120 100644
--- a/src/resources/wallpaper.cpp
+++ b/src/resources/wallpaper.cpp
@@ -78,6 +78,9 @@ static void initWallpaperPaths()
}
}
+/**
+ * Comparison function that puts the largest wallpaper first.
+ */
bool wallpaperCompare(const WallpaperData &a, const WallpaperData &b)
{
int aa = a.width * a.height;
@@ -135,38 +138,33 @@ void Wallpaper::loadWallpapers()
std::string Wallpaper::getWallpaper(int width, int height)
{
- std::vector<WallpaperData>::iterator iter;
- WallpaperData wp;
+ if (wallpaperData.empty())
+ return haveBackup ? (wallpaperPath + wallpaperFile) : std::string();
- // Wallpaper filename container
- std::vector<std::string> wallPaperVector;
+ WallpaperData wallpaper;
+ // Search for the smallest wallpaper at least as large as the screen
+ std::vector<WallpaperData>::iterator iter;
for (iter = wallpaperData.begin(); iter != wallpaperData.end(); iter++)
{
- wp = *iter;
- if (wp.width <= width && wp.height <= height)
- wallPaperVector.push_back(wp.filename);
- }
+ const WallpaperData &wp = *iter;
- if (!wallPaperVector.empty())
- {
- // If we've got more than one occurence of a valid wallpaper...
- if (wallPaperVector.size() > 0)
+ if (wp.width >= width && wp.height >= height)
{
- // Return randomly a wallpaper between vector[0] and
- // vector[vector.size() - 1]
- srand((unsigned) time(0));
- return wallPaperVector
- [int(wallPaperVector.size() * rand() / (RAND_MAX + 1.0))];
+ if (wallpaper.filename.empty() ||
+ (wallpaper.width < wp.width &&
+ wallpaper.height < wp.height))
+ {
+ wallpaper = wp;
+ }
}
- else // If there at least one, we return it
- return wallPaperVector[0];
}
- // Return the backup file if everything else failed...
- if (haveBackup)
- return std::string(wallpaperPath + wallpaperFile);
+ // When no fitting wallpaper was found yet, pick the biggest one
+ if (wallpaper.filename.empty())
+ {
+ wallpaper = wallpaperData.front();
+ }
- // Return an empty string if everything else failed
- return std::string();
+ return wallpaper.filename;
}