Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

948 lines
24 KiB

  1. /*=========================================================================*\
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. File: D2D1helper.h
  4. Module Name: D2D
  5. Description: Helper files over the D2D interfaces and APIs.
  6. \*=========================================================================*/
  7. #pragma once
  8. #ifndef _D2D1_HELPER_H_
  9. #define _D2D1_HELPER_H_
  10. #ifndef _D2D1_H_
  11. #include <d2d1.h>
  12. #endif // #ifndef _D2D1_H_
  13. #ifndef D2D_USE_C_DEFINITIONS
  14. namespace D2D1
  15. {
  16. //
  17. // Forward declared IdentityMatrix function to allow matrix class to use
  18. // these constructors.
  19. //
  20. D2D1FORCEINLINE
  21. D2D1_MATRIX_3X2_F
  22. IdentityMatrix();
  23. //
  24. // The default trait type for objects in D2D is float.
  25. //
  26. template<typename Type>
  27. struct TypeTraits
  28. {
  29. typedef D2D1_POINT_2F Point;
  30. typedef D2D1_SIZE_F Size;
  31. typedef D2D1_RECT_F Rect;
  32. };
  33. template<>
  34. struct TypeTraits<UINT32>
  35. {
  36. typedef D2D1_POINT_2U Point;
  37. typedef D2D1_SIZE_U Size;
  38. typedef D2D1_RECT_U Rect;
  39. };
  40. static inline
  41. FLOAT FloatMax()
  42. {
  43. #ifdef FLT_MAX
  44. return FLT_MAX;
  45. #else
  46. return 3.402823466e+38F;
  47. #endif
  48. }
  49. //
  50. // Construction helpers
  51. //
  52. template<typename Type>
  53. D2D1FORCEINLINE
  54. typename TypeTraits<Type>::Point
  55. Point2(
  56. Type x,
  57. Type y
  58. )
  59. {
  60. typename TypeTraits<Type>::Point point = { x, y };
  61. return point;
  62. }
  63. D2D1FORCEINLINE
  64. D2D1_POINT_2F
  65. Point2F(
  66. FLOAT x = 0.f,
  67. FLOAT y = 0.f
  68. )
  69. {
  70. return Point2<FLOAT>(x, y);
  71. }
  72. D2D1FORCEINLINE
  73. D2D1_POINT_2U
  74. Point2U(
  75. UINT32 x = 0,
  76. UINT32 y = 0
  77. )
  78. {
  79. return Point2<UINT32>(x, y);
  80. }
  81. template<typename Type>
  82. D2D1FORCEINLINE
  83. typename TypeTraits<Type>::Size
  84. Size(
  85. Type width,
  86. Type height
  87. )
  88. {
  89. typename TypeTraits<Type>::Size size = { width, height };
  90. return size;
  91. }
  92. D2D1FORCEINLINE
  93. D2D1_SIZE_F
  94. SizeF(
  95. FLOAT width = 0.f,
  96. FLOAT height = 0.f
  97. )
  98. {
  99. return Size<FLOAT>(width, height);
  100. }
  101. D2D1FORCEINLINE
  102. D2D1_SIZE_U
  103. SizeU(
  104. UINT32 width = 0,
  105. UINT32 height = 0
  106. )
  107. {
  108. return Size<UINT32>(width, height);
  109. }
  110. template<typename Type>
  111. D2D1FORCEINLINE
  112. typename TypeTraits<Type>::Rect
  113. Rect(
  114. Type left,
  115. Type top,
  116. Type right,
  117. Type bottom
  118. )
  119. {
  120. typename TypeTraits<Type>::Rect rect = { left, top, right, bottom };
  121. return rect;
  122. }
  123. D2D1FORCEINLINE
  124. D2D1_RECT_F
  125. RectF(
  126. FLOAT left = 0.f,
  127. FLOAT top = 0.f,
  128. FLOAT right = 0.f,
  129. FLOAT bottom = 0.f
  130. )
  131. {
  132. return Rect<FLOAT>(left, top, right, bottom);
  133. }
  134. D2D1FORCEINLINE
  135. D2D1_RECT_U
  136. RectU(
  137. UINT32 left = 0,
  138. UINT32 top = 0,
  139. UINT32 right = 0,
  140. UINT32 bottom = 0
  141. )
  142. {
  143. return Rect<UINT32>(left, top, right, bottom);
  144. }
  145. D2D1FORCEINLINE
  146. D2D1_RECT_F
  147. InfiniteRect()
  148. {
  149. D2D1_RECT_F rect = { -FloatMax(), -FloatMax(), FloatMax(), FloatMax() };
  150. return rect;
  151. }
  152. D2D1FORCEINLINE
  153. D2D1_ARC_SEGMENT
  154. ArcSegment(
  155. __in CONST D2D1_POINT_2F &point,
  156. __in CONST D2D1_SIZE_F &size,
  157. __in FLOAT rotationAngle,
  158. __in D2D1_SWEEP_DIRECTION sweepDirection,
  159. __in D2D1_ARC_SIZE arcSize
  160. )
  161. {
  162. D2D1_ARC_SEGMENT arcSegment = { point, size, rotationAngle, sweepDirection, arcSize };
  163. return arcSegment;
  164. }
  165. D2D1FORCEINLINE
  166. D2D1_BEZIER_SEGMENT
  167. BezierSegment(
  168. __in CONST D2D1_POINT_2F &point1,
  169. __in CONST D2D1_POINT_2F &point2,
  170. __in CONST D2D1_POINT_2F &point3
  171. )
  172. {
  173. D2D1_BEZIER_SEGMENT bezierSegment = { point1, point2, point3 };
  174. return bezierSegment;
  175. }
  176. D2D1FORCEINLINE
  177. D2D1_ELLIPSE
  178. Ellipse(
  179. __in CONST D2D1_POINT_2F &center,
  180. FLOAT radiusX,
  181. FLOAT radiusY
  182. )
  183. {
  184. D2D1_ELLIPSE ellipse;
  185. ellipse.point = center;
  186. ellipse.radiusX = radiusX;
  187. ellipse.radiusY = radiusY;
  188. return ellipse;
  189. }
  190. D2D1FORCEINLINE
  191. D2D1_ROUNDED_RECT
  192. RoundedRect(
  193. __in CONST D2D1_RECT_F &rect,
  194. FLOAT radiusX,
  195. FLOAT radiusY
  196. )
  197. {
  198. D2D1_ROUNDED_RECT roundedRect;
  199. roundedRect.rect = rect;
  200. roundedRect.radiusX = radiusX;
  201. roundedRect.radiusY = radiusY;
  202. return roundedRect;
  203. }
  204. D2D1FORCEINLINE
  205. D2D1_BRUSH_PROPERTIES
  206. BrushProperties(
  207. __in FLOAT opacity = 1.0,
  208. __in CONST D2D1_MATRIX_3X2_F &transform = D2D1::IdentityMatrix()
  209. )
  210. {
  211. D2D1_BRUSH_PROPERTIES brushProperties;
  212. brushProperties.opacity = opacity;
  213. brushProperties.transform = transform;
  214. return brushProperties;
  215. }
  216. D2D1FORCEINLINE
  217. D2D1_GRADIENT_STOP
  218. GradientStop(
  219. FLOAT position,
  220. __in CONST D2D1_COLOR_F &color
  221. )
  222. {
  223. D2D1_GRADIENT_STOP gradientStop = { position, color };
  224. return gradientStop;
  225. }
  226. D2D1FORCEINLINE
  227. D2D1_QUADRATIC_BEZIER_SEGMENT
  228. QuadraticBezierSegment(
  229. __in CONST D2D1_POINT_2F &point1,
  230. __in CONST D2D1_POINT_2F &point2
  231. )
  232. {
  233. D2D1_QUADRATIC_BEZIER_SEGMENT quadraticBezier = { point1, point2 };
  234. return quadraticBezier;
  235. }
  236. D2D1FORCEINLINE
  237. D2D1_STROKE_STYLE_PROPERTIES
  238. StrokeStyleProperties(
  239. D2D1_CAP_STYLE startCap = D2D1_CAP_STYLE_FLAT,
  240. D2D1_CAP_STYLE endCap = D2D1_CAP_STYLE_FLAT,
  241. D2D1_CAP_STYLE dashCap = D2D1_CAP_STYLE_FLAT,
  242. D2D1_LINE_JOIN lineJoin = D2D1_LINE_JOIN_MITER,
  243. FLOAT miterLimit = 10.0f,
  244. D2D1_DASH_STYLE dashStyle = D2D1_DASH_STYLE_SOLID,
  245. FLOAT dashOffset = 0.0f
  246. )
  247. {
  248. D2D1_STROKE_STYLE_PROPERTIES strokeStyleProperties;
  249. strokeStyleProperties.startCap = startCap;
  250. strokeStyleProperties.endCap = endCap;
  251. strokeStyleProperties.dashCap = dashCap;
  252. strokeStyleProperties.lineJoin = lineJoin;
  253. strokeStyleProperties.miterLimit = miterLimit;
  254. strokeStyleProperties.dashStyle = dashStyle;
  255. strokeStyleProperties.dashOffset = dashOffset;
  256. return strokeStyleProperties;
  257. }
  258. D2D1FORCEINLINE
  259. D2D1_BITMAP_BRUSH_PROPERTIES
  260. BitmapBrushProperties(
  261. D2D1_EXTEND_MODE extendModeX = D2D1_EXTEND_MODE_CLAMP,
  262. D2D1_EXTEND_MODE extendModeY = D2D1_EXTEND_MODE_CLAMP,
  263. D2D1_BITMAP_INTERPOLATION_MODE interpolationMode = D2D1_BITMAP_INTERPOLATION_MODE_LINEAR
  264. )
  265. {
  266. D2D1_BITMAP_BRUSH_PROPERTIES bitmapBrushProperties;
  267. bitmapBrushProperties.extendModeX = extendModeX;
  268. bitmapBrushProperties.extendModeY = extendModeY;
  269. bitmapBrushProperties.interpolationMode = interpolationMode;
  270. return bitmapBrushProperties;
  271. }
  272. D2D1FORCEINLINE
  273. D2D1_LINEAR_GRADIENT_BRUSH_PROPERTIES
  274. LinearGradientBrushProperties(
  275. __in CONST D2D1_POINT_2F &startPoint,
  276. __in CONST D2D1_POINT_2F &endPoint
  277. )
  278. {
  279. D2D1_LINEAR_GRADIENT_BRUSH_PROPERTIES linearGradientBrushProperties;
  280. linearGradientBrushProperties.startPoint = startPoint;
  281. linearGradientBrushProperties.endPoint = endPoint;
  282. return linearGradientBrushProperties;
  283. }
  284. D2D1FORCEINLINE
  285. D2D1_RADIAL_GRADIENT_BRUSH_PROPERTIES
  286. RadialGradientBrushProperties(
  287. __in CONST D2D1_POINT_2F &center,
  288. __in CONST D2D1_POINT_2F &gradientOriginOffset,
  289. FLOAT radiusX,
  290. FLOAT radiusY
  291. )
  292. {
  293. D2D1_RADIAL_GRADIENT_BRUSH_PROPERTIES radialGradientBrushProperties;
  294. radialGradientBrushProperties.center = center;
  295. radialGradientBrushProperties.gradientOriginOffset = gradientOriginOffset;
  296. radialGradientBrushProperties.radiusX = radiusX;
  297. radialGradientBrushProperties.radiusY = radiusY;
  298. return radialGradientBrushProperties;
  299. }
  300. //
  301. // PixelFormat
  302. //
  303. D2D1FORCEINLINE
  304. D2D1_PIXEL_FORMAT
  305. PixelFormat(
  306. __in DXGI_FORMAT dxgiFormat = DXGI_FORMAT_UNKNOWN,
  307. __in D2D1_ALPHA_MODE alphaMode = D2D1_ALPHA_MODE_UNKNOWN
  308. )
  309. {
  310. D2D1_PIXEL_FORMAT pixelFormat;
  311. pixelFormat.format = dxgiFormat;
  312. pixelFormat.alphaMode = alphaMode;
  313. return pixelFormat;
  314. }
  315. //
  316. // Bitmaps
  317. //
  318. D2D1FORCEINLINE
  319. D2D1_BITMAP_PROPERTIES
  320. BitmapProperties(
  321. CONST D2D1_PIXEL_FORMAT &pixelFormat = D2D1::PixelFormat(),
  322. FLOAT dpiX = 96.0f,
  323. FLOAT dpiY = 96.0f
  324. )
  325. {
  326. D2D1_BITMAP_PROPERTIES bitmapProperties;
  327. bitmapProperties.pixelFormat = pixelFormat;
  328. bitmapProperties.dpiX = dpiX;
  329. bitmapProperties.dpiY = dpiY;
  330. return bitmapProperties;
  331. }
  332. //
  333. // Render Targets
  334. //
  335. D2D1FORCEINLINE
  336. D2D1_RENDER_TARGET_PROPERTIES
  337. RenderTargetProperties(
  338. D2D1_RENDER_TARGET_TYPE type = D2D1_RENDER_TARGET_TYPE_DEFAULT,
  339. __in CONST D2D1_PIXEL_FORMAT &pixelFormat = D2D1::PixelFormat(),
  340. FLOAT dpiX = 0.0,
  341. FLOAT dpiY = 0.0,
  342. D2D1_RENDER_TARGET_USAGE usage = D2D1_RENDER_TARGET_USAGE_NONE,
  343. D2D1_FEATURE_LEVEL minLevel = D2D1_FEATURE_LEVEL_DEFAULT
  344. )
  345. {
  346. D2D1_RENDER_TARGET_PROPERTIES renderTargetProperties;
  347. renderTargetProperties.type = type;
  348. renderTargetProperties.pixelFormat = pixelFormat;
  349. renderTargetProperties.dpiX = dpiX;
  350. renderTargetProperties.dpiY = dpiY;
  351. renderTargetProperties.usage = usage;
  352. renderTargetProperties.minLevel = minLevel;
  353. return renderTargetProperties;
  354. }
  355. D2D1FORCEINLINE
  356. D2D1_HWND_RENDER_TARGET_PROPERTIES
  357. HwndRenderTargetProperties(
  358. __in HWND hwnd,
  359. __in D2D1_SIZE_U pixelSize = D2D1::Size(static_cast<UINT>(0), static_cast<UINT>(0)),
  360. __in D2D1_PRESENT_OPTIONS presentOptions = D2D1_PRESENT_OPTIONS_NONE
  361. )
  362. {
  363. D2D1_HWND_RENDER_TARGET_PROPERTIES hwndRenderTargetProperties;
  364. hwndRenderTargetProperties.hwnd = hwnd;
  365. hwndRenderTargetProperties.pixelSize = pixelSize;
  366. hwndRenderTargetProperties.presentOptions = presentOptions;
  367. return hwndRenderTargetProperties;
  368. }
  369. D2D1FORCEINLINE
  370. D2D1_LAYER_PARAMETERS
  371. LayerParameters(
  372. __in CONST D2D1_RECT_F &contentBounds = D2D1::InfiniteRect(),
  373. __in_opt ID2D1Geometry *geometricMask = NULL,
  374. D2D1_ANTIALIAS_MODE maskAntialiasMode = D2D1_ANTIALIAS_MODE_PER_PRIMITIVE,
  375. D2D1_MATRIX_3X2_F maskTransform = D2D1::IdentityMatrix(),
  376. FLOAT opacity = 1.0,
  377. __in_opt ID2D1Brush *opacityBrush = NULL,
  378. D2D1_LAYER_OPTIONS layerOptions = D2D1_LAYER_OPTIONS_NONE
  379. )
  380. {
  381. D2D1_LAYER_PARAMETERS layerParameters = { 0 };
  382. layerParameters.contentBounds = contentBounds;
  383. layerParameters.geometricMask = geometricMask;
  384. layerParameters.maskAntialiasMode = maskAntialiasMode;
  385. layerParameters.maskTransform = maskTransform;
  386. layerParameters.opacity = opacity;
  387. layerParameters.opacityBrush = opacityBrush;
  388. layerParameters.layerOptions = layerOptions;
  389. return layerParameters;
  390. }
  391. D2D1FORCEINLINE
  392. D2D1_DRAWING_STATE_DESCRIPTION
  393. DrawingStateDescription(
  394. D2D1_ANTIALIAS_MODE antialiasMode = D2D1_ANTIALIAS_MODE_PER_PRIMITIVE,
  395. D2D1_TEXT_ANTIALIAS_MODE textAntialiasMode = D2D1_TEXT_ANTIALIAS_MODE_DEFAULT,
  396. D2D1_TAG tag1 = 0,
  397. D2D1_TAG tag2 = 0,
  398. __in const D2D1_MATRIX_3X2_F &transform = D2D1::IdentityMatrix()
  399. )
  400. {
  401. D2D1_DRAWING_STATE_DESCRIPTION drawingStateDescription;
  402. drawingStateDescription.antialiasMode = antialiasMode;
  403. drawingStateDescription.textAntialiasMode = textAntialiasMode;
  404. drawingStateDescription.tag1 = tag1;
  405. drawingStateDescription.tag2 = tag2;
  406. drawingStateDescription.transform = transform;
  407. return drawingStateDescription;
  408. }
  409. //
  410. // Colors, this enum defines a set of predefined colors.
  411. //
  412. class ColorF : public D2D1_COLOR_F
  413. {
  414. public:
  415. enum Enum
  416. {
  417. AliceBlue = 0xF0F8FF,
  418. AntiqueWhite = 0xFAEBD7,
  419. Aqua = 0x00FFFF,
  420. Aquamarine = 0x7FFFD4,
  421. Azure = 0xF0FFFF,
  422. Beige = 0xF5F5DC,
  423. Bisque = 0xFFE4C4,
  424. Black = 0x000000,
  425. BlanchedAlmond = 0xFFEBCD,
  426. Blue = 0x0000FF,
  427. BlueViolet = 0x8A2BE2,
  428. Brown = 0xA52A2A,
  429. BurlyWood = 0xDEB887,
  430. CadetBlue = 0x5F9EA0,
  431. Chartreuse = 0x7FFF00,
  432. Chocolate = 0xD2691E,
  433. Coral = 0xFF7F50,
  434. CornflowerBlue = 0x6495ED,
  435. Cornsilk = 0xFFF8DC,
  436. Crimson = 0xDC143C,
  437. Cyan = 0x00FFFF,
  438. DarkBlue = 0x00008B,
  439. DarkCyan = 0x008B8B,
  440. DarkGoldenrod = 0xB8860B,
  441. DarkGray = 0xA9A9A9,
  442. DarkGreen = 0x006400,
  443. DarkKhaki = 0xBDB76B,
  444. DarkMagenta = 0x8B008B,
  445. DarkOliveGreen = 0x556B2F,
  446. DarkOrange = 0xFF8C00,
  447. DarkOrchid = 0x9932CC,
  448. DarkRed = 0x8B0000,
  449. DarkSalmon = 0xE9967A,
  450. DarkSeaGreen = 0x8FBC8F,
  451. DarkSlateBlue = 0x483D8B,
  452. DarkSlateGray = 0x2F4F4F,
  453. DarkTurquoise = 0x00CED1,
  454. DarkViolet = 0x9400D3,
  455. DeepPink = 0xFF1493,
  456. DeepSkyBlue = 0x00BFFF,
  457. DimGray = 0x696969,
  458. DodgerBlue = 0x1E90FF,
  459. Firebrick = 0xB22222,
  460. FloralWhite = 0xFFFAF0,
  461. ForestGreen = 0x228B22,
  462. Fuchsia = 0xFF00FF,
  463. Gainsboro = 0xDCDCDC,
  464. GhostWhite = 0xF8F8FF,
  465. Gold = 0xFFD700,
  466. Goldenrod = 0xDAA520,
  467. Gray = 0x808080,
  468. Green = 0x008000,
  469. GreenYellow = 0xADFF2F,
  470. Honeydew = 0xF0FFF0,
  471. HotPink = 0xFF69B4,
  472. IndianRed = 0xCD5C5C,
  473. Indigo = 0x4B0082,
  474. Ivory = 0xFFFFF0,
  475. Khaki = 0xF0E68C,
  476. Lavender = 0xE6E6FA,
  477. LavenderBlush = 0xFFF0F5,
  478. LawnGreen = 0x7CFC00,
  479. LemonChiffon = 0xFFFACD,
  480. LightBlue = 0xADD8E6,
  481. LightCoral = 0xF08080,
  482. LightCyan = 0xE0FFFF,
  483. LightGoldenrodYellow = 0xFAFAD2,
  484. LightGreen = 0x90EE90,
  485. LightGray = 0xD3D3D3,
  486. LightPink = 0xFFB6C1,
  487. LightSalmon = 0xFFA07A,
  488. LightSeaGreen = 0x20B2AA,
  489. LightSkyBlue = 0x87CEFA,
  490. LightSlateGray = 0x778899,
  491. LightSteelBlue = 0xB0C4DE,
  492. LightYellow = 0xFFFFE0,
  493. Lime = 0x00FF00,
  494. LimeGreen = 0x32CD32,
  495. Linen = 0xFAF0E6,
  496. Magenta = 0xFF00FF,
  497. Maroon = 0x800000,
  498. MediumAquamarine = 0x66CDAA,
  499. MediumBlue = 0x0000CD,
  500. MediumOrchid = 0xBA55D3,
  501. MediumPurple = 0x9370DB,
  502. MediumSeaGreen = 0x3CB371,
  503. MediumSlateBlue = 0x7B68EE,
  504. MediumSpringGreen = 0x00FA9A,
  505. MediumTurquoise = 0x48D1CC,
  506. MediumVioletRed = 0xC71585,
  507. MidnightBlue = 0x191970,
  508. MintCream = 0xF5FFFA,
  509. MistyRose = 0xFFE4E1,
  510. Moccasin = 0xFFE4B5,
  511. NavajoWhite = 0xFFDEAD,
  512. Navy = 0x000080,
  513. OldLace = 0xFDF5E6,
  514. Olive = 0x808000,
  515. OliveDrab = 0x6B8E23,
  516. Orange = 0xFFA500,
  517. OrangeRed = 0xFF4500,
  518. Orchid = 0xDA70D6,
  519. PaleGoldenrod = 0xEEE8AA,
  520. PaleGreen = 0x98FB98,
  521. PaleTurquoise = 0xAFEEEE,
  522. PaleVioletRed = 0xDB7093,
  523. PapayaWhip = 0xFFEFD5,
  524. PeachPuff = 0xFFDAB9,
  525. Peru = 0xCD853F,
  526. Pink = 0xFFC0CB,
  527. Plum = 0xDDA0DD,
  528. PowderBlue = 0xB0E0E6,
  529. Purple = 0x800080,
  530. Red = 0xFF0000,
  531. RosyBrown = 0xBC8F8F,
  532. RoyalBlue = 0x4169E1,
  533. SaddleBrown = 0x8B4513,
  534. Salmon = 0xFA8072,
  535. SandyBrown = 0xF4A460,
  536. SeaGreen = 0x2E8B57,
  537. SeaShell = 0xFFF5EE,
  538. Sienna = 0xA0522D,
  539. Silver = 0xC0C0C0,
  540. SkyBlue = 0x87CEEB,
  541. SlateBlue = 0x6A5ACD,
  542. SlateGray = 0x708090,
  543. Snow = 0xFFFAFA,
  544. SpringGreen = 0x00FF7F,
  545. SteelBlue = 0x4682B4,
  546. Tan = 0xD2B48C,
  547. Teal = 0x008080,
  548. Thistle = 0xD8BFD8,
  549. Tomato = 0xFF6347,
  550. Turquoise = 0x40E0D0,
  551. Violet = 0xEE82EE,
  552. Wheat = 0xF5DEB3,
  553. White = 0xFFFFFF,
  554. WhiteSmoke = 0xF5F5F5,
  555. Yellow = 0xFFFF00,
  556. YellowGreen = 0x9ACD32,
  557. };
  558. //
  559. // Construct a color, note that the alpha value from the "rgb" component
  560. // is never used.
  561. //
  562. D2D1FORCEINLINE
  563. ColorF(
  564. UINT32 rgb,
  565. FLOAT a = 1.0
  566. )
  567. {
  568. Init(rgb, a);
  569. }
  570. D2D1FORCEINLINE
  571. ColorF(
  572. Enum knownColor,
  573. FLOAT a = 1.0
  574. )
  575. {
  576. Init(knownColor, a);
  577. }
  578. D2D1FORCEINLINE
  579. ColorF(
  580. FLOAT r,
  581. FLOAT g,
  582. FLOAT b,
  583. FLOAT a = 1.0
  584. )
  585. {
  586. this->r = r;
  587. this->g = g;
  588. this->b = b;
  589. this->a = a;
  590. }
  591. private:
  592. D2D1FORCEINLINE
  593. void
  594. Init(
  595. UINT32 rgb,
  596. FLOAT a
  597. )
  598. {
  599. this->r = static_cast<FLOAT>((rgb & sc_redMask) >> sc_redShift) / 255.f;
  600. this->g = static_cast<FLOAT>((rgb & sc_greenMask) >> sc_greenShift) / 255.f;
  601. this->b = static_cast<FLOAT>((rgb & sc_blueMask) >> sc_blueShift) / 255.f;
  602. this->a = a;
  603. }
  604. static const UINT32 sc_redShift = 16;
  605. static const UINT32 sc_greenShift = 8;
  606. static const UINT32 sc_blueShift = 0;
  607. static const UINT32 sc_redMask = 0xff << sc_redShift;
  608. static const UINT32 sc_greenMask = 0xff << sc_greenShift;
  609. static const UINT32 sc_blueMask = 0xff << sc_blueShift;
  610. };
  611. class Matrix3x2F : public D2D1_MATRIX_3X2_F
  612. {
  613. public:
  614. D2D1FORCEINLINE
  615. Matrix3x2F(
  616. FLOAT _11,
  617. FLOAT _12,
  618. FLOAT _21,
  619. FLOAT _22,
  620. FLOAT _31,
  621. FLOAT _32
  622. )
  623. {
  624. this->_11 = _11;
  625. this->_12 = _12;
  626. this->_21 = _21;
  627. this->_22 = _22;
  628. this->_31 = _31;
  629. this->_32 = _32;
  630. }
  631. //
  632. // Creates an identity matrix
  633. //
  634. D2D1FORCEINLINE
  635. Matrix3x2F(
  636. )
  637. {
  638. }
  639. //
  640. // Named quasi-constructors
  641. //
  642. static D2D1FORCEINLINE
  643. Matrix3x2F
  644. Identity()
  645. {
  646. Matrix3x2F identity;
  647. identity._11 = 1.f;
  648. identity._12 = 0.f;
  649. identity._21 = 0.f;
  650. identity._22 = 1.f;
  651. identity._31 = 0.f;
  652. identity._32 = 0.f;
  653. return identity;
  654. }
  655. static D2D1FORCEINLINE
  656. Matrix3x2F
  657. Translation(
  658. D2D1_SIZE_F size
  659. )
  660. {
  661. Matrix3x2F translation;
  662. translation._11 = 1.0; translation._12 = 0.0;
  663. translation._21 = 0.0; translation._22 = 1.0;
  664. translation._31 = size.width; translation._32 = size.height;
  665. return translation;
  666. }
  667. static D2D1FORCEINLINE
  668. Matrix3x2F
  669. Translation(
  670. FLOAT x,
  671. FLOAT y
  672. )
  673. {
  674. return Translation(SizeF(x, y));
  675. }
  676. static D2D1FORCEINLINE
  677. Matrix3x2F
  678. Scale(
  679. D2D1_SIZE_F size,
  680. D2D1_POINT_2F center = D2D1::Point2F()
  681. )
  682. {
  683. Matrix3x2F scale;
  684. scale._11 = size.width; scale._12 = 0.0;
  685. scale._21 = 0.0; scale._22 = size.height;
  686. scale._31 = center.x - size.width * center.x;
  687. scale._32 = center.y - size.height * center.y;
  688. return scale;
  689. }
  690. static D2D1FORCEINLINE
  691. Matrix3x2F
  692. Scale(
  693. FLOAT x,
  694. FLOAT y,
  695. D2D1_POINT_2F center = D2D1::Point2F()
  696. )
  697. {
  698. return Scale(SizeF(x, y), center);
  699. }
  700. static D2D1FORCEINLINE
  701. Matrix3x2F
  702. Rotation(
  703. FLOAT angle,
  704. D2D1_POINT_2F center = D2D1::Point2F()
  705. )
  706. {
  707. Matrix3x2F rotation;
  708. D2D1MakeRotateMatrix(angle, center, &rotation);
  709. return rotation;
  710. }
  711. static D2D1FORCEINLINE
  712. Matrix3x2F
  713. Skew(
  714. FLOAT angleX,
  715. FLOAT angleY,
  716. D2D1_POINT_2F center = D2D1::Point2F()
  717. )
  718. {
  719. Matrix3x2F skew;
  720. D2D1MakeSkewMatrix(angleX, angleY, center, &skew);
  721. return skew;
  722. }
  723. //
  724. // Functions for convertion from the base D2D1_MATRIX_3X2_F to this type
  725. // without making a copy
  726. //
  727. static inline const Matrix3x2F* ReinterpretBaseType(const D2D1_MATRIX_3X2_F *pMatrix)
  728. {
  729. return static_cast<const Matrix3x2F *>(pMatrix);
  730. }
  731. static inline Matrix3x2F* ReinterpretBaseType(D2D1_MATRIX_3X2_F *pMatrix)
  732. {
  733. return static_cast<Matrix3x2F *>(pMatrix);
  734. }
  735. inline
  736. FLOAT
  737. Determinant() const
  738. {
  739. return (_11 * _22) - (_12 * _21);
  740. }
  741. inline
  742. bool
  743. IsInvertible() const
  744. {
  745. return !!D2D1IsMatrixInvertible(this);
  746. }
  747. inline
  748. bool
  749. Invert()
  750. {
  751. return !!D2D1InvertMatrix(this);
  752. }
  753. inline
  754. bool
  755. IsIdentity() const
  756. {
  757. return _11 == 1.f && _12 == 0.f
  758. && _21 == 0.f && _22 == 1.f
  759. && _31 == 0.f && _32 == 0.f;
  760. }
  761. inline
  762. void SetProduct(
  763. const Matrix3x2F &a,
  764. const Matrix3x2F &b
  765. )
  766. {
  767. _11 = a._11 * b._11 + a._12 * b._21;
  768. _12 = a._11 * b._12 + a._12 * b._22;
  769. _21 = a._21 * b._11 + a._22 * b._21;
  770. _22 = a._21 * b._12 + a._22 * b._22;
  771. _31 = a._31 * b._11 + a._32 * b._21 + b._31;
  772. _32 = a._31 * b._12 + a._32 * b._22 + b._32;
  773. }
  774. D2D1FORCEINLINE
  775. Matrix3x2F
  776. operator*(
  777. const Matrix3x2F &matrix
  778. ) const
  779. {
  780. Matrix3x2F result;
  781. result.SetProduct(*this, matrix);
  782. return result;
  783. }
  784. D2D1FORCEINLINE
  785. D2D1_POINT_2F
  786. TransformPoint(
  787. D2D1_POINT_2F point
  788. ) const
  789. {
  790. D2D1_POINT_2F result =
  791. {
  792. point.x * _11 + point.y * _21 + _31,
  793. point.x * _12 + point.y * _22 + _32
  794. };
  795. return result;
  796. }
  797. };
  798. D2D1FORCEINLINE
  799. D2D1_POINT_2F
  800. operator*(
  801. const D2D1_POINT_2F &point,
  802. const D2D1_MATRIX_3X2_F &matrix
  803. )
  804. {
  805. return Matrix3x2F::ReinterpretBaseType(&matrix)->TransformPoint(point);
  806. }
  807. D2D1_MATRIX_3X2_F
  808. IdentityMatrix()
  809. {
  810. return Matrix3x2F::Identity();
  811. }
  812. } // namespace D2D1
  813. D2D1FORCEINLINE
  814. D2D1_MATRIX_3X2_F
  815. operator*(
  816. const D2D1_MATRIX_3X2_F &matrix1,
  817. const D2D1_MATRIX_3X2_F &matrix2
  818. )
  819. {
  820. return
  821. (*D2D1::Matrix3x2F::ReinterpretBaseType(&matrix1)) *
  822. (*D2D1::Matrix3x2F::ReinterpretBaseType(&matrix2));
  823. }
  824. #endif // #ifndef D2D_USE_C_DEFINITIONS
  825. #endif // #ifndef _D2D1_HELPER_H_