mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-23 17:32:02 +01:00
Reduce currentUI usages
This commit is contained in:
parent
26b4ded0f1
commit
5f4aa33edf
22
image.go
22
image.go
@ -98,15 +98,11 @@ func (t *textureQuads) Texture(i int) (u0, v0, u1, v1 float32) {
|
|||||||
return u(float64(src.Min.X), w), v(float64(src.Min.Y), h), u(float64(src.Max.X), w), v(float64(src.Max.Y), h)
|
return u(float64(src.Min.X), w), v(float64(src.Min.Y), h), u(float64(src.Max.X), w), v(float64(src.Max.Y), h)
|
||||||
}
|
}
|
||||||
|
|
||||||
type syncer interface {
|
|
||||||
Sync(func())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Image represents an image.
|
// Image represents an image.
|
||||||
// The pixel format is alpha-premultiplied.
|
// The pixel format is alpha-premultiplied.
|
||||||
// Image implements image.Image.
|
// Image implements image.Image.
|
||||||
type Image struct {
|
type Image struct {
|
||||||
syncer syncer
|
ui *ui
|
||||||
inner *innerImage
|
inner *innerImage
|
||||||
pixels []uint8
|
pixels []uint8
|
||||||
}
|
}
|
||||||
@ -119,8 +115,8 @@ func (i *Image) Size() (width, height int) {
|
|||||||
// Clear resets the pixels of the image into 0.
|
// Clear resets the pixels of the image into 0.
|
||||||
func (i *Image) Clear() (err error) {
|
func (i *Image) Clear() (err error) {
|
||||||
i.pixels = nil
|
i.pixels = nil
|
||||||
i.syncer.Sync(func() {
|
i.ui.use(func() {
|
||||||
err = i.inner.Clear(currentUI.glContext)
|
err = i.inner.Clear(i.ui.glContext)
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -128,8 +124,8 @@ func (i *Image) Clear() (err error) {
|
|||||||
// Fill fills the image with a solid color.
|
// Fill fills the image with a solid color.
|
||||||
func (i *Image) Fill(clr color.Color) (err error) {
|
func (i *Image) Fill(clr color.Color) (err error) {
|
||||||
i.pixels = nil
|
i.pixels = nil
|
||||||
i.syncer.Sync(func() {
|
i.ui.use(func() {
|
||||||
err = i.inner.Fill(currentUI.glContext, clr)
|
err = i.inner.Fill(i.ui.glContext, clr)
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -151,8 +147,8 @@ func (i *Image) DrawImage(image *Image, options *DrawImageOptions) (err error) {
|
|||||||
|
|
||||||
func (i *Image) drawImage(image *innerImage, option *DrawImageOptions) (err error) {
|
func (i *Image) drawImage(image *innerImage, option *DrawImageOptions) (err error) {
|
||||||
i.pixels = nil
|
i.pixels = nil
|
||||||
i.syncer.Sync(func() {
|
i.ui.use(func() {
|
||||||
err = i.inner.drawImage(currentUI.glContext, image, option)
|
err = i.inner.drawImage(i.ui.glContext, image, option)
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -173,9 +169,9 @@ func (i *Image) ColorModel() color.Model {
|
|||||||
// This method loads pixels from GPU to VRAM if necessary.
|
// This method loads pixels from GPU to VRAM if necessary.
|
||||||
func (i *Image) At(x, y int) color.Color {
|
func (i *Image) At(x, y int) color.Color {
|
||||||
if i.pixels == nil {
|
if i.pixels == nil {
|
||||||
i.syncer.Sync(func() {
|
i.ui.use(func() {
|
||||||
var err error
|
var err error
|
||||||
i.pixels, err = i.inner.texture.Pixels(currentUI.glContext)
|
i.pixels, err = i.inner.texture.Pixels(i.ui.glContext)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
8
run.go
8
run.go
@ -28,18 +28,12 @@ import (
|
|||||||
// but this is not strictly guaranteed.
|
// but this is not strictly guaranteed.
|
||||||
// If you need to care about time, you need to check current time every time f is called.
|
// If you need to care about time, you need to check current time every time f is called.
|
||||||
func Run(f func(*Image) error, width, height, scale int, title string) error {
|
func Run(f func(*Image) error, width, height, scale int, title string) error {
|
||||||
err := startUI(width, height, scale, title)
|
ui, err := startUI(width, height, scale, title)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
ui := currentUI
|
|
||||||
defer ui.terminate()
|
defer ui.terminate()
|
||||||
|
|
||||||
currentUI = ui
|
|
||||||
defer func() {
|
|
||||||
currentUI = nil
|
|
||||||
}()
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
// To avoid busy loop when the window is inactive, wait 1/120 [sec] at least.
|
// To avoid busy loop when the window is inactive, wait 1/120 [sec] at least.
|
||||||
ch := time.After(1 * time.Second / 120)
|
ch := time.After(1 * time.Second / 120)
|
||||||
|
31
ui.go
31
ui.go
@ -42,15 +42,16 @@ func init() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
currentUI = &ui{
|
u := &ui{
|
||||||
window: window,
|
window: window,
|
||||||
funcs: make(chan func()),
|
funcs: make(chan func()),
|
||||||
}
|
}
|
||||||
currentUI.run()
|
u.run()
|
||||||
currentUI.use(func() {
|
u.use(func() {
|
||||||
currentUI.glContext = opengl.NewContext()
|
u.glContext = opengl.NewContext()
|
||||||
glfw.SwapInterval(1)
|
glfw.SwapInterval(1)
|
||||||
})
|
})
|
||||||
|
currentUI = u
|
||||||
}
|
}
|
||||||
|
|
||||||
type ui struct {
|
type ui struct {
|
||||||
@ -62,20 +63,21 @@ type ui struct {
|
|||||||
funcs chan func()
|
funcs chan func()
|
||||||
}
|
}
|
||||||
|
|
||||||
func startUI(width, height, scale int, title string) error {
|
func startUI(width, height, scale int, title string) (*ui, error) {
|
||||||
monitor, err := glfw.GetPrimaryMonitor()
|
monitor, err := glfw.GetPrimaryMonitor()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
videoMode, err := monitor.GetVideoMode()
|
videoMode, err := monitor.GetVideoMode()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
x := (videoMode.Width - width*scale) / 2
|
x := (videoMode.Width - width*scale) / 2
|
||||||
y := (videoMode.Height - height*scale) / 3
|
y := (videoMode.Height - height*scale) / 3
|
||||||
|
|
||||||
ch := make(chan struct{})
|
ch := make(chan struct{})
|
||||||
window := currentUI.window
|
ui := currentUI
|
||||||
|
window := ui.window
|
||||||
window.SetFramebufferSizeCallback(func(w *glfw.Window, width, height int) {
|
window.SetFramebufferSizeCallback(func(w *glfw.Window, width, height int) {
|
||||||
close(ch)
|
close(ch)
|
||||||
})
|
})
|
||||||
@ -97,7 +99,6 @@ func startUI(width, height, scale int, title string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ui := currentUI
|
|
||||||
ui.scale = scale
|
ui.scale = scale
|
||||||
|
|
||||||
// For retina displays, recalculate the scale with the framebuffer size.
|
// For retina displays, recalculate the scale with the framebuffer size.
|
||||||
@ -106,7 +107,7 @@ func startUI(width, height, scale int, title string) error {
|
|||||||
ui.use(func() {
|
ui.use(func() {
|
||||||
ui.graphicsContext, err = newGraphicsContext(ui.glContext, width, height, realScale)
|
ui.graphicsContext, err = newGraphicsContext(ui.glContext, width, height, realScale)
|
||||||
})
|
})
|
||||||
return err
|
return ui, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *ui) doEvents() {
|
func (u *ui) doEvents() {
|
||||||
@ -122,10 +123,6 @@ func (u *ui) isClosed() bool {
|
|||||||
return u.window.ShouldClose()
|
return u.window.ShouldClose()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *ui) Sync(f func()) {
|
|
||||||
u.use(f)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *ui) draw(f func(*Image) error) (err error) {
|
func (u *ui) draw(f func(*Image) error) (err error) {
|
||||||
u.use(func() {
|
u.use(func() {
|
||||||
err = u.graphicsContext.preUpdate()
|
err = u.graphicsContext.preUpdate()
|
||||||
@ -133,7 +130,7 @@ func (u *ui) draw(f func(*Image) error) (err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = f(&Image{syncer: u, inner: u.graphicsContext.screen})
|
err = f(&Image{ui: u, inner: u.graphicsContext.screen})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -161,7 +158,7 @@ func (u *ui) newImageFromImage(img image.Image, filter Filter) (*Image, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &Image{syncer: u, inner: innerImage}, nil
|
return &Image{ui: u, inner: innerImage}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *ui) newImage(width, height int, filter Filter) (*Image, error) {
|
func (u *ui) newImage(width, height int, filter Filter) (*Image, error) {
|
||||||
@ -179,7 +176,7 @@ func (u *ui) newImage(width, height int, filter Filter) (*Image, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &Image{syncer: u, inner: innerImage}, nil
|
return &Image{ui: u, inner: innerImage}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *ui) run() {
|
func (u *ui) run() {
|
||||||
|
Loading…
Reference in New Issue
Block a user