XNA Creators Club Online
Page 1 of 1 (5 items)
Sort Posts: Previous Next

GameState + FPS component problem

Last post 04-17-2008 8:16 AM by DrDeth. 4 replies.
  • 04-15-2008 12:08 PM

    GameState + FPS component problem

    Hi, I'm currently building a simple project to learn XNA and the way it's designed to be used.

    So, I used the GameState tutorial, add my model and behaviours, and a simple DrawableGameComponent component to draw current FPS rate.


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.Graphics;
    #endregion

    namespace AITC2
    {
    /// <summary>
    /// This is A game component that is notified when it needs to draw itself.
    /// It implements IDrawable.
    /// </summary>
    public partial class StatisticComponent : Microsoft.Xna.Framework.DrawableGameComponent
    {
    #region Fields

    // To deals with content loading
    ContentManager content;


    // Text management
    SpriteBatch spriteBatch;
    SpriteFont font1;
    Vector2 textPosition;

    int frameRate = 0;
    int frameCounter = 0;
    TimeSpan elapsedTime = TimeSpan.Zero;

    #endregion

    public StatisticComponent(Game game, ContentManager content)
    : base(game)
    {
    // TODO: Construct any child components here
    this.content = content;
    }


    /// <summary>
    /// Allows the game component to perform any initialization it needs to before starting
    /// to run. This is where it can query for any required services and load content.
    /// </summary>
    public override void Initialize()
    {
    // TODO: Add your initialization code here
    //Text position
    textPosition = new Vector2(0, 0);
    base.Initialize();
    }


    /// <summary>
    /// Load graphics content specific to your component.
    /// </summary>
    protected override void LoadContent()
    {
    font1 = content.Load<SpriteFont>("Fonts\\Pescab");
    // Create a new SpriteBatch, which can be used to draw textures.
    spriteBatch = new SpriteBatch(this.Game.GraphicsDevice);
    }


    /// <summary>
    /// Unload graphics content specific to your drawable component.
    /// </summary>
    protected override void UnloadContent()
    {

    }


    /// <summary>
    /// Allows the component 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>
    public override void Update(GameTime gameTime)
    {

    //FPS Stuff
    elapsedTime += gameTime.ElapsedGameTime;

    if (elapsedTime > TimeSpan.FromSeconds(1))
    {
    elapsedTime -= TimeSpan.FromSeconds(1);
    frameRate = frameCounter;
    frameCounter = 0;
    }


    base.Update(gameTime);
    }


    /// <summary>
    /// This is called when the component should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    public override void Draw(GameTime gameTime)
    {


    spriteBatch.Begin();

    frameCounter++;
    //format the stging
    string output = string.Format("fps: {0}", frameRate);

    // Draw the string
    spriteBatch.DrawString(font1, output, textPosition, Color.LightGreen);

    spriteBatch.End();

    base.Draw(gameTime);
    }
    }
    }


    When I quit my GameScreen (where the component is added), I get a "Cannot access a disposed object. Object name: 'Texture2D'." exception (line 116).

    It's the only component that give such a result. Others (like FireParticleSystem from Particle 3D Sample) work fine.

    I found a hack (removing the component before quiting) but it's not the clean way.

    Any idea ?

  • 04-15-2008 12:11 PM In reply to

    Re: GameState + FPS component problem

    Removing the component before quitting is the "clean" way. If your GameScreen adds the component, then your GameScreen should remove the component when it's finished.

     

  • 04-15-2008 12:21 PM In reply to

    Re: GameState + FPS component problem

    The exception happens because this component is taking in a ContentManager from the parent screen. I would guess that ContentManager gets unloaded when the parent screen exits, but the component is still trying to use content from the manager.

    You need to either remove the component at the same time the screen exits, or if you want it to stay active forever, use a different ContentManager that will not be unloaded out from under it.
    XNA Framework Developer - blog - homepage
  • 04-15-2008 4:43 PM In reply to

    Re: GameState + FPS component problem

    Thanks, it's fixed.

    David :
    if I correctly understood, I have to remove manually all the components added in my GamePlayScreen to do it the clean way. But I've included also the particle systems components ( from Particle 3D Sample) and they didn't do exception like my FPS component ... So I'll do some research to explain why.

  • 04-17-2008 8:16 AM In reply to

    Re: GameState + FPS component problem

    I would simply use Game.Content instead of passing the content manager reference to the component.

    --
    Ruina et Stragos
    XNA SA
    --
Page 1 of 1 (5 items) Previous Next