Skip to content

Instantly share code, notes, and snippets.

@derofim
Forked from andrew-pa/crsl.cpp
Created March 10, 2019 09:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derofim/033cb33ed46636071d3983bb7b235981 to your computer and use it in GitHub Desktop.
Save derofim/033cb33ed46636071d3983bb7b235981 to your computer and use it in GitHub Desktop.
Cairo, GL, SDL together
#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