diff --git a/export_test.go b/export_test.go index affa4ce57..8ac230b77 100644 --- a/export_test.go +++ b/export_test.go @@ -17,3 +17,7 @@ package ebiten var ( ImageToBytes = imageToBytes ) + +func PanicOnErrorAtImageAt() { + panicOnErrorAtImageAt = true +} diff --git a/image.go b/image.go index 17ea4cb25..c0c503126 100644 --- a/image.go +++ b/image.go @@ -24,6 +24,10 @@ import ( "github.com/hajimehoshi/ebiten/internal/mipmap" ) +// panicOnErrorAtImageAt indicates whether (*Image).At panics on an error or not. +// This value is set only on testing. +var panicOnErrorAtImageAt bool + // Image represents a rectangle set of pixels. // The pixel format is alpha-premultiplied RGBA. // Image implements image.Image and draw.Image. @@ -726,6 +730,9 @@ func (i *Image) At(x, y int) color.Color { } pix, err := i.mipmap.Pixels(x, y, 1, 1) if err != nil { + if panicOnErrorAtImageAt { + panic(err) + } theUIContext.setError(err) return color.RGBA{} } diff --git a/image_test.go b/image_test.go index afbfe2efd..e2ab203b5 100644 --- a/image_test.go +++ b/image_test.go @@ -49,6 +49,7 @@ func skipTooSlowTests(t *testing.T) bool { } func TestMain(m *testing.M) { + PanicOnErrorAtImageAt() t.MainWithRunLoop(m) } diff --git a/shader.go b/shader.go index 0ee67dfa3..0602f7189 100644 --- a/shader.go +++ b/shader.go @@ -149,13 +149,21 @@ func (s *Shader) Dispose() { } func (s *Shader) convertUniforms(uniforms map[string]interface{}) []interface{} { - names := map[string]int{} + type index struct { + resultIndex int + shaderUniformIndex int + } + + names := map[string]index{} var idx int - for _, n := range s.uniformNames { + for i, n := range s.uniformNames { if strings.HasPrefix(n, "__") { continue } - names[n] = idx + names[n] = index{ + resultIndex: idx, + shaderUniformIndex: i, + } idx++ } @@ -163,16 +171,16 @@ func (s *Shader) convertUniforms(uniforms map[string]interface{}) []interface{} for name, idx := range names { if v, ok := uniforms[name]; ok { // TODO: Check the uniform variable types? - us[idx] = v + us[idx.resultIndex] = v continue } - t := s.uniformTypes[idx] + t := s.uniformTypes[idx.shaderUniformIndex] v := zeroUniformValue(t) if v == nil { panic(fmt.Sprintf("ebiten: unexpected uniform variable type: %s", t.String())) } - us[idx] = v + us[idx.resultIndex] = v } // TODO: Panic if uniforms include an invalid name @@ -188,39 +196,17 @@ func zeroUniformValue(t shaderir.Type) interface{} { return 0 case shaderir.Float: return float32(0) - case shaderir.Vec2: - return make([]float32, 2) - case shaderir.Vec3: - return make([]float32, 3) - case shaderir.Vec4: - return make([]float32, 4) - case shaderir.Mat2: - return make([]float32, 4) - case shaderir.Mat3: - return make([]float32, 9) - case shaderir.Mat4: - return make([]float32, 16) case shaderir.Array: switch t.Sub[0].Main { case shaderir.Bool: return make([]bool, t.Length) case shaderir.Int: return make([]int, t.Length) - case shaderir.Float: - return make([]float32, t.Length) - case shaderir.Vec2: - return make([]float32, t.Length*2) - case shaderir.Vec3: - return make([]float32, t.Length*3) - case shaderir.Vec4: - return make([]float32, t.Length*4) - case shaderir.Mat2: - return make([]float32, t.Length*4) - case shaderir.Mat3: - return make([]float32, t.Length*9) - case shaderir.Mat4: - return make([]float32, t.Length*16) + default: + return make([]float32, t.FloatNum()) } + default: + return make([]float32, t.FloatNum()) } return nil }