Inheritance: IDisposable
Example #1
0
        public Border(World world, LineBatch lines, GraphicsDevice graphics)
        {
            _graphics = graphics;
            _lines = lines;

            // Physics
            float halfWidth = ConvertUnits.ToSimUnits(graphics.Viewport.Width) / 2f - 0.75f;
            float halfHeight = ConvertUnits.ToSimUnits(graphics.Viewport.Height) / 2f - 0.75f;

            Vertices borders = new Vertices(4);
            borders.Add(new Vector2(-halfWidth, halfHeight));  // Lower left
            borders.Add(new Vector2(halfWidth, halfHeight));   // Lower right
            borders.Add(new Vector2(halfWidth, -halfHeight));  // Upper right
            borders.Add(new Vector2(-halfWidth, -halfHeight)); // Upper left

            _anchor = BodyFactory.CreateLoopShape(world, borders);
            _anchor.CollisionCategories = Category.All;
            _anchor.CollidesWith = Category.All;

            // GFX
            _basicEffect = new BasicEffect(graphics);
            _basicEffect.VertexColorEnabled = true;
            _basicEffect.TextureEnabled = true;

            _borderVertices = new[] {
                new VertexPositionColorTexture(new Vector3(-halfWidth, -halfHeight, 0f),Color.White, new Vector2(-halfWidth, -halfHeight) / 5.25f),
                new VertexPositionColorTexture(new Vector3(halfWidth, -halfHeight, 0f),Color.White, new Vector2(halfWidth, -halfHeight) / 5.25f),
                new VertexPositionColorTexture(new Vector3(halfWidth, halfHeight, 0f), Color.White,new Vector2(halfWidth, halfHeight) / 5.25f),
                new VertexPositionColorTexture(new Vector3(-halfWidth, halfHeight, 0f),Color.White, new Vector2(-halfWidth, halfHeight) / 5.25f),
                new VertexPositionColorTexture(new Vector3(-halfWidth - 2f, -halfHeight - 2f, 0f),Color.White, new Vector2(-halfWidth - 2f, -halfHeight - 2f) / 5.25f),
                new VertexPositionColorTexture(new Vector3(halfWidth + 2f, -halfHeight - 2f, 0f),Color.White, new Vector2(halfWidth + 2f, -halfHeight - 2f) / 5.25f),
                new VertexPositionColorTexture(new Vector3(halfWidth + 2f, halfHeight + 2f, 0f), Color.White,new Vector2(halfWidth + 2f, halfHeight + 2f) / 5.25f),
                new VertexPositionColorTexture(new Vector3(-halfWidth - 2f, halfHeight + 2f, 0f), Color.White,new Vector2(-halfWidth - 2f, halfHeight + 2f) / 5.25f)
           };

            _indexBuffer = new short[] { 0, 5, 4, 0, 1, 5, 1, 6, 5, 1, 2, 6, 2, 7, 6, 2, 3, 7, 3, 4, 7, 3, 0, 4 };
        }
 public void Draw(SpriteBatch batch, LineBatch lines, Camera2D camera)
 {
     batch.Begin(0, null, null, null, null, null, camera.View);
     batch.Draw(_body.Image, ConvertUnits.ToDisplayUnits(_chassis.Position), null, Color.White, _chassis.Rotation, _body.Origin, 1f, SpriteEffects.None, 0f);
     batch.End();
     for (int i = 0; i < 3; i++)
     {
         batch.Begin(0, null, null, null, null, null, camera.View);
         batch.Draw(_leftLeg.Image, ConvertUnits.ToDisplayUnits(_leftLegs[i].Position), null, _walkerColors[i], _leftLegs[i].Rotation, _leftLeg.Origin, 1f, SpriteEffects.None, 0f);
         batch.Draw(_leftShoulder.Image, ConvertUnits.ToDisplayUnits(_leftShoulders[i].Position), null, _walkerColors[i], _leftShoulders[i].Rotation, _leftShoulder.Origin, 1f, SpriteEffects.None, 0f);
         batch.Draw(_rightLeg.Image, ConvertUnits.ToDisplayUnits(_rightLegs[i].Position), null, _walkerColors[i], _rightLegs[i].Rotation, _rightLeg.Origin, 1f, SpriteEffects.None, 0f);
         batch.Draw(_rightShoulder.Image, ConvertUnits.ToDisplayUnits(_rightShoulders[i].Position), null, _walkerColors[i], _rightShoulders[i].Rotation, _rightShoulder.Origin, 1f, SpriteEffects.None, 0f);
         batch.End();
         lines.Begin(camera.SimProjection, camera.SimView);
         for (int j = 0; j < 8; j++) // 4 joints pro for schleife...
         {
             lines.DrawLine(_walkerJoints[8 * i + j].WorldAnchorA, _walkerJoints[8 * i + j].WorldAnchorB, ContentWrapper.Grey);
         }
         lines.End();
     }
     batch.Begin(0, null, null, null, null, null, camera.View);
     batch.Draw(_engine.Image, ConvertUnits.ToDisplayUnits(_wheel.Position), null, Color.White, _wheel.Rotation, _engine.Origin, 1f, SpriteEffects.None, 0f);
     batch.End();
 }
        protected override void LoadContent()
        {
            base.LoadContent();

            ContentWrapper.Initialize(this);

            _spriteBatch = new SpriteBatch(GraphicsDevice);
            _lineBatch = new LineBatch(GraphicsDevice);
            _quadRenderer = new QuadRenderer(GraphicsDevice);

            _input.LoadContent(GraphicsDevice.Viewport);
#if WINDOWS
            _counter.LoadContent();
#endif

            // Create rendertarget for transitions
            PresentationParameters pp = GraphicsDevice.PresentationParameters;
            _transitions.Add(new RenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, false, SurfaceFormat.Color, pp.DepthStencilFormat, pp.MultiSampleCount, RenderTargetUsage.DiscardContents));

            _menuScreen = new MenuScreen();

            List<Type> DemosToLoad = new List<Type>();
            Assembly samplesFramework = Assembly.GetExecutingAssembly();
            foreach (Type sampleType in samplesFramework.GetTypes())
            {
                if (sampleType.IsSubclassOf(typeof(PhysicsDemoScreen)))
                {
                    DemosToLoad.Add(sampleType);
                }
            }
            DemosToLoad.Add(DemosToLoad[0]); // HACK: Load the first sample two times, since some delayed creation stuff with the rendertargets always breaks the first preview picture.
            Boolean firstPreview = true;
            foreach (Type sampleType in DemosToLoad)
            {
                PhysicsDemoScreen demoScreen = samplesFramework.CreateInstance(sampleType.ToString()) as PhysicsDemoScreen;
#if WINDOWS
                if (!firstPreview) { Console.WriteLine("Loading demo: " + demoScreen.GetTitle()); }
#endif
                RenderTarget2D preview = new RenderTarget2D(GraphicsDevice, pp.BackBufferWidth / 2, pp.BackBufferHeight / 2, false, SurfaceFormat.Color, pp.DepthStencilFormat, pp.MultiSampleCount, RenderTargetUsage.DiscardContents);

                demoScreen.Framework = this;
                demoScreen.IsExiting = false;

                demoScreen.Sprites = _spriteBatch;
                demoScreen.Lines = _lineBatch;
                demoScreen.Quads = _quadRenderer;

                demoScreen.LoadContent();

                // "Abuse" transition rendertarget to render screen preview
                GraphicsDevice.SetRenderTarget(_transitions[0]);
                GraphicsDevice.Clear(Color.Transparent);

                _quadRenderer.Begin();
                _quadRenderer.Render(Vector2.Zero, new Vector2(_transitions[0].Width, _transitions[0].Height), null, true, ContentWrapper.Grey, Color.White * 0.3f);
                _quadRenderer.End();

                // Update ensures that the screen is fully visible, we "cover" it so that no physics are run
                demoScreen.Update(new GameTime(demoScreen.TransitionOnTime, demoScreen.TransitionOnTime), true, false);
                demoScreen.Draw(new GameTime());
                demoScreen.Draw(new GameTime());

                GraphicsDevice.SetRenderTarget(preview);
                GraphicsDevice.Clear(Color.Transparent);

                _spriteBatch.Begin();
                _spriteBatch.Draw(_transitions[0], preview.Bounds, Color.White);
                _spriteBatch.End();

                GraphicsDevice.SetRenderTarget(null);

                demoScreen.ExitScreen();
                demoScreen.Update(new GameTime(demoScreen.TransitionOffTime, demoScreen.TransitionOffTime), true, false);
                if (!firstPreview)
                {
                    _menuScreen.AddMenuItem(demoScreen, preview);
                }
                else
                {
                    firstPreview = false;
                }
            }

            AddScreen(new BackgroundScreen());
            AddScreen(_menuScreen);
            AddScreen(new LogoScreen(new TimeSpan(0, 0, 0, 3)));

            ResetElapsedTime();
        }