synfig-core  1.0.3
renderer.h
Go to the documentation of this file.
1 /* === S Y N F I G ========================================================= */
21 /* ========================================================================= */
22 
23 /* === S T A R T =========================================================== */
24 
25 #ifndef __SYNFIG_RENDERER_H
26 #define __SYNFIG_RENDERER_H
27 
28 /* === H E A D E R S ======================================================= */
29 
30 #include <ETL/handle>
31 #include <map>
32 #include <limits>
33 
34 /* === M A C R O S ========================================================= */
35 
36 /* === T Y P E D E F S ===================================================== */
37 
38 /* === C L A S S E S & S T R U C T S ======================================= */
39 
40 namespace synfig
41 {
42 
43 class Renderer
44 {
45 public:
46  typedef int RendererId;
47 
49  {
54  };
55 
56  enum { PrimitiveTypeCount = 4 };
57 
59  class PrimitiveDataBase: public etl::shared_object {
60  public:
61  typedef etl::handle<PrimitiveDataBase> Handle;
62  typedef etl::handle<const PrimitiveDataBase> ConstHandle;
63  };
64 
65  template<typename T>
67  public:
68  typedef T Data;
69  typedef etl::handle<PrimitiveData> Handle;
70  typedef etl::handle<const PrimitiveData> ConstHandle;
72  };
73 
77 
78  struct KeyCreate {
81 
82  inline KeyCreate():
85  primitive_type(primitive_type), renderer_id(renderer_id) { }
86  inline bool operator < (const KeyCreate &other) const
87  {
88  return primitive_type < other.primitive_type ? true
89  : other.primitive_type < primitive_type ? false
90  : renderer_id < other.renderer_id;
91  }
92  };
93 
94  typedef KeyCreate KeyCopy;
95 
96  struct KeyConvert {
100 
101  inline KeyConvert():
104  primitive_type(primitive_type), renderer_id_from(renderer_id_from), renderer_id_to(renderer_id_to) { }
105  inline bool operator < (const KeyConvert &other) const
106  {
107  return primitive_type < other.primitive_type ? true
108  : other.primitive_type < primitive_type ? false
109  : renderer_id_from < other.renderer_id_from ? true
110  : other.renderer_id_from < renderer_id_from ? false
111  : renderer_id_to < other.renderer_id_to;
112  }
113  };
114 
116  public:
117  int count;
122  count(0), renderer_id_to(0), next(NULL), func(NULL) { }
123  };
124 
125  typedef std::map<KeyCreate, FuncCreate> BookCreate;
126  typedef std::map<KeyCopy, FuncCopy> BookCopy;
127  typedef std::map<KeyConvert, FuncConvert> BookConvert;
128  typedef std::map<KeyConvert, ConvertChainEntry> BookConvertChain;
129 
130 private:
131  static RendererId last_registered_id;
132  static BookCreate book_create;
133  static BookCopy book_copy;
134  static BookConvert book_convert;
135  static BookConvertChain book_convert_chain;
136 
137  static void build_convert_chain();
138 
139 protected:
140  static void register_renderer(int &id);
141  static void register_func_create(const KeyCreate &key, FuncCreate func);
142  static void register_func_copy(const KeyCopy &key, FuncCopy func);
143  static void register_func_convert(const KeyConvert &key, FuncConvert func);
144  static void unregister_renderer(int &id);
145 
146  template<typename T>
148  { return new T(); }
149 
151  { return primitive; }
152 
153 public:
155  {
156  private:
157  typedef std::map<RendererId, PrimitiveDataBase::Handle> Map;
158  typedef Map::value_type Pair;
159  mutable Map primitives;
160  bool editing;
161 
162  public:
164 
165  private:
166  Map::iterator get_entry(RendererId renderer_id) const;
167 
168  protected:
169  inline PrimitiveBase(PrimitiveType type): editing(false), type(type) { }
170 
171  public:
172  virtual ~PrimitiveBase();
173  inline bool is_editing() const { return editing; }
176  void end_edit_primitive();
177  };
178 
179  template<typename RendererType, Renderer::PrimitiveType primitive_type>
180  class TypesTemplate { };
181 
182  template<PrimitiveType primitive_type>
183  class Primitive: public PrimitiveBase
184  {
185  public:
186  Primitive(): PrimitiveBase(primitive_type) { }
187 
188  template<typename RendererType>
190  {
192  typedef typename PrimitiveData::ConstHandle ConstHandle;
193  ConstHandle handle =
194  ConstHandle::cast_dynamic(
197  return handle ? &handle->data : NULL;
198  }
199 
200  template<typename RendererType>
202  {
203  PrimitiveDataBase::Handle base_handle =
206  if (base_handle)
207  {
210  typedef typename PrimitiveData::Handle Handle;
211  Handle handle = Handle::cast_dynamic(base_handle);
212  if (handle) return &handle->data;
214  }
215  return NULL;
216  }
217 
219  };
220 
221  template<typename T>
223  typedef T Data;
225  };
226 
227  template<typename T>
228  class TypesBase
229  {
230  public:
231  typedef T RendererType;
236  };
237 
242 
243  class Params {
244  public:
245  // TODO: blend method, color
249  inline Params(): out_surface(NULL), back_surface(NULL), mesh_texture_surface(NULL) { }
250  };
251 
252  enum Result {
256  };
257 
258 protected:
260 
261 public:
262  inline bool is_primitive_supported(PrimitiveType primitive_type)
263  { return supported_primitives[primitive_type]; }
264 
265  Renderer();
266  virtual ~Renderer();
267  virtual Result render(const Params &params, const PrimitiveBase &primitive);
268  virtual Result render_surface(const Params &params, const Primitive<PrimitiveTypeSurface> &primitive);
269  virtual Result render_polygon(const Params &params, const Primitive<PrimitiveTypePolygon> &primitive);
270  virtual Result render_colored_polygon(const Params &params, const Primitive<PrimitiveTypeColoredPolygon> &primitive);
271  virtual Result render_mesh(const Params &params, const Primitive<PrimitiveTypeMesh> &primitive);
272 };
273 
274 }; /* end namespace synfig */
275 
276 /* -- E N D ----------------------------------------------------------------- */
277 
278 #endif