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 (
|
import (
|
||||||
"image"
|
"image"
|
||||||
"image/draw"
|
|
||||||
"unsafe"
|
"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.
|
// 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.
|
// 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) {
|
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()))
|
c := C.glfwCreateCursor(&glfwImg, C.int(xhot), C.int(yhot))
|
||||||
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()
|
|
||||||
if err := fetchErrorIgnoringPlatformError(); err != nil {
|
if err := fetchErrorIgnoringPlatformError(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -7,14 +7,36 @@
|
|||||||
package glfw
|
package glfw
|
||||||
|
|
||||||
// #include <stdlib.h>
|
// #include <stdlib.h>
|
||||||
|
// #define GLFW_INCLUDE_NONE
|
||||||
|
// #include "glfw3_unix.h"
|
||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
func bytes(origin []byte) (pointer *uint8, free func()) {
|
import (
|
||||||
n := len(origin)
|
"image"
|
||||||
if n == 0 {
|
"image/draw"
|
||||||
return nil, func() {}
|
)
|
||||||
|
|
||||||
|
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)
|
m := image.NewNRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
|
||||||
return (*uint8)(ptr), func() { C.free(ptr) }
|
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 (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"image"
|
"image"
|
||||||
"image/draw"
|
|
||||||
"sync"
|
"sync"
|
||||||
"unsafe"
|
"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.
|
// images will be rescaled as needed. Good sizes include 16x16, 32x32 and 48x48.
|
||||||
func (w *Window) SetIcon(images []image.Image) error {
|
func (w *Window) SetIcon(images []image.Image) error {
|
||||||
count := len(images)
|
count := len(images)
|
||||||
cimages := make([]C.GLFWimage, count)
|
glfwImgs := make([]C.GLFWimage, 0, count)
|
||||||
freePixels := make([]func(), count)
|
|
||||||
|
|
||||||
for i, img := range images {
|
for _, img := range images {
|
||||||
b := img.Bounds()
|
glfwImg, free := imageToGLFWImage(img)
|
||||||
|
defer free()
|
||||||
m := image.NewNRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
|
glfwImgs = append(glfwImgs, glfwImg)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var p *C.GLFWimage
|
var p *C.GLFWimage
|
||||||
if count > 0 {
|
if count > 0 {
|
||||||
p = &cimages[0]
|
p = &glfwImgs[0]
|
||||||
}
|
}
|
||||||
C.glfwSetWindowIcon(w.data, C.int(count), p)
|
C.glfwSetWindowIcon(w.data, C.int(count), p)
|
||||||
|
|
||||||
for _, v := range freePixels {
|
|
||||||
v()
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := fetchErrorIgnoringPlatformError(); err != nil {
|
if err := fetchErrorIgnoringPlatformError(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user