graphics: Explicit limitation of indices at DrawTriangles

Fixes #728
This commit is contained in:
Hajime Hoshi 2018-11-23 19:01:16 +09:00
parent 62210c89f3
commit b48d501bc0
2 changed files with 9 additions and 1 deletions

View File

@ -440,10 +440,15 @@ type DrawTrianglesOptions struct {
Filter Filter Filter Filter
} }
// MaxIndicesNum is the maximum number of indices for DrawTriangles.
const MaxIndicesNum = graphics.IndicesNum
// DrawTriangles draws a triangle with the specified vertices and their indices. // DrawTriangles draws a triangle with the specified vertices and their indices.
// //
// If len(indices) is not multiple of 3, DrawTriangles panics. // If len(indices) is not multiple of 3, DrawTriangles panics.
// //
// If len(indices) is more than MaxIndicesNum, DrawTriangles panics.
//
// The rule in which DrawTriangles works effectively is same as DrawImage's. // The rule in which DrawTriangles works effectively is same as DrawImage's.
// //
// In contrast to DrawImage, DrawTriangles doesn't care source image edges. // In contrast to DrawImage, DrawTriangles doesn't care source image edges.
@ -473,6 +478,9 @@ func (i *Image) DrawTriangles(vertices []Vertex, indices []uint16, img *Image, o
if len(indices)%3 != 0 { if len(indices)%3 != 0 {
panic("ebiten: len(indices) % 3 must be 0") panic("ebiten: len(indices) % 3 must be 0")
} }
if len(indices) > MaxIndicesNum {
panic("ebiten: len(indices) must be <= MaxIndicesNum")
}
// TODO: Check the maximum value of indices and len(vertices)? // TODO: Check the maximum value of indices and len(vertices)?
if options == nil { if options == nil {

View File

@ -89,7 +89,7 @@ func (q *commandQueue) appendIndices(indices []uint16, offset uint16) {
func (q *commandQueue) doEnqueueDrawImageCommand(dst, src *Image, nvertices, nindices int, color *affine.ColorM, mode graphics.CompositeMode, filter graphics.Filter, forceNewCommand bool) { func (q *commandQueue) doEnqueueDrawImageCommand(dst, src *Image, nvertices, nindices int, color *affine.ColorM, mode graphics.CompositeMode, filter graphics.Filter, forceNewCommand bool) {
if nindices > graphics.IndicesNum { if nindices > graphics.IndicesNum {
panic("not implemented for too many indices") panic("not reached")
} }
if !forceNewCommand && 0 < len(q.commands) { if !forceNewCommand && 0 < len(q.commands) {
if last := q.commands[len(q.commands)-1]; last.CanMerge(dst, src, color, mode, filter) { if last := q.commands[len(q.commands)-1]; last.CanMerge(dst, src, color, mode, filter) {