Esempio n. 1
0
        public void Execute(Entity entity, GameTime gameTime)
        {
            var transform = entity.GetComponent <Transform2D> ();

            if (entity.HasComponent <SpriteRender> ())
            {
                var spriteRender = entity.GetComponent <SpriteRender> ();
                if (spriteRender.IsCameraIndependency != cameraIndependency)
                {
                    return;
                }
                if (!spriteRender.IsVisible || spriteRender.Sprite == null)
                {
                    return;
                }
                spriteBatch.Draw(spriteRender.Sprite,
                                 transform.Position - new Vector2(spriteRender.Sprite.Width / 2, spriteRender.Sprite.Height / 2),
                                 null, spriteRender.OverlayColor,
                                 transform.Rotation, transform.Origin, transform.Scale,
                                 SpriteEffects.None, spriteRender.SortOrder);
            }
            else if (entity.HasComponent <TextRender> ())
            {
                var textRender = entity.GetComponent <TextRender> ();
                if (textRender.IsCameraIndependency != cameraIndependency)
                {
                    return;
                }
                if (!textRender.IsVisible)
                {
                    return;
                }
                var measured = textRender.Font.MeasureString(textRender.Text);
                spriteBatch.DrawString(textRender.Font, textRender.Text,
                                       transform.Position - measured / 2,
                                       textRender.ForegroundColor,
                                       transform.Rotation, transform.Origin, transform.Scale,
                                       SpriteEffects.None, textRender.SortOrder);
            }
            else if (entity.HasComponent <RectangleRender> ())
            {
                var rectRender = entity.GetComponent <RectangleRender> ();
                if (rectRender.IsCameraIndependency != cameraIndependency)
                {
                    return;
                }
                if (!rectRender.IsVisible)
                {
                    return;
                }
                var pos = transform.Position - rectRender.Size / 2;
                if (rectRender.Fill)
                {
                    renderer.FillRectangle(
                        new Rectangle(
                            new Point(( int )pos.X, ( int )pos.Y),
                            new Point(( int )rectRender.Size.X, ( int )rectRender.Size.Y)
                            ),
                        rectRender.Color,
                        transform.Rotation, transform.Scale,
                        rectRender.SortOrder);
                }
                else
                {
                    renderer.DrawRectangle(
                        new Rectangle(
                            new Point(( int )pos.X, ( int )pos.Y),
                            new Point(( int )rectRender.Size.X, ( int )rectRender.Size.Y)
                            ),
                        rectRender.Color,
                        transform.Rotation, transform.Scale,
                        rectRender.SortOrder);
                }
            }
        }
Esempio n. 2
0
        public void ReadAllRecords(GeometryRenderer ctx)
        {
            // We shouldn't have any dependent resources if _curOffset is 0
            // (curOffset == 0) -> (renderData.dependentResources.Count == 0)
            Debug.Assert((curOffset > 0) || (dependentResources.Count == 0));

            // The buffer being null implies that curOffset must be 0.
            // (buffer == null) -> (curOffset == 0)
            Debug.Assert((buffer != null) || (curOffset == 0));

            // The _curOffset must be less than the length, if there is a buffer.
            Debug.Assert((buffer == null) || (curOffset <= buffer.Length));

            if (curOffset > 0)
            {
                unsafe
                {
                    fixed(byte *pByte = this.buffer)
                    {
                        // This pointer points to the current read point in the
                        // instruction stream.
                        byte *pCur = pByte;

                        // This points to the first byte past the end of the
                        // instruction stream (i.e. when to stop)
                        byte *pEndOfInstructions = pByte + curOffset;

                        // Iterate across the entire list of instructions, stopping at the
                        // end or when it has signalled a stop.
                        while ((pCur < pEndOfInstructions) && !ctx.ShouldStopWalking)
                        {
                            RecordHeader *pCurRecord = (RecordHeader *)pCur;

                            switch (pCurRecord->Type)
                            {
                            case RecordType.DrawLine:
                            {
                                DrawLineCommand *data = (DrawLineCommand *)(pCur + sizeof(RecordHeader));
                                ctx.DrawLine(
                                    (Pen)DependentLookup(data->PenIndex),
                                    data->StartPoint,
                                    data->EndPoint
                                    );
                            }
                            break;

                            case RecordType.DrawRectangle:
                            {
                                DrawRectangleCommand *data = (DrawRectangleCommand *)(pCur + sizeof(RecordHeader));
                                ctx.DrawRectangle(
                                    (Brush)DependentLookup(data->BrushIndex),
                                    (Pen)DependentLookup(data->PenIndex),
                                    data->Rectangle
                                    );
                            }
                            break;

                            case RecordType.DrawRoundedRectangle:
                            {
                                DrawRoundedRectangleCommand *data = (DrawRoundedRectangleCommand *)(pCur + sizeof(RecordHeader));
                                ctx.DrawRoundedRectangle(
                                    (Brush)DependentLookup(data->BrushIndex),
                                    (Pen)DependentLookup(data->PenIndex),
                                    data->Rectangle,
                                    data->radiusX,
                                    data->radiusY
                                    );
                            }
                            break;

                            case RecordType.DrawEllipse:
                            {
                                DrawEllipseCommand *data = (DrawEllipseCommand *)(pCur + sizeof(RecordHeader));
                                ctx.DrawEllipse(
                                    (Brush)DependentLookup(data->BrushIndex),
                                    (Pen)DependentLookup(data->PenIndex),
                                    data->Center,
                                    data->RadiusX,
                                    data->RadiusY
                                    );
                            }
                            break;

                            case RecordType.DrawGlyphRun:
                            {
                                DrawGlyphRunCommand *data = (DrawGlyphRunCommand *)(pCur + sizeof(RecordHeader));
                                ctx.DrawGlyphRun(
                                    (Brush)DependentLookup(data->BrushIndex),
                                    (GlyphRun)DependentLookup(data->GlyphRunIndex)
                                    );
                            }
                            break;

                            case RecordType.DrawText:
                            {
                                DrawTextCommand *data = (DrawTextCommand *)(pCur + sizeof(RecordHeader));
                                ctx.DrawText(
                                    (Brush)DependentLookup(data->BrushIndex),
                                    (FormattedText)DependentLookup(data->FormattedTextIndex)
                                    );
                            }
                            break;

                            case RecordType.DrawGeometry:
                            {
                                DrawGeometryCommand *data = (DrawGeometryCommand *)(pCur + sizeof(RecordHeader));
                                ctx.DrawGeometry(
                                    (Brush)DependentLookup(data->BrushIndex),
                                    (Pen)DependentLookup(data->PenIndex),
                                    (Geometry)DependentLookup(data->GeometryIndex)
                                    );
                            }
                            break;

                            case RecordType.DrawImage:
                            {
                                DrawImageCommand *data = (DrawImageCommand *)(pCur + sizeof(RecordHeader));
                                ctx.DrawImage(
                                    (ITexture)DependentLookup(data->ImageSourceIndex),
                                    data->rectangle,
                                    data->UVMin,
                                    data->UVMax
                                    );
                            }
                            break;

                            case RecordType.DrawSlicedImage:
                            {
                                DrawSlicedImageCommand *data = (DrawSlicedImageCommand *)(pCur + sizeof(RecordHeader));
                                ctx.DrawImage(
                                    (ITexture)DependentLookup(data->ImageSourceIndex),
                                    data->Rectangle,
                                    (data->sliceLeft, data->sliceTop, data->sliceRight, data->sliceBottom)
                                    );
                            }
                            break;

                            case RecordType.PushClip:
                            {
                                PushClipCommand *data = (PushClipCommand *)(pCur + sizeof(RecordHeader));
                                ctx.PushClip(
                                    (Geometry)DependentLookup(data->ClipGeometryIndex)
                                    );
                            }
                            break;

                            case RecordType.Pop:
                            {
                                ctx.Pop();
                            }
                            break;

                            default:
                                throw new ArgumentOutOfRangeException();
                                break;
                            }

                            pCur += pCurRecord->Size;
                        }
                    }
                }
            }
        }