ebiten/internal/glfw/util_unix.go

43 lines
871 B
Go
Raw Permalink Normal View History

// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2012 The glfw3-go Authors
// SPDX-FileCopyrightText: 2023 The Ebitengine Authors
2023-07-08 06:59:51 +02:00
//go:build darwin || freebsd || linux || netbsd || openbsd
package glfw
2023-07-07 19:16:39 +02:00
// #include <stdlib.h>
2023-10-31 14:34:41 +01:00
// #define GLFW_INCLUDE_NONE
// #include "glfw3_unix.h"
import "C"
2023-10-31 14:34:41 +01:00
import (
"image"
"image/draw"
)
func imageToGLFWImage(img image.Image) (glfwImg C.GLFWimage, free func()) {
b := img.Bounds()
if b.Dx() == 0 || b.Dy() == 0 {
return C.GLFWimage{
width: C.int(b.Dx()),
height: C.int(b.Dy()),
}, func() {}
}
m := image.NewNRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
draw.Draw(m, m.Bounds(), img, b.Min, draw.Src)
pixels := m.Pix
cpixels := C.CBytes(pixels)
free = func() {
C.free(cpixels)
}
2023-10-31 14:34:41 +01:00
return C.GLFWimage{
width: C.int(b.Dx()),
height: C.int(b.Dy()),
pixels: (*C.uchar)(cpixels),
}, free
}