SpriteSheet.hpp
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 #ifndef NOMLIB_SPRITE_SHEET_HPP
30 #define NOMLIB_SPRITE_SHEET_HPP
31 
32 #include <iostream>
33 #include <string>
34 #include <fstream>
35 #include <vector>
36 #include <cmath>
37 #include <memory>
38 
39 #include "json.h" // jsoncpp library
40 
41 #include "nomlib/config.hpp"
42 #include "nomlib/version.hpp"
43 #include "nomlib/math/Coords.hpp"
44 #include "nomlib/json/FileReader.hpp"
45 #include "nomlib/json/FileWriter.hpp"
46 
47 namespace nom {
48 
49 extern const int NOM_SPRITE_SHEET_MAJOR_VERSION;
50 extern const int NOM_SPRITE_SHEET_MINOR_VERSION;
51 extern const int NOM_SPRITE_SHEET_PATCH_VERSION;
52 
53 //#define NOM_DEBUG_SPRITE_SHEET
54 
56 #define NOM_DEBUG_SPRITE_SHEET_JSON_SAVE
57 
58 //#define NOM_DEBUG_SPRITE_SHEET_JSON_LOAD
59 
63 {
64  public:
65  typedef std::shared_ptr<SpriteSheet> SharedPtr;
66 
69  SpriteSheet ( void );
70 
72  ~SpriteSheet ( void );
73 
75  SpriteSheet ( const std::string& filename );
76 
88  SpriteSheet (
89  const std::string& filename,
90  int32 sheet_width, int32 sheet_height,
91  int32 sprite_width, int32 sprite_height,
92  int32 spacing, int32 padding, int32 num_sprites
93  );
94 
96  SpriteSheet::SharedPtr clone ( void ) const;
97 
99  const Coords& dimensions ( int32 index ) const;
100 
102  int32 frames ( void ) const;
103 
104  const std::string& sheet_filename ( void ) const;
105 
108  bool save ( const std::string& filename );
109 
112  bool load ( const std::string& filename );
113 
115  void dump ( void ) const;
116 
117  private:
119  std::vector<Coords> sheet;
120 
122  std::string sheet_filename_;
123 
127 
130 
133 
135  int32 sheet_width;
136 
139 };
140 
141 
142 } // namespace nom
143 
144 #endif // include guard defined
145 
int32 frames(void) const
Obtain the number of frames this object contains.
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)
int32 sheet_spacing
Source spacing used is saved with the output (meta-data)
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 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)