Created
July 5, 2018 04:35
-
-
Save xkisu/eaa01c898a13e868c3ad06098f525c8d to your computer and use it in GitHub Desktop.
(July 2018) Example of using Skia with an SFML OpenGL window. Snippet extracted from a side project
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
#include <iostream> | |
#include "GrGLInterface.h" | |
#include "GrBackendSurface.h" | |
#include "GrContext.h" | |
#include "SkCanvas.h" | |
#include "SkColorSpace.h" | |
#include "SkSurface.h" | |
#include <SFML/Graphics.hpp> | |
#include <SFML/Window/Window.hpp> | |
// Provides the OpenGL method context for Skia | |
#include <SFML/OpenGL.hpp> | |
int width = 800; | |
int height = 600; | |
GrContext* context; | |
SkSurface* surface; | |
void make_skia_surface () { | |
GrContextOptions options; | |
context = GrContext::MakeGL(nullptr, options).release(); | |
GrGLFramebufferInfo framebufferInfo; | |
framebufferInfo.fFBOID = 0; // assume default framebuffer | |
framebufferInfo.fFormat = GL_RGBA8; | |
SkColorType colorType; | |
if (kRGBA_8888_GrPixelConfig == kSkia8888_GrPixelConfig) { | |
colorType = kRGBA_8888_SkColorType; | |
} else { | |
colorType = kBGRA_8888_SkColorType; | |
} | |
GrBackendRenderTarget backendRenderTarget(width, height, | |
0, // sample count | |
0, // stencil bits | |
framebufferInfo); | |
surface = SkSurface::MakeFromBackendRenderTarget(context, backendRenderTarget, kBottomLeft_GrSurfaceOrigin, colorType, nullptr, nullptr).release(); | |
if (surface == nullptr) { | |
std::cerr << "SkSurface is null" << std::endl; | |
exit(1); | |
} | |
} | |
int main() { | |
const char * title = "Skia and SFML example"; | |
sf::RenderWindow *window = new sf::RenderWindow(sf::VideoMode(width, height), title); | |
make_skia_surface(); | |
const char* helpMessage = "Click and drag to create rects. Press esc to quit."; | |
SkPaint paint; | |
while (window->isOpen()) { | |
sf::Event event; | |
while (window->pollEvent(event)) { | |
// "close requested" event: we close the window | |
if (event.type == sf::Event::Closed) { | |
window->close(); | |
} else if (event.type == sf::Event::Resized) { | |
// adjust the viewport when the window is resized | |
glViewport(0, 0, event.size.width, event.size.height); | |
// simply re-create the canvas on resize | |
make_skia_surface(); | |
} | |
} | |
SkCanvas* canvas = surface->getCanvas(); | |
canvas->clear(SK_ColorWHITE); | |
paint.setColor(SK_ColorGREEN); | |
canvas->drawText(helpMessage, strlen(helpMessage), SkIntToScalar(100), SkIntToScalar(100), paint); | |
canvas->flush(); | |
window->display(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment