29 #include "nomlib/graphics/sprite/SpriteSheet.hpp"
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;
38 sheet_filename_ (
"\0" ), sheet_sprites ( 0 ),
39 sheet_spacing ( 0 ), sheet_padding ( 0 ),
40 sheet_width ( 0 ), sheet_height ( 0 )
46 sheet_filename_ ( filename ), sheet_sprites ( 0 ),
47 sheet_spacing ( 0 ), sheet_padding ( 0 ),
48 sheet_width ( 0 ), sheet_height ( 0 )
50 if ( this->
load ( filename ) ==
false )
52 NOM_LOG_ERR ( NOM,
"Could not load sprite sheet file at: " + filename );
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
62 sheet_filename_ ( filename ), sheet_sprites ( num_sprites ),
63 sheet_spacing ( spacing ), sheet_padding ( padding ),
64 sheet_width ( sheet_width ), sheet_height ( sheet_height )
67 int32 sheet_width_dimensions = ceil ( sheet_width / sprite_width );
70 int32 sheet_height_dimensions = ceil ( sheet_height / sprite_height );
73 int32 total_frames = sheet_width_dimensions * sheet_height_dimensions;
82 if ( num_sprites == 0 )
84 num_sprites = total_frames;
87 this->
sheet.resize ( num_sprites );
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;
98 for ( int32
id = 0;
id < num_sprites;
id++ )
101 if ( rows+1 > ( sheet_width_dimensions ) )
109 y_offset = padding * cols + 1;
117 #if defined (NOM_DEBUG_SPRITE_SHEET)
124 this->
sheet[id].x = ( spacing + rows ) + sprite_width * rows;
128 this->
sheet[id].x = sprite_width * rows;
132 this->
sheet[id].y = sprite_height * ( cols ) + y_offset;
133 this->
sheet[id].width = sprite_width;
134 this->
sheet[id].height = sprite_height;
136 #if defined (NOM_DEBUG_SPRITE_SHEET)
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 );
150 NOM_ASSERT ( this->
sheet.size() == num_sprites );
164 return this->
sheet.at ( index );
169 return this->
sheet.size();
172 const std::string& SpriteSheet::sheet_filename (
void )
const
179 return SpriteSheet::SharedPtr (
new SpriteSheet ( *
this ) );
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 );
189 if ( this->
sheet.empty() )
return false;
192 for (
id = 0;
id < this->
sheet.size(); ++id )
194 node.
insert (
"id",
id,
id );
202 node.
insert (
"sheet_filename", this->sheet_filename(),
id );
208 node.
insert (
"sheet_version", sprite_sheet_version,
id );
209 node.
insert (
"sheet_modified", getCurrentTime(),
id );
211 if ( writer.save ( filename, node ) == false )
213 NOM_LOG_ERR ( NOM,
"Unable to save file: " + filename );
227 std::vector<Coords> buffer_sheet;
229 if ( parser.
load ( filename, root ) == false )
231 NOM_LOG_ERR ( NOM,
"Unable to open JSON file at: " + filename );
237 for (
auto idx = 0; idx < root.size() - 1; ++idx )
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 );
245 buffer_sheet.push_back ( buffer );
251 this->
sheet = buffer_sheet;
255 for (
auto idx = root.size() - 1; idx != root.size(); ++idx )
259 NOM_ASSERT ( this->
sheet.size() == root.get_int (
"sheet_sprites", idx ) );
267 this->
sheet_width = root.get_int (
"sheet_width", idx );
272 this->
sheet_height = root.get_int (
"sheet_height", idx );
275 #if defined ( NOM_DEBUG_SPRITE_SHEET_JSON_LOAD )
285 for (
auto itr = this->
sheet.begin(); itr != this->
sheet.end(); ++itr )
287 NOM_DUMP_VAR(itr->x);
288 NOM_DUMP_VAR(itr->y);
289 NOM_DUMP_VAR(itr->width);
290 NOM_DUMP_VAR(itr->height);
294 NOM_DUMP_VAR(this->sheet_filename());
int32 frames(void) const
Obtain the number of frames this object contains.
void insert(const std::string &key, int index)
Wrapper class for JSON input with JSONCPP.
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.
Interface class for JSON data types.
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)
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...
SpriteSheet::SharedPtr clone(void) const
Make a duplicate of this object's instance.
int32 & h
Reference alias for height instance variable.
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.
Coordinate values (x, y, z, width, height ) wrapper.
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.