Sprite.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/Sprite.hpp"
30 
31 namespace nom {
32 
33 Sprite::Sprite ( void ) : state ( 0 ), scale_factor ( 1 )
34 {
35 NOM_LOG_TRACE ( NOM );
36 }
37 
38 Sprite::Sprite ( int32 width, int32 height ) :
39  Transformable { Coords ( 0, 0, width, height ) }, state ( 0 ),
40  scale_factor ( 1 )
41 
42 {
43 NOM_LOG_TRACE ( NOM );
44 }
45 
47 {
48  this->sprite = other.sprite;
49  this->coords = other.coords;
50  this->state = other.state;
51  this->scale_factor = other.scale_factor;
52 
53  return *this;
54 }
55 
57 {
58 NOM_LOG_TRACE ( NOM );
59 }
60 
61 const Point2i Sprite::size ( void ) const
62 {
63  return Point2i ( this->sprite.width(), this->sprite.height() );
64 }
65 
66 const Point2i Sprite::position ( void ) const
67 {
68  return Point2i ( this->coords.x, this->coords.y );
69 }
70 
71 uint32 Sprite::getState ( void ) const
72 {
73  return this->state;
74 }
75 
76 void Sprite::setState ( uint32 state )
77 {
78  this->state = state;
79 }
80 
82  const std::string& filename, const Color& colorkey,
83  bool use_cache
84  )
85 {
86  this->sprite.load ( filename, 0, use_cache );
87 
88  if ( this->sprite.valid() == false )
89  {
90 NOM_LOG_ERR ( NOM, "Could not load sprite image file: " + filename );
91  return false;
92  }
93 
94  this->setSize ( this->sprite.width(), this->sprite.height() );
95 
96  if ( colorkey != Color::null )
97  {
98  this->sprite.set_colorkey ( colorkey );
99  }
100 
101  return true;
102 }
103 
104 void Sprite::update ( void )
105 {
106  this->sprite.set_position ( Point2i ( this->coords.x, this->coords.y ) );
107 }
108 
109 void Sprite::draw ( RenderTarget target ) const
110 {
111 NOM_ASSERT ( this->sprite.valid() );
112 
113  this->sprite.draw ( target.renderer() );
114 }
115 
116 void Sprite::draw ( RenderTarget target, const double degrees ) const
117 {
118  this->sprite.draw ( target.renderer(), degrees );
119 }
120 
121 bool Sprite::resize ( enum ResizeAlgorithm scaling_algorithm )
122 {
123  if ( this->sprite.valid() == false )
124  {
125 NOM_LOG_ERR ( NOM, "Video surface is invalid." );
126  return false;
127  }
128 
129  if ( this->sprite.resize ( scaling_algorithm ) == false )
130  {
131 NOM_LOG_ERR ( NOM, "Failed to resize the video surface." );
132  return false;
133  }
134 
135  this->scale_factor = this->sprite.getResizeScaleFactor ( scaling_algorithm );
136  this->update();
137 
138  return true;
139 }
140 
141 bool Sprite::resize ( const Point2f& scale_factor )
142 {
143  if ( this->sprite.valid() == false )
144  {
145 NOM_LOG_ERR ( NOM, "Video surface is invalid." );
146  return false;
147  }
148 
149  if ( this->sprite.resize ( scale_factor ) == false )
150  {
151 NOM_LOG_ERR ( NOM, "Failed to resize the video surface." );
152  return false;
153  }
154 
155  //this->scale_factor = this->sprite.getResizeScaleFactor ( scaling_algorithm );
156  this->update();
157 
158  return true;
159 }
160 
161 
162 } // namespace nom
int32 width(void) const
Definition: Texture.cpp:212
void set_position(const Point2i &pos)
Definition: Texture.cpp:201
int32 scale_factor
Scale factor applied if the resize method is called.
Definition: Sprite.hpp:102
bool set_colorkey(const Color &colorkey)
Definition: Texture.cpp:870
static const Color null
Convenience object that will always contain a value of -1.
Definition: Color.hpp:74
void draw(SDL_RENDERER::RawPtr target) const
Definition: Texture.cpp:422
virtual void update(void)
Definition: Sprite.cpp:104
bool load(const std::string &filename, uint32 flags, bool use_cache=false)
Definition: Texture.cpp:353
virtual ~Sprite(void)
Destructor.
Definition: Sprite.cpp:56
bool resize(enum ResizeAlgorithm scaling_algorithm)
Rescale the font with a chosen resizing algorithm.
Definition: Sprite.cpp:121
Texture sprite
Object that holds our sprite image.
Definition: Sprite.hpp:96
bool load(const std::string &filename, const Color &colorkey, bool use_cache=false)
Load a new image onto the Sprite.
Definition: Sprite.cpp:81
void draw(RenderTarget target) const
Render the object onto the primary video buffer – your visible screen.
Definition: Sprite.cpp:109
uint32 state
Convenience instance variable (user-defined)
Definition: Sprite.hpp:99
Sprite(void)
Definition: Sprite.cpp:33
Sprite & operator=(const Sprite &other)
Copy assignment operator.
Definition: Sprite.cpp:46
uint32 getState(void) const
Get the object&#39;s state.
Definition: Sprite.cpp:71
SDL_RENDERER::RawPtr renderer(void) const
Get a raw pointer to the SDL_Renderer in use.
Definition: Renderer.cpp:58
int32 height(void) const
Definition: Texture.cpp:225
int32 getResizeScaleFactor(enum ResizeAlgorithm scaling_algorithm)
Definition: Texture.cpp:836
bool valid(void) const
Is this object initialized – not nullptr?
Definition: Texture.cpp:162
Coordinate values (x, y, z, width, height ) wrapper.
Definition: Coords.hpp:44
void setState(uint32 state)
Set a new state.
Definition: Sprite.cpp:76
Utility class container for RGBA colors.
Definition: Color.hpp:43
bool resize(enum ResizeAlgorithm scaling_algorithm)
Definition: Texture.cpp:559
Base class for bitmap objects.
Definition: Sprite.hpp:46