经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » 游戏设计 » 查看文章
Direct2D 学习笔记(2)
来源:cnblogs  作者:Halation  时间:2019/10/8 9:06:11  对本文有异议
画刷的使用方法
需要包含的文件:<wincodec.h>
需要包含的库: "windowscodecs.lib"
 
1.颜色——D2D_COLOR_F 结构体
D2D_COLOR_F 结构体的原型是D3DCOLORVALUE
  1. typedef struct _D3DCOLORVALUE {
  2. float r;  //红色分量值,范围0-1
  3. float g;  //绿色分量值,范围0-1
  4. float b;  //蓝色分量值,范围0-1
  5. float a;  //alpha分量值(透明度),范围0-1
  6. } D3DCOLORVALUE;

 

其中,四个属性的取值范围是0-1。
可以通过D2D1::ColorF方法获取颜色
  1. D2D1::ColorF(0.0f, 0.0f, 0.0f, 1.0f);     //rgba形式
  2. D2D1::ColorF(0x000000, 1.0f);          //UINT32形式
  3. D2D1::ColorF(D2D1::ColorF::Pink, 1.0f);    //Enum形式特定值

 

2.位图的加载——ID2D1Bitmap
从文件中加载位图:
以下代码可以看作是一个模板,除了第2步中的文件名”filename",以及第4步中的width/height需要更改,其他的只需套用即可完成位图ID2D1Bitmap的初始化,因此一般把它封装成一个函数使用。
  1. //1初始化IWICImagingFactory
  2. IWICImagingFactory *pIWICFactory;
  3. CoInitialize(NULL);
  4. CoCreateIstance(
  5. CLSID_WICImagingFactory,
  6. NULL,
  7. CLSCTX_INPROC_SERVER,
  8. IID_PPV_ARGS(&pIWICFactory)
  9. );
  10. ID2D1Bitmap *pBitmap;
  11. IWICBitmapDecoder *pDecoder;
  12. IWICBitmapFrameDecode *pSource;
  13. IWICStream *pStream;
  14. IWICFormatConverter *pConverter;
  15. IWICBitmapScaler *pScaler;
  16. //2初始化IWICBitmapDecoder
  17. pIWICFactory->CreateDecoderFromFilename(
  18. "filename",    //修改为图片的路径名
  19. NULL,
  20. GENERIC_READ,
  21. WICDecodeMetadataCacheOnload,
  22. &pDecoder
  23. );
  24. //3初始化IWICBitmapFrameDecode
  25. pDecoder->GetFrame(0, &pSource);
  26. //4初始化IWICBitmapScaler
  27. pIWICFactory->CreateBitmapScaler(&pScaler);
  28. pScaler->Initialize(
  29. pSource,
  30. width,   //缩放至width宽度
  31. height,   //缩放至height高度
  32. WICBitmapInterpolationModeCubic
  33. );
  34. //4初始化IWICFormatConverter
  35. pIWICFactory->CreateFormatConverter(&pConverter);
  36. pConverter->Initialize(
  37. pScaler,
  38. GUID_WICPixelFormat32bppPBGRA,
  39. WICBitmapDitherTypeNone,
  40. NULL,
  41. 0.0f,
  42. WICBitmapPaletteTypeMedianCut
  43. );
  44. //5从WIC中加载位图
  45. pRenderTarget->CreateBitmapFromWicBitmap(
  46. pConvert,
  47. NULL,
  48. &pBitmap
  49. );
  50. //6释放临时对象
  51. XXXXX->Release();

 

3.画刷类型
单色刷——ID2D1SolidColorBrush
  1. ID2D1SolidColorBrush *pscBrush;
  2. //创建单色画刷
  3. RenderTarget->CreateSolidColorBrush(
  4. D2D1::ColorF(0, 1.0f),
  5. &pscBrush
  6. );

 

线性渐变刷——ID2D1LinearFradientBrush
  1. ID2D1LinearGradientBrush *plgBrush;
  2. ID2D1GradientStopCollection *pgsCollection;
  3. //创建渐变节点数组
  4. D2D1_FRADIENT_STOP gradientStops[2];
  5. gradientStops[0] = {0.0f, D2D1::ColorF(0, 1.0f)};
  6. gradientStops[1] = {1.0f, D2D1::ColorF(0xffffff, 1.0f)};
  7. //创建渐变条(这一部分可以想象成ps中的那个渐变)
  8. RenderTarget->CreateGradientStopCollection(
  9. gradientStops, //渐变颜色点信息
  10. 2, //渐变颜色点数量
  11. D2D1_GAMMA_2_2,
  12. D2D1_EXTEND_MODE_CLAMP,
  13. &pGsCollection
  14. );
  15. //创建渐变刷
  16. RenderTarget->CreateLinearGradientBrush(
  17. LinearGradientBrushProperties(
  18. D2D1::Point2F(0, 0), //渐变线起始点(窗口坐标)
  19. D2D1::Point2F(150, 150)   //渐变线终点(窗口坐标)
  20. ),
  21. pgsCollection,
  22. &plgBrush
  23. );

 

发散渐变刷——ID2D1RadialGradientBrush
 
  1. g_pTarget->CreateRadialGradientBrush(
  2. RadialGradientBrushProperties(
  3. Point2F(20,20),   //颜色中心
  4. Point2F(0, 0), //颜色偏离中心
  5. 20, //颜色x轴
  6. 20 //颜色y轴
  7. ),
  8. g_pGsCollection,
  9. &g_pRgBrush
  10. );

 

 
位图刷——ID2D1BitmapBrush
 
  1. //使用WIC从文件中加载资源后
  2. pRenderTarget->CreateBitmapBrush(pBitmap, &BitmapBrush);

 

4.画刷使用
  1. pRenderTarget->BeginDraw();
  2. pRenderTarget->FillRectangle(&rect, pxxxBrush);
  3. pRenderTarget->DrawRectangle(&rect, pxxxBrush);
  4. pRenderTarget->EndDraw();

 

下一期将学习位图刷和位图的更多用法。

原文链接:http://www.cnblogs.com/halation/p/11621426.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号