1 /*  $Id: PixmapArray.h,v 1.7 2014/02/04 04:39:31 sarrazip Exp $
2     PixmapArray.h - Object containing an array of Pixmaps.
3 
4     flatzebra - Generic 2D Game Engine library
5     Copyright (C) 1999-2012 Pierre Sarrazin <http://sarrazip.com/>
6 
7     This program is free software; you can redistribute it and/or
8     modify it under the terms of the GNU General Public License
9     as published by the Free Software Foundation; either version 2
10     of the License, or (at your option) any later version.
11 
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16 
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20     02110-1301, USA.
21 */
22 
23 #ifndef _H_PixmapArray
24 #define _H_PixmapArray
25 
26 #include <flatzebra/Couple.h>
27 
28 #include <SDL.h>
29 #include <SDL_image.h>
30 
31 #include <assert.h>
32 #include <vector>
33 
34 
35 namespace flatzebra {
36 
37 
38 class PixmapArray
39 /*  Object containing an array of Pixmaps.
40 */
41 {
42 public:
43 
44     /*  Creates a pixmap array.
45         This object is the owner of the pixmaps, and the destructor will
46         take care of freeing them.
47         The argument is ignored.
48         setArrayElement() will grow the internal vector as needed.
49     */
50     PixmapArray(size_t = 0);
51 
52     /*  Calls freeImages().
53     */
54     ~PixmapArray();
55 
56     /*  Destroys the images and frees the resources taken by the pixmaps.
57     */
58     void freeImages();
59 
60     /*  Returns the pixmap of the image at index 'i' of the
61         arrays given to the constructor of this object.
62         'i' must be lower than the value returned by getNumImages().
63     */
64     SDL_Surface *getImage(size_t i) const;
65     size_t getNumImages() const;
66 
67     /*  'image' must not be null.
68     */
69     void setArrayElement(size_t i, SDL_Surface *image);
70 
71     /*  Sets or gets the size in pixels of the images in the pixmap array.
72         All images in the array are assumed to be of the same size.
73         Neither size.x nor size.y are allowed to be zero.
74     */
75     void setImageSize(Couple size);
76     Couple getImageSize() const;
77 
78 private:
79 
80     std::vector<SDL_Surface *> images;
81     Couple imageSize;  // size in pixels of the images; all assumed same size
82 
83 
84     /*  Forbidden operations:
85     */
86     PixmapArray(const PixmapArray &x);
87     PixmapArray &operator = (const PixmapArray &x);
88 
89 };
90 
91 
92 /*  INLINE METHODS
93 */
94 
95 inline SDL_Surface *
getImage(size_t i)96 PixmapArray::getImage(size_t i) const
97 {
98     assert(i < images.size());
99     return images[i];
100 }
101 
102 
103 inline size_t
getNumImages()104 PixmapArray::getNumImages() const
105 {
106     return images.size();
107 }
108 
109 
110 inline Couple
getImageSize()111 PixmapArray::getImageSize() const
112 {
113     return imageSize;
114 }
115 
116 
117 }  // namespace flatzebra
118 
119 
120 #endif  /* _H_PixmapArray */
121