Skip to content

Instantly share code, notes, and snippets.

@mattleibow
Last active June 9, 2022 17:07
Show Gist options
  • Save mattleibow/fb6bfa5146d776f26febfa3a6d4286f0 to your computer and use it in GitHub Desktop.
Save mattleibow/fb6bfa5146d776f26febfa3a6d4286f0 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <GL/glut.h>
#include <GrContext.h>
#include <SkSurface.h>
#include <SkCanvas.h>
#include <SkPaint.h>
#include <SkShader.h>
#include <../effects/SkGradientShader.h>
GrContext* skiaContext = nullptr;
SkSurface* skiaSurface = nullptr;
static void init_skia(int w, int h)
{
skiaContext = GrContext::Create(kOpenGL_GrBackend, 0);
GrBackendRenderTargetDesc desc;
desc.fWidth = w;
desc.fHeight = h;
desc.fConfig = kSkia8888_GrPixelConfig;
desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
desc.fSampleCnt = 1;
desc.fStencilBits = 0;
desc.fRenderTargetHandle = 0; // assume default framebuffer
skiaSurface = SkSurface::MakeFromBackendRenderTarget(skiaContext, desc, NULL).release();
}
static void cleanup_skia()
{
delete skiaSurface;
delete skiaContext;
}
static void draw_skia(SkCanvas* skiaCanvas)
{
skiaCanvas->clear(SK_ColorBLACK);
SkPaint paint;
paint.setFilterQuality(kLow_SkFilterQuality);
paint.setColor(SK_ColorWHITE);
paint.setTextSize(24.0f);
char outString[64];
sprintf_s(outString, "C++ Rocks! (maybe)");
skiaCanvas->drawText(outString, strlen(outString), 50.f, 200.f, paint);
SkPoint points[] = { SkPoint::Make(0, 50), SkPoint::Make(0, 150) };
SkColor colors[] = { SK_ColorRED, SK_ColorGREEN };
paint.setShader(SkGradientShader::MakeLinear(points, colors, NULL, 2, SkShader::TileMode::kClamp_TileMode));
skiaCanvas->drawCircle(100, 100, 50, paint);
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
SkCanvas* skiaCanvas = skiaSurface->getCanvas();
draw_skia(skiaCanvas);
skiaCanvas->flush();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
init_skia(500, 500);
glutMainLoop();
cleanup_skia();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment