Mana
Loading...
Searching...
No Matches
wallpaper.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "resources/wallpaper.h"
23
24#include "configuration.h"
25
26#include "utils/filesystem.h"
27
28#include <algorithm>
29#include <cstring>
30#include <vector>
31
33{
34 std::string filename;
35 int width;
36 int height;
37};
38
39inline std::ostream& operator <<(std::ostream &os, const WallpaperData &d)
40{
41 os << d.filename << "[" << d.width << "x" << d.height << "]";
42 return os;
43}
44
45static std::vector<WallpaperData> wallpaperData;
46static bool haveBackup; // Is the backup (no size given) version available?
47
48static std::string wallpaperPath;
49static std::string wallpaperFile;
50
51static void initWallpaperPaths()
52{
53 // Init the path
54 wallpaperPath = paths.getStringValue("wallpapers");
55
56 if (wallpaperPath.empty())
57 {
58 wallpaperPath = branding.getStringValue("wallpapersPath");
59
60 if (wallpaperPath.empty())
61 wallpaperPath = "graphics/images/";
62 }
63
64 // Init the default file
65 wallpaperFile = paths.getStringValue("wallpaperFile");
66
67 if (wallpaperFile.empty())
68 {
69 wallpaperFile = branding.getStringValue("wallpaperFile");
70
71 if (wallpaperFile.empty())
72 wallpaperFile = "login_wallpaper.png";
73 }
74}
75
80{
81 int aa = a.width * a.height;
82 int ab = b.width * b.height;
83
84 return (aa > ab || (aa == ab && a.width > b.width));
85}
86
88{
89 wallpaperData.clear();
90 haveBackup = false;
91
92 initWallpaperPaths();
93
94 for (auto fileName : FS::enumerateFiles(wallpaperPath))
95 {
96 // If the backup file is found, we tell it.
97 if (wallpaperFile == fileName)
98 haveBackup = true;
99
100 // If the image format is terminated by: "_<width>x<height>.png"
101 // It is taken as a potential wallpaper.
102 if (auto sizeSuffix = strrchr(fileName, '_'))
103 {
104 int width;
105 int height;
106
107 if (sscanf(sizeSuffix, "_%dx%d.png", &width, &height) == 2)
108 {
109 WallpaperData wp;
110 wp.filename = wallpaperPath;
111 wp.filename.append(fileName);
112 wp.width = width;
113 wp.height = height;
114 wallpaperData.push_back(wp);
115 }
116 }
117 }
118
119 std::sort(wallpaperData.begin(), wallpaperData.end(), wallpaperCompare);
120}
121
122std::string Wallpaper::getWallpaper(int width, int height)
123{
124 if (wallpaperData.empty())
125 return haveBackup ? (wallpaperPath + wallpaperFile) : std::string();
126
127 WallpaperData wallpaper;
128
129 // Search for the smallest wallpaper at least as large as the screen
130 for (auto &wp : wallpaperData)
131 {
132 if (wp.width >= width && wp.height >= height)
133 {
134 if (wallpaper.filename.empty() || (wallpaper.width < wp.width &&
135 wallpaper.height < wp.height))
136 {
137 wallpaper = wp;
138 }
139 }
140 }
141
142 // When no fitting wallpaper was found yet, pick the biggest one
143 if (wallpaper.filename.empty())
144 {
145 wallpaper = wallpaperData.front();
146 }
147
148 return wallpaper.filename;
149}
std::string getStringValue(const std::string &key) const
static void loadWallpapers()
Reads the folder that contains wallpapers and organizes the wallpapers found by area,...
Definition wallpaper.cpp:87
static std::string getWallpaper(int width, int height)
Returns the largest wallpaper for the given resolution, or the default wallpaper if none are found.
Configuration paths
XML default paths information reader.
Definition client.cpp:99
Configuration branding
XML branding information reader.
Definition client.cpp:98
Files enumerateFiles(const std::string &dir)
Returns a list of files in the given directory.
Definition filesystem.h:153
std::string filename
Definition wallpaper.cpp:34
bool wallpaperCompare(const WallpaperData &a, const WallpaperData &b)
Comparison function that puts the largest wallpaper first.
Definition wallpaper.cpp:79
std::ostream & operator<<(std::ostream &os, const WallpaperData &d)
Definition wallpaper.cpp:39