Advertisement
Guest User

Skia minimal path example

a guest
Aug 23rd, 2021
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.24 KB | None | 0 0
  1. #include "GLFW/glfw3.h"
  2. #include "include/gpu/GrBackendSurface.h"
  3. #include "include/gpu/GrDirectContext.h"
  4. #include "include/gpu/gl/GrGLInterface.h"
  5. #include "include/core/SkCanvas.h"
  6. #include "include/core/SkColorSpace.h"
  7. #include "include/core/SkSurface.h"
  8. #include "include/utils/SkParsePath.h"
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. GrDirectContext *sContext = nullptr;
  13. SkSurface *sSurface = nullptr;
  14.  
  15. void error_callback(int error, const char *description) {
  16.     fputs(description, stderr);
  17. }
  18.  
  19. void key_callback(GLFWwindow *window, int key, int scancode, int action,
  20.                   int mods) {
  21.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  22.         glfwSetWindowShouldClose(window, GL_TRUE);
  23. }
  24.  
  25. void init_skia(int w, int h) {
  26.     auto interface = GrGLMakeNativeInterface();
  27.     sContext = GrDirectContext::MakeGL(interface).release();
  28.  
  29.     GrGLFramebufferInfo framebufferInfo;
  30.     framebufferInfo.fFBOID = 0;
  31.     framebufferInfo.fFormat = GL_RGBA8;
  32.  
  33.     SkColorType colorType = kRGBA_8888_SkColorType;
  34.     GrBackendRenderTarget backendRenderTarget(w, h, 0, 0, framebufferInfo);
  35.     sSurface = SkSurface::MakeFromBackendRenderTarget(
  36.                    sContext, backendRenderTarget, kBottomLeft_GrSurfaceOrigin,
  37.                    colorType, nullptr, nullptr)
  38.                    .release();
  39.     if (sSurface == nullptr)
  40.         abort();
  41. }
  42.  
  43. const int kWidth = 960;
  44. const int kHeight = 640;
  45.  
  46. int main(void) {
  47.     GLFWwindow *window;
  48.     glfwSetErrorCallback(error_callback);
  49.     if (!glfwInit()) {
  50.         exit(EXIT_FAILURE);
  51.     }
  52.  
  53.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  54.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  55.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  56.  
  57.     window = glfwCreateWindow(kWidth, kHeight, "SkPath render example", NULL, NULL);
  58.     if (!window) {
  59.         glfwTerminate();
  60.         exit(EXIT_FAILURE);
  61.     }
  62.     glfwMakeContextCurrent(window);
  63.  
  64.     init_skia(kWidth, kHeight);
  65.  
  66.     glfwSwapInterval(0);
  67.     glfwSetKeyCallback(window, key_callback);
  68.  
  69.     SkCanvas *canvas = sSurface->getCanvas();
  70.  
  71.     while (!glfwWindowShouldClose(window)) {
  72.         glfwWaitEvents();
  73.  
  74.         canvas->clear(SK_ColorBLACK);
  75.         canvas->save();
  76.  
  77.         SkPaint paint;
  78.         paint.setAntiAlias(true);
  79.         paint.setColor(SK_ColorWHITE);
  80.  
  81.         SkPath path;
  82.         SkParsePath::FromSVGString(
  83.             "m 70.088251,119.8771 15.654512,29.9861 H 74.806653 L "
  84.             "60.960128,121.4205 h -6.923263 v 28.4427 H 44.555964 V 84.114257 "
  85.             "h 18.52083 q 9.921874,0 14.596178,4.409722 4.718402,4.365624 "
  86.             "4.718402,13.493751 0,6.65868 -3.130902,11.33298 -3.130902,4.63021 "
  87.             "-9.172221,6.52639 z m -16.051386,-4.89479 h 7.408332 q 5.600347,0 "
  88.             "8.378471,-3.2191 2.822222,-3.26319 2.822222,-9.74548 0,-5.732641 "
  89.             "-2.601736,-8.59896 -2.601736,-2.910416 -7.805207,-2.910416 h "
  90.             "-8.202082 z",
  91.             &path);
  92.  
  93.         canvas->drawPath(path, paint);
  94.  
  95.         canvas->restore();
  96.         sContext->flush();
  97.  
  98.         glfwSwapBuffers(window);
  99.     }
  100.  
  101.     delete sSurface;
  102.     delete sContext;
  103.  
  104.     glfwDestroyWindow(window);
  105.     glfwTerminate();
  106.     exit(EXIT_SUCCESS);
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement