GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
Line | Branch | Exec | Source |
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 |
|||
31 |
#include "resources/wallpaperdata.h" |
||
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 |
1 |
static STD_VECTOR<WallpaperData> wallpaperData; |
|
51 |
static bool haveBackup; // Is the backup (no size given) version available? |
||
52 |
|||
53 |
1 |
static std::string wallpaperPath; |
|
54 |
1 |
static std::string wallpaperFile; |
|
55 |
|||
56 |
// Search for the wallpaper path values sequentially.. |
||
57 |
1 |
static void initDefaultWallpaperPaths() |
|
58 |
{ |
||
59 |
// Init the path |
||
60 |
✓✗ | 5 |
wallpaperPath = branding.getStringValue("wallpapersPath"); |
61 |
|||
62 |
✗✓✗✗ ✗✗✗✓ ✓✗✗✗ |
2 |
if (wallpaperPath.empty() || !VirtFs::isDirectory(wallpaperPath)) |
63 |
✓✗✓✗ |
8 |
wallpaperPath = paths.getValue("wallpapers", ""); |
64 |
|||
65 |
✗✓✗✗ ✗✗✗✓ ✓✗✗✗ |
2 |
if (wallpaperPath.empty() || !VirtFs::isDirectory(wallpaperPath)) |
66 |
wallpaperPath = "graphics/images/"; |
||
67 |
|||
68 |
// Init the default file |
||
69 |
✓✗ | 5 |
wallpaperFile = branding.getStringValue("wallpaperFile"); |
70 |
|||
71 |
✗✓✗✗ ✗✗✗✓ ✓✗✗✗ |
2 |
if (!wallpaperFile.empty() && !VirtFs::isDirectory(wallpaperFile)) |
72 |
return; |
||
73 |
✓✗✓✗ |
8 |
wallpaperFile = paths.getValue("wallpaperFile", ""); |
74 |
|||
75 |
✗✓✗✗ ✗✗✗✓ ✓✗✗✗ |
2 |
if (wallpaperFile.empty() || VirtFs::isDirectory(wallpaperFile)) |
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 |
|||
88 |
1 |
void Wallpaper::loadWallpapers() |
|
89 |
{ |
||
90 |
1 |
wallpaperData.clear(); |
|
91 |
1 |
initDefaultWallpaperPaths(); |
|
92 |
✓✗ | 2 |
VirtFs::List *const imgs = VirtFs::enumerateFiles(wallpaperPath); |
93 |
|||
94 |
✓✓ | 12 |
FOR_EACH (StringVectCIter, i, imgs->names) |
95 |
{ |
||
96 |
// First, get the base filename of the image: |
||
97 |
18 |
std::string filename = *i; |
|
98 |
12 |
const std::string name = filename; |
|
99 |
// If the backup file is found, we tell it. |
||
100 |
✓✓ | 6 |
if (filename.find(wallpaperFile) != std::string::npos) |
101 |
1 |
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 |
6 |
size_t separator = filename.rfind('_'); |
|
107 |
✓✗ | 12 |
filename = filename.substr(0, separator); |
108 |
|||
109 |
// Check that the base filename doesn't have any '%' markers. |
||
110 |
6 |
separator = filename.find('%'); |
|
111 |
✓✗ | 6 |
if (separator == std::string::npos) |
112 |
{ |
||
113 |
// Then, append the width and height search mask. |
||
114 |
✓✗ | 6 |
filename.append("_%10dx%10d.png"); |
115 |
|||
116 |
int width; |
||
117 |
int height; |
||
118 |
✗✓ | 12 |
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 |
1 |
VirtFs::freeList(imgs); |
|
134 |
3 |
std::sort(wallpaperData.begin(), wallpaperData.end(), &wallpaperCompare); |
|
135 |
1 |
} |
|
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) |
||
163 |
return pathJoin(wallpaperPath, wallpaperFile); |
||
164 |
|||
165 |
// Return an empty string if everything else failed |
||
166 |
return std::string(); |
||
167 |
✓✗✓✗ |
3 |
} |
Generated by: GCOVR (Version 3.3) |