YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
ygdibase.h
浏览该文件的文档.
1 /*
2  © 2011-2014 FrankHB.
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 "YModules.h"
32 #include YFM_YSLib_Core_YException
33 #include <limits>
34 #include <ystdex/utility.hpp> // for ystdex::cloneable;
35 
36 namespace YSLib
37 {
38 
39 // GDI 基本数据类型和宏定义。
40 
41 namespace Drawing
42 {
43 
44 //前向声明。
45 class Size;
46 class Rect;
47 
48 
53 template<typename _type>
55 {
56 public:
57  static const GBinaryGroup Invalid;
58 
59  _type X, Y;
60 
66  yconstfn
68  : X(0), Y(0)
69  {}
78  explicit yconstfn
79  GBinaryGroup(const Size&) ynothrow;
84  explicit yconstfn
85  GBinaryGroup(const Rect&) ynothrow;
90  template<typename _tScalar1, typename _tScalar2>
91  yconstfn
92  GBinaryGroup(_tScalar1 x, _tScalar2 y) ynothrow
93  : X(x), Y(y)
94  {}
100  template<typename _tPair>
101  yconstfn
102  GBinaryGroup(const _tPair& pr) ynothrow
103  : X(std::get<0>(pr)), Y(std::get<1>(pr))
104  {}
105 
112  {
113  return GBinaryGroup(-X, -Y);
114  }
115 
119  GBinaryGroup&
121  {
122  yunseq(X += val.X, Y += val.Y);
123  return *this;
124  }
128  GBinaryGroup&
130  {
131  yunseq(X -= val.X, Y -= val.Y);
132  return *this;
133  }
134 
135  yconstfn DefGetter(const ynothrow, _type, X, X)
137 
138  DefSetter(_type, X, X)
139  DefSetter(_type, Y, Y)
140 
144  yconstfn DefPred(const ynothrow, Zero, X == 0 && Y == 0)
145 
151  _type&
152  GetRef(bool b = true) ynothrow
153  {
154  return b ? X : Y;
155  }
161  const _type&
162  GetRef(bool b = true) const ynothrow
163  {
164  return b ? X : Y;
165  }
166 };
167 
168 template<typename _type>
169 const GBinaryGroup<_type> GBinaryGroup<_type>::Invalid{
170  std::numeric_limits<_type>::lowest(), std::numeric_limits<_type>::lowest()};
171 
172 
173 //屏幕二元组二元运算。
174 
179 template<typename _type>
180 yconstfn bool
182 {
183  return a.X == b.X && a.Y == b.Y;
184 }
185 
190 template<typename _type>
191 yconstfn bool
193 {
194  return !(a == b);
195 }
196 
201 template<typename _type>
204 {
205  return GBinaryGroup<_type>(a.X + b.X, a.Y + b.Y);
206 }
207 
212 template<typename _type>
215 {
216  return GBinaryGroup<_type>(a.X - b.X, a.Y - b.Y);
217 }
218 
223 template<typename _type, typename _tScalar>
226 {
227  return GBinaryGroup<_type>(val.X * l, val.Y * l);
228 }
229 
230 
236 
237 
243 
244 
250 {
251 public:
256  static const Size Invalid;
257 
258  SDst Width, Height;
259 
265  yconstfn
267  : Width(0), Height(0)
268  {}
273  yconstfn
274  Size(const Size& s) ynothrow
275  : Width(s.Width), Height(s.Height)
276  {}
281  explicit yconstfn
282  Size(const Rect&) ynothrow;
287  template<typename _type>
288  explicit yconstfn
289  Size(const GBinaryGroup<_type>& val) ynothrow
290  : Width(val.X), Height(val.Y)
291  {}
296  template<typename _tScalar1, typename _tScalar2>
297  yconstfn
298  Size(_tScalar1 w, _tScalar2 h) ynothrow
299  : Width(w), Height(h)
300  {}
301 
306  yconstfn PDefHOp(bool, !, ) const ynothrow
307  ImplRet(Width == 0 && Height == 0)
308 
313  explicit yconstfn DefCvt(const ynothrow, bool, Width != 0 || Height != 0)
314 
319  yconstfn DefCvt(const ynothrow, Vec, Vec(Width, Height))
320 
325  yconstfn DefPred(const ynothrow, LineSegment,
326  !((Width == 0) ^ (Height == 0)))
331  yconstfn DefPred(const ynothrow, UnstrictlyEmpty, Width == 0 || Height == 0)
332 
338  SDst&
339  GetRef(bool b = true) ynothrow
340  {
341  return b ? Width : Height;
342  }
348  const SDst&
349  GetRef(bool b = true) const ynothrow
350  {
351  return b ? Width : Height;
352  }
353 };
354 
359 yconstfn bool
360 operator==(const Size& a, const Size& b) ynothrow
361 {
362  return a.Width == b.Width && a.Height == b.Height;
363 }
364 
369 yconstfn bool
370 operator!=(const Size& a, const Size& b) ynothrow
371 {
372  return !(a == b);
373 }
374 
375 
380 template<typename _type>
383 {
384  return GBinaryGroup<_type>(val.X + s.Width, val.Y + s.Height);
385 }
386 
387 
392 template<class _tBinary>
393 yconstfn _tBinary
394 Transpose(_tBinary& obj) ynothrow
395 {
396  return _tBinary(obj.Y, obj.X);
397 }
398 
403 yconstfn auto
404 GetAreaOf(const Size& s) ynothrow -> decltype(s.Width * s.Height)
405 {
406  return s.Width * s.Height;
407 }
408 
409 
416 class YF_API Rect : private Point, private Size
417 {
418 public:
423  static const Rect Invalid;
424 
430  using Point::X;
436  using Point::Y;
442  using Size::Width;
448  using Size::Height;
449 
450 
460  explicit yconstfn
461  Rect(const Point& pt) ynothrow
462  : Point(pt), Size()
463  {}
468  yconstfn
469  Rect(const Size& s) ynothrow
470  : Point(), Size(s)
471  {}
476  yconstfn
477  Rect(const Point& pt, const Size& s) ynothrow
478  : Point(pt), Size(s)
479  {}
484  yconstfn
485  Rect(const Point& pt, SDst w, SDst h) ynothrow
486  : Point(pt.X, pt.Y), Size(w, h)
487  {}
492  yconstfn
493  Rect(SPos x, SPos y, const Size& s) ynothrow
494  : Point(x, y), Size(s.Width, s.Height)
495  {}
500  yconstfn
502  : Point(x, y), Size(w, h)
503  {}
508 
511 
512  Rect&
513  operator=(const Point& pt) ynothrow
514  {
515  yunseq(X = pt.X, Y = pt.Y);
516  return *this;
517  }
518  Rect&
519  operator=(const Size& s) ynothrow
520  {
521  yunseq(Width = s.Width, Height = s.Height);
522  return *this;
523  }
525 
531  Rect&
532  operator&=(const Rect&) ynothrow;
533 
539  Rect&
540  operator|=(const Rect&) ynothrow;
541 
547  using Size::operator!;
548 
554  using Size::operator bool;
555 
560  bool
561  Contains(int px, int py) const ynothrow;
566  PDefH(bool, Contains, const Point& pt) const ynothrow
567  ImplRet(Contains(pt.X, pt.Y))
573  bool
574  Contains(const Rect&) const ynothrow;
575 
580  bool
581  ContainsStrict(int px, int py) const ynothrow;
586  PDefH(bool, ContainsStrict, const Point& pt) const ynothrow
587  ImplRet(ContainsStrict(pt.X, pt.Y))
593  bool
594  ContainsStrict(const Rect&) const ynothrow;
595 
601  using Size::IsLineSegment;
607  using Size::IsUnstrictlyEmpty;
608 
612  yconstfn DefGetter(const ynothrow, const Point&, Point,
613  static_cast<const Point&>(*this))
618  DefGetter(ynothrow, Point&, PointRef, static_cast<Point&>(*this))
622  yconstfn DefGetter(const ynothrow, const Size&, Size,
623  static_cast<const Size&>(*this))
628  DefGetter(ynothrow, Size&, SizeRef, static_cast<Size&>(*this))
629 };
630 
632 
633 
637 yconstfn bool
638 operator==(const Rect& x, const Rect& y) ynothrow
639 {
640  return x.GetPoint() == y.GetPoint() && x.GetSize() == y.GetSize();
641 }
642 
647 yconstfn bool
648 operator!=(const Rect& x, const Rect& y) ynothrow
649 {
650  return !(x == y);
651 }
652 
653 
659 operator+(const Rect& r, const Vec& v) ynothrow
660 {
661  return Rect(r.GetPoint() + v, r.GetSize());
662 }
663 
669 operator-(const Rect& r, const Vec& v) ynothrow
670 {
671  return Rect(r.GetPoint() - v, r.GetSize());
672 }
673 
679 YF_API Rect
680 operator&(const Rect&, const Rect&) ynothrow;
681 
687 YF_API Rect
688 operator|(const Rect&, const Rect&) ynothrow;
689 
695 inline void
696 Diminish(Rect& r, SDst off1 = 1, SDst off2 = 2)
697 {
698  YAssert(r.Width > off2 && r.Height > off2, "Boundary is too small.");
699  yunseq(r.X += off1, r.Y += off1, r.Width -= off2, r.Height -= off2);
700 }
702 
703 
704 template<typename _type>
705 yconstfn
707  : X(r.X), Y(r.Y)
708 {}
709 
710 yconstfn
712  : Width(r.Width), Height(r.Height)
713 {}
714 
715 
722 {
723 public:
724  static const Graphics Invalid;
725 
726 protected:
732  /*
733  \brief 图形区域大小。
734  \since build 405
735  */
737 
738 public:
744  : Graphics({})
745  {}
750  explicit yconstfn
751  Graphics(BitmapPtr b, const Size& s = {}) ynothrow
752  : pBuffer(b), sGraphics(s)
753  {}
758  yconstfn
760  : pBuffer(g.pBuffer), sGraphics(g.sGraphics)
761  {}
766 
767 
771  PDefHOp(bool, !, ) const ynothrow
772  ImplRet(!bool(*this))
773 
778  BitmapPtr
779  operator[](size_t) const ynothrow;
780 
785  explicit DefCvt(const ynothrow, bool,
786  pBuffer && sGraphics.Width != 0 && sGraphics.Height != 0)
787 
788  DefGetter(const ynothrow, BitmapPtr, BufferPtr, pBuffer)
789  DefGetter(const ynothrow, const Size&, Size, sGraphics)
790  DefGetter(const ynothrow, SDst, Width, sGraphics.Width)
791  DefGetter(const ynothrow, SDst, Height, sGraphics.Height)
792  DefGetter(const ynothrow, size_t, SizeOfBuffer,
793  sizeof(PixelType) * GetAreaOf(sGraphics))
794 
801  BitmapPtr
802  at(size_t) const ythrow(GeneralEvent, std::out_of_range);
803 };
804 
805 
810 DeclDerivedI(YF_API, IImage, ystdex::cloneable)
812  DeclIEntry(const Graphics& GetContext() const ynothrow)
813  DeclIEntry(void SetSize(const Size&))
814 
816  DeclIEntry(IImage* clone() const override)
817 EndDecl
818 
819 
820 /*
821 \brief 绘制上下文。
822 \warning 非虚析构。
823 \since build 255
824 */
825 struct YF_API PaintContext
826 {
827  Graphics Target;
828 
834  Point Location;
841  Rect ClipArea;
842 };
843 
850 inline void
851 UpdateClipArea(PaintContext& pc, const Rect& r)
852 {
853  pc.ClipArea = r & pc.Target.GetSize();
854 }
855 
856 inline void
857 UpdateClipSize(PaintContext& pc, const Size& s)
858 {
859  UpdateClipArea(pc, {pc.Location, s});
860 }
862 
863 
869 {
870  RDeg0 = 0,
871  RDeg90 = 1,
872  RDeg180 = 2,
874 };
875 
881 {
884 };
885 
886 } // namespace Drawing;
887 
888 } // namespace YSLib;
889 
890 #endif
891 
实用设施。
yconstfn Rect(SPos x, SPos y, SDst w, SDst h) ynothrow
构造:使用表示位置的两个 SPos 值和表示大小的两个 SDst 值。
Definition: ygdibase.h:501
#define DefDeDtor(_t)
定义默认析构函数。
Definition: YBaseMacro.h:146
bool return true
Definition: DSMain.cpp:177
yconstfn Graphics(BitmapPtr b, const Size &s={}) ynothrow
构造:使用指定位图指针和大小。
Definition: ygdibase.h:751
#define ImplRet(...)
Definition: YBaseMacro.h:97
#define DefPred(_q, _n,...)
Definition: YBaseMacro.h:172
static const Rect Invalid
无效对象。
Definition: ygdibase.h:423
static const Graphics Invalid
无效图形接口上下文。
Definition: ygdibase.h:724
BitmapPtr pBuffer
显示缓冲区指针。
Definition: ygdibase.h:731
#define DefDeCopyCtor(_t)
Definition: YBaseMacro.h:136
yconstfn GBinaryGroup() ynothrow
无参数构造。
Definition: ygdibase.h:67
yconstfn Size(_tScalar1 w, _tScalar2 h) ynothrow
构造:使用两个纯量。
Definition: ygdibase.h:298
#define YF_API
Definition: Platform.h:64
SDst Height
宽和高。
Definition: ygdibase.h:258
static const GBinaryGroup Invalid
无效(不在屏幕坐标系中)对象。
Definition: ygdibase.h:57
yconstfn auto GetAreaOf(const Size &s) ynothrow-> decltype(s.Width *s.Height)
取面积。
Definition: ygdibase.h:404
yconstfn GBinaryGroup(const _tPair &pr) ynothrow
构造:使用纯量对。
Definition: ygdibase.h:102
GBinaryGroup & operator-=(const GBinaryGroup &val) ynothrow
减法赋值。
Definition: ygdibase.h:129
std::uint16_t SDst
屏幕坐标距离。
Definition: Video.h:39
std::int16_t SPos
屏幕坐标度量。
Definition: Video.h:38
CompactPixmapEx & operator=(const CompactPixmapEx &buf)
Definition: ygdi.h:328
yconstfn bool operator==(const GBinaryGroup< _type > &a, const GBinaryGroup< _type > &b) ynothrow
比较:屏幕二元组相等关系。
Definition: ygdibase.h:181
yconstfn GBinaryGroup operator-() const ynothrow
负运算:取加法逆元。
Definition: ygdibase.h:111
std::runtime_error GeneralEvent
一般运行时异常事件类。
Definition: yexcept.h:51
sizeof(AlphaType)*GetAreaOf(GetSize())) using CompactPixmap void SetSize(const Size &) override
重新设置缓冲区大小。
GBinaryGroup< SPos > Point
屏幕二维点(直角坐标表示)。
Definition: ygdibase.h:235
#define yunseq
无序列依赖表达式组求值。
Definition: ydef.h:748
yconstfn Size(const Size &s) ynothrow
复制构造。
Definition: ygdibase.h:274
yconstfn Rect(const Point &pt, SDst w, SDst h) ynothrow
构造:使用屏幕二维点和表示长宽的两个 SDst 值。
Definition: ygdibase.h:485
GBinaryGroup & operator+=(const GBinaryGroup &val) ynothrow
加法赋值。
Definition: ygdibase.h:120
static const Size Invalid
无效对象。
Definition: ygdibase.h:256
#define ynothrow
YSLib 无异常抛出保证:若支持 noexcept 关键字, 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
yconstfn Y DefSetter(_type, X, X) DefSetter(_type
屏幕标准矩形:表示屏幕矩形区域。
Definition: ygdibase.h:416
Orientation
二元方向。
Definition: ygdibase.h:880
yconstfn DefGetter(const ynothrow, _type, X, X) yconstfn DefGetter(const ynothrow
#define EndDecl
Definition: YBaseMacro.h:316
#define ythrow(...)
YSLib 动态异常规范:根据是否使用异常规范宏指定或忽略动态异常规范。
Definition: ydef.h:476
屏幕二元组。
Definition: ygdibase.h:54
DeclDerivedI(YF_API, IImage, ystdex::cloneable) DeclIEntry(const Graphics &GetContext() const ynothrow) DeclIEntry(void SetSize(const Size &)) DeclIEntry(IImage *clone() const override) EndDeclstruct YF_API PaintContext
图像接口。
Definition: ygdibase.h:810
yconstfn GBinaryGroup< _type > operator+(const GBinaryGroup< _type > &a, const GBinaryGroup< _type > &b) ynothrow
加法:屏幕二元组。
Definition: ygdibase.h:203
yconstfn _tBinary Transpose(_tBinary &obj) ynothrow
二元对象转置。
Definition: ygdibase.h:394
yconstfn DefDeCopyCtor(GBinaryGroup) explicit yconstfn GBinaryGroup(const Size &) ynothrow
复制构造:默认实现。
yconstfn Rect(const Size &s) ynothrow
构造:使用 Size 对象。
Definition: ygdibase.h:469
yconstfn Rect(const Point &pt, const Size &s) ynothrow
构造:使用屏幕二维点和 Size 对象。
Definition: ygdibase.h:477
二维图形接口上下文。
Definition: ygdibase.h:721
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:463
yconstfn GBinaryGroup< _type > operator*(const GBinaryGroup< _type > &val, _tScalar l) ynothrow
数乘:屏幕二元组。
Definition: ygdibase.h:225
p1 p1 Y
Definition: ydraw.h:188
Rect operator&(const Rect &a, const Rect &b) ynothrow
Definition: ygdibase.cpp:128
Selected const shared_ptr< ListType > const pair< Color, Color > viewer Contains
Definition: textlist.h:124
yconstfn Rect operator+(const Rect &r, const Vec &v) ynothrow
加法:使用标准矩形 r 和偏移向量 v 构造屏幕标准矩形。
Definition: ygdibase.h:659
Rotation
逆时针旋转角度指示输出指向。
Definition: ygdibase.h:868
yconstfn bool operator!=(const Rect &x, const Rect &y) ynothrow
比较:屏幕标准矩形不等关系。
Definition: ygdibase.h:648
yconstfn Rect operator-(const Rect &r, const Vec &v) ynothrow
减法:使用标准矩形 r 和偏移向量的加法逆元 v 构造屏幕标准矩形。
Definition: ygdibase.h:669
bool operator!=(const HMultiBitmap::iterator &x, const HMultiBitmap::iterator &y) ynothrow
Definition: Image.h:605
#define DefCvt(_q, _t,...)
Definition: YBaseMacro.h:164
yconstfn Size() ynothrow
无参数构造。
Definition: ygdibase.h:266
PixelType * BitmapPtr
Definition: Video.h:295
bounds & r
Definition: ydraw.h:220
if(YB_UNLIKELY(r >=sGraphics.Height)) throw std return pBuffer r *sGraphics Width
Definition: ygdibase.cpp:155
#define DeclIEntry(_sig)
Definition: YBaseMacro.h:314
PDefH(void, DrawLineSeg, const Graphics &g, const Rect &bounds, const Point &p1, const Point &p2, Color c) ImplExpr(DrawLineSeg(g
c yconstfn g
Definition: ystyle.h:104
DefGetter(const ynothrow, AlphaType *, BufferAlphaPtr, pBufferAlpha) DefGetter(const ynothrow
取 Alpha 缓冲区的指针。
Graphics() ynothrow
默认构造:使用空指针和大小。
Definition: ygdibase.h:743
yconstfn Graphics(const Graphics &g) ynothrow
复制构造:浅复制。
Definition: ygdibase.h:759
屏幕区域大小。
Definition: ygdibase.h:249
yconstfn DefDeCtor(Rect) explicit yconstfn Rect(const Point &pt) ynothrow
无参数构造:默认实现。
Definition: ygdibase.h:455
yconstfn GBinaryGroup< _type > operator-(const GBinaryGroup< _type > &a, const GBinaryGroup< _type > &b) ynothrow
减法:屏幕二元组。
Definition: ygdibase.h:214
byte v
#define DefDeCopyAssignment(_t)
Definition: YBaseMacro.h:154
yconstfn Rect(SPos x, SPos y, const Size &s) ynothrow
构造:使用表示位置的两个 SPos 值和 Size 对象。
Definition: ygdibase.h:493
#define YAssert(_expr, _msg)
Definition: cassert.h:73