예제 #1
0
파일: Batcher.cs 프로젝트: v-karpov/Nez
        public Batcher(GraphicsDevice graphicsDevice)
        {
            Insist.IsTrue(graphicsDevice != null);

            this.GraphicsDevice = graphicsDevice;

            _vertexInfo   = new VertexPositionColorTexture4[MAX_SPRITES];
            _textureInfo  = new Texture2D[MAX_SPRITES];
            _vertexBuffer = new DynamicVertexBuffer(graphicsDevice, typeof(VertexPositionColorTexture), MAX_VERTICES, BufferUsage.WriteOnly);
            _indexBuffer  = new IndexBuffer(graphicsDevice, IndexElementSize.SixteenBits, MAX_INDICES, BufferUsage.WriteOnly);
            _indexBuffer.SetData(_indexData);

            _spriteEffect     = new SpriteEffect();
            _spriteEffectPass = _spriteEffect.CurrentTechnique.Passes[0];

            _projectionMatrix = new Matrix(
                0f,                 //(float)( 2.0 / (double)viewport.Width ) is the actual value we will use
                0.0f,
                0.0f,
                0.0f,
                0.0f,
                0f,                 //(float)( -2.0 / (double)viewport.Height ) is the actual value we will use
                0.0f,
                0.0f,
                0.0f,
                0.0f,
                1.0f,
                0.0f,
                -1.0f,
                1.0f,
                0.0f,
                1.0f
                );
        }
예제 #2
0
        /// <summary>
        /// removes a PostProcessor. Note that unload is not called when removing so if you no longer need the PostProcessor be sure to call
        /// unload to free resources.
        /// </summary>
        /// <param name="postProcessor">Step.</param>
        public void RemovePostProcessor(PostProcessor postProcessor)
        {
            Insist.IsTrue(_postProcessors.Contains(postProcessor));

            _postProcessors.Remove(postProcessor);
            postProcessor.Unload();
        }
예제 #3
0
        public void AddVertex(Vector2 vertex, Color color, PrimitiveType primitiveType)
        {
            Insist.IsTrue(_hasBegun, "Invalid state. Begin must be called before AddVertex can be called.");
            Insist.IsFalse(primitiveType == PrimitiveType.LineStrip || primitiveType == PrimitiveType.TriangleStrip,
                           "The specified primitiveType is not supported by PrimitiveBatch");

            if (primitiveType == PrimitiveType.TriangleList)
            {
                if (_triangleVertsCount >= _triangleVertices.Length)
                {
                    FlushTriangles();
                }

                _triangleVertices[_triangleVertsCount].Position = new Vector3(vertex, 0);
                _triangleVertices[_triangleVertsCount].Color    = color;
                _triangleVertsCount++;
            }

            if (primitiveType == PrimitiveType.LineList)
            {
                if (_lineVertsCount >= _lineVertices.Length)
                {
                    FlushLines();
                }

                _lineVertices[_lineVertsCount].Position = new Vector3(vertex, 0);
                _lineVertices[_lineVertsCount].Color    = color;
                _lineVertsCount++;
            }
        }
예제 #4
0
 /// <summary>
 /// Change the rendering primitive type.
 /// If it is PrimitiveType.TriangleStrip then you don't need to setTriangles.
 /// </summary>
 /// <param name="primitiveType">The ordering of the verticies.</param>
 /// <returns>The mesh.</returns>
 public Mesh SetPrimitiveType(PrimitiveType primitiveType)
 {
     Insist.IsTrue(primitiveType == PrimitiveType.TriangleList || primitiveType == PrimitiveType.TriangleStrip,
                   "Only triangles are supported.");
     _primitiveType = primitiveType;
     return(this);
 }
예제 #5
0
파일: Scene.cs 프로젝트: nikarh/Nez
 /// <summary>
 /// removes a SceneComponent from the SceneComponents list
 /// </summary>
 public void RemoveSceneComponent(SceneComponent component)
 {
     Insist.IsTrue(_sceneComponents.Contains(component),
                   "SceneComponent {0} is not in the SceneComponents list!", component);
     _sceneComponents.Remove(component);
     component.OnRemovedFromScene();
 }
예제 #6
0
 /// <summary>
 /// sets the triangle indices for rendering
 /// </summary>
 /// <returns>The triangles.</returns>
 /// <param name="triangles">Triangles.</param>
 public Mesh SetTriangles(int[] triangles)
 {
     Insist.IsTrue(triangles.Length % 3 == 0, "triangles must be a multiple of 3");
     _primitiveCount = triangles.Length / 3;
     _triangles      = triangles;
     return(this);
 }
예제 #7
0
        /// <summary>
        /// removes the item at the given index from the list but does NOT maintain list order
        /// </summary>
        /// <param name="index">Index.</param>
        public void RemoveAtWithSwap(int index)
        {
            Insist.IsTrue(index < Length, "Index out of range!");

            Buffer[index]      = Buffer[Length - 1];
            Buffer[Length - 1] = default(T);
            --Length;
        }
예제 #8
0
        /// <summary>
        /// removes the item at the given index from the list
        /// </summary>
        public void RemoveAt(int index)
        {
            Insist.IsTrue(index < Length, "Index out of range!");

            Length--;
            if (index < Length)
            {
                Array.Copy(Buffer, index + 1, Buffer, index, Length - index);
            }
            Buffer[Length] = default(T);
        }
예제 #9
0
파일: Batcher.cs 프로젝트: v-karpov/Nez
        public void End()
        {
            Insist.IsTrue(_beginCalled, "End was called, but Begin has not yet been called. You must call Begin successfully before you can call End.");
            _beginCalled = false;

            if (!_disableBatching)
            {
                FlushBatch();
            }

            _customEffect = null;
        }
예제 #10
0
        /// <summary>
        /// maximum non-scaled value (0 - float.Max) that the camera zoom can be. Defaults to 3
        /// </summary>
        /// <param name="maxZoom">Max zoom.</param>
        public Camera SetMaximumZoom(float maxZoom)
        {
            Insist.IsTrue(maxZoom > 0, "MaximumZoom must be greater than zero");

            if (_zoom > maxZoom)
            {
                _zoom = maxZoom;
            }

            _maximumZoom = maxZoom;
            return(this);
        }
예제 #11
0
        /// <summary>
        /// minimum non-scaled value (0 - float.Max) that the camera zoom can be. Defaults to 0.3
        /// </summary>
        /// <param name="value">Value.</param>
        public Camera SetMinimumZoom(float minZoom)
        {
            Insist.IsTrue(minZoom > 0, "minimumZoom must be greater than zero");

            if (_zoom < minZoom)
            {
                _zoom = MinimumZoom;
            }

            _minimumZoom = minZoom;
            return(this);
        }
예제 #12
0
        /// <summary>
        /// removes the Renderer from the scene
        /// </summary>
        /// <param name="renderer">Renderer.</param>
        public void RemoveRenderer(Renderer renderer)
        {
            Insist.IsTrue(_renderers.Contains(renderer) || _afterPostProcessorRenderers.Contains(renderer));

            if (renderer.WantsToRenderAfterPostProcessors)
            {
                _afterPostProcessorRenderers.Remove(renderer);
            }
            else
            {
                _renderers.Remove(renderer);
            }
            renderer.Unload();
        }
예제 #13
0
파일: Collider.cs 프로젝트: nikarh/Nez
        public override void OnAddedToEntity()
        {
            if (_colliderRequiresAutoSizing)
            {
                // we only deal with boxes and circles here
                Insist.IsTrue(this is BoxCollider || this is CircleCollider,
                              "Only box and circle colliders can be created automatically");

                var renderable = Entity.GetComponent <RenderableComponent>();
                Debug.WarnIf(renderable == null,
                             "Collider has no shape and no RenderableComponent. Can't figure out how to size it.");
                if (renderable != null)
                {
                    var renderableBounds = renderable.Bounds;

                    // we need the size * inverse scale here because when we autosize the Collider it needs to be without a scaled Renderable
                    var width  = renderableBounds.Width / Entity.Transform.Scale.X;
                    var height = renderableBounds.Height / Entity.Transform.Scale.Y;

                    // circle colliders need special care with the origin
                    if (this is CircleCollider)
                    {
                        var circleCollider = this as CircleCollider;
                        circleCollider.Radius = Math.Max(width, height) * 0.5f;

                        // fetch the Renderable's center, transfer it to local coordinates and use that as the localOffset of our collider
                        LocalOffset = renderableBounds.Center - Entity.Transform.Position;
                    }
                    else
                    {
                        var boxCollider = this as BoxCollider;
                        boxCollider.Width  = width;
                        boxCollider.Height = height;

                        // fetch the Renderable's center, transfer it to local coordinates and use that as the localOffset of our collider
                        LocalOffset = renderableBounds.Center - Entity.Transform.Position;
                    }
                }
            }

            _isParentEntityAddedToScene = true;
            RegisterColliderWithPhysicsSystem();
        }
예제 #14
0
파일: Batcher.cs 프로젝트: foxnne/Nez
        /// <summary>
        /// direct access to setting vert positions, UVs and colors. The order of elements is top-left, top-right, bottom-left, bottom-right
        /// </summary>
        /// <returns>The raw.</returns>
        /// <param name="texture">Texture.</param>
        /// <param name="verts">Verts.</param>
        /// <param name="textureCoords">Texture coords.</param>
        /// <param name="color">Color.</param>
        public unsafe void DrawRaw(Texture2D texture, Vector3[] verts, Vector2[] textureCoords, Color color)
        {
            Insist.IsTrue(verts.Length == 4, "there must be only 4 verts");
            Insist.IsTrue(textureCoords.Length == 4, "there must be only 4 texture coordinates");

            // we're out of space, flush
            if (_numSprites >= MAX_SPRITES)
            {
                FlushBatch();

                fixed(VertexPositionColorTexture4 *vertexInfo = &_vertexInfo[_numSprites])
                {
                    vertexInfo->Position0 = verts[0];
                    vertexInfo->Position1 = verts[1];
                    vertexInfo->Position2 = verts[2];
                    vertexInfo->Position3 = verts[3];

                    vertexInfo->TextureCoordinate0 = textureCoords[0];
                    vertexInfo->TextureCoordinate1 = textureCoords[1];
                    vertexInfo->TextureCoordinate2 = textureCoords[2];
                    vertexInfo->TextureCoordinate3 = textureCoords[3];

                    vertexInfo->Color0 = color;
                    vertexInfo->Color1 = color;
                    vertexInfo->Color2 = color;
                    vertexInfo->Color3 = color;
                }

                if (_disableBatching)
                {
                    _vertexBuffer.SetData(0, _vertexInfo, 0, 1, VertexPositionColorTexture4.RealStride,
                                          SetDataOptions.None);
                    DrawPrimitives(texture, 0, 1);
                }
                else
                {
                    _textureInfo[_numSprites] = texture;
                    _numSprites += 1;
                }
        }
예제 #15
0
        /// <summary>
        /// direct access to setting vert positions, UVs and colors. The order of elements is top-left, top-right, bottom-left, bottom-right
        /// </summary>
        /// <returns>The raw.</returns>
        /// <param name="texture">Texture.</param>
        /// <param name="verts">Verts.</param>
        /// <param name="textureCoords">Texture coords.</param>
        /// <param name="colors">Colors.</param>
        public void DrawRaw(Texture2D texture, System.Numerics.Vector3[] verts, Vector2[] textureCoords, Color[] colors)
        {
            Insist.IsTrue(verts.Length == 4, "there must be only 4 verts");
            Insist.IsTrue(textureCoords.Length == 4, "there must be only 4 texture coordinates");
            Insist.IsTrue(colors.Length == 4, "there must be only 4 colors");

            // we're out of space, flush
            if (_numSprites >= MAX_SPRITES)
            {
                FlushBatch();
            }

            _vertexInfo[_numSprites].Position0 = verts[0];
            _vertexInfo[_numSprites].Position1 = verts[1];
            _vertexInfo[_numSprites].Position2 = verts[2];
            _vertexInfo[_numSprites].Position3 = verts[3];

            _vertexInfo[_numSprites].TextureCoordinate0 = textureCoords[0];
            _vertexInfo[_numSprites].TextureCoordinate1 = textureCoords[1];
            _vertexInfo[_numSprites].TextureCoordinate2 = textureCoords[2];
            _vertexInfo[_numSprites].TextureCoordinate3 = textureCoords[3];

            _vertexInfo[_numSprites].Color0 = colors[0];
            _vertexInfo[_numSprites].Color1 = colors[1];
            _vertexInfo[_numSprites].Color2 = colors[2];
            _vertexInfo[_numSprites].Color3 = colors[3];

            if (_disableBatching)
            {
                _vertexBuffer.SetData(0, _vertexInfo, 0, 1, VertexPositionColorTexture4.RealStride,
                                      SetDataOptions.None);
                DrawPrimitives(texture, 0, 1);
            }
            else
            {
                _textureInfo[_numSprites] = texture;
                _numSprites += 1;
            }
        }
예제 #16
0
 /// <summary>
 /// sets the number of degrees between each subdivision for use with EndCapType.Smooth
 /// </summary>
 /// <returns>The per subdivision.</returns>
 /// <param name="degreesPerSubdivision">Degrees per subdivision.</param>
 public LineRenderer SetDegreesPerSubdivision(float degreesPerSubdivision)
 {
     Insist.IsTrue(degreesPerSubdivision > 0, "degreesPerSubdivision must be greater than 0");
     this.DegreesPerSubdivision = degreesPerSubdivision;
     return(this);
 }