diff --git a/internal/graphics/opengl/context.go b/internal/graphics/opengl/context.go index 8757bff88..21786da5a 100644 --- a/internal/graphics/opengl/context.go +++ b/internal/graphics/opengl/context.go @@ -68,7 +68,9 @@ func (c *Context) bindFramebuffer(f Framebuffer) error { } func (c *Context) SetViewport(f Framebuffer, width, height int) error { - c.bindFramebuffer(f) + if err := c.bindFramebuffer(f); err != nil { + return err + } if c.lastViewportWidth != width || c.lastViewportHeight != height { if err := c.setViewportImpl(width, height); err != nil { return nil diff --git a/internal/graphics/opengl/context_desktop.go b/internal/graphics/opengl/context_desktop.go index 3484dfcec..143528d18 100644 --- a/internal/graphics/opengl/context_desktop.go +++ b/internal/graphics/opengl/context_desktop.go @@ -120,7 +120,7 @@ func (c *Context) Reset() error { } func (c *Context) BlendFunc(mode CompositeMode) { - c.runOnContextThread(func() error { + _ = c.runOnContextThread(func() error { if c.lastCompositeMode == mode { return nil } @@ -183,7 +183,9 @@ func (c *Context) FramebufferPixels(f Framebuffer, width, height int) ([]uint8, }); err != nil { return nil, err } - c.bindFramebuffer(f) + if err := c.bindFramebuffer(f); err != nil { + return nil, err + } if err := c.runOnContextThread(func() error { pixels = make([]uint8, 4*width*height) gl.ReadPixels(0, 0, int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(pixels)) @@ -199,7 +201,7 @@ func (c *Context) FramebufferPixels(f Framebuffer, width, height int) ([]uint8, } func (c *Context) bindTextureImpl(t Texture) error { - c.runOnContextThread(func() error { + _ = c.runOnContextThread(func() error { gl.BindTexture(gl.TEXTURE_2D, uint32(t)) return nil }) @@ -207,7 +209,7 @@ func (c *Context) bindTextureImpl(t Texture) error { } func (c *Context) DeleteTexture(t Texture) { - c.runOnContextThread(func() error { + _ = c.runOnContextThread(func() error { tt := uint32(t) if !gl.IsTexture(tt) { return nil @@ -222,7 +224,7 @@ func (c *Context) DeleteTexture(t Texture) { func (c *Context) IsTexture(t Texture) bool { r := false - c.runOnContextThread(func() error { + _ = c.runOnContextThread(func() error { r = gl.IsTexture(uint32(t)) return nil }) @@ -230,7 +232,7 @@ func (c *Context) IsTexture(t Texture) bool { } func (c *Context) TexSubImage2D(p []uint8, width, height int) { - c.runOnContextThread(func() error { + _ = c.runOnContextThread(func() error { gl.TexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(p)) return nil }) @@ -253,7 +255,9 @@ func (c *Context) NewFramebuffer(texture Texture) (Framebuffer, error) { }); err != nil { return 0, err } - c.bindFramebuffer(Framebuffer(f)) + if err := c.bindFramebuffer(Framebuffer(f)); err != nil { + return 0, err + } if err := c.runOnContextThread(func() error { gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, uint32(texture), 0) s := gl.CheckFramebufferStatus(gl.FRAMEBUFFER) @@ -290,7 +294,7 @@ func (c *Context) FillFramebuffer(r, g, b, a float64) error { } func (c *Context) DeleteFramebuffer(f Framebuffer) { - c.runOnContextThread(func() error { + _ = c.runOnContextThread(func() error { ff := uint32(f) if !gl.IsFramebuffer(ff) { return nil @@ -337,7 +341,7 @@ func (c *Context) NewShader(shaderType ShaderType, source string) (Shader, error } func (c *Context) DeleteShader(s Shader) { - c.runOnContextThread(func() error { + _ = c.runOnContextThread(func() error { gl.DeleteShader(uint32(s)) return nil }) @@ -373,14 +377,14 @@ func (c *Context) NewProgram(shaders []Shader) (Program, error) { } func (c *Context) UseProgram(p Program) { - c.runOnContextThread(func() error { + _ = c.runOnContextThread(func() error { gl.UseProgram(uint32(p)) return nil }) } func (c *Context) DeleteProgram(p Program) { - c.runOnContextThread(func() error { + _ = c.runOnContextThread(func() error { if !gl.IsProgram(uint32(p)) { return nil } @@ -398,7 +402,7 @@ func (c *Context) getUniformLocationImpl(p Program, location string) uniformLoca } func (c *Context) UniformInt(p Program, location string, v int) { - c.runOnContextThread(func() error { + _ = c.runOnContextThread(func() error { l := int32(c.locationCache.GetUniformLocation(c, p, location)) gl.Uniform1i(l, int32(v)) return nil @@ -406,7 +410,7 @@ func (c *Context) UniformInt(p Program, location string, v int) { } func (c *Context) UniformFloats(p Program, location string, v []float32) { - c.runOnContextThread(func() error { + _ = c.runOnContextThread(func() error { l := int32(c.locationCache.GetUniformLocation(c, p, location)) switch len(v) { case 4: @@ -429,7 +433,7 @@ func (c *Context) getAttribLocationImpl(p Program, location string) attribLocati } func (c *Context) VertexAttribPointer(p Program, location string, normalize bool, stride int, size int, v int) { - c.runOnContextThread(func() error { + _ = c.runOnContextThread(func() error { l := c.locationCache.GetAttribLocation(c, p, location) gl.VertexAttribPointer(uint32(l), int32(size), gl.SHORT, normalize, int32(stride), gl.PtrOffset(v)) return nil @@ -437,7 +441,7 @@ func (c *Context) VertexAttribPointer(p Program, location string, normalize bool } func (c *Context) EnableVertexAttribArray(p Program, location string) { - c.runOnContextThread(func() error { + _ = c.runOnContextThread(func() error { l := c.locationCache.GetAttribLocation(c, p, location) gl.EnableVertexAttribArray(uint32(l)) return nil @@ -445,7 +449,7 @@ func (c *Context) EnableVertexAttribArray(p Program, location string) { } func (c *Context) DisableVertexAttribArray(p Program, location string) { - c.runOnContextThread(func() error { + _ = c.runOnContextThread(func() error { l := c.locationCache.GetAttribLocation(c, p, location) gl.DisableVertexAttribArray(uint32(l)) return nil @@ -454,7 +458,7 @@ func (c *Context) DisableVertexAttribArray(p Program, location string) { func (c *Context) NewBuffer(bufferType BufferType, v interface{}, bufferUsage BufferUsage) Buffer { var buffer Buffer - c.runOnContextThread(func() error { + _ = c.runOnContextThread(func() error { var b uint32 gl.GenBuffers(1, &b) gl.BindBuffer(uint32(bufferType), b) @@ -473,21 +477,21 @@ func (c *Context) NewBuffer(bufferType BufferType, v interface{}, bufferUsage Bu } func (c *Context) BindElementArrayBuffer(b Buffer) { - c.runOnContextThread(func() error { + _ = c.runOnContextThread(func() error { gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, uint32(b)) return nil }) } func (c *Context) BufferSubData(bufferType BufferType, data []int16) { - c.runOnContextThread(func() error { + _ = c.runOnContextThread(func() error { gl.BufferSubData(uint32(bufferType), 0, 2*len(data), gl.Ptr(data)) return nil }) } func (c *Context) DeleteBuffer(b Buffer) { - c.runOnContextThread(func() error { + _ = c.runOnContextThread(func() error { bb := uint32(b) gl.DeleteBuffers(1, &bb) return nil @@ -495,14 +499,14 @@ func (c *Context) DeleteBuffer(b Buffer) { } func (c *Context) DrawElements(mode Mode, len int, offsetInBytes int) { - c.runOnContextThread(func() error { + _ = c.runOnContextThread(func() error { gl.DrawElements(uint32(mode), int32(len), gl.UNSIGNED_SHORT, gl.PtrOffset(offsetInBytes)) return nil }) } func (c *Context) Flush() { - c.runOnContextThread(func() error { + _ = c.runOnContextThread(func() error { gl.Flush() return nil }) diff --git a/internal/ui/ui.go b/internal/ui/ui.go index 5f5af75b7..f5349a10b 100644 --- a/internal/ui/ui.go +++ b/internal/ui/ui.go @@ -20,6 +20,6 @@ type UserInterface interface { SwapBuffers() error Terminate() error ScreenScale() float64 - SetScreenSize(width, height int) bool - SetScreenScale(scale float64) bool + SetScreenSize(width, height int) (bool, error) + SetScreenScale(scale float64) (bool, error) } diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index 5a6315daf..aca893d0d 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -111,27 +111,39 @@ func (u *userInterface) runOnMainThread(f func() error) error { return err } -func (u *userInterface) SetScreenSize(width, height int) bool { +func (u *userInterface) SetScreenSize(width, height int) (bool, error) { r := false - u.runOnMainThread(func() error { - r = u.setScreenSize(width, height, u.scale) + if err := u.runOnMainThread(func() error { + var err error + r, err = u.setScreenSize(width, height, u.scale) + if err != nil { + return err + } return nil - }) - return r + }); err != nil { + return false, err + } + return r, nil } -func (u *userInterface) SetScreenScale(scale float64) bool { +func (u *userInterface) SetScreenScale(scale float64) (bool, error) { r := false - u.runOnMainThread(func() error { - r = u.setScreenSize(u.width, u.height, scale) + if err := u.runOnMainThread(func() error { + var err error + r, err = u.setScreenSize(u.width, u.height, scale) + if err != nil { + return err + } return nil - }) - return r + }); err != nil { + return false, err + } + return r, nil } func (u *userInterface) ScreenScale() float64 { s := 0.0 - u.runOnMainThread(func() error { + _ = u.runOnMainThread(func() error { s = u.scale return nil }) @@ -149,7 +161,11 @@ func (u *userInterface) Start(width, height int, scale float64, title string) er if err := u.runOnMainThread(func() error { m := glfw.GetPrimaryMonitor() v := m.GetVideoMode() - if !u.setScreenSize(width, height, scale) { + r, err := u.setScreenSize(width, height, scale) + if err != nil { + return err + } + if !r { return errors.New("ui: Fail to set the screen size") } u.window.SetTitle(title) @@ -181,7 +197,7 @@ func (u *userInterface) pollEvents() error { func (u *userInterface) Update() (interface{}, error) { shouldClose := false - u.runOnMainThread(func() error { + _ = u.runOnMainThread(func() error { shouldClose = u.window.ShouldClose() return nil }) @@ -190,7 +206,7 @@ func (u *userInterface) Update() (interface{}, error) { } var screenSizeEvent *ScreenSizeEvent - u.runOnMainThread(func() error { + _ = u.runOnMainThread(func() error { if !u.sizeChanged { return nil } @@ -231,7 +247,7 @@ func (u *userInterface) Update() (interface{}, error) { } func (u *userInterface) Terminate() error { - u.runOnMainThread(func() error { + _ = u.runOnMainThread(func() error { glfw.Terminate() return nil }) @@ -261,9 +277,9 @@ func (u *userInterface) FinishRendering() error { return nil } -func (u *userInterface) setScreenSize(width, height int, scale float64) bool { +func (u *userInterface) setScreenSize(width, height int, scale float64) (bool, error) { if u.width == width && u.height == height && u.scale == scale { - return false + return false, nil } origScale := u.scale @@ -275,14 +291,16 @@ func (u *userInterface) setScreenSize(width, height int, scale float64) bool { const minWindowWidth = 252 if int(float64(width)*u.actualScreenScale()) < minWindowWidth { u.scale = origScale - return false + return false, nil } u.width = width u.height = height // To make sure the current existing framebuffers are rendered, // swap buffers here before SetSize is called. - u.swapBuffers() + if err := u.swapBuffers(); err != nil { + return false, err + } ch := make(chan struct{}) window := u.window @@ -303,5 +321,5 @@ event: } } u.sizeChanged = true - return true + return true, nil } diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index 944e43719..29eb83836 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -64,13 +64,13 @@ func vsync() { <-ch } -func (u *userInterface) SetScreenSize(width, height int) bool { - return u.setScreenSize(width, height, u.scale) +func (u *userInterface) SetScreenSize(width, height int) (bool, error) { + return u.setScreenSize(width, height, u.scale), nil } -func (u *userInterface) SetScreenScale(scale float64) bool { +func (u *userInterface) SetScreenScale(scale float64) (bool, error) { width, height := u.size() - return u.setScreenSize(width, height, scale) + return u.setScreenSize(width, height, scale), nil } func (u *userInterface) ScreenScale() float64 { diff --git a/internal/ui/ui_mobile.go b/internal/ui/ui_mobile.go index 035f7518b..ba37e3d80 100644 --- a/internal/ui/ui_mobile.go +++ b/internal/ui/ui_mobile.go @@ -102,14 +102,14 @@ func (u *userInterface) SwapBuffers() error { return nil } -func (u *userInterface) SetScreenSize(width, height int) bool { +func (u *userInterface) SetScreenSize(width, height int) (bool, error) { // TODO: Implement - return false + return false, nil } -func (u *userInterface) SetScreenScale(scale float64) bool { +func (u *userInterface) SetScreenScale(scale float64) (bool, error) { // TODO: Implement - return false + return false, nil } func (u *userInterface) ScreenScale() float64 { diff --git a/run.go b/run.go index 498ee3764..f3ccbf2c1 100644 --- a/run.go +++ b/run.go @@ -118,7 +118,9 @@ func SetScreenSize(width, height int) { if width <= 0 || height <= 0 { panic("ebiten: width and height must be positive") } - ui.CurrentUI().SetScreenSize(width, height) + if _, err := ui.CurrentUI().SetScreenSize(width, height); err != nil { + panic(err) + } } // SetScreenScale changes the scale of the screen. @@ -128,7 +130,9 @@ func SetScreenScale(scale float64) { if scale <= 0 { panic("ebiten: scale must be positive") } - ui.CurrentUI().SetScreenScale(scale) + if _, err := ui.CurrentUI().SetScreenScale(scale); err != nil { + panic(err) + } } // ScreenScale returns the current screen scale.