YSTest  PreAlpha_b400_20130424
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
ygdibase.h
浏览该文件的文档.
1 /*
2  Copyright by FrankHB 2011 - 2013.
3 
4  This file is part of the YSLib project, and may only be used,
5  modified, and distributed under the terms of the YSLib project
6  license, LICENSE.TXT. By continuing to use, modify, or distribute
7  this file you indicate that you have read the license and
8  understand and accept it fully.
9 */
10 
28 #ifndef YSL_INC_Core_ygdibase_h_
29 #define YSL_INC_Core_ygdibase_h_ 1
30 
31 #include "yexcept.h"
32 #include <limits>
33 
35 
36 // GDI 基本数据类型和宏定义。
37 
38 YSL_BEGIN_NAMESPACE(Drawing)
39 
40 //前向声明。
41 class Size;
42 class Rect;
43 
44 
49 template<typename _type>
51 {
52 public:
53  static yconstexpr GBinaryGroup Invalid{std::numeric_limits<_type>::lowest(),
54  std::numeric_limits<_type>::lowest()};
55 
56  _type X, Y;
57 
63  yconstfn
64  GBinaryGroup() ynothrow
65  : X(0), Y(0)
66  {}
70  yconstfn DefDeCopyCtor(GBinaryGroup)
75  explicit yconstfn
76  GBinaryGroup(const Size&) ynothrow;
81  explicit yconstfn
82  GBinaryGroup(const Rect&) ynothrow;
87  template<typename _tScalar1, typename _tScalar2>
88  yconstfn
89  GBinaryGroup(_tScalar1 x, _tScalar2 y) ynothrow
90  : X(x), Y(y)
91  {}
92 
97  yconstfn GBinaryGroup
99  {
100  return GBinaryGroup(-X, -Y);
101  }
102 
106  GBinaryGroup&
107  operator+=(const GBinaryGroup& val) ynothrow
108  {
109  yunseq(X += val.X, Y += val.Y);
110  return *this;
111  }
115  GBinaryGroup&
116  operator-=(const GBinaryGroup& val) ynothrow
117  {
118  yunseq(X -= val.X, Y -= val.Y);
119  return *this;
120  }
121 
122  yconstfn DefGetter(const ynothrow, _type, X, X)
123  yconstfn DefGetter(const ynothrow, _type, Y, Y)
124 
125  DefSetter(_type, X, X)
126  DefSetter(_type, Y, Y)
127 
131  yconstfn DefPred(const ynothrow, Zero, X == 0 && Y == 0)
132 
138  _type&
139  GetRef(bool b = true) ynothrow
140  {
141  return b ? X : Y;
142  }
148  const _type&
149  GetRef(bool b = true) const ynothrow
150  {
151  return b ? X : Y;
152  }
153 };
154 
155 template<typename _type>
156 yconstexpr GBinaryGroup<_type> GBinaryGroup<_type>::Invalid;
157 
158 
159 //屏幕二元组二元运算。
160 
165 template<typename _type>
166 yconstfn bool
168 {
169  return a.X == b.X && a.Y == b.Y;
170 }
171 
176 template<typename _type>
177 yconstfn bool
179 {
180  return !(a == b);
181 }
182 
187 template<typename _type>
188 yconstfn GBinaryGroup<_type>
190 {
191  return GBinaryGroup<_type>(a.X + b.X, a.Y + b.Y);
192 }
193 
198 template<typename _type>
199 yconstfn GBinaryGroup<_type>
201 {
202  return GBinaryGroup<_type>(a.X - b.X, a.Y - b.Y);
203 }
204 
209 template<typename _type, typename _tScalar>
210 yconstfn GBinaryGroup<_type>
211 operator*(const GBinaryGroup<_type>& val, _tScalar l) ynothrow
212 {
213  return GBinaryGroup<_type>(val.X * l, val.Y * l);
214 }
215 
216 
222 
223 
229 
230 
236 {
237 public:
242  static const Size Invalid;
243 
244  SDst Width, Height;
245 
251  yconstfn
253  : Width(0), Height(0)
254  {}
259  yconstfn
260  Size(const Size& s) ynothrow
261  : Width(s.Width), Height(s.Height)
262  {}
267  explicit yconstfn
268  Size(const Rect&) ynothrow;
273  template<typename _type>
274  explicit yconstfn
275  Size(const GBinaryGroup<_type>& val) ynothrow
276  : Width(val.X), Height(val.Y)
277  {}
282  template<typename _tScalar1, typename _tScalar2>
283  yconstfn
284  Size(_tScalar1 w, _tScalar2 h) ynothrow
285  : Width(w), Height(h)
286  {}
287 
292  yconstfn PDefHOp(bool, !, ) const ynothrow
293  ImplRet(Width == 0 && Height == 0)
294 
299  explicit yconstfn DefCvt(const ynothrow, bool, Width != 0 || Height != 0)
300 
305  yconstfn DefCvt(const ynothrow, Vec, Vec(Width, Height))
306 
311  yconstfn DefPred(const ynothrow, LineSegment,
312  !((Width == 0) ^ (Height == 0)))
317  yconstfn DefPred(const ynothrow, UnstrictlyEmpty, Width == 0 || Height == 0)
318 
324  SDst&
325  GetRef(bool b = true) ynothrow
326  {
327  return b ? Width : Height;
328  }
334  const SDst&
335  GetRef(bool b = true) const ynothrow
336  {
337  return b ? Width : Height;
338  }
339 };
340 
345 yconstfn bool
346 operator==(const Size& a, const Size& b) ynothrow
347 {
348  return a.Width == b.Width && a.Height == b.Height;
349 }
350 
355 yconstfn bool
356 operator!=(const Size& a, const Size& b) ynothrow
357 {
358  return !(a == b);
359 }
360 
361 
366 template<typename _type>
367 yconstfn GBinaryGroup<_type>
369 {
370  return GBinaryGroup<_type>(val.X + s.Width, val.Y + s.Height);
371 }
372 
373 
378 template<class _tBinary>
379 yconstfn _tBinary
380 Transpose(_tBinary& obj) ynothrow
381 {
382  return _tBinary(obj.Y, obj.X);
383 }
384 
389 yconstfn auto
390 GetAreaOf(const Size& s) ynothrow -> decltype(s.Width * s.Height)
391 {
392  return s.Width * s.Height;
393 }
394 
395 
402 class YF_API Rect : private Point, private Size
403 {
404 public:
409  static const Rect Invalid;
410 
416  using Point::X;
422  using Point::Y;
428  using Size::Width;
434  using Size::Height;
435 
436 
442  yconstfn
444  : Point(), Size()
445  {}
454  explicit yconstfn
455  Rect(const Point& pt) ynothrow
456  : Point(pt), Size()
457  {}
462  yconstfn
463  Rect(const Size& s) ynothrow
464  : Point(), Size(s)
465  {}
470  yconstfn
471  Rect(const Point& pt, const Size& s) ynothrow
472  : Point(pt), Size(s)
473  {}
478  yconstfn
479  Rect(const Point& pt, SDst w, SDst h) ynothrow
480  : Point(pt.X, pt.Y), Size(w, h)
481  {}
486  yconstfn
487  Rect(SPos x, SPos y, const Size& s) ynothrow
488  : Point(x, y), Size(s.Width, s.Height)
489  {}
494  yconstfn
496  : Point(x, y), Size(w, h)
497  {}
498 
501 
502  Rect&
503  operator=(const Point& pt) ynothrow
504  {
505  yunseq(X = pt.X, Y = pt.Y);
506  return *this;
507  }
508  Rect&
509  operator=(const Size& s) ynothrow
510  {
511  yunseq(Width = s.Width, Height = s.Height);
512  return *this;
513  }
515 
521  Rect&
522  operator&=(const Rect&) ynothrow;
523 
529  Rect&
530  operator|=(const Rect&) ynothrow;
531 
537  using Size::operator!;
538 
544  using Size::operator bool;
545 
550  bool
551  Contains(int px, int py) const ynothrow;
556  PDefH(bool, Contains, const Point& pt) const ynothrow
557  ImplRet(Contains(pt.X, pt.Y))
562  bool
563  Contains(const Rect& r) const ynothrow;
564 
569  bool
570  ContainsStrict(int px, int py) const ynothrow;
575  PDefH(bool, ContainsStrict, const Point& pt) const ynothrow
576  ImplRet(ContainsStrict(pt.X, pt.Y))
581  bool
582  ContainsStrict(const Rect& r) const ynothrow;
583 
589  using Size::IsLineSegment;
595  using Size::IsUnstrictlyEmpty;
596 
600  yconstfn DefGetter(const ynothrow, const Point&, Point,
601  static_cast<const Point&>(*this))
606  DefGetter(ynothrow, Point&, PointRef, static_cast<Point&>(*this))
610  yconstfn DefGetter(const ynothrow, const Size&, Size,
611  static_cast<const Size&>(*this))
616  DefGetter(ynothrow, Size&, SizeRef, static_cast<Size&>(*this))
617 };
618 
623 yconstfn bool
624 operator==(const Rect& x, const Rect& y) ynothrow
625 {
626  return x.GetPoint() == y.GetPoint() && x.GetSize() == y.GetSize();
627 }
628 
633 yconstfn bool
634 operator!=(const Rect& x, const Rect& y) ynothrow
635 {
636  return !(x == y);
637 }
638 
639 
644 yconstfn Rect
645 operator+(const Rect& r, const Vec& v) ynothrow
646 {
647  return Rect(r.GetPoint() + v, r.GetSize());
648 }
649 
654 yconstfn Rect
655 operator-(const Rect& r, const Vec& v) ynothrow
656 {
657  return Rect(r.GetPoint() - v, r.GetSize());
658 }
659 
665 Rect
666 operator&(const Rect&, const Rect&) ynothrow;
667 
673 Rect
674 operator|(const Rect&, const Rect&) ynothrow;
675 
676 
677 template<typename _type>
678 yconstfn
679 GBinaryGroup<_type>::GBinaryGroup(const Rect& r) ynothrow
680  : X(r.X), Y(r.Y)
681 {}
682 
683 yconstfn
685  : Width(r.Width), Height(r.Height)
686 {}
687 
688 
694 {
695 public:
696  static const Graphics Invalid;
697 
698 protected:
705 
706 public:
711  explicit yconstfn
712  Graphics(BitmapPtr b = {}, const Size& s = {}) ynothrow
713  : pBuffer(b), size(s)
714  {}
719  yconstfn
721  : pBuffer(g.pBuffer), size(g.size)
722  {}
727 
728 
732  PDefHOp(bool, !, ) const ynothrow
733  ImplRet(!bool(*this))
734 
739  BitmapPtr
740  operator[](size_t) const ynothrow;
741 
746  explicit DefCvt(const ynothrow, bool,
747  pBuffer && size.Width != 0 && size.Height != 0)
748 
749  DefGetter(const ynothrow, BitmapPtr, BufferPtr, pBuffer)
750  DefGetter(const ynothrow, const Size&, Size, size)
751  DefGetter(const ynothrow, SDst, Width, size.Width)
752  DefGetter(const ynothrow, SDst, Height, size.Height)
753  DefGetter(const ynothrow, size_t, SizeOfBuffer,
754  sizeof(PixelType) * GetAreaOf(size))
755 
762  BitmapPtr
763  at(size_t) const ythrow(GeneralEvent, std::out_of_range);
764 };
765 
766 
767 /*
768 \brief 绘制上下文。
769 \warning 非虚析构。
770 \since build 255
771 */
773 {
775 
789 };
790 
791 
796 typedef enum
797 {
798  RDeg0 = 0,
799  RDeg90 = 1,
800  RDeg180 = 2,
802 } Rotation;
803 
808 typedef enum
809 {
812 } Orientation;
813 
814 YSL_END_NAMESPACE(Drawing)
815 
816 YSL_END
817 
818 #endif
819