ManaPlus
wallpaper.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2019 The ManaPlus Developers
6  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include "resources/wallpaper.h"
25 
26 #include "configuration.h"
27 
28 #include "fs/virtfs/fs.h"
29 #include "fs/virtfs/list.h"
30 
32 
33 #include "utils/cast.h"
34 #include "utils/foreach.h"
35 
36 #include <algorithm>
37 
38 #ifdef WIN32
39 #include <sys/time.h>
40 #endif // WIN32
41 
42 #ifdef __clang__
43 #include <ctime>
44 #endif // __clang__
45 
46 #include "debug.h"
47 
48 static bool wallpaperCompare(const WallpaperData &a, const WallpaperData &b);
49 
50 static STD_VECTOR<WallpaperData> wallpaperData;
51 static bool haveBackup; // Is the backup (no size given) version available?
52 
53 static std::string wallpaperPath;
54 static std::string wallpaperFile;
55 
56 // Search for the wallpaper path values sequentially..
58 {
59  // Init the path
60  wallpaperPath = branding.getStringValue("wallpapersPath");
61 
63  wallpaperPath = paths.getValue("wallpapers", "");
64 
66  wallpaperPath = "graphics/images/";
67 
68  // Init the default file
69  wallpaperFile = branding.getStringValue("wallpaperFile");
70 
72  return;
73  wallpaperFile = paths.getValue("wallpaperFile", "");
74 
76  wallpaperFile = "login_wallpaper.png";
77 }
78 
79 static bool wallpaperCompare(const WallpaperData &a, const WallpaperData &b)
80 {
81  const int aa = a.width * a.height;
82  const int ab = b.width * b.height;
83 
84  return aa > ab ||
85  (aa == ab && a.width > b.width);
86 }
87 
89 {
90  wallpaperData.clear();
93 
94  FOR_EACH (StringVectCIter, i, imgs->names)
95  {
96  // First, get the base filename of the image:
97  std::string filename = *i;
98  const std::string name = filename;
99  // If the backup file is found, we tell it.
100  if (filename.find(wallpaperFile) != std::string::npos)
101  haveBackup = true;
102 
103  // If the image format is terminated by: "_<width>x<height>.png"
104  // It is taken as a potential wallpaper.
105 
106  size_t separator = filename.rfind('_');
107  filename = filename.substr(0, separator);
108 
109  // Check that the base filename doesn't have any '%' markers.
110  separator = filename.find('%');
111  if (separator == std::string::npos)
112  {
113  // Then, append the width and height search mask.
114  filename.append("_%10dx%10d.png");
115 
116  int width;
117  int height;
118  if (sscanf(name.c_str(),
119  filename.c_str(),
120  &width,
121  &height) == 2)
122  {
123  WallpaperData wp;
124  wp.filename = wallpaperPath;
125  wp.filename.append(*i);
126  wp.width = width;
127  wp.height = height;
128  wallpaperData.push_back(wp);
129  }
130  }
131  }
132 
133  VirtFs::freeList(imgs);
134  std::sort(wallpaperData.begin(), wallpaperData.end(), &wallpaperCompare);
135 }
136 
137 std::string Wallpaper::getWallpaper(const int width, const int height)
138 {
139  WallpaperData wp;
140 
141  // Wallpaper filename container
142  StringVect wallPaperVector;
143 
144  FOR_EACH (STD_VECTOR<WallpaperData>::const_iterator, iter, wallpaperData)
145  {
146  wp = *iter;
147  if (wp.width <= width && wp.height <= height)
148  wallPaperVector.push_back(wp.filename);
149  }
150 
151  // If we've got more than one occurence of a valid wallpaper...
152  if (!wallPaperVector.empty())
153  {
154  // Return randomly a wallpaper between vector[0] and
155  // vector[vector.size() - 1]
156  srand(CAST_U32(time(nullptr)));
157  return wallPaperVector[CAST_S32(static_cast<double>(
158  wallPaperVector.size()) * rand() / (RAND_MAX + 1.0))];
159  }
160 
161  // Return the backup file if everything else failed...
162  if (haveBackup)
164 
165  // Return an empty string if everything else failed
166  return std::string();
167 }
#define CAST_S32
Definition: cast.h:30
#define CAST_U32
Definition: cast.h:31
std::string getValue(const std::string &key, const std::string &deflt) const
std::string getStringValue(const std::string &key) const
static void loadWallpapers()
Definition: wallpaper.cpp:88
static std::string getWallpaper(const int width, const int height)
Definition: wallpaper.cpp:137
Configuration paths
Configuration branding
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
void freeList(List *const handle)
Definition: fs.cpp:269
bool isDirectory(std::string name)
Definition: fs.cpp:239
List * enumerateFiles(std::string dirName)
Definition: fs.cpp:147
std::string pathJoin(std::string str1, const std::string &str2)
StringVect::const_iterator StringVectCIter
Definition: stringvector.h:31
std::vector< std::string > StringVect
Definition: stringvector.h:29
StringVect names
Definition: list.h:40
std::string filename
Definition: wallpaperdata.h:42
static std::vector< WallpaperData > wallpaperData
Definition: wallpaper.cpp:50
static bool wallpaperCompare(const WallpaperData &a, const WallpaperData &b)
Definition: wallpaper.cpp:79
static std::string wallpaperFile
Definition: wallpaper.cpp:54
static void initDefaultWallpaperPaths()
Definition: wallpaper.cpp:57
static std::string wallpaperPath
Definition: wallpaper.cpp:53
static bool haveBackup
Definition: wallpaper.cpp:51