Site icon Learn C++

How To Use Skia in C++ Builder 12?

How To Use Skia in C++ Builder 12

The latest RAD Studio / C++ Builder 12 comes with fully integrated Skia graphics library support in C++ and throughout the IDE. Skia is an open-source graphic library that provides support for a wide variety of hardware and software platforms. Skia is sponsored and managed by Google but is available for use by anyone under the BSD Free Software License. Skia provides smooth 2D graphics that uses GPU efficiently. In this post we explain how to use Skia in C++ Builder.

What is Skia in modern C++?

Skia is an open-source graphic library that provides support variety of hardware and software platforms. Skia is sponsored and managed by Google but is available for use by anyone under the BSD Free Software License. Mainly designed for Google Chrome and ChromeOS, Android, Flutter, Mozilla Firefox and Firefox OS, and many other products. The core components are done by the Skia development team, but they consider contributions from any source.

How can we use Skia in C++ Builder 12 project code?

First, you need C++ Builder 12.1, or you need Skia4Delphi component installed on C++ Builder 11.x. Skia is a new 2D render feature of RAD Studio / C++ Builder 12.1. It comes with Skia.Package.FMX, Skia.Package.RTL, Skia.Package.VCL. package libraries officially.

If you want to use Skia in your C++ Builder applications, you need to download the latest RAD Studio / C++ Builder. Then create a new C++ Builder FMX application or VCL application or open your project and you just need to enable Skia by right-clicking on your project in the Project window in C++ Builder. You don’t need to install any 3rd party components, after the RAD Studio 12.1 version, it is completely included in the built-in libraries.

In addition to Enabling Skia as above , it is recommended to add FMX.Skia.hpp header to your Project.cpp as shown below.

[crayon-674f3f9ede1d8750245789/]

It is recommended to set true GlobalUseSkia, GlobalUseVulkan and set false GlobalUseSkiaRasterWhenAvailable flags inyour Project.cpp like so:

[crayon-674f3f9ede1e0684662877/]

Is there a simple example to Skia in C++ Builder 12?

Now, we can back to our Form to use Skia graphics. Let’s use its amazing Shader feature to create a gradient background. To do this follow these steps.

1. Drag TSkPainBox component from the Pallette window

2. Select SkPainBox1, in Object Inspector on the left side, goto Events tab and double click to OnDraw() event,

this will create OnDraw() event procedure in your code which is declared in your header.

3. Modify inside as given example below,

[crayon-674f3f9ede1e1021765622/]

more securely, for any graphical issues we can save canvas state and we can use try{ } and __finally{ } as shown below.

[crayon-674f3f9ede1e2036923033/]

then run your application, here is the result:

Is there a full example of how to use Skia in C++ Builder 12?

Here is the full code of this simple Skia example that uses Shaders of GPU to draw a gradient. Very simple and amazing!

[crayon-674f3f9ede1e5776641098/]

If you want to see more examples, please install the latest RAD Studio / C++ Builder12.1 full or trial version and check this sample in Samples folder:
C:\Users\Public\Documents\Embarcadero\Studio\23.0\Samples\CPP\Multi-Device Samples\Skia4Delphi project. It has many examples that runs different Skia graphical examples.

C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

Exit mobile version