mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 11:18:54 +01:00
internal/glfw: refactoring
This commit is contained in:
parent
5da80ef3a7
commit
4a00891960
@ -54,7 +54,6 @@ import "C"
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/draw"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
@ -247,23 +246,10 @@ func (w *Window) SetCursorPos(xpos, ypos float64) error {
|
||||
// The cursor hotspot is specified in pixels, relative to the upper-left corner of the cursor image.
|
||||
// Like all other coordinate systems in GLFW, the X-axis points to the right and the Y-axis points down.
|
||||
func CreateCursor(img image.Image, xhot, yhot int) (*Cursor, error) {
|
||||
b := img.Bounds()
|
||||
glfwImg, free := imageToGLFWImage(img)
|
||||
defer free()
|
||||
|
||||
m := image.NewNRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
|
||||
draw.Draw(m, m.Bounds(), img, b.Min, draw.Src)
|
||||
pixels := m.Pix
|
||||
|
||||
pix, free := bytes(pixels)
|
||||
|
||||
imgC := C.GLFWimage{
|
||||
width: C.int(b.Dx()),
|
||||
height: C.int(b.Dy()),
|
||||
pixels: (*C.uchar)(pix),
|
||||
}
|
||||
|
||||
c := C.glfwCreateCursor(&imgC, C.int(xhot), C.int(yhot))
|
||||
|
||||
free()
|
||||
c := C.glfwCreateCursor(&glfwImg, C.int(xhot), C.int(yhot))
|
||||
if err := fetchErrorIgnoringPlatformError(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -7,14 +7,36 @@
|
||||
package glfw
|
||||
|
||||
// #include <stdlib.h>
|
||||
// #define GLFW_INCLUDE_NONE
|
||||
// #include "glfw3_unix.h"
|
||||
import "C"
|
||||
|
||||
func bytes(origin []byte) (pointer *uint8, free func()) {
|
||||
n := len(origin)
|
||||
if n == 0 {
|
||||
return nil, func() {}
|
||||
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() {}
|
||||
}
|
||||
|
||||
ptr := C.CBytes(origin)
|
||||
return (*uint8)(ptr), func() { C.free(ptr) }
|
||||
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)
|
||||
}
|
||||
|
||||
return C.GLFWimage{
|
||||
width: C.int(b.Dx()),
|
||||
height: C.int(b.Dy()),
|
||||
pixels: (*C.uchar)(cpixels),
|
||||
}, free
|
||||
}
|
||||
|
@ -60,7 +60,6 @@ import "C"
|
||||
import (
|
||||
"errors"
|
||||
"image"
|
||||
"image/draw"
|
||||
"sync"
|
||||
"unsafe"
|
||||
)
|
||||
@ -357,34 +356,20 @@ func (w *Window) SetTitle(title string) error {
|
||||
// images will be rescaled as needed. Good sizes include 16x16, 32x32 and 48x48.
|
||||
func (w *Window) SetIcon(images []image.Image) error {
|
||||
count := len(images)
|
||||
cimages := make([]C.GLFWimage, count)
|
||||
freePixels := make([]func(), count)
|
||||
glfwImgs := make([]C.GLFWimage, 0, count)
|
||||
|
||||
for i, img := range images {
|
||||
b := img.Bounds()
|
||||
|
||||
m := image.NewNRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
|
||||
draw.Draw(m, m.Bounds(), img, b.Min, draw.Src)
|
||||
pixels := m.Pix
|
||||
|
||||
pix, free := bytes(pixels)
|
||||
freePixels[i] = free
|
||||
|
||||
cimages[i].width = C.int(b.Dx())
|
||||
cimages[i].height = C.int(b.Dy())
|
||||
cimages[i].pixels = (*C.uchar)(pix)
|
||||
for _, img := range images {
|
||||
glfwImg, free := imageToGLFWImage(img)
|
||||
defer free()
|
||||
glfwImgs = append(glfwImgs, glfwImg)
|
||||
}
|
||||
|
||||
var p *C.GLFWimage
|
||||
if count > 0 {
|
||||
p = &cimages[0]
|
||||
p = &glfwImgs[0]
|
||||
}
|
||||
C.glfwSetWindowIcon(w.data, C.int(count), p)
|
||||
|
||||
for _, v := range freePixels {
|
||||
v()
|
||||
}
|
||||
|
||||
if err := fetchErrorIgnoringPlatformError(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user