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 {
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 
88 
92 public:
95 
97  bool is_null() const { return !impl; }
98 
100  void throw_if_null() const;
101 
106 
108  const Mat4f &get_transform() const;
109 
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 
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:
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 
157 
160 
163 
165  void set_cliprect(const Rectf &rect);
166 
168 
171  void push_cliprect(const Rectf &rect);
172 
175 
177  void pop_cliprect();
178 
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 
MapMode
Mapping modes.
Definition: canvas.h:65
C clamp(A val, B minval, C maxval)
Definition: cl_math.h:98
friend class Font_Impl
Definition: canvas.h:427
Sizef get_size() const
Returns the current size of the context.
Definition: canvas.h:127
friend class Font_DrawFlat
Definition: canvas.h:429
void fill_rect(float x1, float y1, float x2, float y2, const Colorf &color=Colorf::white)
Draw a filled box / rectangle.
void reset_blend_state()
Set active blend state.
void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Texture2D &texture, const Colorf &color=Colorf::white)
Draw triangles.
Pointf grid_fit(const Pointf &pos)
Snaps the point to the nearest pixel corner.
void fill_ellipse(const Pointf &center, float radius_x, float radius_y, const Gradient &gradient)
Draw a gradient filled ellipse.
void reset_rasterizer_state()
Set active rasterizer state.
2D line segment - Float
Definition: line_segment.h:211
void throw_if_null() const
Throw an exception if this object is invalid.
void fill_triangles(const std::vector< Vec2f > &positions, const Texture2D &texture, const Rect &texture_rect, const Gradient &gradient)
Draw triangles.
friend class Font_DrawScaled
Definition: canvas.h:430
void fill_rect(float x1, float y1, float x2, float y2, const Gradient &color)
Gradient fill.
float get_width() const
Returns the current width of the context.
Definition: canvas.h:121
void pop_cliprect()
Pop current clipping rectangle from the stack.
void mult_transform(const Mat4f &matrix)
Multiplies the passed matrix onto the transform matrix.
void set_cliprect(const Rectf &rect)
Set the current clipping rectangle.
2D (left,top,right,bottom) rectangle structure - Integer
Definition: rect.h:471
void clear_stencil(int value=0)
Clear the stencil buffer.
void fill_triangle(const Trianglef &dest_triangle, const Colorf &color=Colorf::white)
Draw a triangle.
@ tf_rgba8
Definition: texture_format.h:43
void clear(const Colorf &color=Colorf::black)
Clears the whole context using the specified color.
void draw_line_strip(const Vec2f *positions, int num_vertices, const Colorf &color=Colorf::white)
Line Strip.
float get_dip_width() const
Definition: graphic_context.h:352
float get_pixel_ratio() const
Definition: canvas.h:140
friend class Sprite_Impl
Definition: canvas.h:425
void set_map_mode(MapMode mode)
Set the projection mapping mode.
void fill_circle(const Pointf &center, float radius, const Gradient &gradient)
Gradient circle.
Sizef get_dip_size() const
Definition: graphic_context.h:360
void draw_lines(const Vec2f *line_positions, const Vec2f *texture_positions, int num_vertices, const Texture2D &texture, const Colorf &line_color=Colorf::white)
Lines.
void set_depth_stencil_state(const DepthStencilState &state, int stencil_ref=0)
Set active depth stencil state.
void clear_depth(float value=0)
Clear the depth buffer.
2D (x,y) point structure - Float
Definition: point.h:73
void push_cliprect(const Rectf &rect)
Push current clipping rectangle to stack.
2D (left,top,right,bottom) rectangle structure - Float
Definition: rect.h:485
Render batching abstraction.
Definition: render_batcher.h:46
Floating point color description class (for float).
Definition: color.h:661
void set_projection(const Mat4f &matrix)
Set the projection matrix to be used in user projection map mode.
Triangles - Float.
Definition: triangle_math.h:99
friend class Font_DrawSubPixel
Definition: canvas.h:428
Mat4f & get_inverse_transform()
Returns the inverse of the current effective transform matrix.
2D texture object class.
Definition: texture_2d.h:42
void fill_triangles(const std::vector< Vec2f > &positions, const Texture2D &texture, const Rect &texture_rect, const Colorf &color=Colorf::white)
Draw triangles.
void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Texture2D &texture, const Rect &texture_rect, const Colorf &color=Colorf::white)
Draw triangles.
Rectf get_cliprect() const
Returns the current clipping rectangle.
2D Graphics Canvas
Definition: canvas.h:73
void set_blend_state(const BlendState &state, const Colorf &blend_color=Colorf::white, unsigned int sample_mask=0xffffffff)
Set active blend state.
void fill_rect(const Pointf &start, const Pointf &end, const Colorf &color=Colorf::white)
Fill.
void draw_box(const Rectf &rect, const Colorf &color)
Box.
void draw_point(float x1, float y1, const Colorf &color)
Draw a point.
@ map_2d_upper_left
Definition: canvas.h:66
void draw_box(float x1, float y1, float x2, float y2, const Colorf &color=Colorf::white)
Draw a box / rectangle.
StandardProgram
Standard Program.
Definition: graphic_context.h:240
void fill_triangles(const Vec2f *triangle_positions, const Colorf *colors, int num_vertices)
Draw triangles.
void fill_triangles(const std::vector< Vec2f > &positions, const std::vector< Vec2f > &texture_positions, const Texture2D &texture, const Colorf &color=Colorf::white)
Draw triangles.
void fill_triangles(const std::vector< Vec2f > &triangles, const Colorf *colors)
Draw triangles.
PixelBuffer get_pixeldata(TextureFormat texture_format=tf_rgba8, bool clamp=true)
Return the content of the read buffer into a pixel buffer.
Definition: path.h:53
void fill_rect(const Rectf &rect, const Colorf &color)
Fill.
void fill_triangles(const Vec2f *positions, const Vec2f *texture_positions, int num_vertices, const Texture2D &texture, const Colorf &color=Colorf::white)
Draw triangles.
2D vector
Definition: line.h:48
void fill_rect(const Pointf &start, const Pointf &end, const Gradient &gradient)
Gradient fill.
Interface to drawing graphics.
Definition: graphic_context.h:258
float get_pixel_ratio() const
void fill_triangles(const std::vector< Vec2f > &positions, const Texture2D &texture, const Gradient &gradient)
Draw triangles.
Rasterizer state setup.
Definition: rasterizer_state.h:45
void fill_rect(const Rectf &rect, const Gradient &gradient)
Gradient fill.
void fill_triangles(const std::vector< Vec2f > &positions, const std::vector< Vec2f > &texture_positions, const Texture2D &texture, const std::vector< Colorf > &colors)
Draw triangles.
void flush()
Flushes the render batcher currently active.
void draw_line(const LineSegment2f &line_segment, const Colorf &color=Colorf::white)
Line.
4D matrix
Definition: mat2.h:51
Canvas create()
Create a copy of a canvas.
void draw_point(const Pointf &point, const Colorf &color)
Point.
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.
void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Gradient &gradient)
Draw triangles.
TextureFormat
Texture format.
Definition: texture_format.h:41
void reset_depth_stencil_state()
Set active depth stencil state.
void fill_triangles(const std::vector< Vec2f > &triangles, const Gradient &gradient)
Draw triangles.
void draw_line(float x1, float y1, float x2, float y2, const Colorf &color=Colorf::white)
Draw a line.
void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Texture2D &texture, const Gradient &gradient)
Draw triangles.
@ map_user_projection
Definition: canvas.h:68
Definition: clanapp.h:36
void fill_triangles(const Vec2f *positions, const Vec2f *texture_positions, int num_vertices, const Texture2D &texture, const Colorf *colors)
Draw triangles.
Frame-buffer object class.
Definition: frame_buffer.h:73
const Mat4f & get_transform() const
Returns the current effective transform matrix.
void push_cliprect()
Push cliprect.
Canvas(DisplayWindow &window)
Constructs a Canvas.
Image class.
Definition: image.h:60
Pixel data container.
Definition: pixel_buffer.h:69
void draw_line(const Pointf &start, const Pointf &end, const Colorf &color=Colorf::white)
Line.
static Colorf black
Definition: color.h:843
float get_dip_height() const
Definition: graphic_context.h:356
void draw_box(const Pointf &start, const Pointf &end, const Colorf &color=Colorf::white)
Box.
void fill_circle(const Pointf &center, const Pointf &centergradient, float radius, const Gradient &gradient)
Gradient circle.
@ map_2d_lower_left
Definition: canvas.h:67
bool is_null() const
Returns true if this object is invalid.
Definition: canvas.h:97
float get_height() const
Returns the current height of the context.
Definition: canvas.h:124
void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Texture2D &texture, const Rect &texture_rect, const Gradient &gradient)
Draw triangles.
void set_program_object(StandardProgram standard_program)
Set active program object to the standard program specified.
DepthStencil state setup.
Definition: depth_stencil_state.h:45
Blend state setup.
Definition: blend_state.h:45
void fill_triangles(const std::vector< Vec2f > &triangles, const Colorf &color=Colorf::white)
Draw triangles.
void set_batcher(RenderBatcher *batcher)
Specifies which render batcher is to be currently active.
GraphicContext & get_gc() const
Get gc.
void fill_triangle(const Pointf &a, const Pointf &b, const Pointf &c, const Colorf &color=Colorf::white)
Draw a triangle.
void fill_ellipse(const Pointf &center, float radius_x, float radius_y, const Colorf &color=Colorf::white)
Draw a filled ellipse.
2D (width,height) size structure - Float
Definition: size.h:170
void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Colorf &color=Colorf::white)
Draw triangles.
void set_transform(const Mat4f &matrix)
Sets the transform matrix to a new matrix.
void fill_circle(const Pointf &center, float radius, const Colorf &color=Colorf::white)
Circle.
void reset_cliprect()
Removes the set clipping rectangle and empties the cliprect stack.
Top-level window class.
Definition: display_window.h:102
void fill_triangles(const std::vector< Vec2f > &positions, const Texture2D &texture, const Colorf &color=Colorf::white)
Draw triangles.
Canvas(Canvas &canvas, FrameBuffer &framebuffer)
Constructs a Canvas based on a framebuffer. (based on the copy of the canvas)
void set_rasterizer_state(const RasterizerState &state)
Set active rasterizer state.
const Mat4f & get_projection() const
Returns the current effective projection matrix.
Canvas()
Constructs a null instance.
Colorgradient description class.
Definition: gradient.h:42
void fill_circle(float center_x, float center_y, float radius, const Colorf &color=Colorf::white)
Draw a circle.
static Colorf white
Definition: color.h:1251
void draw_lines(const Vec2f *positions, int num_vertices, const Colorf &color=Colorf::white)
Lines.