YSTest: ygdi.cpp 源文件

YSTest PreAlpha_b500_20140530 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
ygdi.cpp
浏览该文件的文档.
1 /*
2 © 2009-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 #include "YSLib/Service/YModules.h"
29 #include YFM_YSLib_Service_YGDI
30 #include YFM_YSLib_Service_YBlit
31 #include YFM_YSLib_Service_YPixel
32
33 using namespace ystdex;
34
35 namespace YSLib
36 {
37
38 namespace Drawing
39 {
40
41 Rect
42 operator+(const Rect& r, const Padding& m)
43 {
44 return Rect(r.X + m.Left, r.Y + m.Top,
45 max<int>(0, r.Width - m.Left - m.Right),
46 max<int>(0, r.Height - m.Top - m.Bottom));
47 }
48
49
50 Padding
51 FetchMargin(const Rect& r, const Size& s)
52 {
53 return Padding(r.X, s.Width - r.X - r.Width,
54 r.Y, s.Height - r.Y - r.Height);
55 }
56
57
58 Point
59 ClipBounds(Rect& clip, const Rect& bounds)
60 {
61 if(!clip.IsUnstrictlyEmpty() && Clip(clip, bounds))
62 return clip.GetPoint() - bounds.GetPoint();
63 clip.GetSizeRef() = {};
64 return {};
65 }
66
67 Point
68 ClipMargin(PaintContext& pc, const Padding& m, const Size& ss)
69 {
70 const Size& ds(pc.Target.GetSize());
71
72 if(GetHorizontalOf(m) < ds.Width && GetVerticalOf(m) < ds.Height)
73 {
74 const auto& pt(pc.Location);
75 const Point dp(max<int>(m.Left, pt.X), max<int>(m.Top, pt.Y));
76 const Point sp(dp - pt);
77 const auto scx(min<int>(ss.Width, ds.Width - m.Right - dp.X) - sp.X),
78 scy(min<int>(ss.Height, ds.Height - m.Bottom - dp.Y) - sp.Y);
79
80 if(scx > 0 && scy > 0)
81 {
82 pc.ClipArea &= Rect(dp, scx, scy);
83 return pc.ClipArea.GetPoint() - pt;
84 }
85 }
86 pc.ClipArea.GetSizeRef() = {};
87 return {};
88 }
89
90
91 CompactPixmap::CompactPixmap(ConstBitmapPtr i, SDst w, SDst h)
92 : BasicImage()
93 //不能提前初始化 size ，否则指针非空和面积非零状态不一致。
94 {
95 SetSize(w, h);
96 if(i)
97 std::copy_n(i, GetAreaOf(GetSize()), pBuffer);
98 }
99 CompactPixmap::CompactPixmap(unique_ptr<PixelType[]> p, const Size& s) ynothrow
100 : BasicImage(Graphics(p.release(), s))
101 {}
102 CompactPixmap::CompactPixmap(const CompactPixmap& buf)
103 : BasicImage()
104 {
105 SetSize(buf.GetSize());
106 if(const auto p = buf.GetBufferPtr())
107 std::copy_n(p, GetAreaOf(GetSize()), pBuffer);
108 }
109 CompactPixmap::CompactPixmap(CompactPixmap&& buf) ynothrow
110 : BasicImage(buf)
111 {
112 buf.pBuffer = {};
113 }
114
115 void
116 CompactPixmap::SetContent(ConstBitmapPtr s, SDst w, SDst h)
117 {
118 SetSize(w, h);
119 if(YB_LIKELY(pBuffer && s))
120 std::copy_n(s, GetAreaOf(GetSize()), pBuffer);
121 }
122 void
123 CompactPixmap::SetSize(const Size& s)
124 {
125 const auto area(GetAreaOf(s));
126
127 try
128 {
129 unique_ptr<PixelType[]> p_new(YB_LIKELY(area != 0) ? new PixelType[area]
130 : nullptr);
131 unique_ptr<PixelType[]> p_old(pBuffer);
132
133 pBuffer = p_new.release();
134 }
135 catch(std::bad_alloc&)
136 {
137 throw LoggedEvent("BitmapBuffer allocation failed.", Alert);
138 }
139 YAssert(bool(pBuffer) == (area != 0), "Buffer corruptied.");
140 sGraphics = s,
141 ClearImage();
142 }
143 void
144 CompactPixmap::SetSizeSwap()
145 {
146 std::swap(sGraphics.Width, sGraphics.Height);
147 ClearImage();
148 }
149
150 void
151 CompactPixmap::ClearImage() const
152 {
153 Drawing::ClearImage(*this);
154 }
155
156
157 CompactPixmapEx::CompactPixmapEx(ConstBitmapPtr i, SDst w, SDst h)
158 : CompactPixmap(), pBufferAlpha()
159 {
160 SetSize(w, h);
161 if(i)
162 std::copy_n(i, GetAreaOf(GetSize()), pBuffer);
163 }
164 CompactPixmapEx::CompactPixmapEx(const CompactPixmapEx& buf)
165 : CompactPixmap(), pBufferAlpha()
166 {
167 SetSize(buf.GetSize());
168 if(const auto p = buf.GetBufferPtr())
169 {
170 std::copy_n(p, GetAreaOf(GetSize()), pBuffer),
171 std::copy_n(buf.GetBufferAlphaPtr(), GetAreaOf(GetSize()),
172 pBufferAlpha);
173 }
174 }
175 CompactPixmapEx::CompactPixmapEx(CompactPixmapEx&& buf) ynothrow
176 : CompactPixmap(std::move(buf)), pBufferAlpha(buf.GetBufferAlphaPtr())
177 {
178 buf.pBufferAlpha = {};
179 }
180
181 void
182 CompactPixmapEx::SetSize(const Size& s)
183 {
184 const auto area(GetAreaOf(s));
185
186 try
187 {
188 unique_ptr<PixelType[]> p_new(YB_LIKELY(area != 0) ? new PixelType[area]
189 : nullptr);
190 unique_ptr<AlphaType[]> p_new_alpha(YB_LIKELY(area != 0)
191 ? new AlphaType[area] : nullptr);
192 unique_ptr<PixelType[]> p_old(pBuffer);
193 unique_ptr<AlphaType[]> p_old_alpha(pBufferAlpha);
194
195 pBuffer = p_new.release();
196 pBufferAlpha = p_new_alpha.release();
197 }
198 catch(std::bad_alloc&)
199 {
200 throw LoggedEvent("CompactPixmapEx allocation failed.", Alert);
201 }
202 YAssert(bool(pBuffer) == (area != 0), "Buffer corruptied.");
203 YAssert(bool(pBufferAlpha) == (area != 0), "Buffer corruptied.");
204 sGraphics = s,
205 ClearImage();
206 }
207
208 void
209 CompactPixmapEx::ClearImage() const
210 {
211 const u32 t = GetAreaOf(sGraphics);
212
213 ClearPixel(pBuffer, t);
214 ClearPixel(pBufferAlpha, t);
215 }
216
217 bool
218 CopyTo(BitmapPtr dst, const Graphics& g, const Size& ds,
219 const Point& dp, const Point& sp, const Size& sc, Rotation rot)
220 {
221 if(~rot & 1 && dst && bool(g))
222 {
223 if(rot == RDeg0)
224 BlitLines<false, false>(CopyLine<true>(), dst, g.GetBufferPtr(),
225 ds, g.GetSize(), dp, sp, sc);
226 else
227 BlitLines<true, true>(CopyLine<false>(), dst, g.GetBufferPtr(),
228 ds, g.GetSize(), dp, sp, sc);
229 return true;
230 }
231 return false;
232 }
233 bool
234 CopyTo(BitmapPtr dst, const CompactPixmapEx& buf, const Size& ds,
235 const Point& dp, const Point& sp, const Size& sc, Rotation rot)
236 {
237 if(~rot & 1 && dst && bool(buf))
238 {
239 if(rot == RDeg0)
240 BlitPixels<false, false>(Shaders::BlitTransparentPoint(), dst,
241 IteratorPair(buf.GetBufferPtr(), buf.GetBufferAlphaPtr()), ds,
242 buf.GetSize(), dp, sp, sc);
243 else
244 BlitPixels<true, true>(Shaders::BlitTransparentPoint(), dst,
245 IteratorPair(buf.GetBufferPtr(), buf.GetBufferAlphaPtr()), ds,
246 buf.GetSize(), dp, sp, sc);
247 return true;
248 }
249 return false;
250 }
251
252 bool
253 BlitTo(BitmapPtr dst, const CompactPixmapEx& buf, const Size& ds,
254 const Point& dp, const Point& sp, const Size& sc, Rotation rot)
255 {
256 if(~rot & 1 && dst && bool(buf))
257 {
258 if(rot == RDeg0)
259 BlitPixels<false, false>(Shaders::BlitAlphaPoint(), dst,
260 IteratorPair(buf.GetBufferPtr(), buf.GetBufferAlphaPtr()), ds,
261 buf.GetSize(), dp, sp, sc);
262 else
263 BlitPixels<true, true>(Shaders::BlitAlphaPoint(), dst,
264 IteratorPair(buf.GetBufferPtr(), buf.GetBufferAlphaPtr()), ds,
265 buf.GetSize(), dp, sp, sc);
266 return true;
267 }
268 return false;
269 }
270
271 } // namespace Drawing;
272
273 } // namespace YSLib;
274
YSLib::Drawing::CompactPixmap::SetSize
h void SetSize(const Size &) override
重新设置缓冲区大小。
Definition: ygdi.cpp:123
YSLib::Drawing::Clip
bool Clip(Rect &x, const Rect &y)
剪切操作：取标准矩形交集并判断是否严格非空。
Definition: ygdi.h:134
YSLib::Drawing::CompactPixmap
标准矩形像素图缓冲区。
Definition: ygdi.h:201
YSLib::Drawing::FetchMargin
YF_API Padding FetchMargin(const Rect &, const Size &)
取内边界相对于外边界的边距。
Definition: ygdi.cpp:51
YSLib::u32
std::uint32_t u32
Definition: yadaptor.h:69
YSLib::Drawing::CopyLine< false >
Definition: yblit.h:503
YSLib::Drawing::GetVerticalOf
SDst GetVerticalOf(const Padding &m)
取竖直边距和。
Definition: ygdi.h:116
YSLib::Drawing::Graphics::pBuffer
BitmapPtr pBuffer
显示缓冲区指针。
Definition: ygdibase.h:731
YSLib::Drawing::bounds
bounds
Definition: ydraw.h:188
YSLib::Drawing::ClearPixel
_tOut ClearPixel(_tOut dst, size_t n) ynothrow
清除指定位置的 n 个连续像素。
Definition: yblit.h:424
YSLib::Drawing::ClearImage
YF_API void ClearImage(const Graphics &)
清除图形接口上下文缓冲区。
Definition: yblit.cpp:89
YSLib::Drawing::Size::Height
SDst Height
宽和高。
Definition: ygdibase.h:258
YSLib::Drawing::GetAreaOf
yconstfn auto GetAreaOf(const Size &s) ynothrow-> decltype(s.Width *s.Height)
取面积。
Definition: ygdibase.h:404
YSLib::Drawing::ClipBounds
YF_API Point ClipBounds(Rect &, const Rect &)
根据指定源的边界优化绘制上下文的剪切区域。
Definition: ygdi.cpp:59
platform::SDst
std::uint16_t SDst
屏幕坐标距离。
Definition: Video.h:39
YSLib::Drawing::CompactPixmap::ClearImage
virtual void ClearImage() const
清除缓冲区。
Definition: ygdi.cpp:151
YSLib::Drawing::CompactPixmap::SetSizeSwap
void SetSizeSwap()
交换宽和高；同时清除缓冲区。
Definition: ygdi.cpp:144
YSLib::Drawing::BasicImage
使用 Graphics 定义的基本图像。
Definition: ygdi.h:167
platform::Descriptions::Alert
Definition: ycommon.h:72
YSLib::Drawing::CompactPixmapEx
CompactPixmapEx(ConstBitmapPtr, SDst, SDst)
构造：使用指定位图指针和大小。
YSLib::Drawing::IteratorPair
ystdex::pair_iterator< ConstBitmapPtr, const AlphaType * > IteratorPair
Alpha 光栅化源迭代器对。
Definition: yblit.h:520
ystdex::swap
void swap(any &x, any &y)
交换对象。
Definition: any.h:729
YSLib::Drawing::Padding::Right
SPos Right
Definition: ygdi.h:52
YSLib::Drawing::SetSize
sizeof(AlphaType)*GetAreaOf(GetSize())) using CompactPixmap void SetSize(const Size &) override
重新设置缓冲区大小。
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
YSLib::Drawing::GBinaryGroup::Y
_type Y
分量。
Definition: ygdibase.h:59
YSLib::Drawing::Rect
屏幕标准矩形：表示屏幕矩形区域。
Definition: ygdibase.h:416
YSLib::Drawing::GBinaryGroup< SPos >
YSLib::Drawing::Padding::Top
SPos Top
Definition: ygdi.h:52
YSLib::Drawing::GBinaryGroup::X
_type X
Definition: ygdibase.h:59
YSLib::Drawing::Graphics
二维图形接口上下文。
Definition: ygdibase.h:721
YSLib::Drawing::Shaders::BlitAlphaPoint
像素计算：Alpha 混合。
Definition: YPixel.h:647
YSLib::Drawing::Padding::Left
SPos Left
Definition: ygdi.h:52
YSLib::Drawing::CompactPixmap::SetContent
*this void SetContent(ConstBitmapPtr, SDst, SDst)
设置内容。
Definition: ygdi.cpp:116
YSLib::Drawing::RDeg0
Definition: ygdibase.h:870
Rotation
Rotation
逆时针旋转角度指示输出指向。
Definition: ygdibase.h:868
platform_ex::Windows::s
return s
Definition: MinGW32.cpp:49
YSLib::Drawing::Shaders::BlitTransparentPoint
像素迭代器透明操作。
Definition: YPixel.h:60
YSLib::Drawing::operator+
Rect operator+(const Rect &r, const Padding &m)
Definition: ygdi.cpp:42
YSLib::LoggedEvent
记录日志的异常事件类。
Definition: yexcept.h:58
YSLib::Drawing::ClipMargin
YF_API Point ClipMargin(PaintContext &, const Padding &, const Size &)
根据指定边距和源的大小优化绘制上下文的剪切区域。
Definition: ygdi.cpp:68
YSLib::Drawing::Padding
空白样式。
Definition: ygdi.h:46
YSLib::Drawing::Padding::Bottom
SPos Bottom
Definition: ygdi.h:52
platform::BitmapPtr
PixelType * BitmapPtr
Definition: Video.h:295
YSLib::Drawing::CopyTo
YF_API bool CopyTo(BitmapPtr, const CompactPixmapEx &, const Size &, const Point &, const Point &, const Size &, Rotation=RDeg0)
位图缓冲区向指针指定的缓冲区复制。
Definition: ygdi.cpp:234
YSLib::Drawing::r
bounds & r
Definition: ydraw.h:220
YSLib::Drawing::g
c yconstfn g
Definition: ystyle.h:104
YB_LIKELY
#define YB_LIKELY(expr)
Definition: ydef.h:297
YModules.h
platform::ConstBitmapPtr
const PixelType * ConstBitmapPtr
Definition: Video.h:296
YSLib::Drawing::Size
屏幕区域大小。
Definition: ygdibase.h:249
YSLib::Drawing::CompactPixmap::CompactPixmap
CompactPixmap(unique_ptr< PixelType[]>, const Size &) ynothrow
构造：使用指定位图指针和大小。
Definition: ygdi.cpp:99
YSLib::Drawing::Size::Width
SDst Width
Definition: ygdibase.h:258
platform::AlphaType
ystdex::octet AlphaType
Definition: Video.h:186
YSLib::Drawing::CopyLine
扫描线：按指定扫描顺序复制一行像素。
Definition: yblit.h:490
YAssert
#define YAssert(_expr, _msg)
Definition: cassert.h:73
YSLib::Drawing::BlitTo
YF_API bool BlitTo(BitmapPtr, const CompactPixmapEx &, const Size &, const Point &, const Point &, const Size &, Rotation=RDeg0)
贴图：位图缓冲区向指针指定的缓冲区以贴图算法复制。
Definition: ygdi.cpp:253
YSLib::Drawing::Graphics::sGraphics
Size sGraphics
Definition: ygdibase.h:736