honey_the_codewitch

htcw_gfx 2.0 vector API

Sep 7th, 2024
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.66 KB | Source Code | 0 0
  1. #ifndef HTCW_GFX_CANVAS_HPP
  2. #define HTCW_GFX_CANVAS_HPP
  3. #include "gfx_core.hpp"
  4. #include "gfx_pixel.hpp"
  5. #include "gfx_palette.hpp"
  6. #include "gfx_bitmap.hpp"
  7. #include "gfx_font.hpp"
  8. #include "gfx_encoding.hpp"
  9. #include "gfx_vector_core.hpp"
  10. namespace gfx {
  11. enum struct canvas_paint_type {
  12.     none = 0,
  13.     solid,
  14.     gradient,
  15.     texture
  16. };
  17. enum struct canvas_texture_type {
  18.     plain = 0,
  19.     tiled
  20. };
  21. struct canvas_texture {
  22.     canvas_texture_type type;
  23.     float opacity;
  24.     ::gfx::matrix matrix;
  25.     const_bitmap<rgba_pixel<32>>* source;
  26. };
  27. struct canvas_gradient_stop {
  28.     float offset;
  29.     rgba_pixel<32> color;
  30. };
  31. enum struct canvas_gradient_type {
  32.     linear = 0,
  33.     radial = 1
  34. };
  35. enum struct canvas_spread_method {
  36.     pad = 0,
  37.     reflect,
  38.     repeat
  39. };
  40. struct canvas_gradient {
  41.     canvas_gradient_type type;
  42.     canvas_spread_method spread;
  43.     ::gfx::matrix matrix;
  44.     float values[6];
  45.     canvas_gradient_stop* stops;
  46.     size_t stops_size;
  47. };
  48. struct canvas_dash {
  49.     float offset;
  50.     float* values;
  51.     size_t values_size;
  52. };
  53. enum struct canvas_line_cap {
  54.     butt = 0, ///< Flat edge at the end of the stroke.
  55.     rount, ///< Rounded ends at the end of the stroke.
  56.     square ///< Square ends at the end of the stroke.
  57. };
  58. enum struct canvas_line_join {
  59.     miter=0, ///< Miter join with sharp corners.
  60.     round, ///< Rounded join.
  61.     bevel ///< Beveled join with a flattened corner.
  62. };
  63. struct canvas_stroke_style {
  64.     float width;
  65.     canvas_line_cap cap;
  66.     canvas_line_join join;
  67.     float miter_limit;
  68. };
  69. enum struct canvas_fill_rule {
  70.     non_zero = 0,
  71.     even_odd
  72. };
  73. enum canvas_compositing_mode {
  74.     source = 0,
  75.     source_over,
  76.     destination_inside,
  77.     destination_outside
  78. };
  79. class canvas final {
  80. public:
  81.     typedef gfx_result(*on_write_callback_type)(const rect16& bounds, rgba_pixel<32> color, void* state);
  82. private:
  83.     struct canvas_data {
  84.         rgba_pixel<32> stroke_color;
  85.         canvas_gradient stroke_gradient;
  86.         canvas_texture stroke_texture;
  87.         canvas_stroke_style stroke_style;
  88.         canvas_dash stroke_dash;
  89.         canvas_paint_type stroke_paint_type;
  90.         rgba_pixel<32> fill_color;
  91.         canvas_gradient fill_gradient;
  92.         canvas_texture fill_texture;
  93.         canvas_paint_type fill_paint_type;
  94.         stream* font_stream;
  95.         size_t font_index;
  96.         float font_size;
  97.         canvas_fill_rule fill_rule;
  98.         canvas_compositing_mode compositing_mode;
  99.         float opacity;
  100.         ::gfx::matrix matrix;
  101.     };
  102.     void* m_info;
  103.     canvas_data* m_data;
  104.     size16 m_dimensions;
  105.     on_write_callback_type m_write_callback;
  106.     void* m_callback_state;
  107.     static bool write_callback(int x, int y, int length, uint32_t color, void* state);
  108.     canvas(const canvas& rhs)=delete;
  109.     canvas& operator=(const canvas& rhs)=delete;
  110. public:
  111.     canvas();
  112.     canvas(size16 dimensions);
  113.     canvas(canvas&& rhs);
  114.     ~canvas();
  115.     canvas& operator=(canvas&& rhs);
  116.     gfx_result initialize();
  117.     bool initialized() const;
  118.     void deinitialize();
  119.     on_write_callback_type on_write_callback() const;
  120.     void* on_write_callback_state() const;
  121.     void on_write_callback(on_write_callback_type callback, void* state=nullptr);
  122.  
  123.     size16 dimensions() const;
  124.     void dimensions(size16 value);
  125.     rect16 bounds() const;
  126.    
  127.     rgba_pixel<32> stroke_color() const;
  128.     void stroke_color(rgba_pixel<32> value);
  129.     canvas_gradient stroke_gradient() const;
  130.     void stroke_gradient(const canvas_gradient& value);
  131.     canvas_texture stroke_texture() const;
  132.     void stroke_texture(const canvas_texture& value);
  133.     canvas_stroke_style stroke_style() const;
  134.     void stroke_style(const canvas_stroke_style& value);
  135.     canvas_dash stroke_dash() const;
  136.     void stroke_dash(const canvas_dash& value);
  137.     canvas_paint_type stroke_paint_type() const;
  138.     void stroke_paint_type(canvas_paint_type value);
  139.     rgba_pixel<32> fill_color() const;
  140.     void fill_color(rgba_pixel<32> value);
  141.     canvas_gradient fill_gradient() const;
  142.     void fill_gradient(const canvas_gradient& value);
  143.     canvas_texture fill_texture() const;
  144.     void fill_texture(const canvas_texture& value);
  145.     canvas_paint_type fill_paint_type() const;
  146.     void fill_paint_type(canvas_paint_type value);
  147.     stream* font() const;
  148.     void font(stream& ttf_stream, size_t index);
  149.     float font_size() const;
  150.     void font_size(float value);
  151.     canvas_fill_rule fill_rule() const;
  152.     void fill_rule(canvas_fill_rule value);
  153.     canvas_compositing_mode compositing_mode() const;
  154.     void compositing_mode(canvas_compositing_mode value);
  155.     float opacity() const;
  156.     void opacity(float value);
  157.     ::gfx::matrix matrix() const;
  158.     void matrix(const ::gfx::matrix& value);
  159.     rectf fill_bounds() const;
  160.     rectf stroke_bounds() const;
  161.     rectf clip_bounds() const;
  162.     // path builder - separate class?
  163.     gfx_result move_to(pointf location);
  164.     gfx_result line_to(pointf location);
  165.     gfx_result quad_to(pointf point1, pointf point2);
  166.     gfx_result cubic_to(pointf point1, pointf point2, pointf point3);
  167.     gfx_result arc_to(sizef radiuses,float angle, bool large_arc, bool sweep, pointf location);
  168.     gfx_result close_path();
  169.  
  170.     gfx_result rectangle(const rectf& bounds);
  171.     gfx_result rounded_rectangle(const rectf bounds, sizef radiuses);
  172.     gfx_result ellipse(pointf center, sizef radiuses);
  173.     gfx_result circle(pointf center, float radius);
  174.     gfx_result arc(pointf center, float radius, float a0, float a1, bool ccw);
  175.     gfx_result path();
  176.     gfx_result text(pointf location, const text_info& info);
  177.     gfx_result clip();
  178.    
  179. };
  180. };
  181. #endif
Tags: vector
Add Comment
Please, Sign In to add comment