OpenXcom  1.0
Open-source clone of the original X-Com
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ShaderDrawHelper.h
1 /*
2  * Copyright 2010-2014 OpenXcom Developers.
3  *
4  * This file is part of OpenXcom.
5  *
6  * OpenXcom is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * OpenXcom is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef OPENXCOM_SHADERDRAWHELPER_H
21 #define OPENXCOM_SHADERDRAWHELPER_H
22 
23 #include "Surface.h"
24 #include "GraphSubset.h"
25 #include <vector>
26 
27 namespace OpenXcom
28 {
29 namespace helper
30 {
31 
36 class Nothing
37 {
38 
39 };
40 
45 template<typename T>
46 class Scalar
47 {
48 public:
49  T& ref;
50  inline Scalar(T& t) : ref(t)
51  {
52 
53  }
54 };
55 
56 
62 template<typename Pixel>
64 {
65 public:
66  typedef Pixel* PixelPtr;
67  typedef Pixel& PixelRef;
68 
69 protected:
70  const PixelPtr _orgin;
71  const GraphSubset _range_base;
72  GraphSubset _range_domain;
73  const int _pitch;
74 
75 public:
77  inline ShaderBase(const ShaderBase& s):
78  _orgin(s.ptr()),
79  _range_base(s._range_base),
80  _range_domain(s.getDomain()),
81  _pitch(s.pitch())
82  {
83 
84  }
85 
96  inline ShaderBase(std::vector<Pixel>& f, int max_x, int max_y):
97  _orgin(&(f[0])),
98  _range_base(max_x, max_y),
99  _range_domain(max_x, max_y),
100  _pitch(max_x)
101  {
102 
103  }
104 
105  inline PixelPtr ptr() const
106  {
107  return _orgin;
108  }
109  inline int pitch() const
110  {
111  return _pitch;
112  }
113 
114  inline void setDomain(const GraphSubset& g)
115  {
116  _range_domain = GraphSubset::intersection(g, _range_base);
117  }
118  inline const GraphSubset& getDomain() const
119  {
120  return _range_domain;
121  }
122  inline const GraphSubset& getBaseDomain() const
123  {
124  return _range_base;
125  }
126 
127  inline const GraphSubset& getImage() const
128  {
129  return _range_domain;
130  }
131 };
132 
138 template<typename Pixel>
139 class ShaderBase<const Pixel>
140 {
141 public:
142  typedef const Pixel* PixelPtr;
143  typedef const Pixel& PixelRef;
144 
145 protected:
146  const PixelPtr _orgin;
147  const GraphSubset _range_base;
148  GraphSubset _range_domain;
149  const int _pitch;
150 
151 public:
153  inline ShaderBase(const ShaderBase& s):
154  _orgin(s.ptr()),
155  _range_base(s.getBaseDomain()),
156  _range_domain(s.getDomain()),
157  _pitch(s.pitch())
158  {
159 
160  }
161 
163  inline ShaderBase(const ShaderBase<Pixel>& s):
164  _orgin(s.ptr()),
165  _range_base(s.getBaseDomain()),
166  _range_domain(s.getDomain()),
167  _pitch(s.pitch())
168  {
169 
170  }
171 
182  inline ShaderBase(const std::vector<Pixel>& f, int max_x, int max_y):
183  _orgin(&(f[0])),
184  _range_base(max_x, max_y),
185  _range_domain(max_x, max_y),
186  _pitch(max_x)
187  {
188 
189  }
190 
191  inline PixelPtr ptr() const
192  {
193  return _orgin;
194  }
195  inline int pitch() const
196  {
197  return _pitch;
198  }
199 
200  inline void setDomain(const GraphSubset& g)
201  {
202  _range_domain = GraphSubset::intersection(g, _range_base);
203  }
204  inline const GraphSubset& getDomain() const
205  {
206  return _range_domain;
207  }
208  inline const GraphSubset& getBaseDomain() const
209  {
210  return _range_base;
211  }
212 
213  inline const GraphSubset& getImage() const
214  {
215  return _range_domain;
216  }
217 };
218 
225 template<>
226 class ShaderBase<Uint8>
227 {
228 public:
229  typedef Uint8* PixelPtr;
230  typedef Uint8& PixelRef;
231 
232 protected:
233  const PixelPtr _orgin;
234  const GraphSubset _range_base;
235  GraphSubset _range_domain;
236  const int _pitch;
237 
238 public:
240  inline ShaderBase(const ShaderBase& s):
241  _orgin(s.ptr()),
242  _range_base(s.getBaseDomain()),
243  _range_domain(s.getDomain()),
244  _pitch(s.pitch())
245  {
246 
247  }
248 
256  inline ShaderBase(Surface* s):
257  _orgin((Uint8*) s->getSurface()->pixels),
258  _range_base(s->getWidth(), s->getHeight()),
259  _range_domain(s->getWidth(), s->getHeight()),
260  _pitch(s->getSurface()->pitch)
261  {
262 
263  }
264 
275  inline ShaderBase(std::vector<Uint8>& f, int max_x, int max_y):
276  _orgin(&(f[0])),
277  _range_base(max_x, max_y),
278  _range_domain(max_x, max_y),
279  _pitch(max_x)
280  {
281 
282  }
283 
284  inline PixelPtr ptr() const
285  {
286  return _orgin;
287  }
288  inline int pitch() const
289  {
290  return _pitch;
291  }
292 
293  inline void setDomain(const GraphSubset& g)
294  {
295  _range_domain = GraphSubset::intersection(g, _range_base);
296  }
297  inline const GraphSubset& getDomain() const
298  {
299  return _range_domain;
300  }
301  inline const GraphSubset& getBaseDomain() const
302  {
303  return _range_base;
304  }
305 
306  inline const GraphSubset& getImage() const
307  {
308  return _range_domain;
309  }
310 };
311 
318 template<>
319 class ShaderBase<const Uint8>
320 {
321 public:
322  typedef const Uint8* PixelPtr;
323  typedef const Uint8& PixelRef;
324 
325 protected:
326  const PixelPtr _orgin;
327  const GraphSubset _range_base;
328  GraphSubset _range_domain;
329  const int _pitch;
330 
331 public:
333  inline ShaderBase(const ShaderBase& s):
334  _orgin(s.ptr()),
335  _range_base(s.getBaseDomain()),
336  _range_domain(s.getDomain()),
337  _pitch(s.pitch())
338  {
339 
340  }
341 
343  inline ShaderBase(const ShaderBase<Uint8>& s):
344  _orgin(s.ptr()),
345  _range_base(s.getBaseDomain()),
346  _range_domain(s.getDomain()),
347  _pitch(s.pitch())
348  {
349 
350  }
351 
359  inline ShaderBase(const Surface* s):
360  _orgin((Uint8*) s->getSurface()->pixels),
361  _range_base(s->getWidth(), s->getHeight()),
362  _range_domain(s->getWidth(), s->getHeight()),
363  _pitch(s->getSurface()->pitch)
364  {
365 
366  }
367 
378  inline ShaderBase(const std::vector<Uint8>& f, int max_x, int max_y):
379  _orgin(&(f[0])),
380  _range_base(max_x, max_y),
381  _range_domain(max_x, max_y),
382  _pitch(max_x)
383  {
384 
385  }
386 
387  inline PixelPtr ptr() const
388  {
389  return _orgin;
390  }
391  inline int pitch() const
392  {
393  return _pitch;
394  }
395 
396  inline void setDomain(const GraphSubset& g)
397  {
398  _range_domain = GraphSubset::intersection(g, _range_base);
399  }
400  inline const GraphSubset& getDomain() const
401  {
402  return _range_domain;
403  }
404  inline const GraphSubset& getBaseDomain() const
405  {
406  return _range_base;
407  }
408 
409  inline const GraphSubset& getImage() const
410  {
411  return _range_domain;
412  }
413 };
414 
415 
418 template<typename SurfaceType>
419 struct controler
420 {
421  //NOT IMPLEMENTED ANYWHERE!
422  //you need create your own specification or use different type, no default version
423 
429  inline const GraphSubset& get_range();
435  inline void mod_range(GraphSubset& g);
440  inline void set_range(const GraphSubset& g);
441 
442  inline void mod_y(int& begin, int& end);
443  inline void set_y(const int& begin, const int& end);
444  inline void inc_y();
445 
446 
447  inline void mod_x(int& begin, int& end);
448  inline void set_x(const int& begin, const int& end);
449  inline void inc_x();
450 
451  inline int& get_ref();
452 };
453 
455 template<typename T>
456 struct controler<Scalar<T> >
457 {
458  T& ref;
459 
460  inline controler(const Scalar<T>& s) : ref(s.ref)
461  {
462 
463  }
464 
465  //cant use this function
466  //inline GraphSubset get_range()
467 
468  inline void mod_range(GraphSubset&)
469  {
470  //nothing
471  }
472  inline void set_range(const GraphSubset&)
473  {
474  //nothing
475  }
476 
477  inline void mod_y(int&, int&)
478  {
479  //nothing
480  }
481  inline void set_y(const int&, const int&)
482  {
483  //nothing
484  }
485  inline void inc_y()
486  {
487  //nothing
488  }
489 
490 
491  inline void mod_x(int&, int&)
492  {
493  //nothing
494  }
495  inline void set_x(const int&, const int&)
496  {
497  //nothing
498  }
499  inline void inc_x()
500  {
501  //nothing
502  }
503 
504  inline T& get_ref()
505  {
506  return ref;
507  }
508 };
509 
511 template<>
513 {
514  const int i;
515  inline controler(const Nothing&) : i(0)
516  {
517 
518  }
519 
520  //cant use this function
521  //inline GraphSubset get_range()
522 
523  inline void mod_range(GraphSubset&)
524  {
525  //nothing
526  }
527  inline void set_range(const GraphSubset&)
528  {
529  //nothing
530  }
531 
532  inline void mod_y(int&, int&)
533  {
534  //nothing
535  }
536  inline void set_y(const int&, const int&)
537  {
538  //nothing
539  }
540  inline void inc_y()
541  {
542  //nothing
543  }
544 
545  inline void mod_x(int&, int&)
546  {
547  //nothing
548  }
549  inline void set_x(const int&, const int&)
550  {
551  //nothing
552  }
553  inline void inc_x()
554  {
555  //nothing
556  }
557 
558  inline const int& get_ref()
559  {
560  return i;
561  }
562 };
563 
564 template<typename PixelPtr, typename PixelRef>
566 {
567 
568  const PixelPtr data;
569  PixelPtr ptr_pos_y;
570  PixelPtr ptr_pos_x;
571  GraphSubset range;
572  int start_x;
573  int start_y;
574 
575  const std::pair<int, int> step;
576 
577 
578  controler_base(PixelPtr base, const GraphSubset& d, const GraphSubset& r, const std::pair<int, int>& s) :
579  data(base + d.beg_x*s.first + d.beg_y*s.second),
580  ptr_pos_y(0), ptr_pos_x(0),
581  range(r),
582  start_x(), start_y(),
583  step(s)
584  {
585 
586  }
587 
588 
589  inline const GraphSubset& get_range()
590  {
591  return range;
592  }
593 
594  inline void mod_range(GraphSubset& r)
595  {
596  r = GraphSubset::intersection(range, r);
597  }
598 
599  inline void set_range(const GraphSubset& r)
600  {
601  start_x = r.beg_x - range.beg_x;
602  start_y = r.beg_y - range.beg_y;
603  range = r;
604  }
605 
606  inline void mod_y(int&, int&)
607  {
608  ptr_pos_y = data + step.first * start_x + step.second * start_y;
609  }
610  inline void set_y(const int& begin, const int&)
611  {
612  ptr_pos_y += step.second*begin;
613  }
614  inline void inc_y()
615  {
616  ptr_pos_y += step.second;
617  }
618 
619 
620  inline void mod_x(int&, int&)
621  {
622  ptr_pos_x = ptr_pos_y;
623  }
624  inline void set_x(const int& begin, const int&)
625  {
626  ptr_pos_x += step.first*begin;
627  }
628  inline void inc_x()
629  {
630  ptr_pos_x += step.first;
631  }
632 
633  inline PixelRef get_ref()
634  {
635  return *ptr_pos_x;
636  }
637 };
638 
639 
640 
641 template<typename Pixel>
642 struct controler<ShaderBase<Pixel> > : public controler_base<typename ShaderBase<Pixel>::PixelPtr, typename ShaderBase<Pixel>::PixelRef>
643 {
644  typedef typename ShaderBase<Pixel>::PixelPtr PixelPtr;
645  typedef typename ShaderBase<Pixel>::PixelRef PixelRef;
646 
648 
649  controler(const ShaderBase<Pixel>& f) : base_type(f.ptr(), f.getDomain(), f.getImage(), std::make_pair(1, f.pitch()))
650  {
651 
652  }
653 
654 };
655 
656 }//namespace helper
657 
658 }//namespace OpenXcom
659 
660 #endif /* SHADERDRAWHELPER_H */
661 
helper class for handling implementation differences in different surfaces types Used in function Sha...
Definition: ShaderDrawHelper.h:419
ShaderBase(const ShaderBase< Pixel > &s)
copy constructor
Definition: ShaderDrawHelper.h:163
ShaderBase(const ShaderBase &s)
copy constructor
Definition: ShaderDrawHelper.h:77
ShaderBase(Surface *s)
create surface using surface s as data source.
Definition: ShaderDrawHelper.h:256
const GraphSubset & get_range()
function used only when SurfaceType can be used as destination surface if that type should not be use...
ShaderBase(std::vector< Pixel > &f, int max_x, int max_y)
create surface using vector f as data source.
Definition: ShaderDrawHelper.h:96
ShaderBase(const std::vector< Pixel > &f, int max_x, int max_y)
create surface using vector f as data source.
Definition: ShaderDrawHelper.h:182
ShaderBase(const ShaderBase< Uint8 > &s)
copy constructor
Definition: ShaderDrawHelper.h:343
ShaderBase(const ShaderBase &s)
copy constructor
Definition: ShaderDrawHelper.h:333
This is surface argument to ShaderDraw.
Definition: ShaderDrawHelper.h:226
void set_range(const GraphSubset &g)
set final drawing range.
ShaderBase(const ShaderBase &s)
copy constructor
Definition: ShaderDrawHelper.h:240
ShaderBase(const ShaderBase &s)
copy constructor
Definition: ShaderDrawHelper.h:153
This is empty argument to ShaderDraw.
Definition: ShaderDrawHelper.h:36
Element that is blit (rendered) onto the screen.
Definition: Surface.h:39
Definition: GraphSubset.h:31
This is surface argument to ShaderDraw.
Definition: ShaderDrawHelper.h:63
This is scalar argument to ShaderDraw.
Definition: ShaderDrawHelper.h:46
ShaderBase(std::vector< Uint8 > &f, int max_x, int max_y)
create surface using vector f as data source.
Definition: ShaderDrawHelper.h:275
ShaderBase(const Surface *s)
create surface using surface s as data source.
Definition: ShaderDrawHelper.h:359
Definition: ShaderDrawHelper.h:565
void mod_range(GraphSubset &g)
function used only when SurfaceType is used as source surface.
ShaderBase(const std::vector< Uint8 > &f, int max_x, int max_y)
create surface using vector f as data source.
Definition: ShaderDrawHelper.h:378