ManaPlus
imageset.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2017 The ManaPlus Developers
6  *
7  * This file is part of The ManaPlus Client.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include "resources/imageset.h"
24 
25 #include "logger.h"
26 
27 #include "resources/image/image.h"
28 
29 #include "utils/cast.h"
30 #include "utils/dtor.h"
31 
32 #include "debug.h"
33 
34 ImageSet::ImageSet(Image *const img, const int width, const int height,
35  const int margin, const int spacing) :
36  Resource(),
37  mImages(),
38  mWidth(width),
39  mHeight(height),
40  mOffsetX(0),
41  mOffsetY(0)
42 {
43  if (img != nullptr)
44  {
45  const int hAdd = height + spacing;
46  const int wAdd = width + spacing;
47  for (int y = margin; y + height <= img->getHeight() - margin;
48  y += hAdd)
49  {
50  for (int x = margin; x + width <= img->getWidth() - margin;
51  x += wAdd)
52  {
53  Image *const image = img->getSubImage(x, y, width, height);
54  image->mNotCount = true;
55  mImages.push_back(image);
56  }
57  }
58  }
59 }
60 
62 {
64 }
65 
66 Image* ImageSet::get(const size_type i) const
67 {
68  if (i >= mImages.size())
69  {
70  logger->log("Warning: No sprite %d in this image set",
71  CAST_S32(i));
72  return nullptr;
73  }
74  return mImages[i];
75 }
76 
78 {
79  return static_cast<int>(sizeof(ImageSet)) +
81  static_cast<int>(mImages.capacity() * sizeof(Image));
82 }
Image * get(const size_type i) const
Definition: imageset.cpp:66
bool mNotCount
Definition: resource.h:88
int calcMemoryLocal() const
Definition: resource.cpp:75
ImageSet(Image *const img, const int w, const int h, const int margin=0, const int spacing=0)
Definition: imageset.cpp:34
Logger * logger
Definition: logger.cpp:95
#define CAST_S32
Definition: cast.h:29
void delete_all(Container &c)
Definition: dtor.h:55
virtual Image * getSubImage(const int x, const int y, const int width, const int height)
Definition: image.cpp:465
std::vector< Image * >::size_type size_type
Definition: imageset.h:65
int calcMemoryLocal() const
Definition: imageset.cpp:77
Definition: image.h:61
std::vector< Image * > mImages
Definition: imageset.h:90
int getWidth() const A_INLINE
Definition: image.h:116
virtual ~ImageSet()
Definition: imageset.cpp:61
void log(const char *const log_text,...)
Definition: logger.cpp:243
int getHeight() const A_INLINE
Definition: image.h:122