Created
June 27, 2023 17:05
-
-
Save wuxianggujun/88b3699cabeff5acc87f1c6f0cfb5b71 to your computer and use it in GitHub Desktop.
qt结合Skia绘制图形在窗口上
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by WuXiangGuJun on 2023/6/18. | |
// | |
#pragma once | |
#include <iostream> | |
#include <QTime> | |
#include <QTimer> | |
#include <QOpenGLWidget> | |
#include <QOpenGLFunctions> | |
#include <QApplication> | |
#include <QMainWindow> | |
#include <QPaintEvent> | |
#include <QPainter> | |
#include "include/core/SkColorSpace.h" | |
#include <include/core/SkFont.h> | |
#include <include/core/SkStream.h> | |
#include "include/core/SkData.h" | |
#include <include/core/SkCanvas.h> | |
#include <include/core/SkPaint.h> | |
#include "include/core/SkImageInfo.h" | |
#include "include/core/SkPixmap.h" | |
#include "include/core/SkSurface.h" | |
#include "include/core/SkSurfaceProps.h" | |
#include "include/core/SkRect.h" | |
#include "include/encode/SkPngEncoder.h" | |
#include <include/gpu/GrDirectContext.h> | |
#include <include/gpu/GrBackendSurface.h> | |
#include <include/gpu/gl/GrGLInterface.h> | |
class LineNumberWidget : public QOpenGLWidget, protected QOpenGLFunctions { | |
private: | |
sk_sp<GrDirectContext> grDirectContext = nullptr; | |
sk_sp<SkSurface> skSurface = nullptr; | |
SkImageInfo info; | |
public: | |
explicit LineNumberWidget(QWidget *widget = nullptr) : QOpenGLWidget(widget) { | |
} | |
protected: | |
void initializeGL() override { | |
initializeOpenGLFunctions();//初始化OPenGL功能函数 | |
glClearColor(0, 0, 0, 0); | |
grDirectContext = makeContext(); | |
skSurface = createSurface(grDirectContext.get(), this->width(), this->height()); | |
if (skSurface == nullptr) abort(); | |
} | |
sk_sp<GrDirectContext> makeContext() { | |
auto grGLInterface = GrGLMakeNativeInterface(); | |
return GrDirectContext::MakeGL(grGLInterface); | |
} | |
sk_sp<SkSurface> createSurface(GrRecordingContext *grRecordingContext, int width, int height) { | |
GrGLFramebufferInfo framebufferInfo; | |
framebufferInfo.fFBOID = 0; | |
framebufferInfo.fFormat = GL_RGBA8; | |
SkColorType colorType = kRGBA_8888_SkColorType; | |
GrBackendRenderTarget backendRenderTarget(width, height, 0, 0, framebufferInfo); | |
return SkSurface::MakeFromBackendRenderTarget(grRecordingContext, | |
backendRenderTarget, kBottomLeft_GrSurfaceOrigin, colorType, | |
nullptr, nullptr); | |
} | |
void resizeGL(int w, int h) override { | |
skSurface = createSurface(grDirectContext.get(), w, h); | |
glViewport(0, 0, w, h); | |
grDirectContext->resetContext(); | |
update(); | |
} | |
void paintGL() override { | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
auto canvas = skSurface->getCanvas(); | |
draw(canvas); | |
} | |
void draw(SkCanvas *canvas) { | |
const SkScalar scale = 256.0f; | |
const SkScalar R = 0.45f * scale; | |
const SkScalar TAU = 6.2831853f; | |
SkPath path; | |
path.moveTo(R, 0.0f); | |
for (int i = 1; i < 7; ++i) { | |
SkScalar theta = 3 * i * TAU / 7; | |
path.lineTo(R * cos(theta), R * sin(theta)); | |
} | |
path.close(); | |
SkPaint p; | |
p.setAntiAlias(true); | |
canvas->clear(SK_ColorWHITE); | |
canvas->save(); | |
canvas->translate(0.5f * scale, 0.5f * scale); | |
canvas->drawPath(path, p); | |
canvas->flush(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment