ManaPlus
Static Public Member Functions
Wallpaper Class Reference

#include <wallpaper.h>

Static Public Member Functions

static void loadWallpapers ()
 
static std::string getWallpaper (const int width, const int height)
 

Detailed Description

Handles organizing and choosing of wallpapers.

Definition at line 34 of file wallpaper.h.

Member Function Documentation

◆ getWallpaper()

std::string Wallpaper::getWallpaper ( const int  width,
const int  height 
)
static

Returns the largest wallpaper for the given resolution, or the default wallpaper if none are found.

Parameters
widththe desired width
heightthe desired height
Returns
the file to use, or empty if no wallpapers are useable

Definition at line 137 of file wallpaper.cpp.

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
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
std::string pathJoin(std::string str1, const std::string &str2)
std::vector< std::string > StringVect
Definition: stringvector.h:29
std::string filename
Definition: wallpaperdata.h:42
static std::vector< WallpaperData > wallpaperData
Definition: wallpaper.cpp:50
static std::string wallpaperFile
Definition: wallpaper.cpp:54
static std::string wallpaperPath
Definition: wallpaper.cpp:53
static bool haveBackup
Definition: wallpaper.cpp:51

References CAST_S32, CAST_U32, WallpaperData::filename, FOR_EACH, haveBackup, WallpaperData::height, pathJoin(), wallpaperData, wallpaperFile, wallpaperPath, and WallpaperData::width.

Referenced by PRAGMA45(), Desktop::setBestFittingWallpaper(), TestLauncher::testFps2(), TestLauncher::testFps3(), and TestLauncher::testRescale().

◆ loadWallpapers()

void Wallpaper::loadWallpapers ( )
static

Reads the folder that contains wallpapers and organizes the wallpapers found by area, width, and height.

Definition at line 88 of file wallpaper.cpp.

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 }
void freeList(List *const handle)
Definition: fs.cpp:269
List * enumerateFiles(std::string dirName)
Definition: fs.cpp:147
StringVect::const_iterator StringVectCIter
Definition: stringvector.h:31
StringVect names
Definition: list.h:40
static bool wallpaperCompare(const WallpaperData &a, const WallpaperData &b)
Definition: wallpaper.cpp:79
static void initDefaultWallpaperPaths()
Definition: wallpaper.cpp:57

References VirtFs::enumerateFiles(), WallpaperData::filename, FOR_EACH, VirtFs::freeList(), haveBackup, WallpaperData::height, initDefaultWallpaperPaths(), VirtFs::List::names, wallpaperCompare(), wallpaperData, wallpaperFile, wallpaperPath, and WallpaperData::width.

Referenced by Desktop::Desktop(), PRAGMA45(), Desktop::reloadWallpaper(), TestLauncher::testFps2(), TestLauncher::testFps3(), and TestLauncher::testRescale().


The documentation for this class was generated from the following files: