From 3efabe988ae26b2dfea0c60d08b06dd98647b883 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 17 Oct 2016 11:16:17 +0900 Subject: [PATCH] graphics: Reduce magic numbers --- internal/graphics/command.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/internal/graphics/command.go b/internal/graphics/command.go index 2562cdaf4..80c8e5a48 100644 --- a/internal/graphics/command.go +++ b/internal/graphics/command.go @@ -108,7 +108,7 @@ func (q *commandQueue) Flush(context *opengl.Context) error { } // NOTE: WebGL doesn't seem to have Check gl.MAX_ELEMENTS_VERTICES or gl.MAX_ELEMENTS_INDICES so far. // Let's use them to compare to len(quads) in the future. - if maxQuads < len(vertices)/16 { + if maxQuads < len(vertices)/quadVertexNum { return errors.New(fmt.Sprintf("len(quads) must be equal to or less than %d", maxQuads)) } numc := len(g) @@ -118,7 +118,7 @@ func (q *commandQueue) Flush(context *opengl.Context) error { return err } if c, ok := c.(*drawImageCommand); ok { - indexOffsetInBytes += 6 * len(c.vertices) / 16 * 2 + indexOffsetInBytes += 6 * len(c.vertices) / quadVertexNum * 2 } } if 0 < numc { @@ -161,6 +161,10 @@ type drawImageCommand struct { mode opengl.CompositeMode } +const ( + quadVertexNum = 16 // 4 * 2 [vertices] * 2 [tex_coords] +) + func (c *drawImageCommand) Exec(context *opengl.Context, indexOffsetInBytes int) error { if err := c.dst.framebuffer.setAsViewport(context); err != nil { return err @@ -195,13 +199,13 @@ func (c *drawImageCommand) Exec(context *opengl.Context, indexOffsetInBytes int) func (c *drawImageCommand) split(quadsNum int) [2]*drawImageCommand { c1 := *c c2 := *c - c1.vertices = c.vertices[:quadsNum*16] - c2.vertices = c.vertices[quadsNum*16:] + c1.vertices = c.vertices[:quadsNum*quadVertexNum] + c2.vertices = c.vertices[quadsNum*quadVertexNum:] return [2]*drawImageCommand{&c1, &c2} } func (c *drawImageCommand) quadsNum() int { - return len(c.vertices) / 16 + return len(c.vertices) / quadVertexNum } type replacePixelsCommand struct {