示例#1
0
        public bool BuildTexturedQuads(Rectangle targetArea, out TexturedQuad[] quads)
        {
            // calc where each point is
            int left  = (int)Math.Floor((double)targetArea.Left / MatrixSquareCount1D);
            int right = (int)Math.Floor((double)targetArea.Right / MatrixSquareCount1D);

            if (targetArea.Right % MatrixSquareCount1D == 0)
            {
                right--;
            }
            int top    = (int)Math.Floor((double)targetArea.Top / MatrixSquareCount1D);
            int bottom = (int)Math.Floor((double)targetArea.Bottom / MatrixSquareCount1D);

            if (targetArea.Bottom % MatrixSquareCount1D == 0)
            {
                bottom--;
            }

            // process intersections into quads
            TexturedQuad[] temp      = new TexturedQuad[(right - left) * (bottom - top)];
            int            tempCount = 0;

            for (int x = left; x <= right; x++)
            {
                for (int y = top; y < bottom; y++)
                {
                    Texture square = MatrixSquares[x][y];
                    if (square != null)
                    {
                        int bLeft   = x * MatrixSquareSize;
                        int bRight  = bLeft + MatrixSquareSize;
                        int bTop    = y * MatrixSquareSize;
                        int bBottom = bTop + MatrixSquareSize;

                        // test to see if intersects this square
                        if (targetArea.Left > bRight ||
                            targetArea.Right < bLeft ||
                            targetArea.Top > bBottom ||
                            targetArea.Bottom < bTop)
                        {
                            continue;
                        }

                        // clip original target area
                        int rLeft   = targetArea.Left < bLeft ? bLeft : targetArea.Left;
                        int rRight  = targetArea.Right > bRight ? bRight : targetArea.Right;
                        int rTop    = targetArea.Top < bTop ? bTop : targetArea.Top;
                        int rBottom = targetArea.Bottom > bBottom ? bBottom : targetArea.Bottom;

                        CustomVertex.TransformedTextured[] quad = new CustomVertex.TransformedTextured[]
                        {
                            new CustomVertex.TransformedTextured(rLeft, rTop, 1, 1, 0, 0),
                            new CustomVertex.TransformedTextured(rRight, rTop, 1, 1, 1, 0),
                            new CustomVertex.TransformedTextured(rLeft, rBottom, 1, 1, 0, 1),
                            new CustomVertex.TransformedTextured(rRight, rBottom, 1, 1, 1, 1)
                        };

                        temp[tempCount++] = new TexturedQuad(quad, square);
                    }
                }
            }

            // refactor quads array
            if (tempCount > 0)
            {
                quads = new TexturedQuad[tempCount];
                Array.Copy(temp, quads, tempCount);
                return(true);
            }
            else
            {
                quads = null;
                return(false);
            }
        }
        public bool BuildTexturedQuads(Rectangle targetArea, out TexturedQuad[] quads)
        {
            // calc where each point is
            int left = (int)Math.Floor((double)targetArea.Left / MatrixSquareCount1D);
            int right = (int)Math.Floor((double)targetArea.Right / MatrixSquareCount1D);
            if (targetArea.Right % MatrixSquareCount1D == 0)
                right--;
            int top = (int)Math.Floor((double)targetArea.Top / MatrixSquareCount1D);
            int bottom = (int)Math.Floor((double)targetArea.Bottom / MatrixSquareCount1D);
            if (targetArea.Bottom % MatrixSquareCount1D == 0)
                bottom--;

            // process intersections into quads
            TexturedQuad[] temp = new TexturedQuad[(right - left) * (bottom - top)];
            int tempCount = 0;
            for (int x = left; x <= right; x++)
            {
                for (int y = top; y < bottom; y++)
                {
                    Texture square = MatrixSquares[x][y];
                    if (square != null)
                    {
                        int bLeft = x * MatrixSquareSize;
                        int bRight = bLeft + MatrixSquareSize;
                        int bTop = y * MatrixSquareSize;
                        int bBottom = bTop + MatrixSquareSize;

                        // test to see if intersects this square
                        if (targetArea.Left > bRight ||
                            targetArea.Right < bLeft ||
                            targetArea.Top > bBottom ||
                            targetArea.Bottom < bTop)
                            continue;

                        // clip original target area
                        int rLeft = targetArea.Left < bLeft ? bLeft : targetArea.Left;
                        int rRight = targetArea.Right > bRight ? bRight : targetArea.Right;
                        int rTop = targetArea.Top < bTop ? bTop : targetArea.Top;
                        int rBottom = targetArea.Bottom > bBottom ? bBottom : targetArea.Bottom;

                        CustomVertex.TransformedTextured[] quad = new CustomVertex.TransformedTextured[]
                        {
                            new CustomVertex.TransformedTextured(rLeft, rTop, 1, 1, 0, 0),
                            new CustomVertex.TransformedTextured(rRight, rTop, 1, 1, 1, 0),
                            new CustomVertex.TransformedTextured(rLeft, rBottom, 1, 1, 0, 1),
                            new CustomVertex.TransformedTextured(rRight, rBottom, 1, 1, 1, 1) 
                        };

                        temp[tempCount++] = new TexturedQuad(quad, square);
                    }
                }
            }

            // refactor quads array
            if (tempCount > 0)
            {
                quads = new TexturedQuad[tempCount];
                Array.Copy(temp, quads, tempCount);
                return true;
            }
            else
            {
                quads = null;
                return false;
            }
        }