Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Game1.cs
- using System;
- using System.Reflection;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Myra.Utility;
- namespace Game3
- {
- /// <summary>
- /// This is the main type for your game.
- /// </summary>
- public class Game1 : Game
- {
- private enum Mode
- {
- BeginEnd,
- ReflectionFlush,
- Immediate,
- OneBatch
- }
- private readonly GraphicsDeviceManager _graphics;
- private readonly FPSCounter _fpsCounter = new FPSCounter();
- private SpriteBatch _spriteBatch;
- private SpriteFont _spriteFont;
- private Mode _mode = Mode.BeginEnd;
- private KeyboardState _lastState;
- private object[] _parameters = new object[] { SpriteSortMode.Deferred, null };
- public Game1()
- {
- _graphics = new GraphicsDeviceManager(this);
- this.InactiveSleepTime = TimeSpan.FromTicks(1);
- this.IsFixedTimeStep = false;
- this.TargetElapsedTime = TimeSpan.FromTicks(1);
- _graphics.SynchronizeWithVerticalRetrace = false;
- this._graphics.ApplyChanges();
- Content.RootDirectory = "Content";
- IsMouseVisible = true;
- }
- /// <summary>
- /// LoadContent will be called once per game and is the place to load
- /// all of your content.
- /// </summary>
- protected override void LoadContent()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- _spriteBatch = new SpriteBatch(GraphicsDevice);
- // TODO: use this.Content to load your game content here
- _spriteFont = Content.Load<SpriteFont>("MyFont");
- this.RasterizerState = new RasterizerState
- {
- ScissorTestEnable = true
- };
- Setup = _spriteBatch.GetType().GetMethod("Setup", BindingFlags.Instance | BindingFlags.NonPublic);
- BatcherField = _spriteBatch.GetType().GetField("_batcher", BindingFlags.Instance | BindingFlags.NonPublic);
- Batcher = BatcherField.GetValue(_spriteBatch);
- DrawBatch = Batcher.GetType().GetMethod("DrawBatch");
- }
- public object Batcher { get; private set; }
- public RasterizerState RasterizerState { get; set; }
- public MethodInfo Setup { get; private set; }
- public FieldInfo BatcherField { get; private set; }
- public MethodInfo DrawBatch { get; private set; }
- /// <summary>
- /// UnloadContent will be called once per game and is the place to unload
- /// game-specific content.
- /// </summary>
- protected override void UnloadContent()
- {
- // TODO: Unload any non ContentManager content here
- }
- /// <summary>
- /// Allows the game to run logic such as updating the world,
- /// checking for collisions, gathering input, and playing audio.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- protected override void Update(GameTime gameTime)
- {
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
- Exit();
- // TODO: Add your update logic here
- KeyboardState keyboard = Keyboard.GetState();
- if (keyboard.IsKeyDown(Keys.Space) && !_lastState.IsKeyDown(Keys.Space))
- {
- switch ( _mode )
- {
- case Mode.BeginEnd :
- _mode = Mode.ReflectionFlush;
- break;
- case Mode.ReflectionFlush :
- _mode = Mode.Immediate;
- break;
- case Mode.Immediate :
- _mode = Mode.OneBatch;
- break;
- case Mode.OneBatch :
- _mode = Mode.BeginEnd;
- break;
- default :
- throw new ArgumentOutOfRangeException();
- }
- }
- _lastState = keyboard;
- base.Update(gameTime);
- }
- /// <summary>
- /// This is called when the game should draw itself.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- protected override void Draw(GameTime gameTime)
- {
- _fpsCounter.Update();
- GraphicsDevice.Clear(Color.CornflowerBlue);
- // TODO: Add your drawing code here
- _spriteBatch.Begin(_mode == Mode.Immediate ? SpriteSortMode.Immediate : SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp,
- null, RasterizerState);
- if (_mode == Mode.ReflectionFlush)
- {
- // We need to explicity call Setup
- Setup.Invoke(_spriteBatch, null);
- }
- float y = 0;
- for (int i = 0; i < 500; i++)
- {
- _spriteBatch.DrawString(_spriteFont, "Hello, World!", new Vector2(y, y), Color.AliceBlue);
- switch ( _mode )
- {
- case Mode.BeginEnd :
- _spriteBatch.End();
- _spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, RasterizerState);
- break;
- case Mode.ReflectionFlush :
- DrawBatch.Invoke(Batcher, _parameters);
- break;
- case Mode.OneBatch:
- break;
- }
- y += 0.9f;
- }
- _spriteBatch.DrawString(_spriteFont, string.Format("Mode: {0}", _mode), new Vector2(200, 0), Color.White);
- _spriteBatch.DrawString(_spriteFont, string.Format("FPS: {0:0.##}", _fpsCounter.FPS), new Vector2(200, 20), Color.White);
- _spriteBatch.End();
- base.Draw(gameTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement