C# 大量CPU利用率-XNA游戏循环

C# 大量CPU利用率-XNA游戏循环,c#,performance,profiling,lag,frame-rate,C#,Performance,Profiling,Lag,Frame Rate,我用ANTS profiler浏览了我的游戏,看看是什么导致了我的FPS大幅下降,我发现这与玩家的动作有关。请看,每当我的播放机移动时,一个名为Microsoft.Win32.SafeNativeMethods.QueryPerformanceCounter的东西就会占用我大约75%的CPU。根据ANTS的说法,它链接到System.Diagnostics.Stopwatch.GetTimestamp。以下是探查器结果的屏幕截图: 这是我所有的代码: Player.cs namespace I

我用ANTS profiler浏览了我的游戏,看看是什么导致了我的FPS大幅下降,我发现这与玩家的动作有关。请看,每当我的播放机移动时,一个名为
Microsoft.Win32.SafeNativeMethods.QueryPerformanceCounter
的东西就会占用我大约75%的CPU。根据ANTS的说法,它链接到
System.Diagnostics.Stopwatch.GetTimestamp
。以下是探查器结果的屏幕截图:

这是我所有的代码:

Player.cs

namespace Innovationally
{
public class Player
{
    public Texture2D PlayerTexture;
    public Vector2 Position;
    public bool Active;
    public float angle;
    public int coins { get; set; }
    public int bombs { get; set; }
    public int normalaelever { get; set; }
    public int health { get; set; }
    public int damage { get; set; }
    public int maximumhealth { get; set; }
    public int problemelever { get; set; }
    public int mvgelever { get; set; }
    public int elever { get; set; }
    public int nyckel { get; set; }
    public int Width
    {
        get { return PlayerTexture.Width; }
    }
    public int Height
    {
        get { return PlayerTexture.Height; }
    }
    public void Initialize(Texture2D texture, Vector2 position)
    {
        PlayerTexture = texture;
        Position = position;
        Active = true;
        elever = 0;
        mvgelever = 0;
        normalaelever = 0;
        coins = 0;
        bombs = 0;
        health = 100;
        maximumhealth = 100;
        damage = 20;
        nyckel = 99;
        angle = (float)Math.PI * 2;
    }
    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(PlayerTexture, Position, null, Color.White, angle, new Vector2(PlayerTexture.Width / 2, PlayerTexture.Height / 2), 1f, SpriteEffects.None, 0f);
    }
}
}
游戏1.cs

namespace Innovationally
{
enum GameState
{
    TITLESCREEN,
    HELPSCREEN,
    PLAYING,
    WON,
    CONTROLS,
    LOST
}
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    GameState gameState = GameState.TITLESCREEN;

    //PLAYER STATS
    public static Player player;
    KeyboardState currentKeyboardState;
    KeyboardState previousKeyboardState;
    float playerMoveSpeed;
    int bHit;

    //LEVEL STATS
    Dictionary<Texture2D, uint[]> pixelMaps = new Dictionary<Texture2D,uint[]>();
    int level_number = 0;
    int loadlevel = 0;
    Texture2D hud, level0, level1, level2, level3, level4, level5;
    Vector2 levelPos;

    //ROOM STATS
    List<int> tile_life = new List<int>();
    Texture2D tile_gfx, stairsUp, stairsDown;
    List<Vector2> tile_position = new List<Vector2>();
    List<int> tile_type = new List<int>();
    List<int> tile_elev = new List<int>();
    int antlabb = 0;
    int antvapen = 0;
    int antpolis = 0;
    int antwavers = 0;
    TimeSpan researchSpan;
    int researchTime;

    //MISC
    SpriteFont font;
    Loot loot;

    //GAMEPLAY STATS
    TimeSpan timeElapsed;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        graphics.PreferredBackBufferHeight = 800;
        graphics.PreferredBackBufferWidth = 900;
    } 
    protected override void Initialize()
    {
        player = new Player();
        playerMoveSpeed = 4.0f;
        tile_elev.Add(1);
        levelPos.X = 0;
        levelPos.Y = 0;
        loot = new Loot();
        researchTime = 160 - (antlabb * 40);
        base.Initialize();
    }
    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);

        Vector2 playerPosition = new Vector2(430, 450);
        //LEVEL STATS
        LaddaLevel(level_number);
        level0 = Content.Load<Texture2D>("level0");
        level1 = Content.Load<Texture2D>("level1");
        level2 = Content.Load<Texture2D>("level2");
        level3 = Content.Load<Texture2D>("level3");
        level4 = Content.Load<Texture2D>("level4");
        level5 = Content.Load<Texture2D>("level5");
        player.Initialize(Content.Load<Texture2D>("Leftplayer"), playerPosition);
        //As you can see I'm using the new PixelMaps function here... 
        pixelMaps = PixelMaps(new[] { level0, level1, level2, level3, level4, level5, player.PlayerTexture });
        hud = Content.Load<Texture2D>("hud");

        //ROOM STATS
        tile_gfx = Content.Load<Texture2D>("tile");
        stairsUp = Content.Load<Texture2D>("stairsUp");
        stairsDown = Content.Load<Texture2D>("stairsDown");

        font = Content.Load<SpriteFont>("SpriteFont1");

        //SOMEONE STOP THE MUSIC, MUSIC.
        MediaPlayer.Volume = 0.5f;
        MediaPlayer.IsRepeating = true;
        MediaPlayer.Play(Content.Load<Song>("barn-beat"));
    }
    public void LaddaLevel(int nummer)
    {
        StreamReader SR = new StreamReader(nummer.ToString());

        string bana = SR.ReadToEnd();
        SR.Close();
        int temp_positionY = 0;
        int temp_positionX = 0;

        tile_position.Clear(); 
        tile_type.Clear();    
        tile_life.Clear();
        tile_elev.Clear();

        for (int i = 0; i < bana.Length; i++)
        {
            switch (bana[i])
            {
                case ' ':
                    temp_positionX++;
                    break;
                case '0':
                    tile_life.Add(loot.myRnd.Next(8));
                    tile_position.Add(new Vector2((temp_positionX * 100), (temp_positionY * 100)));
                    temp_positionX++;
                    tile_type.Add(int.Parse(bana[i].ToString()));
                    break;
                case '8': 
                    tile_position.Add(new Vector2((temp_positionX * 100), (temp_positionY * 100)));
                    temp_positionX++;
                    tile_type.Add(int.Parse(bana[i].ToString()));
                    tile_life.Add(8);
                    break;
                case '9': 
                    tile_position.Add(new Vector2((temp_positionX * 100), (temp_positionY * 100)));
                    temp_positionX++;
                    tile_type.Add(int.Parse(bana[i].ToString()));
                    tile_life.Add(9);
                    break;
                case '\n':
                    temp_positionY++;
                    temp_positionX = 0;
                    break;
            }
        }
    }
    protected override void UnloadContent()
    {
    }
    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();
        previousKeyboardState = currentKeyboardState;
        currentKeyboardState = Keyboard.GetState();
        if (currentKeyboardState.IsKeyDown(Keys.Escape) && previousKeyboardState.IsKeyUp(Keys.Escape))
            this.Exit();

        switch (gameState)
        {
            case GameState.TITLESCREEN:
                UpdatePlayer(gameTime);
                if (currentKeyboardState.IsKeyDown(Keys.S) && previousKeyboardState.IsKeyUp(Keys.S))
                    gameState = GameState.PLAYING;
                if (currentKeyboardState.IsKeyDown(Keys.H) && previousKeyboardState.IsKeyUp(Keys.H))
                    gameState = GameState.HELPSCREEN;
                break;
            case GameState.HELPSCREEN:
                if (currentKeyboardState.IsKeyDown(Keys.B) && previousKeyboardState.IsKeyUp(Keys.B))
                    gameState = GameState.TITLESCREEN;
                break;
            case GameState.PLAYING:
                timeElapsed += gameTime.ElapsedGameTime;
                UpdatePlayer(gameTime);
                UpdateResearchCenters(gameTime);
                UpdateCollisions(gameTime);
                break;
            case GameState.LOST:
                if (currentKeyboardState.IsKeyDown(Keys.S) && previousKeyboardState.IsKeyUp(Keys.S))
                {
                    loadlevel = 0;
                    gameState = GameState.PLAYING;
                }
                break;
        }
        base.Update(gameTime);
    }
    private void UpdatePlayer(GameTime gameTime)
    {
        if (currentKeyboardState.IsKeyDown(Keys.Left))
        {
            player.angle = (float)Math.PI * 1.5f;
            player.Position.X -= playerMoveSpeed;
        }
        if (currentKeyboardState.IsKeyDown(Keys.Right))
        {
            player.angle = (float)Math.PI / 2;
            player.Position.X += playerMoveSpeed;
        }
        if (currentKeyboardState.IsKeyDown(Keys.Up))
        {
            player.angle = (float)Math.PI * 2;
            player.Position.Y -= playerMoveSpeed;
        }
        if (currentKeyboardState.IsKeyDown(Keys.Down))
        {
            player.angle = (float)Math.PI;
            player.Position.Y += playerMoveSpeed;
        }
        if (player.health <= 0)
            gameState = GameState.LOST;

        switch (gameState)
        {
            case GameState.TITLESCREEN:
                if (player.Position.X > 800)
                    this.Exit();
                if (player.Position.X < 0)
                    gameState = GameState.PLAYING;
                if (player.Position.Y > 900)
                    gameState = GameState.HELPSCREEN;
                if (player.Position.Y < 0)
                    gameState = GameState.CONTROLS;
                break;
        }
    }
    public void UpdateResearchCenters(GameTime gameTime)
    {
        researchSpan -= (gameTime.ElapsedGameTime);
        researchTime = (int)researchSpan.Seconds;
        if (researchTime <= 0)
        {
            loot.RandomResearch();
            researchTime = 160 - (antlabb*40);
        }
    }
    public static uint[] Pixels(Texture2D texture)
    {
        uint[] data = new uint[texture.Width * texture.Height];
        texture.GetData(data);
        return data;
    }
    public static Dictionary<Texture2D, uint[]> PixelMaps(IEnumerable<Texture2D> textures)
    {
        return textures.ToDictionary(t => t, Pixels);
    }
    public static int TestCollision(uint[] t1, int t1Width, Rectangle r, uint[] t2)
    {
        for (var x = r.X; x < r.X + r.Width; x++)
            for (var y = r.Y; y < r.Y + r.Height; y++)
            {
                var i = x + (y * t1Width);
                /*if (((t1[i] & 0xff000000) > 0) && (t2[i] == 0xffC3C3C3))
                {
                    return 1;
                }
                if (((t1[i] & 0xff000000) > 0) && (t2[i] == 0xff000000))
                {
                    return 2;
                }*/
            }
        return 0;
    }
    public void UpdateCollisions(GameTime gameTime)
    {
        Rectangle playerBox = new Rectangle((int)player.Position.X - 20, (int)player.Position.Y - 20, 40, 37);
        Rectangle levelBox = new Rectangle(0, 0, 900, 800);

        for (int i = 0; i < tile_position.Count; i++)
        {
            Rectangle tileBox = new Rectangle((int)tile_position[i].X, (int)tile_position[i].Y, 100, 100);
            if (playerBox.Intersects(tileBox))
            {
                if (tile_life[i] <= 9)
                {
                    if (tile_life[i] == 9 && currentKeyboardState.IsKeyDown(Keys.Space)) 
                    {
                        loadlevel += 1;
                        LaddaLevel(loadlevel);
                    }
                    else if (tile_life[i] == 8 && currentKeyboardState.IsKeyDown(Keys.Space))
                    {
                        loadlevel -= 1;
                        LaddaLevel(loadlevel);
                    }
                    else if (tile_life[i] == 7 && currentKeyboardState.IsKeyDown(Keys.Space) && player.nyckel >= 1)
                    {
                        loot.RandomLoot();
                        player.nyckel -= 1;
                        tile_life[i] = 70; 
                    }
                    else if (tile_life[i] == 6 && currentKeyboardState.IsKeyDown(Keys.Space) && (player.mvgelever >=  1 || player.normalaelever >= 1 || player.problemelever >= 1))
                    {
                        tile_life[i] = 60;
                        if (player.mvgelever != 0)
                            player.mvgelever -= 1;
                        else if (player.normalaelever != 0)
                            player.normalaelever -= 1;
                        else if (player.problemelever != 0)
                            player.problemelever -= 1;
                        antlabb += 1;
                    }
                }
            }
        }
        var playerMap = pixelMaps[player.PlayerTexture];
        var level0Map = pixelMaps[level0];
        if (loadlevel == 0)
            bHit = TestCollision(playerMap, 15, playerBox, level0Map);

        if (bHit == 2)
            this.Exit();
    }
    private void DrawHud()
    {
        string timeString = "TIME: " + timeElapsed.Minutes.ToString("00") + ":" + timeElapsed.Seconds.ToString("00");

        spriteBatch.Draw(hud, new Vector2(0, 0), Color.White);
        spriteBatch.DrawString(font, timeString, new Vector2(15, 35), Color.White);
        spriteBatch.DrawString(font, "Level " + (loadlevel + 1), new Vector2(15, 10), Color.White);
        spriteBatch.DrawString(font, "" + player.mvgelever, new Vector2(739, 55), Color.White);
        spriteBatch.DrawString(font, "" + player.problemelever, new Vector2(799, 55), Color.White);
        spriteBatch.DrawString(font, "" + player.normalaelever, new Vector2(859, 55), Color.White);
        spriteBatch.DrawString(font, "" + antwavers, new Vector2(454, 55), Color.White);
        spriteBatch.DrawString(font, "" + antpolis, new Vector2(514, 55), Color.White);
        spriteBatch.DrawString(font, "" + antvapen, new Vector2(574, 55), Color.White);
        spriteBatch.DrawString(font, "" + antlabb, new Vector2(633, 55), Color.White);
        spriteBatch.DrawString(font, "" + player.coins, new Vector2(359, 55), Color.White);
        spriteBatch.DrawString(font, "" + player.nyckel, new Vector2(328, 55), Color.White);
        spriteBatch.DrawString(font, "" + player.bombs, new Vector2(296, 55), Color.White);
        spriteBatch.DrawString(font, "RESEARCH IN: " + researchTime, new Vector2(15, 55), Color.White);
    }
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();
        switch (gameState)
        {
            case GameState.TITLESCREEN:
                break;
            case GameState.PLAYING:
                if (loadlevel == 0)
                    spriteBatch.Draw(level0, new Vector2(0, 0), Color.White);
                if (loadlevel == 1)
                    spriteBatch.Draw(level1, new Vector2(0, 0), Color.White);
                if (loadlevel == 2)
                    spriteBatch.Draw(level2, new Vector2(0, 0), Color.White);
                if (loadlevel == 3)
                    spriteBatch.Draw(level3, new Vector2(0, 0), Color.White);
                if (loadlevel == 4)
                    spriteBatch.Draw(level4, new Vector2(0, 0), Color.White);
                if (loadlevel == 5)
                    spriteBatch.Draw(level5, new Vector2(0, 0), Color.White);
                for (int i = 0; i < tile_position.Count; i++)
                {
                    switch (tile_life[i])
                    {
                        case 0: spriteBatch.Draw(tile_gfx, tile_position[i], Color.White);
                            break;
                        case 1: spriteBatch.Draw(tile_gfx, tile_position[i], Color.HotPink);
                            break;
                        case 2: spriteBatch.Draw(tile_gfx, tile_position[i], Color.YellowGreen);
                            break;
                        case 3: spriteBatch.Draw(tile_gfx, tile_position[i], Color.Purple);
                            break;
                        case 4: spriteBatch.Draw(tile_gfx, tile_position[i], Color.Yellow);
                            break;
                        case 5: spriteBatch.Draw(tile_gfx, tile_position[i], Color.Silver);
                            break;
                        case 6: spriteBatch.Draw(tile_gfx, tile_position[i], Color.Gold);
                            break;
                        case 60: spriteBatch.Draw(tile_gfx, tile_position[i], Color.Gold);
                            spriteBatch.Draw(player.PlayerTexture, new Vector2(tile_position[i].X + 35, tile_position[i].Y + 40), Color.White);
                            break;
                        case 7: spriteBatch.Draw(tile_gfx, tile_position[i], Color.Orange);
                            break;
                        case 70: spriteBatch.Draw(tile_gfx, tile_position[i], Color.DarkOrange);
                            break;
                        case 8: spriteBatch.Draw(stairsDown, tile_position[i], Color.White);
                            break;
                        case 9: spriteBatch.Draw(stairsUp, tile_position[i], Color.White);
                            break;
                    }
                }
                DrawHud();
                player.Draw(spriteBatch);
                break;
        }
        spriteBatch.End();
        base.Draw(gameTime);
    }
}
}
命名空间创新
{
枚举配子状态
{
标题屏幕,
帮助屏幕,
玩,
赢了
控制,
迷路的
}
公共类游戏1:Microsoft.Xna.Framework.Game
{
图形管理器图形;
雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧雪碧;
GameState GameState=GameState.TITLESCREEN;
//球员统计
公共静态播放器;
键盘状态currentKeyboardState;
键盘状态以前的键盘状态;
浮球运动速度;
int bHit;
//等级统计
Dictionary pixelMaps=新字典();
整数级_数=0;
int loadlevel=0;
纹理2D hud,标高0、标高1、标高2、标高3、标高4、标高5;
向量2-levelPos;
//房间统计
List tile_life=新列表();
纹理2D瓷砖,楼梯向上,楼梯向下;
列表平铺位置=新列表();
列表平铺类型=新列表();
List tile_elev=新列表();
int-antlabb=0;
int-antvapen=0;
int-antpolis=0;
int=0;
时间跨度研究跨度;
国际研究时间;
//杂项
SpriteFont字体;
掠夺;
//游戏性统计
时间跨度时间流逝;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
graphics.PreferredBackBufferHeight=800;
graphics.PreferredBackBufferWidth=900;
} 
受保护的覆盖无效初始化()
{
player=新玩家();
playerMoveSpeed=4.0f;
瓷砖标高增加(1);
levelPos.X=0;
levelPos.Y=0;
战利品=新战利品();
研究时间=160-(ANTLAB*40);
base.Initialize();
}
受保护的覆盖void LoadContent()
{
spriteBatch=新spriteBatch(图形设备);
矢量2播放位置=新矢量2(430450);
//等级统计
LaddaLevel(等级号);
level0=内容加载(“level0”);
level1=Content.Load(“level1”);
level2=内容加载(“level2”);
level3=内容加载(“level3”);
level4=内容加载(“level4”);
level5=内容加载(“level5”);
player.Initialize(Content.Load(“Leftplayer”)、playerPosition);
//正如你所看到的,我在这里使用新的PixelMaps函数。。。
像素地图=像素地图(新[]{level0,level1,level2,level3,level4,level5,player.PlayerTexture});
hud=内容加载(“hud”);
//房间统计
tileu gfx=内容加载(“tile”);
stairsUp=Content.Load(“stairsUp”);
stairsDown=Content.Load(“stairsDown”);
font=Content.Load(“SpriteFont1”);
//有人停止音乐,音乐。
MediaPlayer。音量=0.5f;
MediaPlayer.IsRepeating=true;
MediaPlayer.Play(Content.Load(“barn beat”);
}
公共无效LaddaLevel(整数)
{
StreamReader SR=新的StreamReader(numer.ToString());
字符串bana=SR.ReadToEnd();
高级关闭();
内部温度位置Y=0;
内部温度位置X=0;
平铺位置。清除();
tile_type.Clear();
平铺生活;
瓷砖标高清除();
对于(int i=0;i