Add error checks

This commit is contained in:
Hajime Hoshi 2016-08-02 02:23:23 +09:00
parent 5801bc53df
commit c7106f595c
5 changed files with 37 additions and 13 deletions

View File

@ -64,7 +64,7 @@ func DebugPrint(image *ebiten.Image, str string) error {
return defaultDebugPrintState.DebugPrint(image, str) return defaultDebugPrintState.DebugPrint(image, str)
} }
func (d *debugPrintState) drawText(rt *ebiten.Image, str string, x, y int, c color.Color) { func (d *debugPrintState) drawText(rt *ebiten.Image, str string, x, y int, c color.Color) error {
ur, ug, ub, ua := c.RGBA() ur, ug, ub, ua := c.RGBA()
const max = math.MaxUint16 const max = math.MaxUint16
r := float64(ur) / max r := float64(ur) / max
@ -81,7 +81,10 @@ func (d *debugPrintState) drawText(rt *ebiten.Image, str string, x, y int, c col
} }
op.GeoM.Translate(float64(x+1), float64(y)) op.GeoM.Translate(float64(x+1), float64(y))
op.ColorM.Scale(r, g, b, a) op.ColorM.Scale(r, g, b, a)
rt.DrawImage(d.textImage, op) if err := rt.DrawImage(d.textImage, op); err != nil {
return err
}
return nil
} }
// DebugPrint prints the given text str on the given image r. // DebugPrint prints the given text str on the given image r.
@ -104,7 +107,11 @@ func (d *debugPrintState) DebugPrint(r *ebiten.Image, str string) error {
return err return err
} }
} }
d.drawText(r, str, 1, 1, color.NRGBA{0x00, 0x00, 0x00, 0x80}) if err := d.drawText(r, str, 1, 1, color.NRGBA{0x00, 0x00, 0x00, 0x80}); err != nil {
d.drawText(r, str, 0, 0, color.NRGBA{0xff, 0xff, 0xff, 0xff}) return err
}
if err := d.drawText(r, str, 0, 0, color.NRGBA{0xff, 0xff, 0xff, 0xff}); err != nil {
return err
}
return nil return nil
} }

View File

@ -47,13 +47,19 @@ func (c *graphicsContext) GLContext() *opengl.Context {
func (c *graphicsContext) SetSize(screenWidth, screenHeight int, screenScale float64) error { func (c *graphicsContext) SetSize(screenWidth, screenHeight int, screenScale float64) error {
if c.screen != nil { if c.screen != nil {
c.screen.Dispose() if err := c.screen.Dispose(); err != nil {
return err
}
} }
if c.offscreen != nil { if c.offscreen != nil {
c.offscreen.Dispose() if err := c.offscreen.Dispose(); err != nil {
return err
}
} }
if c.offscreen2 != nil { if c.offscreen2 != nil {
c.offscreen2.Dispose() if err := c.offscreen2.Dispose(); err != nil {
return err
}
} }
offscreen, err := newVolatileImage(screenWidth, screenHeight, FilterNearest) offscreen, err := newVolatileImage(screenWidth, screenHeight, FilterNearest)
if err != nil { if err != nil {
@ -74,7 +80,9 @@ func (c *graphicsContext) SetSize(screenWidth, screenHeight int, screenScale flo
if err != nil { if err != nil {
return err return err
} }
c.screen.Clear() if err := c.screen.Clear(); err != nil {
return err
}
c.offscreen = offscreen c.offscreen = offscreen
c.offscreen2 = offscreen2 c.offscreen2 = offscreen2

View File

@ -182,7 +182,9 @@ func (c *drawImageCommand) Exec(context *opengl.Context, indexOffsetInBytes int)
geoM: c.geo, geoM: c.geo,
colorM: c.color, colorM: c.color,
} }
p.begin() if err := p.begin(); err != nil {
return err
}
defer p.end() defer p.end()
// TODO: We should call glBindBuffer here? // TODO: We should call glBindBuffer here?
// The buffer is already bound at begin() but it is counterintuitive. // The buffer is already bound at begin() but it is counterintuitive.

View File

@ -56,7 +56,9 @@ func Reset(context *opengl.Context) error {
} }
func (s *openGLState) reset(context *opengl.Context) error { func (s *openGLState) reset(context *opengl.Context) error {
context.Reset() if err := context.Reset(); err != nil {
return err
}
s.lastProgram = zeroProgram s.lastProgram = zeroProgram
s.lastProjectionMatrix = nil s.lastProjectionMatrix = nil
s.lastModelviewMatrix = nil s.lastModelviewMatrix = nil
@ -132,7 +134,7 @@ type programContext struct {
colorM Matrix colorM Matrix
} }
func (p *programContext) begin() { func (p *programContext) begin() error {
c := p.context c := p.context
if p.state.lastProgram != p.program { if p.state.lastProgram != p.program {
c.UseProgram(p.program) c.UseProgram(p.program)
@ -215,7 +217,10 @@ func (p *programContext) begin() {
// We don't have to call gl.ActiveTexture here: GL_TEXTURE0 is the default active texture // We don't have to call gl.ActiveTexture here: GL_TEXTURE0 is the default active texture
// See also: https://www.opengl.org/sdk/docs/man2/xhtml/glActiveTexture.xml // See also: https://www.opengl.org/sdk/docs/man2/xhtml/glActiveTexture.xml
c.BindTexture(p.texture) if err := c.BindTexture(p.texture); err != nil {
return err
}
return nil
} }
func (p *programContext) end() { func (p *programContext) end() {

4
run.go
View File

@ -81,7 +81,9 @@ func Run(f func(*Image) error, width, height int, scale float64, title string) e
} }
close(ch) close(ch)
}() }()
ui.Main() if err := ui.Main(); err != nil {
return err
}
return <-ch return <-ch
} }