canvas.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2016 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 #pragma once
33 
34 #include "../Render/graphic_context.h"
35 
36 namespace clan
37 {
40 
41  class Canvas_Impl;
42  class GraphicContext;
43  class PrimitivesArray;
44  class Sprite_Impl;
45  class Image;
46  class GlyphCache;
47  class Draw;
48  class RenderBatcher;
49  class Colorf;
50  class Pointf;
51  class Gradient;
52  class LineSegment2f;
53  class LineSegment2;
54  class Trianglef;
55  class Triangle;
56  class DisplayWindow;
57  class DisplayWindowDescription;
58  class Path;
59  class Pen;
60  class Brush;
61 
63  enum MapMode
64  {
68  };
69 
71  class Canvas
72  {
73  public:
75  Canvas();
76 
78  Canvas(Canvas &canvas, FrameBuffer &framebuffer);
79 
81  explicit Canvas(DisplayWindow &window);
82 
83  ~Canvas();
84 
86  Canvas create();
87 
89  bool is_null() const { return !impl; }
90  explicit operator bool() const { return bool(impl); }
91 
93  void throw_if_null() const;
94 
98  GraphicContext& get_gc() const;
99 
101  const Mat4f &get_transform() const;
102 
107 
109  const Mat4f &get_projection() const;
110 
111  operator GraphicContext&() const { return get_gc(); }
112 
114  inline float get_width() const { return get_gc().get_dip_width(); }
115 
117  inline float get_height() const { return get_gc().get_dip_height(); }
118 
120  inline Sizef get_size() const { return get_gc().get_dip_size(); }
121 
123  Rectf get_cliprect() const;
124 
126  PixelBuffer get_pixeldata(const Rect& rect, TextureFormat texture_format = tf_rgba8, bool clamp = true);
127 
129  PixelBuffer get_pixeldata(TextureFormat texture_format = tf_rgba8, bool clamp = true);
130 
133  float get_pixel_ratio() const { return get_gc().get_pixel_ratio(); }
134 
136  void set_rasterizer_state(const RasterizerState &state);
137 
139  void set_blend_state(const BlendState &state, const Colorf &blend_color = StandardColorf::white(), unsigned int sample_mask = 0xffffffff);
140 
142  void set_depth_stencil_state(const DepthStencilState &state, int stencil_ref = 0);
143 
145  void reset_rasterizer_state();
146 
148  void reset_blend_state();
149 
152 
154  void set_cliprect(const Rectf &rect);
155 
157 
160  void push_cliprect(const Rectf &rect);
161 
163  void push_cliprect();
164 
166  void pop_cliprect();
167 
169  void reset_cliprect();
170 
172  void clear(const Colorf &color = StandardColorf::black());
173 
177  void clear_stencil(int value = 0);
178 
182  void clear_depth(float value = 0);
183 
185  void set_program_object(StandardProgram standard_program);
186 
188  void set_map_mode(MapMode mode);
189 
193  void set_viewport(const Rectf &viewport);
194 
196  void set_projection(const Mat4f &matrix);
197 
202  void set_batcher(RenderBatcher *batcher);
203 
205  void set_transform(const Mat4f &matrix);
206 
208  void mult_transform(const Mat4f &matrix);
209 
211  void flush();
212 
214  void draw_point(float x1, float y1, const Colorf &color);
215 
221  void draw_point(const Pointf &point, const Colorf &color);
222 
224  void draw_line(float x1, float y1, float x2, float y2, const Colorf &color = StandardColorf::white());
225 
232  void draw_line(const Pointf &start, const Pointf &end, const Colorf &color = StandardColorf::white());
233 
239  void draw_line(const LineSegment2f &line_segment, const Colorf &color = StandardColorf::white());
240 
242  void draw_lines(const Vec2f *positions, int num_vertices, const Colorf &color = StandardColorf::white());
243 
245  void draw_lines(const Vec2f *line_positions, const Vec2f *texture_positions, int num_vertices, const Texture2D &texture, const Colorf &line_color = StandardColorf::white());
246 
248  void draw_line_strip(const Vec2f *positions, int num_vertices, const Colorf &color = StandardColorf::white());
249 
251  void draw_box(float x1, float y1, float x2, float y2, const Colorf &color = StandardColorf::white());
252 
259  void draw_box(const Pointf &start, const Pointf &end, const Colorf &color = StandardColorf::white());
260 
266  void draw_box(const Rectf &rect, const Colorf &color);
267 
269  void fill_rect(float x1, float y1, float x2, float y2, const Colorf &color = StandardColorf::white());
270 
277  void fill_rect(const Pointf &start, const Pointf &end, const Colorf &color = StandardColorf::white());
278 
284  void fill_rect(const Rectf &rect, const Colorf &color);
285 
294  void fill_rect(float x1, float y1, float x2, float y2, const Gradient &color);
295 
302  void fill_rect(const Pointf &start, const Pointf &end, const Gradient &gradient);
303 
309  void fill_rect(const Rectf &rect, const Gradient &gradient);
310 
312  void fill_circle(float center_x, float center_y, float radius, const Colorf &color = StandardColorf::white());
313 
320  void fill_circle(const Pointf &center, float radius, const Colorf &color = StandardColorf::white());
321 
328  void fill_circle(const Pointf &center, float radius, const Gradient &gradient);
329 
337  void fill_circle(const Pointf &center, const Pointf &centergradient, float radius, const Gradient &gradient);
338 
340  void fill_triangle(const Pointf &a, const Pointf &b, const Pointf &c, const Colorf &color = StandardColorf::white());
341 
343  void fill_triangle(const Trianglef &dest_triangle, const Colorf &color = StandardColorf::white());
344 
346  void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Colorf &color = StandardColorf::white());
347 
349  void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Gradient &gradient);
350 
352  void fill_triangles(const Vec2f *triangle_positions, const Colorf *colors, int num_vertices);
353 
355  void fill_triangles(const std::vector<Vec2f> &triangles, const Colorf *colors);
356 
358  void fill_triangles(const std::vector<Vec2f> &triangles, const Colorf &color = StandardColorf::white());
359 
361  void fill_triangles(const std::vector<Vec2f> &triangles, const Gradient &gradient);
362 
364  void fill_triangles(const Vec2f *positions, const Vec2f *texture_positions, int num_vertices, const Texture2D &texture, const Colorf &color = StandardColorf::white());
365 
367  void fill_triangles(const Vec2f *positions, const Vec2f *texture_positions, int num_vertices, const Texture2D &texture, const Colorf *colors);
368 
370  void fill_triangles(const std::vector<Vec2f> &positions, const std::vector<Vec2f> &texture_positions, const Texture2D &texture, const Colorf &color = StandardColorf::white());
371 
373  void fill_triangles(const std::vector<Vec2f> &positions, const std::vector<Vec2f> &texture_positions, const Texture2D &texture, const std::vector<Colorf> &colors);
374 
376  void fill_triangles(const std::vector<Vec2f> &positions, const Texture2D &texture, const Colorf &color = StandardColorf::white());
377 
379  void fill_triangles(const std::vector<Vec2f> &positions, const Texture2D &texture, const Rect &texture_rect, const Colorf &color = StandardColorf::white());
380 
382  void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Texture2D &texture, const Colorf &color = StandardColorf::white());
383 
385  void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Texture2D &texture, const Rect &texture_rect, const Colorf &color = StandardColorf::white());
386 
388  void fill_triangles(const std::vector<Vec2f> &positions, const Texture2D &texture, const Gradient &gradient);
389 
391  void fill_triangles(const std::vector<Vec2f> &positions, const Texture2D &texture, const Rect &texture_rect, const Gradient &gradient);
392 
394  void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Texture2D &texture, const Gradient &gradient);
395 
397  void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Texture2D &texture, const Rect &texture_rect, const Gradient &gradient);
398 
400  void fill_ellipse(const Pointf &center, float radius_x, float radius_y, const Colorf &color = StandardColorf::white());
401 
403  void fill_ellipse(const Pointf &center, float radius_x, float radius_y, const Gradient &gradient);
404 
406  Pointf grid_fit(const Pointf &pos);
407 
408  private:
409  std::shared_ptr<Canvas_Impl> impl;
410 
411  friend class Sprite_Impl;
412  friend class Image;
413  friend class Font_Impl;
414  friend class Font_DrawSubPixel;
415  friend class Font_DrawFlat;
416  friend class Font_DrawScaled;
417  friend class Path;
418  };
419 
420  // Helper class to save the transform state for exception safety
422  {
423  public:
424  TransformState(Canvas *current_canvas) : canvas(current_canvas), matrix(current_canvas->get_transform()) {}
426  const Mat4f matrix;
427  private:
428  Canvas *canvas;
429  };
430 
431  // Helper class to save the clip rect state for exception safety
433  {
434  public:
435  ClipRectState(Canvas *current_canvas) : canvas(current_canvas), cliprect(current_canvas->get_cliprect()) {}
436  ~ClipRectState() { (Rectf(canvas->get_size()) == cliprect) ? canvas->reset_cliprect() : canvas->set_cliprect(cliprect); }
438  private:
439  Canvas *canvas;
440  };
441 
442  // Helper class to control the cliprect stack state for exception safety
444  {
445  public:
446  ClipRectStack(Canvas *current_canvas) : canvas(current_canvas) {}
447  ~ClipRectStack() { for (; push_count > 0; --push_count) canvas->pop_cliprect(); }
448 
449  void push_cliprect(const Rectf &rect) { push_count++; canvas->push_cliprect(rect); }
450  void push_cliprect() { push_count++; canvas->push_cliprect(); }
451  void pop_cliprect() { if (push_count) { --push_count; canvas->pop_cliprect(); } }
452  private:
453  unsigned int push_count = 0;
454  clan::Canvas *canvas;
455  };
456 
458 }
ClipRectState(Canvas *current_canvas)
Definition: canvas.h:435
Mat4f & get_inverse_transform()
Returns the inverse of the current effective transform matrix.
Definition: clanapp.h:35
~ClipRectStack()
Definition: canvas.h:447
Floating point color description class (for float).
Definition: color.h:798
MapMode
Mapping modes.
Definition: canvas.h:63
Sizef get_size() const
Returns the current size of the context.
Definition: canvas.h:120
Rasterizer state setup.
Definition: rasterizer_state.h:43
void set_map_mode(MapMode mode)
Set the projection mapping mode.
Colorgradient description class.
Definition: gradient.h:40
void reset_cliprect()
Removes the set clipping rectangle and empties the cliprect stack.
Frame-buffer object class.
Definition: frame_buffer.h:71
2D Graphics Canvas
Definition: canvas.h:71
friend class Sprite_Impl
Definition: canvas.h:411
float get_pixel_ratio() const
Pixel data container.
Definition: pixel_buffer.h:67
Blend state setup.
Definition: blend_state.h:43
Triangles - Float.
Definition: triangle_math.h:80
TransformState(Canvas *current_canvas)
Definition: canvas.h:424
Definition: canvas.h:432
void fill_ellipse(const Pointf &center, float radius_x, float radius_y, const Colorf &color=StandardColorf::white())
Draw a filled ellipse.
void push_cliprect(const Rectf &rect)
Push current clipping rectangle to stack.
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.
Canvas()
Constructs a null instance.
void fill_circle(float center_x, float center_y, float radius, const Colorf &color=StandardColorf::white())
Draw a circle.
void flush()
Flushes the render batcher currently active.
Render batching abstraction.
Definition: render_batcher.h:44
void push_cliprect()
Push cliprect.
~TransformState()
Definition: canvas.h:425
void draw_point(float x1, float y1, const Colorf &color)
Draw a point.
Pointf grid_fit(const Pointf &pos)
Snaps the point to the nearest pixel corner.
void set_transform(const Mat4f &matrix)
Sets the transform matrix to a new matrix.
void set_blend_state(const BlendState &state, const Colorf &blend_color=StandardColorf::white(), unsigned int sample_mask=0xffffffff)
Set active blend state.
StandardProgram
Standard Program.
Definition: graphic_context.h:238
void push_cliprect(const Rectf &rect)
Definition: canvas.h:449
2D (left,top,right,bottom) rectangle structure - Integer
Definition: rect.h:488
float get_height() const
Returns the current height of the context.
Definition: canvas.h:117
Definition: path.h:52
void clear(const Colorf &color=StandardColorf::black())
Clears the whole context using the specified color.
2D texture object class.
Definition: texture_2d.h:40
const Mat4f & get_transform() const
Returns the current effective transform matrix.
4D matrix
Definition: mat2.h:50
2D (left,top,right,bottom) rectangle structure - Float
Definition: rect.h:502
void set_cliprect(const Rectf &rect)
Set the current clipping rectangle.
void draw_box(float x1, float y1, float x2, float y2, const Colorf &color=StandardColorf::white())
Draw a box / rectangle.
friend class Font_DrawSubPixel
Definition: canvas.h:414
float get_dip_height() const
Definition: graphic_context.h:349
const Mat4f & get_projection() const
Returns the current effective projection matrix.
void clear_depth(float value=0)
Clear the depth buffer.
friend class Font_Impl
Definition: canvas.h:413
void reset_blend_state()
Set active blend state.
void pop_cliprect()
Pop current clipping rectangle from the stack.
void clear_stencil(int value=0)
Clear the stencil buffer.
Definition: canvas.h:67
Definition: canvas.h:65
TextureFormat
Texture format.
Definition: texture_format.h:38
void draw_line_strip(const Vec2f *positions, int num_vertices, const Colorf &color=StandardColorf::white())
Line Strip.
float get_dip_width() const
Definition: graphic_context.h:345
void reset_rasterizer_state()
Set active rasterizer state.
void set_rasterizer_state(const RasterizerState &state)
Set active rasterizer state.
static Colorf black()
Definition: color.h:1747
void throw_if_null() const
Throw an exception if this object is invalid.
Image class.
Definition: image.h:58
GraphicContext & get_gc() const
Get gc.
Interface to drawing graphics.
Definition: graphic_context.h:256
void fill_rect(float x1, float y1, float x2, float y2, const Colorf &color=StandardColorf::white())
Draw a filled box / rectangle.
void draw_line(float x1, float y1, float x2, float y2, const Colorf &color=StandardColorf::white())
Draw a line.
void fill_triangle(const Pointf &a, const Pointf &b, const Pointf &c, const Colorf &color=StandardColorf::white())
Draw a triangle.
ClipRectStack(Canvas *current_canvas)
Definition: canvas.h:446
void set_batcher(RenderBatcher *batcher)
Specifies which render batcher is to be currently active.
static Colorf white()
Definition: color.h:1884
void pop_cliprect()
Definition: canvas.h:451
void set_viewport(const Rectf &viewport)
void reset_depth_stencil_state()
Set active depth stencil state.
float get_width() const
Returns the current width of the context.
Definition: canvas.h:114
const Mat4f matrix
Definition: canvas.h:426
void set_program_object(StandardProgram standard_program)
Set active program object to the standard program specified.
~ClipRectState()
Definition: canvas.h:436
Sizef get_dip_size() const
Definition: graphic_context.h:353
void push_cliprect()
Definition: canvas.h:450
DepthStencil state setup.
Definition: depth_stencil_state.h:43
void mult_transform(const Mat4f &matrix)
Multiplies the passed matrix onto the transform matrix.
2D (x,y) point structure - Float
Definition: point.h:71
void draw_lines(const Vec2f *positions, int num_vertices, const Colorf &color=StandardColorf::white())
Lines.
void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Colorf &color=StandardColorf::white())
Draw triangles.
friend class Font_DrawFlat
Definition: canvas.h:415
Top-level window class.
Definition: display_window.h:100
void set_projection(const Mat4f &matrix)
Set the projection matrix to be used in user projection map mode.
Definition: texture_format.h:41
float get_pixel_ratio() const
Definition: canvas.h:133
2D line segment - Float
Definition: line_segment.h:187
Definition: canvas.h:66
friend class Font_DrawScaled
Definition: canvas.h:416
void set_depth_stencil_state(const DepthStencilState &state, int stencil_ref=0)
Set active depth stencil state.
Canvas create()
Create a copy of a canvas.
Definition: canvas.h:421
const Rectf cliprect
Definition: canvas.h:437
Rectf get_cliprect() const
Returns the current clipping rectangle.
Definition: canvas.h:443
bool is_null() const
Returns true if this object is invalid.
Definition: canvas.h:89
2D (width,height) size structure - Float
Definition: size.h:183