SpriteBatch.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/SpriteBatch.hpp"
30 
31 namespace nom {
32 
33 SpriteBatch::SpriteBatch ( void ) : sheet_id ( 0 )
34 {
35 NOM_LOG_TRACE ( NOM );
36 }
37 
38 SpriteBatch::SpriteBatch ( const SpriteSheet& sheet ) : sheet_id ( 0 )
39 {
40 NOM_LOG_TRACE ( NOM );
41 
42  Coords dims;
43 
44  this->sprite_sheet = sheet;
45 
46  dims = this->sprite_sheet.dimensions(0);
47 
48  Sprite ( dims.width, dims.height );
49 
50  this->sheet_id = 0;
51 }
52 
53 SpriteBatch::SpriteBatch ( const std::string& filename ) : sheet_id ( 0 )
54 {
55 NOM_LOG_TRACE ( NOM );
56 
57  Coords dims;
58 
59  this->sprite_sheet.load ( filename );
60 
61  dims = this->sprite_sheet.dimensions(0);
62 
63  Sprite ( dims.width, dims.height );
64 
65  this->sheet_id = 0;
66 }
67 
69 {
70  this->sprite = other.sprite;
71  this->coords = other.coords;
72  this->offsets = other.offsets;
73  this->state = other.state;
74  this->sprite_sheet = other.sprite_sheet;
75  this->sheet_id = other.sheet_id;
76  this->scale_factor = other.scale_factor;
77 
78  return *this;
79 }
80 
82 {
83 NOM_LOG_TRACE ( NOM );
84 }
85 
86 int32 SpriteBatch::getSheetID ( void ) const
87 {
88 //NOM_ASSERT ( this->sheet_id != ( this->frames() - 1 ) );
89  return this->sheet_id;
90 }
91 
92 int32 SpriteBatch::frames ( void ) const
93 {
94  return this->sprite_sheet.frames();
95 }
96 
97 const std::string& SpriteBatch::sheet_filename ( void ) const
98 {
99  return this->sprite_sheet.sheet_filename();
100 }
101 
102 void SpriteBatch::setSheetID ( int32 id )
103 {
104 //NOM_ASSERT ( id != ( this->frames() - 1 ) );
105  this->sheet_id = id;
106 }
107 
108 void SpriteBatch::update ( void )
109 {
110  Coords dims = this->sprite_sheet.dimensions ( this->getSheetID() );
111 
112  this->offsets.setPosition (
113  dims.x * this->scale_factor,
114  dims.y * this->scale_factor
115  );
116 
117  this->offsets.setSize (
118  dims.width * this->scale_factor,
119  dims.height * this->scale_factor
120  );
121 
122  this->sprite.set_bounds ( this->offsets );
123  this->sprite.set_position ( Point2i ( this->coords.x, this->coords.y ) );
124 }
125 
126 
127 } // namespace nom
Extended bitmap object rendering – utilizes sprite sheets.
Definition: SpriteBatch.hpp:44
int32 frames(void) const
Obtain the number of frames this object contains.
Coords offsets
Source (input) coordinates – used for sprite sheet positioning.
Definition: SpriteBatch.hpp:82
void set_position(const Point2i &pos)
Definition: Texture.cpp:201
int32 frames(void) const
Obtain the number of frames available.
Definition: SpriteBatch.cpp:92
int32 scale_factor
Scale factor applied if the resize method is called.
Definition: Sprite.hpp:102
SpriteBatch & operator=(const SpriteBatch &other)
Copy assignment operator.
Definition: SpriteBatch.cpp:68
void setSheetID(int32 id)
Set a new frame ID to render.
virtual ~SpriteBatch(void)
Destructor.
Definition: SpriteBatch.cpp:81
bool load(const std::string &filename)
Texture sprite
Object that holds our sprite image.
Definition: Sprite.hpp:96
int32 sheet_id
The sheet&#39;s frame ID presently in use.
Definition: SpriteBatch.hpp:88
uint32 state
Convenience instance variable (user-defined)
Definition: Sprite.hpp:99
Sprite(void)
Definition: Sprite.cpp:33
Specialized class container for the creation of sprite sheets via RFC 4627 JSON-compliant file input...
Definition: SpriteSheet.hpp:62
int32 getSheetID(void) const
Get the object&#39;s current sheet_id.
Definition: SpriteBatch.cpp:86
SpriteSheet sprite_sheet
Our attached sprite sheet object.
Definition: SpriteBatch.hpp:85
Coordinate values (x, y, z, width, height ) wrapper.
Definition: Coords.hpp:44
void update(void)
const Coords & dimensions(int32 index) const
Get the calculations made for a particular ID number.