0
点赞
收藏
分享

微信扫一扫

C++ 使用 Skia 绘图

C++ 使用 Skia 绘图

索引

前言

Skia 准备

  1. 下载Skia,国内skia编译门槛搞,所以此处直接使用Jetbrain在GitHub上的库(本文使用m93)

    Releases · JetBrains/skia-build (github.com)

  2. 分别下载Debug和Release两个版本,放至一个目录下(建议目录)

    Skia
    | Debug
    | | Debug压缩包解压
    | Release
    | | Release压缩包解压
    

项目实践——【Hello World】

  1. 创建项目并设置项目属性

    创建控制台项目,建议使用预编译头。此处开始笔者将指引您如何配置项目属性。首先,右键单击项目→属性(Properties)→C/C++→常规(General)→附加包含目录(Additional Include Directories),按照上一步建议的目录,分别对应Debug和Release填入。
    在这里插入图片描述

    注意:Debug和Release一定要区分

  2. 链接静态库

    因为是初学,暂时只用得到skia.lib

    同样的,右键单击项目→属性(Properties)→链接器(Linker)→常规(General)→附加库目录(Additional Library Directories),将Debug和Release中静态库的目录分别对应输入。

    在这里插入图片描述

    执行完上述步骤,再转到链接器(Linker)→输入(Input)→附加依赖项(Additional Dependencies)

    在这里插入图片描述

  3. 引入头文件

    我们已经配置好了Debug和Release环境,但是实际还需要一些东西。

    在预编译头末尾添加以下代码:

    // Skia Dependencies
    #include <d3d12.h>
    #pragma comment(lib, "D3D12.lib")
    #include <d3dcompiler.h>
    #pragma comment(lib, "d3dcompiler.lib")
    #pragma comment(lib, "Opengl32.lib")
    #ifdef _DEBUG
    #include <dxgi1_3.h>
    #pragma comment(lib, "DXGI.lib")
    #endif // _DEBUG
    

    解释一下,这些东西是Skia的依赖项,所以一定要加上,因为有用到DXGIGetDebugInterface1(具体是什么我也不知道)但这是DXGI.lib的一个函数,并且Debug环境下不可缺少,所以用预编译命令设置在Debug模式下链接DXGI.lib。这样就可以通过编译。

    注意:如果要引用Skia的头文件,须在上述代码片段前引入;如果预编译头文件中有Windows.h,请将其移至上述代码后,否则会出现大量错误

  4. 编写代码

    预编译头(笔者是pch.h文件)

    // pch.h: This is a precompiled header file.
    // Files listed below are compiled only once, improving build performance for future builds.
    // This also affects IntelliSense performance, including code completion and many code browsing features.
    // However, files listed here are ALL re-compiled if any one of them is updated between builds.
    // Do not add files here that you will be updating frequently as this negates the performance advantage.
    
    #ifndef PCH_H
    #define PCH_H
    
    // add headers that you want to pre-compile here
    
    #endif //PCH_H
    
    // Skia
    #include <include/core/SkCanvas.h>
    #include <include/core/SkPaint.h>
    #include <include/core/SkPath.h>
    #include <include/core/SkShader.h>
    #include <src/utils/SkShaderUtils.h>
    #include <include/core/SkTypeface.h>
    #include <include/core/SkTypes.h>
    #include <include/core/SkFont.h>
    #include <include/core/SkFontStyle.h>
    #include <include/core/SkFontTypes.h>
    #include <include/core/SkSurface.h>
    #include <include/codec/SkCodec.h>
    #include <include/core/SkPngChunkReader.h>
    #include <src/core/SkShaderCodeDictionary.h>
    #include <src/core/SkBitmapDevice.h>
    
    // Skia Depency
    #include <d3d12.h>
    #pragma comment(lib, "D3D12.lib")
    #include <d3dcompiler.h>
    #pragma comment(lib, "d3dcompiler.lib")
    //#include <wingdi.h>
    #pragma comment(lib, "Opengl32.lib")
    #ifdef _DEBUG
    #include <dxgi1_3.h>
    #pragma comment(lib, "DXGI.lib")
    #endif // _DEBUG
    
    #include <Windows.h>
    

    主程序代码(main.cpp)

    #include "pch.h"
    #include <iostream>
    
    int main()
    {
    	SkBitmap bitmap;
    	SkImageInfo imageInfo = SkImageInfo::Make(480, 320, kBGRA_8888_SkColorType, kPremul_SkAlphaType);
    	bitmap.allocPixels(imageInfo, imageInfo.minRowBytes());
    	SkCanvas canvas(bitmap);
    	SkPaint paint;
    	canvas.clear(0x00000000);
    	paint.setColor(SK_ColorBLACK);
    
    	SkRect rect;
    	rect.fBottom = 100;
    	rect.fTop = 10;
    	rect.fLeft = 10;
    	rect.fRight = 100;
    	SkFont font;
    	font.setSize(64);
    	font.setTypeface(SkTypeface::MakeFromName("Microsoft YaHei", SkFontStyle::Normal()));
    	canvas.drawSimpleText("Hello World", sizeof("Hello World") - 1, SkTextEncoding::kUTF8, 0, 64, font, paint);
    	SkFILEWStream stream("D:\\test.png");
    	SkEncodeImage(&stream, bitmap, SkEncodedImageFormat::kPNG, 100);
    	return 0;
    }
    

    编译运行,此时,代码中对应目录下会出现一张图片,上面有Hello World。至此,我们已经完成了Skia的Hello World项目。

【2022.4.30 编写,未完】

举报

相关推荐

0 条评论