Closed Kangaroux closed 3 years ago
The Cairo docs show that it's fairly easy to integrate Cairo with SDL. While the notefield could be rewritten to just use image blitting and thus not need Cairo I think it's worth keeping Cairo around as that will be really useful later on for custom UIs.
I created an example to test SDL + Cairo, using the latest version of CairoSharp.
This uses SDL to create a new window and handle events. I grabbed the SDL_Surface
for the window and used that as the target for Cairo.ImageSurface
and created a Cairo.Context
using that surface. Once you have the context you can do pretty much anything you want with Cairo. When you're done drawing you swap the buffer with SDL_UpdateWindowSurface()
.
using SDL2;
namespace CairoExample
{
class Program
{
static void Main(string[] args)
{
SDL.SDL_Init(SDL.SDL_INIT_VIDEO);
var win = SDL.SDL_CreateWindow("CairoExample", 0, 0, 800, 600, SDL.SDL_WindowFlags.SDL_WINDOW_SHOWN);
Cairo.ImageSurface cairoSurface;
unsafe
{
var surface = (SDL.SDL_Surface*)SDL.SDL_GetWindowSurface(win);
cairoSurface = new Cairo.ImageSurface(
surface->pixels, Cairo.Format.RGB24, surface->w, surface->h, surface->pitch
);
}
var ctx = new Cairo.Context(cairoSurface);
var quit = false;
SDL.SDL_Event e;
while (!quit)
{
SDL.SDL_PollEvent(out e);
if (e.type == SDL.SDL_EventType.SDL_QUIT)
{
quit = true;
}
ctx.SetSourceRGB(0.2, 0.4, 0.6);
ctx.Paint();
ctx.SetSourceRGB(1, 1, 1);
ctx.LineWidth = 3;
ctx.MoveTo(0, 0);
ctx.LineTo(800, 600);
ctx.Stroke();
SDL.SDL_UpdateWindowSurface(win);
}
SDL.SDL_DestroyWindow(win);
SDL.SDL_Quit();
}
}
}
Interestingly the SDL_Surface
for the window seems to be 32 bits (after checking surface->format
) but configuring Cairo to use a 24 bit surface works just fine...
📖 Overview
This is a large issue that should be handled by several PRs.
Some of the work that is involved: