canvas.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2013 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 "../api_display.h"
36 #include "../Render/graphic_context.h"
37 
38 namespace clan
39 {
42 
43 class Canvas_Impl;
44 class GraphicContext;
45 class PrimitivesArray;
46 class Sprite_Impl;
47 class Image;
48 class GlyphCache;
49 class Draw;
50 class RenderBatcher;
51 class Colorf;
52 class Pointf;
53 class Gradient;
54 class LineSegment2f;
55 class LineSegment2;
56 class Trianglef;
57 class Triangle;
58 class DisplayWindow;
59 class DisplayWindowDescription;
60 
62 enum MapMode
63 {
67 };
68 
70 class CL_API_DISPLAY Canvas
71 {
74 
75 public:
77  Canvas();
78 
80  Canvas(Canvas &canvas, FrameBuffer &framebuffer);
81 
83  explicit Canvas(DisplayWindow &window);
84 
85  ~Canvas();
86 
90 public:
92  Canvas create();
93 
95  bool is_null() const { return !impl; }
96 
98  void throw_if_null() const;
99 
103  GraphicContext& get_gc() const;
104 
106  const Mat4f &get_modelview() const;
107 
109  const Mat4f &get_projection() const;
110 
111  operator GraphicContext&() const {return get_gc();}
112 
114  inline int get_width() const {return get_gc().get_width();}
115 
117  inline int get_height() const {return get_gc().get_height();}
118 
120  inline Size get_size() const {return get_gc().get_size();}
121 
123  Rect 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 
134 public:
136  void set_rasterizer_state(const RasterizerState &state);
137 
139  void set_blend_state(const BlendState &state, const Colorf &blend_color = Colorf::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 
151  void reset_depth_stencil_state();
152 
154  void set_cliprect(const Rect &rect);
155 
157 
160  void push_cliprect(const Rect &rect);
161 
163  void push_cliprect();
164 
166  void pop_cliprect();
167 
169  void reset_cliprect();
170 
172  void clear(const Colorf &color = Colorf::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 
191  void set_projection(const Mat4f &matrix);
192 
197  void set_batcher(RenderBatcher *batcher);
198 
200  void set_modelview(const Mat4f &matrix);
201 
203  void mult_modelview(const Mat4f &matrix);
204 
206  void push_modelview();
207 
209 
211  void set_translate(float x, float y, float z = 0.0);
212  void set_translate(const Vec2f &vec) { set_translate(vec.x, vec.y, 0.0f); }
213  void set_translate(const Vec3f &vec) { set_translate(vec.x, vec.y, vec.z); }
214  void set_translate(int x, int y, int z = 0) { set_translate((float) x, (float) y, (float) z); }
215 
217 
221  void mult_translate(float x, float y, float z = 0.0);
222  void mult_translate(const Vec2f &vec) { mult_translate(vec.x, vec.y, 0.0f); }
223  void mult_translate(const Vec3f &vec) { mult_translate(vec.x, vec.y, vec.z); }
224  void mult_translate(int x, int y, int z = 0) { mult_translate((float) x, (float) y, (float) z); }
225 
227 
232  void push_translate(float x, float y, float z = 0.0);
233  void push_translate(const Vec2f &vec) { push_translate(vec.x, vec.y, 0.0f); }
234  void push_translate(const Vec3f &vec) { push_translate(vec.x, vec.y, vec.z); }
235  void push_translate(int x, int y, int z = 0) { push_translate((float) x, (float) y, (float) z); }
236 
238  void set_rotate(const Angle &angle, float x = 0.0, float y = 0.0, float z = 1.0, bool normalize = true);
239  void set_rotate(const Angle &angle, const Vec2f &vec, bool normalize = true) { set_rotate(angle, vec.x, vec.y, 1.0f, normalize); }
240  void set_rotate(const Angle &angle, const Vec3f &vec, bool normalize = true) { set_rotate(angle, vec.x, vec.y, vec.z, normalize); }
241 
243  void mult_rotate(const Angle &angle, float x = 0.0, float y = 0.0, float z = 1.0, bool normalize = true);
244  void mult_rotate(const Angle &angle, const Vec2f &vec, bool normalize = true) { mult_rotate(angle, vec.x, vec.y, 1.0f, normalize); }
245  void mult_rotate(const Angle &angle, const Vec3f &vec, bool normalize = true) { mult_rotate(angle, vec.x, vec.y, vec.z, normalize); }
246 
248  void push_rotate(const Angle &angle, float x = 0.0, float y = 0.0, float z = 1.0);
249  void push_rotate(const Angle &angle, const Vec2f &vec) { push_rotate(angle, vec.x, vec.y, 1.0f); }
250  void push_rotate(const Angle &angle, const Vec3f &vec) { push_rotate(angle, vec.x, vec.y, vec.z); }
251 
253  void set_scale(float x, float y, float z = 1.0);
254  void set_scale(const Vec2f &vec) { set_scale(vec.x, vec.y, 1.0f); }
255  void set_scale(const Vec3f &vec) { set_scale(vec.x, vec.y, vec.z); }
256  void set_scale(int x, int y, int z = 1) { set_scale((float) x, (float) y, (float) z); }
257 
259  void mult_scale(float x, float y, float z = 1.0);
260  void mult_scale(const Vec2f &vec) { mult_scale(vec.x, vec.y, 1.0f); }
261  void mult_scale(const Vec3f &vec) { mult_scale(vec.x, vec.y, vec.z); }
262  void mult_scale(int x, int y, int z = 1) { mult_scale((float) x, (float) y, (float) z); }
263 
265  void push_scale(float x, float y, float z = 1.0);
266  void push_scale(const Vec2f &vec) { push_scale(vec.x, vec.y, 1.0f); }
267  void push_scale(const Vec3f &vec) { push_scale(vec.x, vec.y, vec.z); }
268  void push_scale(int x, int y, int z = 1) { push_scale((float) x, (float) y, (float) z); }
269 
271  void pop_modelview();
272 
274  void flush();
275 
277  void draw_point(float x1, float y1, const Colorf &color);
278 
284  void draw_point(const Pointf &point, const Colorf &color);
285 
287  void draw_line(float x1, float y1, float x2, float y2, const Colorf &color = Colorf::white);
288 
295  void draw_line(const Pointf &start, const Pointf &end, const Colorf &color = Colorf::white);
296 
302  void draw_line(const LineSegment2f &line_segment, const Colorf &color = Colorf::white);
303 
305  void draw_lines(const Vec2f *positions, int num_vertices, const Colorf &color = Colorf::white);
306 
308  void draw_lines(const Vec2f *line_positions, const Vec2f *texture_positions, int num_vertices, const Texture2D &texture, const Colorf &line_color = Colorf::white);
309 
311  void draw_line_strip(const Vec2f *positions, int num_vertices, const Colorf &color = Colorf::white);
312 
314  void draw_box(float x1, float y1, float x2, float y2, const Colorf &color = Colorf::white);
315 
322  void draw_box(const Pointf &start, const Pointf &end, const Colorf &color = Colorf::white);
323 
329  void draw_box(const Rectf &rect, const Colorf &color);
330 
332  void fill_rect(float x1, float y1, float x2, float y2, const Colorf &color = Colorf::white);
333 
340  void fill_rect(const Pointf &start, const Pointf &end, const Colorf &color = Colorf::white);
341 
347  void fill_rect(const Rectf &rect, const Colorf &color);
348 
357  void fill_rect(float x1, float y1, float x2, float y2, const Gradient &color);
358 
365  void fill_rect(const Pointf &start, const Pointf &end, const Gradient &gradient);
366 
372  void fill_rect(const Rectf &rect, const Gradient &gradient);
373 
375  void fill_circle(float center_x, float center_y, float radius, const Colorf &color = Colorf::white);
376 
383  void fill_circle(const Pointf &center, float radius, const Colorf &color = Colorf::white);
384 
391  void fill_circle(const Pointf &center, float radius, const Gradient &gradient);
392 
400  void fill_circle(const Pointf &center, const Pointf &centergradient, float radius, const Gradient &gradient);
401 
403  void fill_triangle(const Pointf &a, const Pointf &b, const Pointf &c, const Colorf &color = Colorf::white);
404 
406  void fill_triangle(const Trianglef &dest_triangle, const Colorf &color = Colorf::white);
407 
409  void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Colorf &color = Colorf::white);
410 
412  void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Gradient &gradient);
413 
415  void fill_triangles(const Vec2f *triangle_positions, const Colorf *colors, int num_vertices);
416 
418  void fill_triangles(const std::vector<Vec2f> &triangles, const Colorf *colors);
419 
421  void fill_triangles(const std::vector<Vec2f> &triangles, const Colorf &color = Colorf::white);
422 
424  void fill_triangles(const std::vector<Vec2f> &triangles, const Gradient &gradient);
425 
427  void fill_triangles(const Vec2f *positions, const Vec2f *texture_positions, int num_vertices, const Texture2D &texture, const Colorf &color = Colorf::white);
428 
430  void fill_triangles(const Vec2f *positions, const Vec2f *texture_positions, int num_vertices, const Texture2D &texture, const Colorf *colors);
431 
433  void fill_triangles(const std::vector<Vec2f> &positions, const std::vector<Vec2f> &texture_positions, const Texture2D &texture, const Colorf &color = Colorf::white);
434 
436  void fill_triangles(const std::vector<Vec2f> &positions, const std::vector<Vec2f> &texture_positions, const Texture2D &texture, const std::vector<Colorf> &colors);
437 
439  void fill_triangles(const std::vector<Vec2f> &positions, const Texture2D &texture, const Colorf &color = Colorf::white);
440 
442  void fill_triangles(const std::vector<Vec2f> &positions, const Texture2D &texture, const Rect &texture_rect, const Colorf &color = Colorf::white);
443 
445  void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Texture2D &texture, const Colorf &color = Colorf::white);
446 
448  void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Texture2D &texture, const Rect &texture_rect, const Colorf &color = Colorf::white);
449 
451  void fill_triangles(const std::vector<Vec2f> &positions, const Texture2D &texture, const Gradient &gradient);
452 
454  void fill_triangles(const std::vector<Vec2f> &positions, const Texture2D &texture, const Rect &texture_rect, const Gradient &gradient);
455 
457  void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Texture2D &texture, const Gradient &gradient);
458 
460  void fill_triangles(const Vec2f *triangle_positions, int num_vertices, const Texture2D &texture, const Rect &texture_rect, const Gradient &gradient);
461 
463  void fill_ellipse(const Pointf &center, float radius_x, float radius_y, const Colorf &color = Colorf::white);
464 
466  void fill_ellipse(const Pointf &center, float radius_x, float radius_y, const Gradient &gradient);
467 
471 public:
472 
476 private:
477  std::shared_ptr<Canvas_Impl> impl;
478 
479  friend class Sprite_Impl;
480  friend class Image;
481  friend class GlyphCache;
483 };
484 
485 }
486 
void push_translate(int x, int y, int z=0)
Definition: canvas.h:235
void mult_translate(const Vec2f &vec)
Definition: canvas.h:222
void mult_scale(const Vec2f &vec)
Definition: canvas.h:260
Floating point color description class (for float).
Definition: color.h:661
MapMode
Mapping modes.
Definition: canvas.h:62
Type x
Definition: vec3.h:81
Rasterizer state setup.
Definition: rasterizer_state.h:45
void push_translate(const Vec2f &vec)
Definition: canvas.h:233
Angle class.
Definition: angle.h:63
Colorgradient description class.
Definition: gradient.h:42
static Colorf white
Definition: color.h:1245
Frame-buffer object class.
Definition: frame_buffer.h:73
2D Graphics Canvas
Definition: canvas.h:70
void mult_translate(const Vec3f &vec)
Definition: canvas.h:223
Type y
Definition: vec3.h:82
Pixel data container.
Definition: pixel_buffer.h:69
int get_width() const
Returns the current width of the context.
Definition: canvas.h:114
Blend state setup.
Definition: blend_state.h:45
Triangles - Float.
Definition: triangle_math.h:99
void mult_rotate(const Angle &angle, const Vec2f &vec, bool normalize=true)
Definition: canvas.h:244
void set_rotate(const Angle &angle, const Vec2f &vec, bool normalize=true)
Definition: canvas.h:239
void push_rotate(const Angle &angle, const Vec3f &vec)
Definition: canvas.h:250
Render batching abstraction.
Definition: render_batcher.h:44
StandardProgram
Standard Program.
Definition: graphic_context.h:240
void set_translate(const Vec2f &vec)
Definition: canvas.h:212
2D (left,top,right,bottom) rectangle structure - Integer
Definition: rect.h:453
void set_translate(int x, int y, int z=0)
Definition: canvas.h:214
Type x
Definition: vec2.h:82
void mult_scale(int x, int y, int z=1)
Definition: canvas.h:262
2D texture object class.
Definition: texture_2d.h:42
void push_scale(int x, int y, int z=1)
Definition: canvas.h:268
2D (left,top,right,bottom) rectangle structure - Float
Definition: rect.h:467
void set_rotate(const Angle &angle, const Vec3f &vec, bool normalize=true)
Definition: canvas.h:240
int get_height() const
Returns the current height of the context.
Definition: canvas.h:117
Definition: canvas.h:66
Definition: canvas.h:64
TextureFormat
Texture format.
Definition: texture_format.h:41
void mult_translate(int x, int y, int z=0)
Definition: canvas.h:224
void set_translate(const Vec3f &vec)
Definition: canvas.h:213
void set_scale(const Vec2f &vec)
Definition: canvas.h:254
void push_rotate(const Angle &angle, const Vec2f &vec)
Definition: canvas.h:249
void push_scale(const Vec2f &vec)
Definition: canvas.h:266
3D vector
Definition: line_ray.h:49
Image class.
Definition: image.h:60
Interface to drawing graphics.
Definition: graphic_context.h:257
Size get_size() const
Returns the current size of the context.
Definition: canvas.h:120
void push_translate(const Vec3f &vec)
Definition: canvas.h:234
void set_scale(int x, int y, int z=1)
Definition: canvas.h:256
2D (width,height) size structure - Integer
Definition: size.h:157
DepthStencil state setup.
Definition: depth_stencil_state.h:45
4D matrix
Definition: mat2.h:52
static Colorf black
Definition: color.h:837
void mult_scale(const Vec3f &vec)
Definition: canvas.h:261
2D (x,y) point structure - Float
Definition: point.h:73
void push_scale(const Vec3f &vec)
Definition: canvas.h:267
Top-level window class.
Definition: display_window.h:85
Definition: texture_format.h:44
2D line segment - Float
Definition: line_segment.h:211
Definition: canvas.h:65
void set_scale(const Vec3f &vec)
Definition: canvas.h:255
void mult_rotate(const Angle &angle, const Vec3f &vec, bool normalize=true)
Definition: canvas.h:245
bool is_null() const
Returns true if this object is invalid.
Definition: canvas.h:95
Type z
Definition: vec3.h:83
Type y
Definition: vec2.h:83
C clamp(A val, B minval, C maxval)
Definition: cl_math.h:91