/// <summary>
 ///		Removes the given camera from the rendering list.
 /// </summary>
 /// <param name="camera">Camera to remove from rendering list.</param>
 public void DetachCamera(CameraNode camera)
 {
     _cameraList.Remove(camera);
 }
 /// <summary>
 ///		Renders this entity.
 /// </summary>
 /// <param name="position">Position where this entity's parent node was rendered.</param>
 public override void Render(Transformation transformation, CameraNode camera, int layer)
 {
     if (_image == null) return;
     //Statistics.StoreInt("Nodes Rendered", Statistics.ReadInt("Nodes Rendered") + 1);
     Transformation relativeTransformation = CalculateRelativeTransformation(transformation);
     relativeTransformation.Z = transformation.Z;
     SetupRenderingState(relativeTransformation);
     GraphicsManager.RenderImage(_image, relativeTransformation.X, relativeTransformation.Y, relativeTransformation.Z, _frame);
 }
 /// <summary>
 ///		Adds the given camera from the rendering list.
 /// </summary>
 /// <param name="camera">Camera to add to rendering list.</param>
 public void AttachCamera(CameraNode camera)
 {
     if (_cameraList.Contains(camera)) return;
     _cameraList.Add(camera);
 }
        /// <summary>
        ///		Sets the rendering state so that all children of this 
        ///		camera are drawn correctly.
        /// </summary>
        /// <param name="position">Position where this entity's parent node was rendered.</param>
        public override void Render(Transformation transformation, CameraNode camera, int layer)
        {
            Transformation relativeTransformation = CalculateRelativeTransformation(transformation);
            SetupRenderingState(relativeTransformation);

            // Are we currnetly rendering the layer this entity is on?
            if (layer != _depthLayer)
            {
                if (_visible == true) RenderChildren(relativeTransformation, camera, layer);
                return;
            }

            //Statistics.StoreInt("Nodes Rendered", Statistics.ReadInt("Nodes Rendered") + 1);

            // Render all the particles attached to this emitter.
            if (_visible == true || _forceVisibility == true)
            {
                GraphicsManager.PushRenderState();
                foreach (ParticleNode particleNode in _particleList)
                    particleNode.Render(transformation, camera, layer);
                GraphicsManager.PopRenderState();
            }

            // Render the bounding box and sizing points if we have been asked to do so.
            if (_renderBoundingBox == true || _forceBoundingBoxVisibility == true) RenderBoundingBox(relativeTransformation, camera);
            if (_renderSizingPoints == true) RenderSizingPoints(relativeTransformation, camera);
            if (_renderEventLines == true) RenderEventLines(relativeTransformation, camera);
            if (_forceGlobalDebugVisibility == true) RenderDebug(relativeTransformation, camera);

            // Render all the children of this entity.
            RenderChildren(relativeTransformation, camera, layer);
        }
        /// <summary>
        ///		Renders this tilemap segment at a relative position to its parent node.
        /// </summary>
        /// <param name="transformation">Transformation of parent node.</param>
        public override void Render(Transformation transformation, CameraNode camera, int layer)
        {
            if (_tileData == null) return;

            // Work out where and how we should be rendered.
            Transformation relativeTransformation = CalculateRelativeTransformation(transformation);
            SetupRenderingState(relativeTransformation);

            // Are we currnetly rendering the layer this entity is on? Or are we not visible.
            if (layer != _depthLayer || CanBeSeen(relativeTransformation) == false)
            {
                if (_visible == true) RenderChildren(relativeTransformation, camera, layer);
                return;
            }

            //Statistics.StoreInt("Nodes Rendered", Statistics.ReadInt("Nodes Rendered") + 1);

            // Work out if all the tiles are visible and what are not.
            int segmentLeft = (int)(-((relativeTransformation.X / _tileWidth) / relativeTransformation.ScaleX) - 1);
            int segmentTop = (int)(-((relativeTransformation.Y / _tileHeight) / relativeTransformation.ScaleY) - 1);
            int segmentRight = (int)(segmentLeft + ((GraphicsManager.Resolution[0] / _tileWidth) / relativeTransformation.ScaleX) + 2);
            int segmentBottom = (int)(segmentTop + ((GraphicsManager.Resolution[1] / _tileHeight) / relativeTransformation.ScaleY) + 2);

            // Cap all the segments off so we don't try to draw outside
            // the bounds of this array segment.
            segmentLeft = Math.Min(_width - 1, Math.Max(0, segmentLeft));
            segmentTop = Math.Min(_height - 1, Math.Max(0, segmentTop));
            segmentRight = Math.Min(_width, Math.Max(0, segmentRight));
            segmentBottom = Math.Min(_height, Math.Max(0, segmentBottom));

            if (_visible == true || _forceVisibility == true)
            {
                // Render normally if a prerendered version is not available.
                if (_preRenderedImage == null)
                {
                    //System.Console.WriteLine("Rendered tilemap segment of " + (segmentRight - segmentLeft) + "x" + (segmentBottom - segmentTop) + " (real size: " + (_width + "," + _height) + " tiles:" + ((segmentRight - segmentLeft) * (segmentBottom - segmentTop)) + ")");
                    // Go through all the visible tiles and render them.
                    for (int tileX = segmentLeft; tileX < segmentRight; tileX++)
                        for (int tileY = segmentTop; tileY < segmentBottom; tileY++)
                        {
                            if (_tileData[tileX, tileY] == null || _tileData[tileX, tileY].IsVisible == false || _tileData[tileX, tileY].Tileset == null || _tileData[tileX, tileY].Frame < 0 || _tileData[tileX, tileY].Frame >= _tileData[tileX, tileY].Tileset.Image.FrameCount) continue;
                            _tileData[tileX, tileY].Render(relativeTransformation, camera, layer);
                            /*
                            float tileScaleX = relativeTransformation.ScaleX + Math.Abs(_tileData[tileX, tileY].Transformation.ScaleX) - 1.0f;
                            float tileScaleY = relativeTransformation.ScaleY + Math.Abs(_tileData[tileX, tileY].Transformation.ScaleY) - 1.0f;
                            if (tileScaleX < 0) tileScaleX = -tileScaleX;
                            if (tileScaleY < 0) tileScaleY = -tileScaleY;

                            float tx = relativeTransformation.X + ((tileX * _tileWidth) * tileScaleX);
                            float ty = relativeTransformation.Y + ((tileY * _tileHeight) * tileScaleY);

                            GraphicsManager.VertexColors.AllVertexs = _tileData[tileX, tileY].Color;
                            GraphicsManager.BlendMode = _tileData[tileX, tileY].BlendMode;
                            GraphicsManager.ScaleFactor = new float[] { tileScaleX, tileScaleY };
                            GraphicsManager.RenderImage(_tileData[tileX, tileY].Tileset.Image, tx, ty, relativeTransformation.Z, _tileData[tileX, tileY].Frame);
                        */}
                }

                // w00ties a prerender exists, time for a nice big speedup!
                else
                    GraphicsManager.RenderImage(_preRenderedImage, relativeTransformation.X, relativeTransformation.Y, relativeTransformation.Z, 0);

                // If we have been asked to render a grid then do so.
                if (_renderGrid == true)
                {
                    float width = (_width * _tileWidth) * Math.Abs(relativeTransformation.ScaleX);
                    float height = (_height * _tileHeight) * Math.Abs(relativeTransformation.ScaleY);

                    GraphicsManager.VertexColors.AllVertexs = _gridColor;
                    for (int x = 0; x <= _width; x++)
                    {
                        float lX = (relativeTransformation.X + (x * (_tileWidth * Math.Abs(relativeTransformation.ScaleX))));
                        float lY = relativeTransformation.Y;
                        GraphicsManager.RenderLine(lX, lY, relativeTransformation.Z, lX, lY + height, relativeTransformation.Z);
                    }
                    for (int y = 0; y <= _height; y++)
                    {
                        float lX = relativeTransformation.X;
                        float lY = (relativeTransformation.Y + (y * (_tileHeight * Math.Abs(relativeTransformation.ScaleY))));
                        GraphicsManager.RenderLine(lX, lY, relativeTransformation.Z, lX + width, lY, relativeTransformation.Z);
                    }

                }
            }

            // Render bits and pieces that are required.
            if (_renderCollisionBox == true || _forceCollisionBoxVisibility == true || _forceGlobalCollisionBoxVisibility) RenderCollisionBox(relativeTransformation, camera);
            if (_renderBoundingBox == true || _forceBoundingBoxVisibility == true || _forceGlobalBoundingBoxVisibility) RenderBoundingBox(relativeTransformation, camera);
            if (_renderSizingPoints == true) RenderSizingPoints(relativeTransformation, camera);
            if (_renderEventLines == true || _forceGlobalEventLineVisibility) RenderEventLines(relativeTransformation, camera);
            if (_forceGlobalDebugVisibility == true) RenderDebug(relativeTransformation, camera);

            // Render all the children of this entity.
            if (_visible == true) RenderChildren(relativeTransformation, camera, layer);
        }
        /// <summary>
        ///		Called by the base engine class when its safe to begin initialization.
        /// </summary>
        protected override bool Begin()
        {
            // We alwasy want to run with the editor state!
            ScriptExecutionProcess.DefaultToEditorState = true;

            // Bind all function sets to the global virtual machine.
            NativeFunctionSet.RegisterCommandSetsToVirtualMachine(VirtualMachine.GlobalInstance);

            // Set the output file.
            DebugLogger.OutputFile = _logPath + "\\Editor " + DateTime.Now.Day + "-" + DateTime.Now.Month + "-" + DateTime.Now.Year + " " + DateTime.Now.Hour + "-" + DateTime.Now.Minute + "-" + DateTime.Now.Second + ".log";

            // Ahhhh, no game given!!
            if (_gameName == "")
            {
                MessageBox.Show("No game was specified. Please pass a command line of -game:<ident> when running this application.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
                return true;
            }

            // Make sure this game has not already been compiled.
            if (_gameConfigFile["resources:usepakfiles", "0"] == "1")
                DebugLogger.WriteLog("Unable to edit game, game's media has already been compiled into pak files.", LogAlertLevel.FatalError);

            _fpsLimit = int.Parse(_engineConfigFile["graphics:fpslimit", _fpsLimit.ToString()]);

            // Disable all resource caching.
            //ResourceManager.ResourceCacheEnabled = false;

            // Create the editor's window.
            _window = new EditorWindow();
            _window.FormClosing += new FormClosingEventHandler(OnClosing);
            _window.Show();
            AudioManager.Driver.AttachToControl(_window);
            InputManager.Driver.AttachToControl(_window);

            // Load in the default tileset if it exists and add it to the tileset list.
            if (ResourceManager.ResourceExists(_tilesetPath + "\\default.xml") == true)
            {
                DebugLogger.WriteLog("Found default tileset, loading...");
                Tileset.AddToTilesetPool(new Tileset(_tilesetPath + "\\default.xml"));
            }

            // Load in the default font if it exists.
            if (ResourceManager.ResourceExists(_fontPath + "\\default.xml") == true)
            {
                DebugLogger.WriteLog("Found default bitmap font, loading...");
                GraphicsManager.DefaultBitmapFont = new BitmapFont(_fontPath + "\\default.xml");
            }

            // Load in the required language pack.
            string languageFile = _languagePath + "\\" + _language + ".xml";
            if (ResourceManager.ResourceExists(languageFile) == true)
            {
                DebugLogger.WriteLog("Loading language pack for language " + _language + ".");
                LanguageManager.LoadLanguagePack(_languagePath + "\\" + _language + ".xml", _language);
            }
            else
                DebugLogger.WriteLog("Unable to find language pack for language " + _language + ".", LogAlertLevel.FatalError);

            // Setup a camera that we can view the scene from.
            _camera = new CameraNode("Root Camera");
            _map.SceneGraph.AttachCamera(_camera);
            _camera.BackgroundImage = new Image(ReflectionMethods.GetEmbeddedResourceStream("grid.png"), 0);
            _camera.ClearColor = unchecked((int)0xFFACACAC);

            // Show the tip-of-the-day window.
            if (_engineConfigFile["editor:showtipsonstartup", "1"] == "1")
                (new TipOfTheDayWindow()).ShowDialog();

            // Disable collision processing.
            CollisionManager.Enabled = false;

            return false;
        }
        /// <summary>
        ///		Sets the rendering state so that all children of this 
        ///		camera are drawn correctly.
        /// </summary>
        /// <param name="position">Position where this entity's parent node was rendered.</param>
        public override void Render(Transformation transformation, CameraNode camera, int layer)
        {
            //Statistics.StoreInt("Nodes Rendered", Statistics.ReadInt("Nodes Rendered") + 1);

            Transformation relativeTransformation = CalculateRelativeTransformation(transformation);
            SetupRenderingState(relativeTransformation);

            GraphicsManager.ClearColor	= _clearColor;
            GraphicsManager.Viewport	= _viewport;
            GraphicsManager.ClearScene();

            if (_backgroundImage != null)
            {
                GraphicsManager.ScaleFactor = new float[] { _zoom, _zoom, 1.0f };
                GraphicsManager.TileImage(_backgroundImage, relativeTransformation.X, relativeTransformation.Y, relativeTransformation.Z);

                // Clear up the depth buffer so this image is always at the back.
                GraphicsManager.ClearDepthBuffer();
            }

            // Set the audio listener position at this cameras current position.
            Audio.AudioManager.ListenerPosition = new Vector((-relativeTransformation.X) + (_viewport.Width / 2), -(relativeTransformation.Y) + (_viewport.Height / 2), 0.0f);

            RenderChildren(relativeTransformation, camera, layer);
        }
        /// <summary>
        ///		Gets the transformation of this entity relative to all the nodes above it, including
        ///		the given camera.
        /// </summary>
        /// <param name="camera">Camera to get transformation relative to.</param>
        public Transformation CalculateTransformation(CameraNode camera)
        {
            ArrayList parentNodes = new ArrayList();
            SceneNode parent = this.Parent;
            while (parent != null)
            {
                parentNodes.Add(parent);
                parent = parent.Parent;
            }
            parentNodes.Reverse();

            Transformation relativeTransformation = camera.CalculateRelativeTransformation(new Transformation(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f));
            foreach (SceneNode node in parentNodes)
                relativeTransformation = node.CalculateRelativeTransformation(relativeTransformation);

            return CalculateRelativeTransformation(relativeTransformation);
        }
 public virtual bool RectangleCollisionBoxIntersect(Rectangle rectangle, CameraNode camera)
 {
     Transformation relativeTransformation = CalculateTransformation(camera);
     Rectangle relativeRectangle = new Rectangle((int)relativeTransformation.X, (int)relativeTransformation.Y, (int)(_collisionRectangle.Width * Math.Abs(relativeTransformation.ScaleX)), (int)(_collisionRectangle.Height * Math.Abs(relativeTransformation.ScaleY)));
     return rectangle.IntersectsWith(relativeRectangle);
 }
        /// <summary>
        ///		Renders the event lines of this entity.
        /// </summary>
        /// <param name="transformation">Transformation to render event lines at.</param>
        protected virtual void RenderEventLines(Transformation transformation, CameraNode camera)
        {
            GraphicsManager.PushRenderState();
            GraphicsManager.ClearRenderState();
            GraphicsManager.DepthBufferEnabled = false;
            GraphicsManager.VertexColors.AllVertexs = _eventLineColor;

            foreach (SceneNode node in _eventNodes)
            {
                EntityNode entityNode = node as EntityNode;
                if (entityNode == null) continue;

                Transformation relativeTransformation = entityNode.CalculateTransformation(camera);
                int x1 = (int)(transformation.X + ((_boundingRectangle.Width * transformation.ScaleX) / 2));
                int y1 = (int)(transformation.Y + ((_boundingRectangle.Height * transformation.ScaleY) / 2));
                int x2 = (int)(relativeTransformation.X + ((entityNode._boundingRectangle.Width * relativeTransformation.ScaleX) / 2));
                int y2 = (int)(relativeTransformation.Y + ((entityNode._boundingRectangle.Height * relativeTransformation.ScaleY) / 2));
                GraphicsManager.RenderLine(x1, y1, transformation.Z, x2, y2, relativeTransformation.Z);
            }

            GraphicsManager.PopRenderState();
        }
        /// <summary>
        ///		Renders the sizing points of this entity.
        /// </summary>
        /// <param name="transformation">Transformation to render sizing points at.</param>
        protected void RenderSizingPoints(Transformation transformation, CameraNode camera)
        {
            GraphicsManager.PushRenderState();
            GraphicsManager.ClearRenderState();
            GraphicsManager.DepthBufferEnabled = false;
            GraphicsManager.VertexColors.AllVertexs = _sizingPointsColor;

            int halfSize = _sizingPointsSize / 2;
            int x = (int)(transformation.X - halfSize);
            int y = (int)(transformation.Y - halfSize);
            int w = (int)(_boundingRectangle.Width * Math.Abs(transformation.ScaleX));
            int h = (int)(_boundingRectangle.Height * Math.Abs(transformation.ScaleY));
            GraphicsManager.RenderRectangle(x, y, transformation.Z, _sizingPointsSize, _sizingPointsSize, false);
            GraphicsManager.RenderRectangle(x + w, y, transformation.Z, _sizingPointsSize, _sizingPointsSize, false);
            GraphicsManager.RenderRectangle(x + (w / 2), y, transformation.Z, _sizingPointsSize, _sizingPointsSize, false);
            GraphicsManager.RenderRectangle(x + w, y + h, transformation.Z, _sizingPointsSize, _sizingPointsSize, false);
            GraphicsManager.RenderRectangle(x + w, y + (h / 2), transformation.Z, _sizingPointsSize, _sizingPointsSize, false);
            GraphicsManager.RenderRectangle(x, y + h, transformation.Z, _sizingPointsSize, _sizingPointsSize, false);
            GraphicsManager.RenderRectangle(x, y + (h / 2), transformation.Z, _sizingPointsSize, _sizingPointsSize, false);
            GraphicsManager.RenderRectangle(x + (w / 2), y + h, transformation.Z, _sizingPointsSize, _sizingPointsSize, false);

            GraphicsManager.PopRenderState();
        }
        /// <summary>
        ///		Renders the debug information of this entity.
        /// </summary>
        /// <param name="transformation">Transformation to render debug information at.</param>
        protected void RenderDebug(Transformation transformation, CameraNode camera)
        {
            GraphicsManager.PushRenderState();
            GraphicsManager.ClearRenderState();
            GraphicsManager.DepthBufferEnabled = false;

            GraphicsManager.ScaleFactor = new float[3] { 1.0f, 1.0f, 1.0f };
            float[] resolutionScale = GraphicsManager.ResolutionScale;
            GraphicsManager.ResolutionScale = new float[2] { 1.0f, 1.0f };

            string debugText = "(" + transformation.X + "," + transformation.Y + "," + transformation.Z + ") (" + transformation.ScaleX + "," + transformation.ScaleY + "," + transformation.ScaleZ + ") (" + transformation.AngleX + "," + transformation.AngleY + "," + transformation.AngleZ + ")";
            GraphicsManager.VertexColors.AllVertexs = unchecked((int)0xFF000000);
            GraphicsManager.RenderRectangle((transformation.X * resolutionScale[0]), (transformation.Y * resolutionScale[1]) - 30, transformation.Z, GraphicsManager.BitmapFont.TextWidth(debugText, false), GraphicsManager.BitmapFont.TextHeight(debugText, false));
            GraphicsManager.VertexColors.AllVertexs = _debugInfoColor;
            GraphicsManager.RenderText(debugText, (transformation.X * resolutionScale[0]), (transformation.Y * resolutionScale[1]) - 30, transformation.Z);

            debugText = "[Solid:\"" + _solid + "\" Visible:\""+_visible+"\" Frame:\""+_frame+"\" Event:\""+_event+"\" Name:\""+_name+"\"]";
            GraphicsManager.VertexColors.AllVertexs = unchecked((int)0xFF000000);
            GraphicsManager.RenderRectangle((transformation.X * resolutionScale[0]), (transformation.Y * resolutionScale[1]) - 15, transformation.Z, GraphicsManager.BitmapFont.TextWidth(debugText, false), GraphicsManager.BitmapFont.TextHeight(debugText, false));
            GraphicsManager.VertexColors.AllVertexs = _debugInfoColor;
            GraphicsManager.RenderText(debugText, (transformation.X * resolutionScale[0]), (transformation.Y * resolutionScale[1]) - 15, transformation.Z);

            GraphicsManager.PopRenderState();
        }
        /// <summary>
        ///		Renders the collision box of this entity.
        /// </summary>
        /// <param name="transformation">Transformation to render collisionbox at.</param>
        protected void RenderCollisionBox(Transformation transformation, CameraNode camera)
        {
            if (_solid == false)
                return;

            GraphicsManager.PushRenderState();
            GraphicsManager.ClearRenderState();
            GraphicsManager.DepthBufferEnabled = false;
            GraphicsManager.VertexColors.AllVertexs = _collisionBoxColor;
            GraphicsManager.RenderRectangle(transformation.X + (_collisionRectangle.X * transformation.ScaleX), transformation.Y + (_collisionRectangle.Y * transformation.ScaleY), transformation.Z, Math.Abs(_collisionRectangle.Width * transformation.ScaleX), _collisionRectangle.Height * Math.Abs(transformation.ScaleY), false);
            GraphicsManager.PopRenderState();
        }
 /// <summary>
 ///		Renders the bounding box of this entity.
 /// </summary>
 /// <param name="transformation">Transformation to render bounding box at.</param>
 protected void RenderBoundingBox(Transformation transformation, CameraNode camera)
 {
     GraphicsManager.PushRenderState();
     GraphicsManager.ClearRenderState();
     GraphicsManager.DepthBufferEnabled = false;
     GraphicsManager.VertexColors.AllVertexs = _boundingBoxColor;
     GraphicsManager.RenderRectangle(transformation.X, transformation.Y, transformation.Z, Math.Abs(_boundingRectangle.Width * transformation.ScaleX), _boundingRectangle.Height * Math.Abs(transformation.ScaleY), false);
     GraphicsManager.PopRenderState();
 }
 /// <summary>
 ///		Called when this node needs to be rendered.
 /// </summary>
 public virtual void Render(Transformation transformation, CameraNode camera, int layer)
 {
     RenderChildren(transformation, camera, layer);
 }
        /// <summary>
        ///     Returns true if the given rectangle collides with any of the sizing points of this entity.
        /// </summary>
        /// <param name="rectangle">Entry to check insection against.</param>
        /// <param name="camera">Camera to check inserection from.</param>
        /// <returns>True if rectangle and a sizing point collide.</returns>
        public bool RectangleSizingPointsIntersect(Rectangle rectangle, CameraNode camera)
        {
            Transformation transformation = CalculateTransformation(camera);
            int x = (int)(transformation.X - (_sizingPointsSize / 2)), y = (int)(transformation.Y - (_sizingPointsSize / 2));
            int w = (int)(_boundingRectangle.Width * camera.Zoom), h = (int)(_boundingRectangle.Height * camera.Zoom);

            if (rectangle.IntersectsWith(new Rectangle(x, y, _sizingPointsSize, _sizingPointsSize)))
                return true;
            else if (rectangle.IntersectsWith(new Rectangle(x + w, y, _sizingPointsSize, _sizingPointsSize)))
                return true;
            else if (rectangle.IntersectsWith(new Rectangle(x, y + h, _sizingPointsSize, _sizingPointsSize)))
                return true;
            else if (rectangle.IntersectsWith(new Rectangle(x + w, y + h, _sizingPointsSize, _sizingPointsSize)))
                return true;
            else if (rectangle.IntersectsWith(new Rectangle(x + (w / 2), y, _sizingPointsSize, _sizingPointsSize)))
                return true;
            else if (rectangle.IntersectsWith(new Rectangle(x + (w / 2), y + h, _sizingPointsSize, _sizingPointsSize)))
                return true;
            else if (rectangle.IntersectsWith(new Rectangle(x, y + (h / 2), _sizingPointsSize, _sizingPointsSize)))
                return true;
            else if (rectangle.IntersectsWith(new Rectangle(x + w, y + (h / 2), _sizingPointsSize, _sizingPointsSize)))
                return true;
            else
                return false;
        }
        /// <summary>
        ///		Renders all child nodes of this node.
        /// </summary>
        public void RenderChildren(Transformation transformation, CameraNode camera, int layer)
        {
            foreach (SceneNode child in _childList)
            {
                // Not on the same layer? Not got any children? Return.
                if (layer != child.DepthLayer && (child.Children.Count == 0)) continue;

                //HighPreformanceTimer timer = new HighPreformanceTimer();
                GraphicsManager.PushRenderState();
                child.Render(transformation, camera, layer);
                GraphicsManager.PopRenderState();
                //if (timer.DurationMillisecond > 2)
               //     System.Console.WriteLine(child.ToString() + " (" + ((child as EntityNode) != null ? (((EntityNode)child).Image != null ? ((EntityNode)child).Image.URL : "") + "," + ((EntityNode)child).RenderMode : "") + ") - Rendered in " + timer.DurationMillisecond);
            }
        }
        /// <summary>
        ///		Renders this entity at a position releative to its parent.
        /// </summary>
        /// <param name="position">Position where this entity's parent node was rendered.</param>
        public override void Render(Transformation transformation, CameraNode camera, int layer)
        {
            // Setup rendering mode.
            //HighPreformanceTimer timer = new HighPreformanceTimer();
            Transformation relativeTransformation = CalculateRelativeTransformation(transformation);
            SetupRenderingState(relativeTransformation);
            //if (timer.DurationMillisecond > 1.0f)
            //    System.Console.WriteLine((_image != null ? _image.URL : this.ToString()) + " worked out transformation in " + timer.DurationMillisecond);
            //timer.Restart();

            // Are we currnetly rendering the layer this entity is on?
            if (layer != _depthLayer)
            {
                if (_visible == true) RenderChildren(relativeTransformation, camera, layer);
                return;
            }

            if ((_visible == true || _forceVisibility == true) && (_renderMode == EntityRenderMode.TiledImage || CanBeSeen(relativeTransformation) == true))
            {
                //Statistics.StoreInt("Nodes Rendered", Statistics.ReadInt("Nodes Rendered") + 1);

                Shader previousShader = null;
                if (_shader != null)
                {
                    previousShader = GraphicsManager.Shader;
                    GraphicsManager.Shader = _shader;
                }

                switch (_renderMode)
                {
                    case EntityRenderMode.Image:
                        if (_image == null) break;
                        GraphicsManager.RenderImage(_image, relativeTransformation.X + (((_boundingRectangle.Width - _image.Width) * transformation.ScaleX) / 2), relativeTransformation.Y + (((_boundingRectangle.Height - _image.Height) * transformation.ScaleY) / 2), relativeTransformation.Z, _frame);
                        break;
                    case EntityRenderMode.ContainedImage:
                        {
                            if (_image == null) break;
                            Rectangle originalViewport = GraphicsManager.Viewport;
                            GraphicsManager.Viewport = new Rectangle((int)relativeTransformation.X,
                                                                     (int)relativeTransformation.Y,
                                                                     (int)(_boundingRectangle.Width * Math.Abs(relativeTransformation.ScaleX)),
                                                                     (int)(_boundingRectangle.Height * Math.Abs(relativeTransformation.ScaleY)));
                            GraphicsManager.RenderImage(_image, relativeTransformation.X + (((_boundingRectangle.Width - _image.Width) * transformation.ScaleX) / 2), relativeTransformation.Y + (((_boundingRectangle.Height - _image.Height) * transformation.ScaleY) / 2), relativeTransformation.Z, _frame);
                            GraphicsManager.Viewport = originalViewport;
                        }
                        break;
                    case EntityRenderMode.TiledImage:
                        if (_image == null) break;
                        GraphicsManager.TileImage(_image, relativeTransformation.X, relativeTransformation.Y, relativeTransformation.Z, _frame);
                        break;
                    case EntityRenderMode.ContainedTiledImage:
                        {
                            if (_image == null) break;
                            Rectangle originalViewport = GraphicsManager.Viewport;
                            GraphicsManager.Viewport = new Rectangle((int)relativeTransformation.X,
                                                                     (int)relativeTransformation.Y,
                                                                     (int)(_boundingRectangle.Width * Math.Abs(relativeTransformation.ScaleX)),
                                                                     (int)(_boundingRectangle.Height * Math.Abs(relativeTransformation.ScaleY)));
                            GraphicsManager.TileImage(_image, relativeTransformation.X, relativeTransformation.Y, relativeTransformation.Z, _frame);
                            GraphicsManager.Viewport = originalViewport;
                        }
                        break;
                    case EntityRenderMode.Oval:
                        GraphicsManager.RenderOval(relativeTransformation.X + (_boundingRectangle.X * transformation.ScaleX), relativeTransformation.Y + (_boundingRectangle.Y * transformation.ScaleY), relativeTransformation.Z, _boundingRectangle.Width, _boundingRectangle.Height);
                        break;
                    case EntityRenderMode.HollowOval:
                        GraphicsManager.RenderOval(relativeTransformation.X + (_boundingRectangle.X * transformation.ScaleX), relativeTransformation.Y + (_boundingRectangle.Y * transformation.ScaleY), relativeTransformation.Z, _boundingRectangle.Width, _boundingRectangle.Height, false);
                        break;
                    case EntityRenderMode.Pixel:
                        GraphicsManager.RenderPixel(relativeTransformation.X + ((_boundingRectangle.Width * transformation.ScaleX) / 2), relativeTransformation.Y + ((_boundingRectangle.Height * transformation.ScaleX) / 2), relativeTransformation.Z);
                        break;
                    case EntityRenderMode.Rectangle:
                        GraphicsManager.RenderRectangle(relativeTransformation.X + (_boundingRectangle.X * transformation.ScaleX), relativeTransformation.Y + (_boundingRectangle.Y * transformation.ScaleY), relativeTransformation.Z, _boundingRectangle.Width, _boundingRectangle.Height);
                        break;
                    case EntityRenderMode.HollowRectangle:
                        GraphicsManager.RenderRectangle(relativeTransformation.X + (_boundingRectangle.X * transformation.ScaleX), relativeTransformation.Y + (_boundingRectangle.Y * transformation.ScaleY), relativeTransformation.Z, _boundingRectangle.Width, _boundingRectangle.Height, false);
                        break;
                    case EntityRenderMode.Text:
                        GraphicsManager.RenderText(_text, relativeTransformation.X + (((_boundingRectangle.Width - GraphicsManager.TextWidth(_text, true)) * transformation.ScaleX) / 2), relativeTransformation.Y + (((_boundingRectangle.Height - GraphicsManager.TextHeight(_text, true)) * transformation.ScaleY) / 2), relativeTransformation.Z, true);
                        break;
                    case EntityRenderMode.Mesh:
                        if (_mesh == null) break;
                        GraphicsManager.RenderMesh(_mesh, relativeTransformation.X + (((_boundingRectangle.Width - _mesh.Width) * transformation.ScaleX) / 2), relativeTransformation.Y + (((_boundingRectangle.Height - _mesh.Height) * transformation.ScaleY) / 2), relativeTransformation.Z);
                        break;
                }

                if (_shader != null)
                    GraphicsManager.Shader = previousShader;
            }

            // Render bits and pieces that are required.
            if (_renderCollisionBox == true || _forceCollisionBoxVisibility == true || _forceGlobalCollisionBoxVisibility) RenderCollisionBox(relativeTransformation, camera);
            if (_renderBoundingBox == true || _forceBoundingBoxVisibility == true || _forceGlobalBoundingBoxVisibility) RenderBoundingBox(relativeTransformation, camera);
            if (_renderSizingPoints == true) RenderSizingPoints(relativeTransformation, camera);
            if (_renderEventLines == true || _forceGlobalEventLineVisibility) RenderEventLines(relativeTransformation, camera);
            if (_forceGlobalDebugVisibility == true) RenderDebug(relativeTransformation, camera);

            //if (timer.DurationMillisecond > 1.0f)
            //    System.Console.WriteLine((_image != null ? _image.URL : this.ToString()) + " rendered in " + timer.DurationMillisecond);

            // Render all the children of this entity.
            if (_visible == true) RenderChildren(relativeTransformation, camera, layer);
        }
 /// <summary>
 ///		Renders this entity at a position releative to its parent.
 /// </summary>
 /// <param name="position">Position where this entity's parent node was rendered.</param>
 public override void Render(Transformation transformation, CameraNode camera, int layer)
 {
     if (layer == _depthLayer && _process != null && _process.Process != null && _renderFunction != null)
         _process.Process[0].InvokeFunction(_renderFunction, true, true, true);
     base.Render(transformation, camera, layer);
 }
 public void CreateCamera(ScriptThread thread)
 {
     EntityNode node = new CameraNode();
     thread.SetReturnValue(new SceneNodeScriptObject(node));
 }