public static LevelBackgroundGF CreateLevelBackground(GraphicsDevice device, GravitiForceLevel gfl, ContentManager content)
        {
            LevelBackgroundGF result = new LevelBackgroundGF();
            result.gfl = gfl;

            texture = content.Load<Texture2D>(@"gfx/Alien.bmap");

            /*
            result.declaration = new VertexDeclaration(device, VertexPositionTexture.VertexElements);

            //                 result.texture =  TextureLoader.FromFile(
            //                device,
            //                Path.Combine(DirectorySettings.MediaDir, textureFilename),
            //                D3DX.Default, D3DX.Default, D3DX.Default, 0, Format.Unknown,
            //                Pool.Managed, Filter.Triangle,
            //                Filter.Triangle, 0);

            Bitmap levelBitmap = gfl.GetBitmap();
            MemoryStream ms = new MemoryStream();
            levelBitmap = new Bitmap(levelBitmap);
            levelBitmap.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp);;
            //levelBitmap.Save(@"c:\test.bmp",System.Drawing.Imaging.ImageFormat.Bmp);;
            ms.Position = 0;
            result.texture = Texture2D.FromFile(device, ms);

            //result.texture = content.Load<Texture2D>(@"test");
            //result.vertexes = VertexBuffer.CreateGeneric<Direct3D.CustomVertex.PositionTextured>(device,
            //    4, Usage.WriteOnly, Direct3D.CustomVertex.PositionTextured.Format, Pool.Managed, null);

            result.bounds = new Rectangle(0, 0, levelBitmap.Width, levelBitmap.Height);

            //result.vertexes.

            VertexPositionTexture[] pt = new VertexPositionTexture[4];
            result.vertexes = pt;
            pt[0].Position = new Vector3(result.bounds.Left, result.bounds.Top, 0);
            pt[1].Position = new Vector3(result.bounds.Right, result.bounds.Top, 0);
            pt[2].Position = new Vector3(result.bounds.Right, result.bounds.Bottom, 0);
            pt[3].Position = new Vector3(result.bounds.Left, result.bounds.Bottom, 0);

            pt[0].TextureCoordinate = new Vector2(0, 1);
            pt[1].TextureCoordinate = new Vector2(1, 1);
            pt[2].TextureCoordinate = new Vector2(1, 0);
            pt[3].TextureCoordinate = new Vector2(0, 0);

            result.levelWidth = levelBitmap.Width;
            */

            return result;
        }
        public void Render(GraphicsDevice device, BasicEffect basicEffect, ObjectShip ship, LevelBackgroundGF levelBackground)
        {
            basicEffect.World = Matrix.Identity;

            basicEffect.LightingEnabled = false;
            device.RenderState.DepthBufferEnable = false;
            device.RenderState.CullMode = CullMode.None;

            device.RenderState.PointSpriteEnable = true;
            //device.RenderState.PointScaleEnable = true ;
            device.RenderState.PointSize = 10.0f;
            device.RenderState.PointSizeMin = 0.00f;
            device.RenderState.PointSizeMax = 100.00f;
            //device.RenderState.PointScaleA = 0.00f;
            //device.RenderState.PointScaleB = 0.00f;
            //device.RenderState.PointScaleC = 1.00f;

            device.RenderState.AlphaBlendEnable = true;
            device.RenderState.SourceBlend = Blend.One;
            device.RenderState.DestinationBlend = Blend.One;

            basicEffect.Texture = texture;
            basicEffect.TextureEnabled = true;

            int count = 0;

            bool hackColliding = false;
            for(int i = 0; i < 6; i++)
            {
                Vector3 v = ship.colisionPoints[i];
                //v = Vector3.TransformCoordinate(v, ship.renderMatrix);
                v = Vector3.Transform(v, ship.renderMatrix);

            ////                Vector3 v = ship.Position;
            ////                v.Y = v.Y + (float)(ship.boundingBoxMax.Y * ship.scale * Math.Cos(ship.Rotation.Z));
            ////                v.X = v.X - (float)(ship.boundingBoxMax.Y * ship.scale * Math.Sin(ship.Rotation.Z));
                bool collide = levelBackground.CheckCollision(v);

                VertexPositionColor pv;
                pv.Position = v;
                pv.Color = collide ? Color.Yellow : Color.White;

                if(collide)
                {
                    vertices[count] = pv;
                    count++;
                }

                hackColliding |= collide;
            }
            if(!ship.hackColliding && hackColliding)
            {
                ship.Position = ship.OldPosition;
                ship.Speed = -ship.Speed * 0.3f;
                SoundHandler.Checkpoint();
            }
            ship.hackColliding = hackColliding;

            device.VertexDeclaration = new VertexDeclaration(device, VertexPositionColor.VertexElements);

            //// Unlock the vertex buffer
            //vertexBuffer.Unlock();
            //// Render any remaining particles
            if (count > 0)
            {
                basicEffect.Begin();
                foreach(EffectPass pass in basicEffect.CurrentTechnique.Passes)
                {
                    pass.Begin();

                    device.DrawUserPrimitives(PrimitiveType.PointList, vertices, 0, count);

                    pass.End();
                }
                basicEffect.End();
            }

            //// Reset render states
            //device.RenderState.PointSpriteEnable = false;
            //device.RenderState.PointScaleEnable = false;

            //device.RenderState.PointSpriteEnable = true;
            //device.RenderState.PointScaleEnable = true ;
            //device.RenderState.AlphaBlendEnable = false;

            device.RenderState.PointSpriteEnable = false;
            device.RenderState.DepthBufferWriteEnable = true;
            device.RenderState.SourceBlend = Blend.SourceAlpha;
            device.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
        }
        public void OnResetDevice(object sender, EventArgs e)
        {
            Device dev = (Device)sender;
            gfl = null;

            playerShips = new ObjectShip[Settings.Current.Players.Length];

            if((levelTreeView.SelectedNode != null) && (levelTreeView.SelectedNode.Tag is GravitiForceLevel))
            {
                gfl = (GravitiForceLevel)levelTreeView.SelectedNode.Tag;
                levelBackground = LevelBackgroundGF.CreateLevelBackground(dev, gfl);

                shipBase = new ShipBase[2];
                shipBase[0] = new ShipBase(new Vector3((float)(gfl.playerBase[0].X - 104), (float)(997f - gfl.playerBase[0].Y), 0f));
                shipBase[1] = new ShipBase(new Vector3((float)(gfl.playerBase[1].X - 104), (float)(997f - gfl.playerBase[1].Y), 0f));

                for(int i = 0; i < playerShips.Length; i++)
                {
                    playerShips[i] = ObjectShip.CreateShip(dev);

                    playerShips[i].Position = shipBase[i].Position;
                }
                bullerBuffer = new BulletBuffer(dev);
            }
            else
            {
                //levelBackground = LevelBackgroundTest.CreateLevelBackground(dev);
            }
            lastTime = Environment.TickCount;

            deviceLost = false;
        }