From 7a4fce3b7527fe7fad2da77fdd301fdf1a74c47e Mon Sep 17 00:00:00 2001 From: faiface Date: Mon, 16 Jan 2017 00:53:02 +0100 Subject: [PATCH] remove error return value from NewTexture --- picture.go | 7 +------ pixelgl/texture.go | 22 +++++++++++++--------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/picture.go b/picture.go index 1d731b9..3b29071 100644 --- a/picture.go +++ b/picture.go @@ -5,7 +5,6 @@ import ( "image/draw" "github.com/faiface/pixel/pixelgl" - "github.com/pkg/errors" ) // Picture is a raster picture. It is usually used with sprites. @@ -31,16 +30,12 @@ func NewPicture(img image.Image, smooth bool) *Picture { var texture *pixelgl.Texture pixelgl.Do(func() { - var err error - texture, err = pixelgl.NewTexture( + texture = pixelgl.NewTexture( img.Bounds().Dx(), img.Bounds().Dy(), smooth, rgba.Pix, ) - if err != nil { - panic(errors.Wrap(err, "failed to create picture")) - } }) return &Picture{ diff --git a/pixelgl/texture.go b/pixelgl/texture.go index 4cb7690..b14e5b7 100644 --- a/pixelgl/texture.go +++ b/pixelgl/texture.go @@ -12,10 +12,14 @@ type Texture struct { width, height int } -// NewTexture creates a new texture with the specified width and height. -// The pixels must be a sequence of RGBA values. -func NewTexture(width, height int, smooth bool, pixels []uint8) (*Texture, error) { - texture := &Texture{ +// NewTexture creates a new texture with the specified width and height with some initial +// pixel values. The pixels must be a sequence of RGBA values. +func NewTexture(width, height int, smooth bool, pixels []uint8) *Texture { + if len(pixels) != width*height*4 { + panic("failed to create new texture: wrong number of pixels") + } + + tex := &Texture{ tex: binder{ restoreLoc: gl.TEXTURE_BINDING_2D, bindFunc: func(obj uint32) { @@ -26,10 +30,10 @@ func NewTexture(width, height int, smooth bool, pixels []uint8) (*Texture, error height: height, } - gl.GenTextures(1, &texture.tex.obj) + gl.GenTextures(1, &tex.tex.obj) - texture.Begin() - defer texture.End() + tex.Begin() + defer tex.End() gl.TexImage2D( gl.TEXTURE_2D, @@ -56,9 +60,9 @@ func NewTexture(width, height int, smooth bool, pixels []uint8) (*Texture, error gl.GenerateMipmap(gl.TEXTURE_2D) - runtime.SetFinalizer(texture, (*Texture).delete) + runtime.SetFinalizer(tex, (*Texture).delete) - return texture, nil + return tex } func (t *Texture) delete() {