ungerik / go-cairo

Go binding for the cairo graphics library
Other
142 stars 32 forks source link

Support for cairo_image_surface_create_from_data #20

Closed usedbytes closed 6 years ago

usedbytes commented 6 years ago

Pass in an unsafe.Pointer to the surface data. This allows for surfaces to be created e.g. from an SDL surface (would address #3):

package main

import (
    "time"
    "github.com/veandco/go-sdl2/sdl"
    "github.com/ungerik/go-cairo"
)

func main() {
    sdl.Init(sdl.INIT_EVERYTHING);
    defer sdl.Quit()

    window, _ := sdl.CreateWindow("test", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
        800, 600, sdl.WINDOW_SHOWN)
    sdlSurface, _ := window.GetSurface()
    sdlSurface.FillRect(nil, 0)

    cairoSurface := cairo.NewSurfaceFromData(sdlSurface.Data(),
        cairo.FORMAT_ARGB32, int(sdlSurface.W), int(sdlSurface.H),
        int(sdlSurface.Pitch))
    cairoSurface.SelectFontFace("serif", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
    cairoSurface.SetFontSize(32.0)
    cairoSurface.SetSourceRGB(0.0, 0.0, 1.0)
    cairoSurface.MoveTo(10.0, 50.0)
    cairoSurface.ShowText("Hello World")
    cairoSurface.Finish()

    window.UpdateSurface()

    time.Sleep(5 * time.Second)
}