nomlib
Loading...
Searching...
No Matches
Gradient.hpp
1/******************************************************************************
2
3 nomlib - C++11 cross-platform game engine
4
5Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
132. 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
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON 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
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28******************************************************************************/
29#ifndef NOMLIB_SDL2_GRADIENT_HPP
30#define NOMLIB_SDL2_GRADIENT_HPP
31
32#include <iostream>
33#include <string>
34#include <vector>
35#include <memory>
36
37#include "nomlib/config.hpp"
38#include "nomlib/math/Size2.hpp"
39#include "nomlib/math/Transformable.hpp"
40#include "nomlib/math/Color4.hpp"
41#include "nomlib/graphics/IDrawable.hpp"
42#include "nomlib/graphics/shapes/Rectangle.hpp"
43#include "nomlib/core/unique_ptr.hpp"
44
45namespace nom {
46
47// Forward declarations
48class Texture;
49
51class Gradient: public Transformable
52
53{
54 public:
55 typedef Gradient self_type;
56 typedef Transformable derived_class;
57
58 typedef self_type* raw_ptr;
59 typedef std::unique_ptr<self_type> unique_ptr;
60 typedef std::shared_ptr<self_type> shared_ptr;
61
66 {
67 Top = 0, // Top down
68 Bottom, // Bottom's up!
69 Left, // Left to right
70 Right // Right to left
71 };
72
79 Gradient( void );
80
85 const Color4iColors& colors,
86 const Point2i& pos,
87 const Size2i& size,
88 const Point2i& margin,
90 );
91
93 virtual ~Gradient( void );
94
97
101 ObjectTypeInfo type( void ) const;
102
110 Texture* texture() const;
111
119 bool valid( void ) const;
120
122 const Color4i& start_color( void ) const;
123
125 const Color4i& end_color( void ) const;
126
127 Gradient::FillDirection fill_direction ( void ) const;
128 bool dithering( void ) const;
129 const Point2i& margins ( void ) const;
130
132 const Color4iColors& colors( void ) const;
133
138 void set_position( const Point2i& coords );
139
144 void set_size( const Size2i& size );
145
148 void set_colors( const Color4iColors& colors );
149
151 void reverse_colors( void );
152
153 void set_fill_direction ( Gradient::FillDirection direction );
154
155 //void set_size ( const Size2i& size );
156 void set_margins ( const Point2i& margin );
157 void set_dithering( bool state );
158
163 void draw( RenderTarget& target ) const;
164
165 private:
167 void update( void );
168
169 void strategy_top_down( void );
170 void strategy_left_right( void );
171
178
180 std::vector<Rectangle> rectangles_;
181
187 std::shared_ptr<Texture> texture_;
188
194 Color4iColors gradient_;
195
197 Point2i margins_;
198
201
204};
205
206} // namespace nom
207
208#endif // include guard defined
209
226
229
virtual ~Gradient(void)
Destructor.
enum Gradient::FillDirection fill_direction_
Color fill axis – X or Y increment.
Definition Gradient.hpp:200
ObjectTypeInfo type(void) const
Re-implements the IObject::type method.
void draw(RenderTarget &target) const
Implements the IDrawable::draw method.
void set_size(const Size2i &size)
Re-implements the Transformable::set_size method.
std::shared_ptr< Texture > texture_
Render cache for the gradient.
Definition Gradient.hpp:187
Gradient(void)
Default constructor; initialize the object to an invalid state.
const Color4i & end_color(void) const
Get the last (ending) color used in the gradient fill.
std::vector< Rectangle > rectangles_
Rectangles cache; this is used to build the texture cache.
Definition Gradient.hpp:180
const Color4iColors & colors(void) const
Get the gradient colors container.
Texture * texture() const
Get the underlying texture of the rendered gradient.
void set_position(const Point2i &coords)
Re-implements the Transformable::set_position method.
void set_colors(const Color4iColors &colors)
Set the gradient colors used in the rendering of the gradient fill.
Gradient(const Color4iColors &colors, const Point2i &pos, const Size2i &size, const Point2i &margin, Gradient::FillDirection direction)
Construct a complete object state.
bool valid(void) const
Query the validity of the object.
bool dithering_
Dithering (of gradient colors) state.
Definition Gradient.hpp:203
bool update_cache()
Synchronize the texture cache with the generated rectangles.
Color4iColors gradient_
The starting & ending colors used in the gradient fill.
Definition Gradient.hpp:194
const Color4i & start_color(void) const
Get the first (starting) color used in the gradient fill.
void update(void)
Implements the IDrawable::update method.
void reverse_colors(void)
Swap the first and last color used in the gradient fill.
Point2i margins_
Additional offset coordinates, in pixels.
Definition Gradient.hpp:197
IDrawable::raw_ptr clone(void) const
Implements the required IDrawable::clone method.
self_type * raw_ptr
A type definition for a C pointer – raw pointer – to an IDrawable object.
Definition IDrawable.hpp:48
const RenderWindow RenderTarget
Definition IDrawable.hpp:59
Run-time type information (RTTI) container class.
const Size2i & size(void) const
Getter for stored size dimensions.