YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
Video.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 YCL_INC_Video_h_
29 #define YCL_INC_Video_h_ 1
30 
31 #include "YModules.h"
32 #include YFM_YCLib_YCommon
33 #include <ystdex/cstdint.hpp>
34 
35 namespace platform
36 {
37 
38 using SPos = std::int16_t;
39 using SDst = std::uint16_t;
40 
41 
46 template<size_t _vX, size_t _vY, size_t _vZ, size_t _vA>
47 struct XYZATrait
48 {
49  static yconstexpr size_t ABitsN = _vA;
50  static yconstexpr size_t XBitsN = _vX;
51  static yconstexpr size_t YBitsN = _vY;
52  static yconstexpr size_t ZBitsN = _vZ;
53  static yconstexpr size_t XYBitsN = XBitsN + YBitsN;
54  static yconstexpr size_t XYZBitsN = XBitsN + YBitsN + ZBitsN;
55  static yconstexpr size_t BitsN = XBitsN + YBitsN + ZBitsN + ABitsN;
56  static yconstexpr size_t BytesN = (BitsN + CHAR_BIT - 1) / CHAR_BIT;
57  static yconstexpr bool GAligned = XBitsN / CHAR_BIT == XYBitsN / CHAR_BIT;
58  static yconstexpr bool RAligned = XYBitsN / CHAR_BIT == XYZBitsN / CHAR_BIT;
59  static yconstexpr size_t UIntBitsN = BitsN > 64 ? 64 : BitsN;
60 
65  using IntegerType
67 
68  static yconstexpr IntegerType AMask = ((1 << ABitsN) - 1) << XYZBitsN;
69  static yconstexpr IntegerType XMask = (1 << XBitsN) - 1;
70  static yconstexpr IntegerType YMask = ((1 << YBitsN) - 1) << XBitsN;
71  static yconstexpr IntegerType ZMask = ((1 << ZBitsN) - 1) << XYBitsN;
73 
75 };
76 
77 
86 template<size_t _vB, size_t _vG, size_t _vR, size_t _vA>
87 union YB_ATTR(packed) YB_ATTR(
88  aligned(yalignof(typename XYZATrait<_vB, _vG, _vR, _vA>::IntegerType))) BGRA
89 {
90  using Trait = XYZATrait<_vB, _vG, _vR, _vA>;
91 
92  static_assert(Trait::BitsN <= 64,
93  "Width larger than 64 bits is unimplemented");
94 
95  typename Trait::ArrayType Bytes;
96  typename Trait::IntegerType Integer;
97 
98 //#if !LITTLE_ENDIAN
99 //# error Unsupported integer endianness found.
100 //#endif
101 
103  yconstfn
104  BGRA(typename Trait::IntegerType i)
105  : Integer(i)
106  {}
107  yconstfn
108  BGRA(typename Trait::BType b, typename Trait::GType g,
109  typename Trait::RType r, typename Trait::AType a)
110  : Integer(b | g << Trait::XBitsN | r << Trait::XYBitsN
111  | a << Trait::XYZBitsN)
112  {}
113 
115  yconstfn DefCvt(const ynothrow, typename Trait::IntegerType, Integer)
116 
117 
118  yconstfn DefGetter(const ynothrow, typename Trait::AType, A,
119  (Integer & Trait::AMask) >> Trait::XYZBitsN)
120  yconstfn DefGetter(const ynothrow, typename Trait::BType, B,
121  Integer & Trait::XMask)
123  yconstfn DefGetter(const ynothrow, typename Trait::GType, G,
124  (Integer & Trait::YMask) >> Trait::XBitsN)
126  yconstfn DefGetter(const ynothrow, typename Trait::RType, R,
127  (Integer & Trait::ZMask) >> Trait::XYBitsN)
128 };
129 
130 
139 template<size_t _vR, size_t _vG, size_t _vB, size_t _vA>
140 union YB_ATTR(packed) YB_ATTR(
141  aligned(yalignof(typename XYZATrait<_vB, _vG, _vR, _vA>::IntegerType))) RGBA
142 {
143  using Trait = XYZATrait<_vR, _vG, _vB, _vA>;
144 
145  static_assert(Trait::BitsN <= 64, "Width larger than 64 unimplemented");
146 
147  typename Trait::ArrayType Bytes;
148  typename Trait::IntegerType Integer;
149 
150 //#if !LITTLE_ENDIAN
151 //# error Unsupported integer endianness found.
152 //#endif
153 
154  DefDeCtor(RGBA)
155  yconstfn
156  RGBA(typename Trait::IntegerType i)
157  : Integer(i)
158  {}
159  yconstfn
160  RGBA(typename Trait::BType r, typename Trait::GType g,
161  typename Trait::RType b, typename Trait::AType a)
162  : Integer(r | g << Trait::XBitsN | b << Trait::XYBitsN
163  | a << Trait::XYZBitsN)
164  {}
165 
167  yconstfn DefCvt(const ynothrow, typename Trait::IntegerType, Integer)
168 
169 
170  yconstfn DefGetter(const ynothrow, typename Trait::AType, A,
171  (Integer & Trait::AMask) >> Trait::XYZBitsN)
172  yconstfn DefGetter(const ynothrow, typename Trait::BType, B,
173  (Integer & Trait::ZMask) >> Trait::XYBitsN)
175  yconstfn DefGetter(const ynothrow, typename Trait::GType, G,
176  (Integer & Trait::YMask) >> Trait::XBitsN)
178  yconstfn DefGetter(const ynothrow, typename Trait::RType, R,
179  Integer & Trait::XMask)
180 };
181 
182 
184 
185 using MonoType = ystdex::octet;
186 using AlphaType = ystdex::octet;
188 
189 #if YCL_DS
190 
195 # define YCL_PIXEL_FORMAT_XYZ555 0xAABBCCDD
196 
202 using PixelType = RGBA<5, 5, 5, 1>;
208 yconstfn PDefH(AlphaType, FetchAlpha, PixelType px) ynothrow
209  ImplRet(px.GetA() != 0 ? 0xFF : 0)
210 
216 yconstfn PDefH(PixelType, FetchOpaque, PixelType px) ynothrow
217  ImplRet(px.Integer | 1 << 15)
218 
219 
223 yconstfn PDefH(std::uint16_t, FetchPixel, MonoType r, MonoType g, MonoType b)
224  ynothrow
225  ImplRet(r >> 3 | std::uint16_t(g >> 3) << 5 | std::uint16_t(b >> 3) << 10)
226 
227 # define DefColorH_(hex, name) name = \
228  (FetchPixel(((hex) >> 16) & 0xFF, ((hex) >> 8) & 0xFF, (hex) & 0xFF) \
229  | 1 << 15)
230 #elif YCL_Win32 || YCL_Android
231 
236 # define YCL_PIXEL_FORMAT_XYZ888 0xAADDCCBB
237 
248 using PixelType = BGRA<8, 8, 8, 8>;
249 
254 yconstfn PDefH(AlphaType, FetchAlpha, PixelType px) ynothrow
255  ImplRet(px.GetA())
256 
262 yconstfn PDefH(PixelType, FetchOpaque, PixelType px) ynothrow
263  ImplRet({px.GetB(), px.GetG(), px.GetR(), 0xFF})
264 
270 yconstfn PDefH(std::uint32_t, FetchPixel,
271  AlphaType r, AlphaType g, AlphaType b) ynothrow
272  ImplRet(r | g << 8 | std::uint32_t(b) << 16)
273 
274 # if YB_HAS_CONSTEXPR
276 # define YCL_FetchPixel(r, g, b) platform::FetchPixel(r, g, b)
277 # else
278 # define YCL_FetchPixel(r, g, b) ((r) | (g) << 8 | std::uint32_t(b) << 16)
279 # endif
280 
288 # define DefColorH_(hex, name) \
289  name = (YCL_FetchPixel((((hex) >> 16) & 0xFF), \
290  (((hex) >> 8) & 0xFF), ((hex) & 0xFF)) << 8 | 0xFF)
291 #else
292 # error "Unsupported platform found."
293 #endif
294 
295 using BitmapPtr = PixelType*;
296 using ConstBitmapPtr = const PixelType*;
297 
298 
300 namespace ColorSpace
301 {
302 // #define DefColorA(r, g, b, name) name = ARGB16(1, r, g, b),
303 #define HexAdd0x(hex) 0x##hex
304 #define DefColorH(hex_, name) DefColorH_(HexAdd0x(hex_), name)
305 
311 enum ColorSet : PixelType::Trait::IntegerType
312 {
313  DefColorH(00FFFF, Aqua),
314  DefColorH(000000, Black),
315  DefColorH(0000FF, Blue),
316  DefColorH(FF00FF, Fuchsia),
317  DefColorH(808080, Gray),
318  DefColorH(008000, Green),
319  DefColorH(00FF00, Lime),
320  DefColorH(800000, Maroon),
321  DefColorH(000080, Navy),
322  DefColorH(808000, Olive),
323  DefColorH(800080, Purple),
324  DefColorH(FF0000, Red),
325  DefColorH(C0C0C0, Silver),
326  DefColorH(008080, Teal),
327  DefColorH(FFFFFF, White),
328  DefColorH(FFFF00, Yellow)
329 };
330 
331 #undef YCL_FetchPixel
332 #undef DefColorH
333 #undef DefColorH_
334 #undef HexAdd0x
335 } // namespace ColorSpace;
336 
337 
340 {
341 public:
343 
344 private:
349  MonoType r, g, b;
355 
356 public:
361  yconstfn
362  Color() ynothrow
363  : r(0), g(0), b(0), a(0)
364  {}
369  yconstfn
370  Color(PixelType px) ynothrow
371 #if YCL_DS
372  : r(px.GetR() << 3), g(px.GetG() << 3), b(px.GetB() << 3),
373  a(FetchAlpha(px) ? 0xFF : 0x00)
374 #elif YCL_Win32 || YCL_Android
375  : r(px.GetR()), g(px.GetG()), b(px.GetB()), a(px.GetA())
376 #endif
377  {}
382  yconstfn
383  Color(ColorSet cs) ynothrow
384 #if YCL_DS
385  : Color(PixelType(cs))
386 #elif YCL_Win32 || YCL_Android
387  : r((cs & 0xFF00) >> 8), g((cs & 0xFF0000) >> 16),
388  b((cs & 0xFF000000) >> 24), a(0xFF)
389 #endif
390  {}
395  yconstfn
396  Color(MonoType r_, MonoType g_, MonoType b_, AlphaType a_ = 0xFF) ynothrow
397  : r(r_), g(g_), b(b_), a(a_)
398  {}
404  template<typename _tScalar>
405  yconstfn
406  Color(_tScalar r_, _tScalar g_, _tScalar b_, AlphaType a_ = 0xFF) ynothrow
407  : Color(MonoType(r_), MonoType(g_), MonoType(b_), a_)
408  {}
409 
414  yconstfn
415  operator PixelType() const ynothrow
416  {
417 #if YCL_DS
418  return int(a != 0) << 15 | FetchPixel(r, g, b);
419 #elif YCL_Win32 || YCL_Android
420  return {b, g, r, a};
421 #endif
422  }
423 
428  yconstfn DefGetter(const ynothrow, MonoType, A, a)
433  yconstfn DefGetter(const ynothrow, MonoType, B, b)
438  yconstfn DefGetter(const ynothrow, MonoType, G, g)
443  yconstfn DefGetter(const ynothrow, MonoType, R, r)
444 };
445 
446 
451 namespace Consoles
452 {
453 
458 enum Color
459 {
460  Black = 0,
476 };
477 
484  ColorSpace::Green, ColorSpace::Teal, ColorSpace::Maroon, ColorSpace::Purple,
485  ColorSpace::Olive, ColorSpace::Silver, ColorSpace::Gray, ColorSpace::Blue,
486  ColorSpace::Lime, ColorSpace::Aqua, ColorSpace::Red, ColorSpace::Yellow,
487  ColorSpace::Fuchsia, ColorSpace::White};
488 
489 } // namespace Consoles;
490 
496 YF_API void
497 YConsoleInit(std::uint8_t dspIndex, Color fc = ColorSpace::White,
498  Color bc = ColorSpace::Black);
499 
500 
505 YF_API bool
506 InitVideo();
507 
508 } // namespace platform;
509 
510 
511 namespace platform_ex
512 {
513 
514 #if YCL_DS
515 
518 YF_API void
519 ResetVideo();
520 
521 
526 InitScrUp(int&);
527 
532 InitScrDown(int&);
533 
539 YF_API void
540 ScreenSynchronize(platform::PixelType*, const platform::PixelType*) ynothrow;
541 #endif
542 
543 #if YCL_DS || YF_Hosted
544 
549 class YF_API DSVideoState
550 {
551 #if YF_Hosted
552 private:
553  bool LCD_main_on_top = true;
554 #endif
555 
556 public:
557  bool
558  IsLCDMainOnTop() const;
559 
560  void
561  SetLCDMainOnTop(bool);
562 
563  void
564  SwapLCD();
565 };
566 #endif
567 
568 } // namespace platform_ex;
569 
570 #endif
571 
#define DefColorH(hex_, name)
Definition: Video.h:304
ystdex::byte[BytesN] ArrayType
Definition: Video.h:74
static yconstexpr size_t BitsN
Definition: Video.h:55
YF_API bool InitVideo()
初始化视频输出。
Definition: Video.cpp:133
取指定整数类型的位宽度。
Definition: cstdint.hpp:43
static yconstexpr size_t BytesN
Definition: Video.h:56
yconstfn Color(ColorSet cs) ynothrow
构造:使用默认颜色。
Definition: Video.h:383
typename ystdex::make_width_int< ZBitsN >::unsigned_least_type RType
Definition: Video.h:64
yconstfn Color() ynothrow
无参数构造:所有分量为 0 的默认颜色。
Definition: Video.h:362
MonoType r
RGB 分量。
Definition: Video.h:349
#define YF_API
Definition: Platform.h:64
unsigned char byte
字节类型。
Definition: ydef.h:555
static yconstexpr size_t ZBitsN
Definition: Video.h:52
std::uint16_t SDst
屏幕坐标距离。
Definition: Video.h:39
std::int16_t SPos
屏幕坐标度量。
Definition: Video.h:38
union _vA::IntegerType RGBA
Definition: Video.h:142
XYZA 特征。
Definition: Video.h:47
static yconstexpr bool RAligned
Definition: Video.h:58
yconstfn Integer &Trait::AMask Trait::XYZBitsN yconstfn DefGetter(const ynothrow, typename Trait::BType, B, Integer &Trait::XMask) yconstfn DefGetter(const ynothrow
yconstfn DefCvt(const ynothrow, typename Trait::IntegerType, Integer) yconstfn DefGetter(const ynothrow
yconstexpr platform::Color ConsoleColors[]
控制台颜色。
Definition: Video.h:483
std::size_t size ynothrow
union _vA::IntegerType BGRA
Definition: Video.h:89
static yconstexpr size_t ABitsN
Definition: Video.h:49
ColorSet
默认颜色集。
Definition: Video.h:311
typename ystdex::make_width_int< YBitsN >::unsigned_least_type GType
Definition: Video.h:63
typename ystdex::make_width_int< ABitsN >::unsigned_least_type AType
Definition: Video.h:61
static yconstexpr size_t UIntBitsN
Definition: Video.h:59
Trait::IntegerType Integer
Definition: Video.h:96
AlphaType a
Alpha 分量。
Definition: Video.h:354
ystdex::octet MonoType
Definition: Video.h:185
#define yalignof(_type)
指定特定类型的对齐。
Definition: ydef.h:442
YF_API void YConsoleInit(std::uint8_t dspIndex, Color fc=ColorSpace::White, Color bc=ColorSpace::Black)
启动控制台。
yconstexpr const KeyInput KeyIndex ynothrow ImplRet(char()) namespace KeyCodes
本机按键编码。
Definition: Keys.h:191
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:463
yconstfn Color(_tScalar r_, _tScalar g_, _tScalar b_, AlphaType a_=0xFF) ynothrow
构造:使用相同类型转换为单色的 RGB 值和 alpha位。
Definition: Video.h:406
yconstfn A
Definition: Video.h:118
typename make_width_int<(_vWidth<=8U?8U:(_vWidth<=16U?16U:(_vWidth<=32U?32U:64U)))>::unsigned_least_type unsigned_least_type
Definition: cstdint.hpp:88
union YB_ATTR(packed) YB_ATTR(aligned(yalignof(typename XYZATrait< _vB
BGRA 四元组。
Color
控制台颜色枚举。
Definition: Video.h:458
static yconstexpr size_t YBitsN
Definition: Video.h:51
yconstfn Color(MonoType r_, MonoType g_, MonoType b_, AlphaType a_=0xFF) ynothrow
构造:使用 RGB 值和 alpha 位。
Definition: Video.h:396
#define yconstexpr
指定编译时常量表达式。
Definition: ydef.h:462
static yconstexpr size_t XBitsN
Definition: Video.h:50
yconstfn Integer &Trait::AMask Trait::XYZBitsN yconstfn G
Definition: Video.h:123
DefDeCtor(BGRA) yconstfn BGRA(typename Trait
Definition: Video.h:102
PixelType * BitmapPtr
Definition: Video.h:295
bounds & r
Definition: ydraw.h:220
static yconstexpr IntegerType XMask
Definition: Video.h:69
ISO C 标准整数类型操作。
static yconstexpr IntegerType ZMask
Definition: Video.h:71
c yconstfn g
Definition: ystyle.h:104
Trait::ArrayType Bytes
Definition: Video.h:93
颜色。
Definition: Video.h:339
const PixelType * ConstBitmapPtr
Definition: Video.h:296
static yconstexpr bool GAligned
Definition: Video.h:57
typename ystdex::make_width_int< UIntBitsN >::unsigned_least_type IntegerType
Definition: Video.h:66
static yconstexpr size_t XYZBitsN
Definition: Video.h:54
yconstfn Color(PixelType px) ynothrow
构造:使用本机颜色对象。
Definition: Video.h:370
void octet
Definition: ydef.h:565
typename ystdex::make_width_int< XBitsN >::unsigned_least_type BType
Definition: Video.h:62
static yconstexpr IntegerType AMask
Definition: Video.h:68
static yconstexpr IntegerType YMask
Definition: Video.h:70
PDefH(bool, ufexists, const _tString &str) ynothrow ImplRet(ufexists(str.c_str())) YF_API char16_t *u16getcwd_n(char16_t *buf
判断指定字符串为文件名的文件是否存在。
ystdex::octet AlphaType
Definition: Video.h:186
static yconstexpr size_t XYBitsN
Definition: Video.h:53
static yconstexpr size_t Width
Definition: Video.h:72