00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "resources/wallpaper.h"
00023
00024 #include "log.h"
00025
00026 #include "utils/strprintf.h"
00027
00028 #include <algorithm>
00029 #include <cstring>
00030 #include <vector>
00031
00032 #include <physfs.h>
00033
00034 #define WALLPAPER_FOLDER "graphics/images/"
00035 #define WALLPAPER_BASE "login_wallpaper"
00036
00037 struct WallpaperSize
00038 {
00039 int width;
00040 int height;
00041 };
00042
00043 static std::vector<WallpaperSize> wallpaperSizes;
00044 static bool haveBackup;
00045
00046 bool wallpaperCompare(WallpaperSize a, WallpaperSize b)
00047 {
00048 int aa = a.width * a.height;
00049 int ab = b.width * b.height;
00050
00051 return (aa > ab || (aa == ab && a.width > b.width));
00052 }
00053
00054 void Wallpaper::loadWallpapers()
00055 {
00056 wallpaperSizes.clear();
00057
00058 size_t baseLen = strlen(WALLPAPER_BASE);
00059 haveBackup = false;
00060
00061 char **imgs = PHYSFS_enumerateFiles(WALLPAPER_FOLDER);
00062
00063 for (char **i = imgs; *i != NULL; i++)
00064 {
00065 if (strncmp(*i, WALLPAPER_BASE, baseLen) == 0)
00066 {
00067 int width;
00068 int height;
00069
00070 if (strlen(*i) == baseLen + 4)
00071 {
00072 if (haveBackup)
00073 logger->log("Duplicate default wallpaper!");
00074 else
00075 haveBackup = true;
00076 }
00077 else if (sscanf(*i, WALLPAPER_BASE "_%dx%d.png",
00078 &width, &height) == 2)
00079 {
00080 WallpaperSize wp;
00081 wp.width = width;
00082 wp.height = height;
00083 wallpaperSizes.push_back(wp);
00084 }
00085 }
00086 }
00087
00088 PHYSFS_freeList(imgs);
00089
00090 std::sort(wallpaperSizes.begin(), wallpaperSizes.end(), wallpaperCompare);
00091 }
00092
00093 std::string Wallpaper::getWallpaper(int width, int height)
00094 {
00095 std::vector<WallpaperSize>::iterator iter;
00096 WallpaperSize wp;
00097
00098 for (iter = wallpaperSizes.begin(); iter != wallpaperSizes.end(); iter++)
00099 {
00100 wp = *iter;
00101 if (wp.width <= width && wp.height <= height)
00102 {
00103 return std::string(strprintf(WALLPAPER_FOLDER WALLPAPER_BASE
00104 "_%dx%d.png", wp.width, wp.height));
00105 }
00106 }
00107
00108 if (haveBackup)
00109 return std::string(WALLPAPER_FOLDER WALLPAPER_BASE ".png");
00110
00111 return std::string();
00112 }