From bce513e08316ec01bf3384062740b4d8198cdb3e Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Wed, 8 Feb 2012 22:54:15 +0100 Subject: 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 --- src/resources/wallpaper.cpp | 46 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 24 deletions(-) (limited to 'src/resources/wallpaper.cpp') 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::iterator iter; - WallpaperData wp; + if (wallpaperData.empty()) + return haveBackup ? (wallpaperPath + wallpaperFile) : std::string(); - // Wallpaper filename container - std::vector wallPaperVector; + WallpaperData wallpaper; + // Search for the smallest wallpaper at least as large as the screen + std::vector::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; } -- cgit v1.2.3-70-g09d2