canvas.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2015 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 ** Harry Storbacka
28 ** Kenneth Gangstoe
29 ** Mark Page
30 */
31
32
33 #pragma once
34
35 #include "../Render/graphic_context.h"
36
37 namespace clan
38 {
41
42 class Canvas_Impl;
43 class GraphicContext;
44 class PrimitivesArray;
45 class Sprite_Impl;
46 class Image;
47 class GlyphCache;
48 class Draw;
49 class RenderBatcher;
50 class Colorf;
51 class Pointf;
52 class Gradient;
53 class LineSegment2f;
54 class LineSegment2;
55 class Trianglef;
56 class Triangle;
57 class DisplayWindow;
58 class DisplayWindowDescription;
59 class Path;
60 class Pen;
61 class Brush;
62
64 enum MapMode
65 {
66 map_2d_upper_left,
67 map_2d_lower_left,
68 map_user_projection
69 };
70
72 class Canvas
73 {
76
77 public:
79 Canvas();
80
82 Canvas(Canvas &canvas, FrameBuffer &framebuffer);
83
85 explicit Canvas(DisplayWindow &window);
86
87 ~Canvas();
88
92 public:
94 Canvas create();
95
97 bool is_null() const { return !impl; }
98
100 void throw_if_null() const;
101
105 GraphicContext& get_gc() const;
106
108 const Mat4f &get_transform() const;
109
113 Mat4f &get_inverse_transform();
114
116 const Mat4f &get_projection() const;
117
118 operator GraphicContext&() const { return get_gc(); }
119
121 inline float get_width() const { return get_gc().get_dip_width(); }
122
124 inline float get_height() const { return get_gc().get_dip_height(); }
125
127 inline Sizef get_size() const { return get_gc().get_dip_size(); }
128
130 Rectf get_cliprect() const;
131
133 PixelBuffer get_pixeldata(const Rect& rect, TextureFormat texture_format = tf_rgba8, bool clamp = true);
134
136 PixelBuffer get_pixeldata(TextureFormat texture_format = tf_rgba8, bool clamp = true);
137
140 float get_pixel_ratio() const { return get_gc().get_pixel_ratio(); }
141
145 public:
147 void set_rasterizer_state(const RasterizerState &state);
148
150 void set_blend_state(const BlendState &state, const Colorf &blend_color = Colorf::white, unsigned int sample_mask = 0xffffffff);
151
153 void set_depth_stencil_state(const DepthStencilState &state, int stencil_ref = 0);
154
156 void reset_rasterizer_state();
157
159 void reset_blend_state();
160
162 void reset_depth_stencil_state();
163
165 void set_cliprect(const Rectf &rect);
166
168
171 void push_cliprect(const Rectf &rect);
172
174 void push_cliprect();
175
177 void pop_cliprect();
178
180 void reset_cliprect();
181
183 void clear(const Colorf &color = Colorf::black);
184
188 void clear_stencil(int value = 0);
189
193 void clear_depth(float value = 0);
194
196 void set_program_object(StandardProgram standard_program);
197
199 void set_map_mode(MapMode mode);
200
202 void set_projection(const Mat4f &matrix);
203
208 void set_batcher(RenderBatcher *batcher);
209
211 void set_transform(const Mat4f &matrix);
212
214 void mult_transform(const Mat4f &matrix);
215
217 void flush();
218
220 void draw_point(float x1, float y1, const Colorf &color);
221
227 void draw_point(const Pointf &point, const Colorf &color);
228
230 void draw_line(float x1, float y1, float x2, float y2, const Colorf &color = Colorf::white);
231
238 void draw_line(const Pointf &start, const Pointf &end, const Colorf &color = Colorf::white);
239
245 void draw_line(const LineSegment2f &line_segment, const Colorf &color = Colorf::white);
246
248 void draw_lines(const Vec2f *positions, int num_vertices, const Colorf &color = Colorf::white);
249
251 void draw_lines(const Vec2f *line_positions, const Vec2f *texture_positions, int num_vertices, const Texture2D &texture, const Colorf &line_color = Colorf::white);
252
254 void draw_line_strip(const Vec2f *positions, int num_vertices, const Colorf &color = Colorf::white);
255
257 void draw_box(float x1, float y1, float x2, float y2, const Colorf &color = Colorf::white);
258
265 void draw_box(const Pointf &start, const Pointf &end, const Colorf &color = Colorf::white);
266
272 void draw_box(const Rectf &rect, const Colorf &color);
273
275 void fill_rect(float x1, float y1, float x2, float y2, const Colorf &color = Colorf::white);
276
283 void fill_rect(const Pointf &start, const Pointf &end, const Colorf &color = Colorf::white);
284
290 void fill_rect(const Rectf &rect, const Colorf &color);
291
300 void fill_rect(float x1, float y1, float x2, float y2, const Gradient &color);
301
308 void fill_rect(const Pointf &start, const Pointf &end, const Gradient &gradient);
309
315 void fill_rect(const Rectf &rect, const Gradient &gradient);
316
318 void fill_circle(float center_x, float center_y, float radius, const Colorf &color = Colorf::white);
319
326 void fill_circle(const Pointf &center, float radius, const Colorf &color = Colorf::white);
327
334 void fill_circle(const Pointf &center, float radius, const Gradient &gradient);
335
343 void fill_circle(const Pointf &center, const Pointf &centergradient, float radius, const Gradient &gradient);
344
346 void fill_triangle(const Pointf &a, const Pointf &b, const Pointf &c, const Colorf &color = Colorf::white);
347
349 void fill_triangle(const Trianglef &dest_triangle, const Colorf &color = Colorf::white);
350
352 void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Colorf &color = Colorf::white);
353
355 void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Gradient &gradient);
356
358 void fill_triangles(const Vec2f *triangle_positions, const Colorf *colors, int num_vertices);
359
361 void fill_triangles(const std::vector<Vec2f> &triangles, const Colorf *colors);
362
364 void fill_triangles(const std::vector<Vec2f> &triangles, const Colorf &color = Colorf::white);
365
367 void fill_triangles(const std::vector<Vec2f> &triangles, const Gradient &gradient);
368
370 void fill_triangles(const Vec2f *positions, const Vec2f *texture_positions, int num_vertices, const Texture2D &texture, const Colorf &color = Colorf::white);
371
373 void fill_triangles(const Vec2f *positions, const Vec2f *texture_positions, int num_vertices, const Texture2D &texture, const Colorf *colors);
374
376 void fill_triangles(const std::vector<Vec2f> &positions, const std::vector<Vec2f> &texture_positions, const Texture2D &texture, const Colorf &color = Colorf::white);
377
379 void fill_triangles(const std::vector<Vec2f> &positions, const std::vector<Vec2f> &texture_positions, const Texture2D &texture, const std::vector<Colorf> &colors);
380
382 void fill_triangles(const std::vector<Vec2f> &positions, const Texture2D &texture, const Colorf &color = Colorf::white);
383
385 void fill_triangles(const std::vector<Vec2f> &positions, const Texture2D &texture, const Rect &texture_rect, const Colorf &color = Colorf::white);
386
388 void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Texture2D &texture, const Colorf &color = Colorf::white);
389
391 void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Texture2D &texture, const Rect &texture_rect, const Colorf &color = Colorf::white);
392
394 void fill_triangles(const std::vector<Vec2f> &positions, const Texture2D &texture, const Gradient &gradient);
395
397 void fill_triangles(const std::vector<Vec2f> &positions, const Texture2D &texture, const Rect &texture_rect, const Gradient &gradient);
398
400 void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Texture2D &texture, const Gradient &gradient);
401
403 void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Texture2D &texture, const Rect &texture_rect, const Gradient &gradient);
404
406 void fill_ellipse(const Pointf &center, float radius_x, float radius_y, const Colorf &color = Colorf::white);
407
409 void fill_ellipse(const Pointf &center, float radius_x, float radius_y, const Gradient &gradient);
410
412 Pointf grid_fit(const Pointf &pos);
413
417 public:
418
422 private:
423 std::shared_ptr<Canvas_Impl> impl;
424
425 friend class Sprite_Impl;
426 friend class Image;
427 friend class Font_Impl;
428 friend class Font_DrawSubPixel;
429 friend class Font_DrawFlat;
430 friend class Font_DrawScaled;
431 friend class Path;
433 };
434
435 }
436
clan::GraphicContext::get_dip_size
Sizef get_dip_size() const
Definition: graphic_context.h:360
clan::Canvas::fill_circle
void fill_circle(float center_x, float center_y, float radius, const Colorf &color=Colorf::white)
Draw a circle.
clan
Definition: clanapp.h:35
clan::Canvas::draw_lines
void draw_lines(const Vec2f *positions, int num_vertices, const Colorf &color=Colorf::white)
Lines.
clan::Canvas::Font_DrawScaled
friend class Font_DrawScaled
Definition: canvas.h:430
clan::Colorf
Floating point color description class (for float).
Definition: color.h:660
clan::MapMode
MapMode
Mapping modes.
Definition: canvas.h:64
clan::GraphicContext::get_dip_height
float get_dip_height() const
Definition: graphic_context.h:356
clan::RasterizerState
Rasterizer state setup.
Definition: rasterizer_state.h:44
clan::Canvas::pop_cliprect
void pop_cliprect()
Pop current clipping rectangle from the stack.
clan::Canvas::fill_rect
void fill_rect(float x1, float y1, float x2, float y2, const Colorf &color=Colorf::white)
Draw a filled box / rectangle.
clan::Gradient
Colorgradient description class.
Definition: gradient.h:41
clan::Colorf::white
static Colorf white
Definition: color.h:1251
clan::FrameBuffer
Frame-buffer object class.
Definition: frame_buffer.h:72
clan::Canvas::reset_rasterizer_state
void reset_rasterizer_state()
Set active rasterizer state.
clan::Canvas
2D Graphics Canvas
Definition: canvas.h:72
clan::Canvas::clear
void clear(const Colorf &color=Colorf::black)
Clears the whole context using the specified color.
clan::Canvas::draw_line_strip
void draw_line_strip(const Vec2f *positions, int num_vertices, const Colorf &color=Colorf::white)
Line Strip.
clan::PixelBuffer
Pixel data container.
Definition: pixel_buffer.h:68
clan::Canvas::mult_transform
void mult_transform(const Mat4f &matrix)
Multiplies the passed matrix onto the transform matrix.
clan::Canvas::set_map_mode
void set_map_mode(MapMode mode)
Set the projection mapping mode.
clan::BlendState
Blend state setup.
Definition: blend_state.h:44
clan::Trianglef
Triangles - Float.
Definition: triangle_math.h:98
clan::Canvas::set_cliprect
void set_cliprect(const Rectf &rect)
Set the current clipping rectangle.
clan::Canvas::clear_stencil
void clear_stencil(int value=0)
Clear the stencil buffer.
clan::Canvas::get_pixel_ratio
float get_pixel_ratio() const
Definition: canvas.h:140
clan::Canvas::get_inverse_transform
Mat4f & get_inverse_transform()
Returns the inverse of the current effective transform matrix.
clan::Canvas::Font_DrawSubPixel
friend class Font_DrawSubPixel
Definition: canvas.h:428
clan::Canvas::Sprite_Impl
friend class Sprite_Impl
Definition: canvas.h:425
clan::RenderBatcher
Render batching abstraction.
Definition: render_batcher.h:45
clan::Canvas::get_projection
const Mat4f & get_projection() const
Returns the current effective projection matrix.
clan::Canvas::set_depth_stencil_state
void set_depth_stencil_state(const DepthStencilState &state, int stencil_ref=0)
Set active depth stencil state.
clan::Canvas::clear_depth
void clear_depth(float value=0)
Clear the depth buffer.
clan::Canvas::set_projection
void set_projection(const Mat4f &matrix)
Set the projection matrix to be used in user projection map mode.
clan::GraphicContext::get_pixel_ratio
float get_pixel_ratio() const
clan::Canvas::draw_box
void draw_box(float x1, float y1, float x2, float y2, const Colorf &color=Colorf::white)
Draw a box / rectangle.
clan::StandardProgram
StandardProgram
Standard Program.
Definition: graphic_context.h:239
clan::Rect
2D (left,top,right,bottom) rectangle structure - Integer
Definition: rect.h:470
clan::Path
Definition: path.h:52
clan::Canvas::set_blend_state
void set_blend_state(const BlendState &state, const Colorf &blend_color=Colorf::white, unsigned int sample_mask=0xffffffff)
Set active blend state.
clan::Canvas::~Canvas
~Canvas()
clan::Texture2D
2D texture object class.
Definition: texture_2d.h:41
clan::Canvas::draw_point
void draw_point(float x1, float y1, const Colorf &color)
Draw a point.
clan::Rectf
2D (left,top,right,bottom) rectangle structure - Float
Definition: rect.h:484
clan::Canvas::create
Canvas create()
Create a copy of a canvas.
clan::Canvas::get_pixeldata
PixelBuffer get_pixeldata(const Rect &rect, TextureFormat texture_format=tf_rgba8, bool clamp=true)
Return the content of the read buffer into a pixel buffer.
clan::map_user_projection
Definition: canvas.h:68
clan::Key::a
clan::map_2d_upper_left
Definition: canvas.h:66
clan::TextureFormat
TextureFormat
Texture format.
Definition: texture_format.h:40
clan::Canvas::throw_if_null
void throw_if_null() const
Throw an exception if this object is invalid.
clan::Vec2< float >
clan::Canvas::draw_line
void draw_line(float x1, float y1, float x2, float y2, const Colorf &color=Colorf::white)
Draw a line.
clan::Canvas::flush
void flush()
Flushes the render batcher currently active.
clan::Canvas::get_size
Sizef get_size() const
Returns the current size of the context.
Definition: canvas.h:127
clan::Canvas::push_cliprect
void push_cliprect()
Push cliprect.
clan::Image
Image class.
Definition: image.h:59
clan::GraphicContext
Interface to drawing graphics.
Definition: graphic_context.h:257
clan::Canvas::reset_depth_stencil_state
void reset_depth_stencil_state()
Set active depth stencil state.
clan::Canvas::get_width
float get_width() const
Returns the current width of the context.
Definition: canvas.h:121
clan::BrushWrapMode::clamp
clan::Canvas::get_cliprect
Rectf get_cliprect() const
Returns the current clipping rectangle.
clan::TextAlignment::center
clan::Canvas::set_program_object
void set_program_object(StandardProgram standard_program)
Set active program object to the standard program specified.
clan::Canvas::set_batcher
void set_batcher(RenderBatcher *batcher)
Specifies which render batcher is to be currently active.
clan::DepthStencilState
DepthStencil state setup.
Definition: depth_stencil_state.h:44
clan::Mat4
4D matrix
Definition: mat2.h:51
clan::Colorf::black
static Colorf black
Definition: color.h:843
clan::Key::b
clan::Pointf
2D (x,y) point structure - Float
Definition: point.h:72
clan::Canvas::set_transform
void set_transform(const Mat4f &matrix)
Sets the transform matrix to a new matrix.
clan::Key::c
clan::StyleValueType::color
value is an url
clan::DisplayWindow
Top-level window class.
Definition: display_window.h:101
clan::tf_rgba8
Definition: texture_format.h:43
clan::LineSegment2f
2D line segment - Float
Definition: line_segment.h:210
clan::Canvas::Canvas
Canvas()
Constructs a null instance.
clan::GraphicContext::get_dip_width
float get_dip_width() const
Definition: graphic_context.h:352
clan::map_2d_lower_left
Definition: canvas.h:67
clan::Canvas::get_gc
GraphicContext & get_gc() const
Get gc.
clan::Canvas::fill_triangle
void fill_triangle(const Pointf &a, const Pointf &b, const Pointf &c, const Colorf &color=Colorf::white)
Draw a triangle.
clan::Canvas::fill_ellipse
void fill_ellipse(const Pointf &center, float radius_x, float radius_y, const Colorf &color=Colorf::white)
Draw a filled ellipse.
clan::Canvas::is_null
bool is_null() const
Returns true if this object is invalid.
Definition: canvas.h:97
clan::Canvas::fill_triangles
void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Colorf &color=Colorf::white)
Draw triangles.
clan::Canvas::get_height
float get_height() const
Returns the current height of the context.
Definition: canvas.h:124
clan::Canvas::Font_Impl
friend class Font_Impl
Definition: canvas.h:427
clan::Canvas::reset_cliprect
void reset_cliprect()
Removes the set clipping rectangle and empties the cliprect stack.
clan::Canvas::Font_DrawFlat
friend class Font_DrawFlat
Definition: canvas.h:429
clan::Canvas::reset_blend_state
void reset_blend_state()
Set active blend state.
clan::Sizef
2D (width,height) size structure - Float
Definition: size.h:169
clan::Canvas::grid_fit
Pointf grid_fit(const Pointf &pos)
Snaps the point to the nearest pixel corner.
clan::Canvas::set_rasterizer_state
void set_rasterizer_state(const RasterizerState &state)
Set active rasterizer state.
clan::Canvas::get_transform
const Mat4f & get_transform() const
Returns the current effective transform matrix.
clan::Key::end