-
-
Save derofim/033cb33ed46636071d3983bb7b235981 to your computer and use it in GitHub Desktop.
Cairo, GL, SDL together
This file contains hidden or 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 <cairo/cairo.h> | |
#include <cairo/cairo-gl.h> | |
#include <SDL2/SDL.h> | |
#include <SDL2/SDL_syswm.h> | |
int main(int argc, char* argv[]) { | |
SDL_Init(SDL_INIT_VIDEO); | |
auto win = SDL_CreateWindow("crsl", 300, 300, 512, 512, SDL_WINDOW_OPENGL); | |
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); | |
/*SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, | |
SDL_GL_CONTEXT_PROFILE_CORE); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); | |
SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);*/ | |
SDL_GL_SetSwapInterval(1); | |
auto glcx = SDL_GL_CreateContext(win); | |
SDL_SysWMinfo wmifo; | |
SDL_GetWindowWMInfo(win, &wmifo); | |
auto cd = cairo_glx_device_create(wmifo.info.x11.display, | |
(GLXContext)glcx); | |
if(!cd) { | |
std::cout << "failed to get GLX device" << std::endl; | |
return 0; | |
} | |
auto gl_surf = cairo_gl_surface_create_for_window(cd, wmifo.info.x11.window, 512, 512); | |
if(!gl_surf) { | |
std::cout << "failed to get GL surface" << std::endl; | |
return 0; | |
} | |
auto cx = cairo_create(gl_surf); | |
float t = 0.; | |
while(true) { | |
cairo_set_source_rgb(cx, 1.0, t, 0.5); | |
cairo_fill(cx); | |
SDL_GL_SwapWindow(win); | |
t += 0.01; | |
} | |
SDL_GL_DeleteContext(glcx); | |
SDL_DestroyWindow(win); | |
SDL_Quit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment