canvas.h
1/*
2** ClanLib SDK
3** Copyright (c) 1997-2020 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
36namespace 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 class MapMode
64 {
68 };
69
71 class Canvas
72 {
73 public:
76
78 Canvas(Canvas &canvas, FrameBuffer &framebuffer);
79
81 explicit Canvas(DisplayWindow &window);
82
84
87
89 bool is_null() const { return !impl; }
90 explicit operator bool() const { return bool(impl); }
91
93 void throw_if_null() const;
94
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
124
126 PixelBuffer get_pixeldata(const Rect& rect, TextureFormat texture_format = TextureFormat::rgba8, bool clamp = true);
127
130
133 float get_pixel_ratio() const { return get_gc().get_pixel_ratio(); }
134
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
146
149
152
154 void set_cliprect(const Rectf &rect);
155
157
160 void push_cliprect(const Rectf &rect);
161
164
167
170
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
189
193 void set_viewport(const Rectf &viewport);
194
196 void set_projection(const Mat4f &matrix);
197
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) : matrix(current_canvas->get_transform()), canvas(current_canvas) {}
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) : cliprect(current_canvas->get_cliprect()), canvas(current_canvas) {}
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}
Blend state setup.
Definition blend_state.h:44
2D Graphics Canvas
Definition canvas.h:72
void fill_triangle(const Trianglef &dest_triangle, const Colorf &color=StandardColorf::white())
Draw a triangle.
Canvas(DisplayWindow &window)
Constructs a Canvas.
void set_viewport(const Rectf &viewport)
void fill_triangles(const std::vector< Vec2f > &positions, const Texture2D &texture, const Rect &texture_rect, const Gradient &gradient)
Draw triangles.
void fill_rect(float x1, float y1, float x2, float y2, const Gradient &color)
Gradient fill.
void clear_stencil(int value=0)
Clear the stencil buffer.
void fill_rect(const Rectf &rect, const Colorf &color)
Fill.
void set_rasterizer_state(const RasterizerState &state)
Set active rasterizer state.
void draw_box(const Pointf &start, const Pointf &end, const Colorf &color=StandardColorf::white())
Box.
void push_cliprect(const Rectf &rect)
Push current clipping rectangle to stack.
void fill_triangles(const std::vector< Vec2f > &positions, const Texture2D &texture, const Rect &texture_rect, const Colorf &color=StandardColorf::white())
Draw triangles.
Pointf grid_fit(const Pointf &pos)
Snaps the point to the nearest pixel corner.
void set_batcher(RenderBatcher *batcher)
Specifies which render batcher is to be currently active.
Sizef get_size() const
Returns the current size of the context.
Definition canvas.h:120
void fill_circle(float center_x, float center_y, float radius, const Colorf &color=StandardColorf::white())
Draw a circle.
Canvas()
Constructs a null instance.
const Mat4f & get_transform() const
Returns the current effective transform matrix.
const Mat4f & get_projection() const
Returns the current effective projection matrix.
void clear(const Colorf &color=StandardColorf::black())
Clears the whole context using the specified color.
void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Gradient &gradient)
Draw triangles.
void draw_point(float x1, float y1, const Colorf &color)
Draw a point.
friend class Font_DrawScaled
Definition canvas.h:416
PixelBuffer get_pixeldata(const Rect &rect, TextureFormat texture_format=TextureFormat::rgba8, bool clamp=true)
Return the content of the read buffer into a pixel buffer.
friend class Font_Impl
Definition canvas.h:413
void set_blend_state(const BlendState &state, const Colorf &blend_color=StandardColorf::white(), unsigned int sample_mask=0xffffffff)
Set active blend state.
float get_pixel_ratio() const
Definition canvas.h:133
void mult_transform(const Mat4f &matrix)
Multiplies the passed matrix onto the transform matrix.
void fill_circle(const Pointf &center, float radius, const Colorf &color=StandardColorf::white())
Circle.
void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Texture2D &texture, const Rect &texture_rect, const Colorf &color=StandardColorf::white())
Draw triangles.
void flush()
Flushes the render batcher currently active.
void draw_box(const Rectf &rect, const Colorf &color)
Box.
void reset_cliprect()
Removes the set clipping rectangle and empties the cliprect stack.
void throw_if_null() const
Throw an exception if this object is invalid.
void draw_box(float x1, float y1, float x2, float y2, const Colorf &color=StandardColorf::white())
Draw a box / rectangle.
void fill_triangles(const Vec2f *triangle_positions, const Colorf *colors, int num_vertices)
Draw triangles.
void pop_cliprect()
Pop current clipping rectangle from the stack.
void draw_line(const Pointf &start, const Pointf &end, const Colorf &color=StandardColorf::white())
Line.
void fill_rect(const Rectf &rect, const Gradient &gradient)
Gradient fill.
void draw_line(const LineSegment2f &line_segment, const Colorf &color=StandardColorf::white())
Line.
void draw_line_strip(const Vec2f *positions, int num_vertices, const Colorf &color=StandardColorf::white())
Line Strip.
void set_program_object(StandardProgram standard_program)
Set active program object to the standard program specified.
PixelBuffer get_pixeldata(TextureFormat texture_format=TextureFormat::rgba8, bool clamp=true)
Return the content of the read buffer into a pixel buffer.
void reset_blend_state()
Set active blend state.
void draw_line(float x1, float y1, float x2, float y2, const Colorf &color=StandardColorf::white())
Draw a line.
void fill_ellipse(const Pointf &center, float radius_x, float radius_y, const Colorf &color=StandardColorf::white())
Draw a filled ellipse.
float get_height() const
Returns the current height of the context.
Definition canvas.h:117
bool is_null() const
Returns true if this object is invalid.
Definition canvas.h:89
void set_transform(const Mat4f &matrix)
Sets the transform matrix to a new matrix.
void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Texture2D &texture, const Rect &texture_rect, const Gradient &gradient)
Draw triangles.
void push_cliprect()
Push cliprect.
void fill_rect(const Pointf &start, const Pointf &end, const Colorf &color=StandardColorf::white())
Fill.
void fill_triangles(const std::vector< Vec2f > &triangles, const Gradient &gradient)
Draw triangles.
void reset_depth_stencil_state()
Set active depth stencil state.
Canvas create()
Create a copy of a canvas.
GraphicContext & get_gc() const
Get gc.
void fill_triangles(const std::vector< Vec2f > &triangles, const Colorf *colors)
Draw triangles.
friend class Font_DrawSubPixel
Definition canvas.h:414
void fill_triangles(const std::vector< Vec2f > &positions, const Texture2D &texture, const Colorf &color=StandardColorf::white())
Draw triangles.
friend class Sprite_Impl
Definition canvas.h:411
void fill_circle(const Pointf &center, const Pointf &centergradient, float radius, const Gradient &gradient)
Gradient circle.
void reset_rasterizer_state()
Set active rasterizer state.
Rectf get_cliprect() const
Returns the current clipping rectangle.
void fill_rect(float x1, float y1, float x2, float y2, const Colorf &color=StandardColorf::white())
Draw a filled box / rectangle.
void fill_rect(const Pointf &start, const Pointf &end, const Gradient &gradient)
Gradient fill.
void fill_triangles(const Vec2f *positions, const Vec2f *texture_positions, int num_vertices, const Texture2D &texture, const Colorf *colors)
Draw triangles.
void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Texture2D &texture, const Gradient &gradient)
Draw triangles.
void set_projection(const Mat4f &matrix)
Set the projection matrix to be used in user projection map mode.
friend class Font_DrawFlat
Definition canvas.h:415
void fill_ellipse(const Pointf &center, float radius_x, float radius_y, const Gradient &gradient)
Draw a gradient filled ellipse.
Mat4f & get_inverse_transform()
Returns the inverse of the current effective transform matrix.
void fill_triangles(const std::vector< Vec2f > &positions, const std::vector< Vec2f > &texture_positions, const Texture2D &texture, const std::vector< Colorf > &colors)
Draw triangles.
Canvas(Canvas &canvas, FrameBuffer &framebuffer)
Constructs a Canvas based on a framebuffer. (based on the copy of the canvas)
void set_depth_stencil_state(const DepthStencilState &state, int stencil_ref=0)
Set active depth stencil state.
float get_width() const
Returns the current width of the context.
Definition canvas.h:114
void set_map_mode(MapMode mode)
Set the projection mapping mode.
void draw_point(const Pointf &point, const Colorf &color)
Point.
void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Texture2D &texture, const Colorf &color=StandardColorf::white())
Draw triangles.
void set_cliprect(const Rectf &rect)
Set the current clipping rectangle.
void fill_circle(const Pointf &center, float radius, const Gradient &gradient)
Gradient circle.
void draw_lines(const Vec2f *positions, int num_vertices, const Colorf &color=StandardColorf::white())
Lines.
void fill_triangles(const std::vector< Vec2f > &triangles, const Colorf &color=StandardColorf::white())
Draw triangles.
void clear_depth(float value=0)
Clear the depth buffer.
void fill_triangles(const std::vector< Vec2f > &positions, const Texture2D &texture, const Gradient &gradient)
Draw triangles.
void fill_triangles(const std::vector< Vec2f > &positions, const std::vector< Vec2f > &texture_positions, const Texture2D &texture, const Colorf &color=StandardColorf::white())
Draw triangles.
void draw_lines(const Vec2f *line_positions, const Vec2f *texture_positions, int num_vertices, const Texture2D &texture, const Colorf &line_color=StandardColorf::white())
Lines.
void fill_triangles(const Vec2f *positions, const Vec2f *texture_positions, int num_vertices, const Texture2D &texture, const Colorf &color=StandardColorf::white())
Draw triangles.
void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Colorf &color=StandardColorf::white())
Draw triangles.
void fill_triangle(const Pointf &a, const Pointf &b, const Pointf &c, const Colorf &color=StandardColorf::white())
Draw a triangle.
Definition canvas.h:444
void push_cliprect()
Definition canvas.h:450
ClipRectStack(Canvas *current_canvas)
Definition canvas.h:446
void push_cliprect(const Rectf &rect)
Definition canvas.h:449
void pop_cliprect()
Definition canvas.h:451
~ClipRectStack()
Definition canvas.h:447
Definition canvas.h:433
ClipRectState(Canvas *current_canvas)
Definition canvas.h:435
const Rectf cliprect
Definition canvas.h:437
~ClipRectState()
Definition canvas.h:436
Floating point color description class (for float).
Definition color.h:799
DepthStencil state setup.
Definition depth_stencil_state.h:44
Top-level window class.
Definition display_window.h:101
Frame-buffer object class.
Definition frame_buffer.h:72
Colorgradient description class.
Definition gradient.h:41
Interface to drawing graphics.
Definition graphic_context.h:257
Sizef get_dip_size() const
Definition graphic_context.h:353
float get_dip_height() const
Definition graphic_context.h:349
float get_dip_width() const
Definition graphic_context.h:345
float get_pixel_ratio() const
Image class.
Definition image.h:59
2D line segment - Float
Definition line_segment.h:188
4D matrix
Definition vec4.h:58
Definition path.h:53
Pixel data container.
Definition pixel_buffer.h:68
2D (x,y) point structure - Float
Definition point.h:72
Rasterizer state setup.
Definition rasterizer_state.h:44
2D (left,top,right,bottom) rectangle structure - Integer
Definition rect.h:489
2D (left,top,right,bottom) rectangle structure - Float
Definition rect.h:503
Render batching abstraction.
Definition render_batcher.h:45
2D (width,height) size structure - Float
Definition size.h:184
static Colorf black()
Definition color.h:1747
static Colorf white()
Definition color.h:1884
2D texture object class.
Definition texture_2d.h:41
Definition canvas.h:422
const Mat4f matrix
Definition canvas.h:426
~TransformState()
Definition canvas.h:425
TransformState(Canvas *current_canvas)
Definition canvas.h:424
Triangles - Float.
Definition triangle_math.h:81
TextureFormat
Texture format.
Definition texture_format.h:39
StandardProgram
Standard Program.
Definition graphic_context.h:239
MapMode
Mapping modes.
Definition canvas.h:64
Definition clanapp.h:36
@ color
value is an url