Gradient.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/Gradient.hpp"
30 
31 namespace nom {
32 
34 {
35  Color gradient_color[2];
36  gradient_color[0] = Color::LightGray;
37  gradient_color[1] = Color::Gray;
38 
39  this->initialize (
40  gradient_color,
41  Coords ( 0, 0, 0, 0 ), 0, 0, Gradient::FillDirection::Left
42  );
43 }
44 
46  Color gradient_color[2],
47  const Coords& bounds, int32 x_margin, int32 y_margin,
48  Gradient::FillDirection direction
49  )
50  : gradient {}, coords { bounds },
51  x_margin ( x_margin ), y_margin ( y_margin ), fill_direction ( direction ),
52  enable_dithering ( true )
53 {
54  this->setStartColor ( gradient_color[0] );
55  this->setEndColor ( gradient_color[1] );
56 }
57 /* FIXME
58 {
59  this->initialize (
60  gradient_color,
61  bounds, x_margin, y_margin, direction
62  );
63 }
64 FIXME */
65 
67 
69  Color gradient_color[2],
70  const Coords& bounds,
71  int32 x_margin, int32 y_margin,
72  Gradient::FillDirection direction
73  )
74 {
75  this->setStartColor ( gradient_color[0] );
76  this->setEndColor ( gradient_color[1] );
77  this->setSize ( bounds.width, bounds.height );
78  this->setPosition ( bounds.x, bounds.y );
79  this->setMargins ( x_margin, y_margin );
80  this->setFillDirection ( direction );
81 
82  // Internal option
83  this->enableDithering ( true );
84 }
85 
86 const Coords Gradient::getPosition ( void ) const
87 {
88  return this->coords;
89 }
90 
91 const Coords Gradient::getSize ( void ) const
92 {
93  return this->coords;
94 }
95 
96 bool Gradient::dithering ( void ) const
97 {
98  return this->enable_dithering;
99 }
100 
101 void Gradient::setPosition ( int32 x, int32 y )
102 {
103  this->coords.x = x;
104  this->coords.y = y;
105 }
106 
107 void Gradient::setSize ( int32 width, int32 height )
108 {
109  this->coords.width = width;
110  this->coords.height = height;
111 }
112 
113 Color Gradient::getStartColor ( void ) const
114 {
115  return this->gradient[0];
116 }
117 
118 Color Gradient::getEndColor ( void ) const
119 {
120  return this->gradient[1];
121 }
122 
123 void Gradient::setStartColor ( const Color& starting_color )
124 {
125  this->gradient[0] = starting_color;
126 }
127 
128 void Gradient::setEndColor ( const Color& ending_color )
129 {
130  this->gradient[1] = ending_color;
131 }
132 
133 void Gradient::reverseColors ( void )
134 {
135  std::swap ( this->gradient[0], this->gradient[1] );
136 }
137 
138 Gradient::FillDirection Gradient::getFillDirection ( void ) const
139 {
140  return this->fill_direction;
141 }
142 
143 void Gradient::setFillDirection ( Gradient::FillDirection direction )
144 {
145  this->fill_direction = direction;
146 }
147 
148 void Gradient::setMargins ( int32 x, int32 y )
149 {
150  this->x_margin = x;
151  this->y_margin = y;
152 }
153 
154 void Gradient::enableDithering ( bool toggle )
155 {
156  this->enable_dithering = toggle;
157 }
158 
159 void Gradient::strategyTopDown ( void )
160 {
161  uint32 y_offset = ( this->coords.y + this->coords.height ) - this->y_margin;
162 
163  float currentR = (float) gradient[0].red;
164  float currentG = (float) gradient[0].green;
165  float currentB = (float) gradient[0].blue;
166 
167  float destR = (float) ( gradient[1].red - gradient[0].red ) / ( float ) ( this->coords.height - this->y_margin );
168  float destG = (float) ( gradient[1].green - gradient[0].green ) / ( float ) ( this->coords.height - this->y_margin );
169  float destB = (float) ( gradient[1].blue - gradient[0].blue ) / ( float ) ( this->coords.height - this->y_margin );
170 
171  for ( uint32 rows = this->coords.y + this->y_margin; rows < y_offset; rows++ )
172  {
173  this->rectangles.push_back ( IDrawable::UniquePtr ( new Rectangle ( Coords ( this->coords.x + this->x_margin, rows, this->coords.width - this->x_margin, 1 ), Color ( currentR, currentG, currentB ) ) ) );
174 
175  if ( this->dithering() )
176  {
177  currentR += destR;
178  currentG += destG;
179  currentB += destB;
180  }
181  } // end blit loop
182 }
183 
184 void Gradient::strategyLeftToRight ( void )
185 {
186  uint32 x_offset = ( this->coords.x + this->coords.width ) - this->x_margin;
187 
188  float currentR = (float) gradient[0].red;
189  float currentG = (float) gradient[0].green;
190  float currentB = (float) gradient[0].blue;
191 
192  float destR = (float) ( gradient[1].red - gradient[0].red ) / ( float ) ( this->coords.width - this->x_margin );
193  float destG = (float) ( gradient[1].green - gradient[0].green ) / ( float ) ( this->coords.width - this->x_margin );
194  float destB = (float) ( gradient[1].blue - gradient[0].blue ) / ( float ) ( this->coords.width - this->x_margin );
195 
196  for ( uint32 rows = this->coords.x + this->x_margin; rows < x_offset; rows++ )
197  {
198  this->rectangles.push_back ( IDrawable::UniquePtr ( new Rectangle ( Coords ( rows, this->coords.y + this->y_margin, 1, this->coords.height - this->y_margin ), Color ( currentR, currentG, currentB ) ) ) );
199 
200  if ( this->dithering() )
201  {
202  currentR += destR;
203  currentG += destG;
204  currentB += destB;
205  }
206  } // end blit loop
207 }
208 
209 void Gradient::update ( void )
210 {
211  this->rectangles.clear();
212 
213  if ( this->getFillDirection() == FillDirection::Top )
214  {
215  this->strategyTopDown();
216  }
217  else if ( this->getFillDirection() == FillDirection::Bottom )
218  {
219  this->reverseColors();
220  this->strategyTopDown();
221  }
222  else if ( this->getFillDirection() == FillDirection::Left )
223  {
224  this->strategyLeftToRight();
225  }
226  else if ( this->getFillDirection() == FillDirection::Right )
227  {
228  this->reverseColors();
229  this->strategyLeftToRight();
230  }
231 }
232 
233 void Gradient::draw ( RenderTarget target ) const
234 {
235  for ( auto idx = 0; idx != this->rectangles.size(); ++idx )
236  {
237  this->rectangles[idx]->draw ( target );
238  }
239 }
240 
241 
242 } // namespace nom
void initialize(Color gradient_color[2], const Coords &bounds, int32 x_margin, int32 y_margin, Gradient::FillDirection direction)
Fully initialize this object.
Definition: Gradient.cpp:68
void update(void)
Definition: Gradient.cpp:209
virtual ~Gradient(void)
Destructor; OK to inherit me.
Definition: Gradient.cpp:66
IDrawable::UniqueDrawables rectangles
Definition: Gradient.hpp:120
void draw(RenderTarget target) const
Render the object onto the primary video buffer – your visible screen.
Definition: Gradient.cpp:233
int32 x_margin
x coordinate offset
Definition: Gradient.hpp:135
bool enable_dithering
Toggle automatic dithering of colors.
Definition: Gradient.hpp:144
Gradient(void)
Definition: Gradient.cpp:33
static const Color LightGray
Additional predefined color constants.
Definition: Color.hpp:87
enum Gradient::FillDirection fill_direction
Color fill axis – X or Y increment.
Definition: Gradient.hpp:141
Coords coords
Rendering coordinates (X, Y, width &amp; height)
Definition: Gradient.hpp:132
std::unique_ptr< IDrawable > UniquePtr
std::unique_ptr definition type for your convenience, sir!
Definition: IDrawable.hpp:47
Color gradient[2]
Definition: Gradient.hpp:129
Coordinate values (x, y, z, width, height ) wrapper.
Definition: Coords.hpp:44
int32 y_margin
y coordinate offset
Definition: Gradient.hpp:138
Utility class container for RGBA colors.
Definition: Color.hpp:43