SpriteSheet.cpp
1 /******************************************************************************
2 
3  nomlib - C++11 cross-platform game engine
4 
5 Copyright (c) 2013, Jeffrey Carpenter <jeffrey.carp@gmail.com>
6 All rights reserved.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 ******************************************************************************/
29 #include "nomlib/graphics/sprite/SpriteSheet.hpp"
30 
31 namespace nom {
32 
33 const int NOM_SPRITE_SHEET_MAJOR_VERSION = 0;
34 const int NOM_SPRITE_SHEET_MINOR_VERSION = 2;
35 const int NOM_SPRITE_SHEET_PATCH_VERSION = 0;
36 
38  sheet_filename_ ( "\0" ), sheet_sprites ( 0 ),
39  sheet_spacing ( 0 ), sheet_padding ( 0 ),
40  sheet_width ( 0 ), sheet_height ( 0 )
41 {}
42 
44 
45 SpriteSheet::SpriteSheet ( const std::string& filename ):
46  sheet_filename_ ( filename ), sheet_sprites ( 0 ),
47  sheet_spacing ( 0 ), sheet_padding ( 0 ),
48  sheet_width ( 0 ), sheet_height ( 0 )
49 {
50  if ( this->load ( filename ) == false )
51  {
52 NOM_LOG_ERR ( NOM, "Could not load sprite sheet file at: " + filename );
53  }
54 }
55 
57  const std::string& filename,
58  int32 sheet_width, int32 sheet_height,
59  int32 sprite_width, int32 sprite_height,
60  int32 spacing, int32 padding, int32 num_sprites
61  ):
62  sheet_filename_ ( filename ), sheet_sprites ( num_sprites ),
63  sheet_spacing ( spacing ), sheet_padding ( padding ),
64  sheet_width ( sheet_width ), sheet_height ( sheet_height )
65 {
66  // Total number of sprites going from left to right
67  int32 sheet_width_dimensions = ceil ( sheet_width / sprite_width );
68 
69  // Total number of sprites going from top to bottom
70  int32 sheet_height_dimensions = ceil ( sheet_height / sprite_height );
71 
72  // Total number of sprites on the sheet
73  int32 total_frames = sheet_width_dimensions * sheet_height_dimensions;
74 
75  int32 y_offset = 0; // This is used with padding calculation
76 
77  int32 rows = 0; // counter used in calculations (left to right)
78  int32 cols = 0; // counter used in calculations (top to bottom)
79 
80  // Assume whole sheet coordinates extraction if num_sprites argument passed
81  // is zero.
82  if ( num_sprites == 0 )
83  {
84  num_sprites = total_frames;
85  }
86 
87  this->sheet.resize ( num_sprites ); // Make room for storage in our vector
88 
89  // Initializing the first sprite frame
90  this->sheet[0].x = spacing;
91  this->sheet[0].y = padding;
92  this->sheet[0].width = sprite_width;
93  this->sheet[0].height = sprite_height;
94 
95  y_offset = padding;
96 
97  // For while the id variable is less than number of sprites, commence..!
98  for ( int32 id = 0; id < num_sprites; id++ )
99  {
100  // If we have reached the end of the row, go back to the front of the next row
101  if ( rows+1 > ( sheet_width_dimensions ) )
102  {
103  rows = 0;
104  cols += 1;
105 
106  // Apply proper padding offset
107  if ( padding )
108  {
109  y_offset = padding * cols + 1;
110  }
111  else
112  {
113  y_offset = padding;
114  }
115  }
116 
117 #if defined (NOM_DEBUG_SPRITE_SHEET)
118  NOM_DUMP_VAR(cols);
119 #endif
120 
121  // Apply proper spacing calculations
122  if ( spacing )
123  {
124  this->sheet[id].x = ( spacing + rows ) + sprite_width * rows;
125  }
126  else
127  {
128  this->sheet[id].x = sprite_width * rows;
129  }
130 
131  // plus one padding (we count from zero)
132  this->sheet[id].y = sprite_height * ( cols ) + y_offset;
133  this->sheet[id].width = sprite_width;
134  this->sheet[id].height = sprite_height;
135 
136 #if defined (NOM_DEBUG_SPRITE_SHEET)
137  NOM_DUMP_VAR ( id );
138  NOM_DUMP_VAR ( this->sheet[id].x );
139  NOM_DUMP_VAR ( this->sheet[id].y );
140  NOM_DUMP_VAR ( this->sheet[id].width );
141  NOM_DUMP_VAR ( this->sheet[id].height );
142 #endif
143 
144  rows++;
145  // ...check please!
146  // ...The show must go on..
147  } // end for loop
148 
149  this->sheet_filename_ = filename;
150 NOM_ASSERT ( this->sheet.size() == num_sprites );
151  this->sheet_sprites = this->sheet.size();
152  this->sheet_spacing = spacing;
153  this->sheet_padding = padding;
154 /* FIXME
155 NOM_ASSERT ( sprite_width * num_sprites == sheet_width );
156  this->sheet_width = sheet_width;
157 NOM_ASSERT ( sprite_height * num_sprites == sheet_height );
158 FIXME */
159  this->sheet_height = sheet_height;
160 }
161 
162 const Coords& SpriteSheet::dimensions ( int32 index ) const
163 {
164  return this->sheet.at ( index );
165 }
166 
167 int32 SpriteSheet::frames ( void ) const
168 {
169  return this->sheet.size();
170 }
171 
172 const std::string& SpriteSheet::sheet_filename ( void ) const
173 {
174  return this->sheet_filename_;
175 }
176 
177 SpriteSheet::SharedPtr SpriteSheet::clone ( void ) const
178 {
179  return SpriteSheet::SharedPtr ( new SpriteSheet ( *this ) );
180 }
181 
182 bool SpriteSheet::save ( const std::string& filename )
183 {
184  nom::JSON::Value node;
185  nom::JSON::FileWriter writer;
186 
187  std::string sprite_sheet_version = std::to_string ( NOM_SPRITE_SHEET_MAJOR_VERSION ) + "." + std::to_string ( NOM_SPRITE_SHEET_MINOR_VERSION ) + "." + std::to_string ( NOM_SPRITE_SHEET_PATCH_VERSION );
188 
189  if ( this->sheet.empty() ) return false;
190 
191  uint32 id = 0;
192  for ( id = 0; id < this->sheet.size(); ++id )
193  {
194  node.insert ( "id", id, id );
195  node.insert ( "x", this->sheet[id].x, id );
196  node.insert ( "y", this->sheet[id].y, id );
197  node.insert ( "width", this->sheet[id].w, id );
198  node.insert ( "height", this->sheet[id].h, id );
199  }
200 
201  // Push out our file meta-data last
202  node.insert ( "sheet_filename", this->sheet_filename(), id );
203  node.insert ( "sheet_sprites", this->sheet_sprites, id );
204  node.insert ( "sheet_spacing", this->sheet_spacing, id );
205  node.insert ( "sheet_padding", this->sheet_padding, id );
206  node.insert ( "sheet_width", this->sheet_width, id );
207  node.insert ( "sheet_height", this->sheet_height, id );
208  node.insert ( "sheet_version", sprite_sheet_version, id );
209  node.insert ( "sheet_modified", getCurrentTime(), id );
210 
211  if ( writer.save ( filename, node ) == false )
212  {
213 NOM_LOG_ERR ( NOM, "Unable to save file: " + filename );
214  return false;
215  }
216 
217  return true;
218 }
219 
220 bool SpriteSheet::load ( const std::string& filename )
221 {
222  nom::JSON::FileReader parser;
223  nom::JSON::Value root;
224 
225  // Temporary holding buffers to hold data until we are ready to commit
226  Coords buffer;
227  std::vector<Coords> buffer_sheet;
228 
229  if ( parser.load ( filename, root ) == false )
230  {
231 NOM_LOG_ERR ( NOM, "Unable to open JSON file at: " + filename );
232  return false;
233  }
234 
235  // Populate our sheet vector while skipping the last JSON object;
236  // this should always be file mete-data!
237  for ( auto idx = 0; idx < root.size() - 1; ++idx )
238  {
239  buffer.x = root.get_int ( "x", idx );
240  buffer.y = root.get_int ( "y", idx );
241  buffer.w = root.get_int ("width", idx );
242  buffer.h = root.get_int ("height", idx );
243 
244  // Commit contents to our buffer if all goes well
245  buffer_sheet.push_back ( buffer );
246  }
247 
248  // If we have gotten this far, we assume all is well, so let's commit the data
249  // as valid sprite sheet data!
250  this->sheet.clear();
251  this->sheet = buffer_sheet;
252 
253  // Populate our other instance variables with the last JSON object;
254  // this should always be file mete-data!
255  for ( auto idx = root.size() - 1; idx != root.size(); ++idx )
256  {
257  this->sheet_filename_ = root.get_string ( "sheet_filename", idx );
258 
259 NOM_ASSERT ( this->sheet.size() == root.get_int ( "sheet_sprites", idx ) );
260  this->sheet_sprites = root.get_int ( "sheet_sprites", idx );
261 
262  this->sheet_spacing = root.get_int ( "sheet_spacing", idx );
263  this->sheet_padding = root.get_int ( "sheet_padding", idx );
264 /* FIXME
265 NOM_ASSERT ( this->sheet[0].width * this->sheet.size() == root.get_int ( "sheet_width", idx ) );
266 FIXME */
267  this->sheet_width = root.get_int ( "sheet_width", idx );
268 
269 /* FIXME
270 NOM_ASSERT ( this->sheet[0].height * this->sheet.size() == root.get_int ( "sheet_height", idx ) );
271 FIXME */
272  this->sheet_height = root.get_int ( "sheet_height", idx );
273  }
274 
275 #if defined ( NOM_DEBUG_SPRITE_SHEET_JSON_LOAD )
276  this->dump();
277 #endif
278 
279  return true;
280 }
281 
282 void SpriteSheet::dump ( void ) const
283 {
284  // Sheet vector state
285  for ( auto itr = this->sheet.begin(); itr != this->sheet.end(); ++itr )
286  {
287 NOM_DUMP_VAR(itr->x);
288 NOM_DUMP_VAR(itr->y);
289 NOM_DUMP_VAR(itr->width);
290 NOM_DUMP_VAR(itr->height);
291  }
292 
293  // Sheet's meta-data
294 NOM_DUMP_VAR(this->sheet_filename());
295 NOM_DUMP_VAR(this->sheet_sprites);
296 NOM_DUMP_VAR(this->sheet_spacing);
297 NOM_DUMP_VAR(this->sheet_padding);
298 NOM_DUMP_VAR(this->sheet_width);
299 NOM_DUMP_VAR(this->sheet_height);
300 }
301 
302 
303 } // namespace nom
int32 frames(void) const
Obtain the number of frames this object contains.
void insert(const std::string &key, int index)
Definition: Value.cpp:192
Wrapper class for JSON input with JSONCPP.
Definition: FileReader.hpp:46
bool load(const std::string &filename)
int32 sheet_padding
Source padding used is saved with the output (meta-data)
std::string sheet_filename_
Source filename used is saved with the output (meta-data)
Wrapper class for JSON output with JSONCPP.
Definition: FileWriter.hpp:47
Interface class for JSON data types.
Definition: Value.hpp:45
int32 sheet_spacing
Source spacing used is saved with the output (meta-data)
bool load(const std::string &filename, Value &object, bool parse_comments=false)
Definition: FileReader.cpp:37
void dump(void) const
Dump the state of this object instance.
Specialized class container for the creation of sprite sheets via RFC 4627 JSON-compliant file input...
Definition: SpriteSheet.hpp:62
SpriteSheet::SharedPtr clone(void) const
Make a duplicate of this object&#39;s instance.
int32 & h
Reference alias for height instance variable.
Definition: Coords.hpp:106
int32 sheet_width
Source sheet_width used is saved with the output (meta-data)
bool save(const std::string &filename)
std::vector< Coords > sheet
Our sprite sheet values container.
~SpriteSheet(void)
Destructor.
Definition: SpriteSheet.cpp:43
Coordinate values (x, y, z, width, height ) wrapper.
Definition: Coords.hpp:44
const Coords & dimensions(int32 index) const
Get the calculations made for a particular ID number.
int32 sheet_height
Source sheet_height used is saved with the output (meta-data)
int32 & w
Reference alias for width instance variable.
Definition: Coords.hpp:103