OpenChartProject / OpenChart

✨ NEW REPO ✨ https://github.com/OpenChartProject/OpenChart-web
https://github.com/OpenChartProject/OpenChart-web
Other
8 stars 3 forks source link

Transition from Gtk to SDL #92

Closed Kangaroux closed 3 years ago

Kangaroux commented 4 years ago

📖 Overview

This is a large issue that should be handled by several PRs.

Some of the work that is involved:

Kangaroux commented 4 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.

Kangaroux commented 3 years ago

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();
        }
    }
}
Kangaroux commented 3 years ago

image

Kangaroux commented 3 years ago

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...