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 "export.hpp"
38#include "nomlib/config.hpp"
39#include "nomlib/math/Size2.hpp"
40#include "nomlib/math/Transformable.hpp"
41#include "nomlib/math/Color4.hpp"
42#include "nomlib/graphics/IDrawable.hpp"
43#include "nomlib/graphics/shapes/Rectangle.hpp"
44#include "nomlib/core/unique_ptr.hpp"
45
46namespace nom {
47
48// Forward declarations
49class Texture;
50
52class NOM_EXPORT Gradient: public Transformable
53
54{
55 public:
56 typedef Gradient self_type;
57 typedef Transformable derived_class;
58
59 typedef self_type* raw_ptr;
60 typedef std::unique_ptr<self_type> unique_ptr;
61 typedef std::shared_ptr<self_type> shared_ptr;
62
67 {
68 Top = 0, // Top down
69 Bottom, // Bottom's up!
70 Left, // Left to right
71 Right // Right to left
72 };
73
80 Gradient( void );
81
86 const Color4iColors& colors,
87 const Point2i& pos,
88 const Size2i& size,
89 const Point2i& margin,
91 );
92
94 virtual ~Gradient( void );
95
98
102 ObjectTypeInfo type( void ) const;
103
111 Texture* texture() const;
112
120 bool valid( void ) const;
121
123 const Color4i& start_color( void ) const;
124
126 const Color4i& end_color( void ) const;
127
128 Gradient::FillDirection fill_direction ( void ) const;
129 bool dithering( void ) const;
130 const Point2i& margins ( void ) const;
131
133 const Color4iColors& colors( void ) const;
134
139 void set_position( const Point2i& coords );
140
145 void set_size( const Size2i& size );
146
149 void set_colors( const Color4iColors& colors );
150
152 void reverse_colors( void );
153
154 void set_fill_direction ( Gradient::FillDirection direction );
155
156 //void set_size ( const Size2i& size );
157 void set_margins ( const Point2i& margin );
158 void set_dithering( bool state );
159
164 void draw( RenderTarget& target ) const;
165
166 private:
168 void update( void );
169
170 void strategy_top_down( void );
171 void strategy_left_right( void );
172
179
181 std::vector<Rectangle> rectangles_;
182
188 std::shared_ptr<Texture> texture_;
189
195 Color4iColors gradient_;
196
198 Point2i margins_;
199
202
205};
206
207} // namespace nom
208
209#endif // include guard defined
210
227
230
virtual ~Gradient(void)
Destructor.
enum Gradient::FillDirection fill_direction_
Color fill axis – X or Y increment.
Definition Gradient.hpp:201
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:188
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:181
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:204
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:195
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:198
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:49
const RenderWindow RenderTarget
Definition IDrawable.hpp:60
Run-time type information (RTTI) container class.
const Size2i & size(void) const
Getter for stored size dimensions.