开发者

XNA 4.0 - What happens when the window is minimized?

开发者 https://www.devze.com 2023-02-01 15:34 出处:网络
I\'m learning F#, and decided to try making simple XNA games for windows using F# (pure enthusiasm) , and got a window with some images showing up.

I'm learning F#, and decided to try making simple XNA games for windows using F# (pure enthusiasm) , and got a window with some images showing up.

Here's the code:

(*Methods*)     
member self.DrawSprites() = 
    _spriteBatch.Begin()
    for i = 0 to _list.Length-1 do
        let spentity = _list.List.ElementAt(i)
        _spriteBatch.Draw(spentity.ImageTexture,new Rectangle(100,100,(int)spentity.Width,(int)spentity.Height),Color.White)      
    _spriteBatch.End()

(*Overriding*)   
override self.Initialize() =
    ChangeGraphicsProfile()                              
    _graphicsDevice <- _graphics.GraphicsDevice
    _list.AddSprite(0,"NagatoYuki",992.0,990.0)
    base.Initialize() 

override self.LoadContent() =         
    _spriteBatch <- new SpriteBatch(_graphicsDevice)
    base.LoadContent()

override self.Draw(gameTime : GameTime) =
    base.Draw(gameTime)
    _graphics.GraphicsDevice.Clear(Color.CornflowerBlue)
    self.DrawSprites()

And the AddSprite Metho开发者_Python百科d:

   member self.AddSprite(ID : int,imageTexture : string , width : float, height : float) = 
      let texture = content.Load<Texture2D>(imageTexture)
      list <- list @ [new SpriteEntity(ID,list.Length, texture,Vector2.Zero,width,height)]

The _list object has a ContentManager, here's the constructor:

   type SpriteList(_content : ContentManager byref) =
      let mutable content = _content
      let mutable list = []

But I can't minimize the window, since when it regains its focus, i get this error:

ObjectDisposedException

Cannot access a disposed object.

Object name: 'GraphicsDevice'.

What is happening?


Well after struggling for some time I got it to work. But it doesn't seem "right" (thinking that way, using XNA and F# doesn't seem right either, but it's fun.)

(*Methods*)     
member self.DrawSprites() = 
    _spriteBatch.Begin()
    for i = 0 to _list.Length-1 do
        let spentity = _list.List.ElementAt(i)
        if spentity.ImageTexture.IsDisposed then
            spentity.ImageTexture <- _list.Content.Load<Texture2D>(spentity.Name)
        _spriteBatch.Draw(spentity.ImageTexture,new Rectangle(100,100,(int)spentity.Width,(int)spentity.Height),Color.White)      
    _spriteBatch.End()

(*Overriding*)   
override self.Initialize() =
    ChangeGraphicsProfile()           
    _list.AddSprite(0,"NagatoYuki",992.0,990.0)
    base.Initialize() 

override self.LoadContent() =   
    ChangeGraphicsProfile()           
    _graphicsDevice <- _graphics.GraphicsDevice
    _spriteBatch <- new SpriteBatch(_graphicsDevice)
    base.LoadContent()

I adjust the graphicsDevice whenever my game needs to LoadContent, and in the DrawSprites() method I check if the texture is disposed, if it is, load it up again.

But this thing bugs me. I didn't know I had to Load all Content again everytime the window is minimized.

(And the code makes it look like Initialize() loads Content, and LoadContent() initializes, but oh well)


What you are observing is normal behaviour, it's by the way not specific to F#. See http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.game.loadcontent.aspx

This method is called by Initialize. Also, it is called any time the game content needs to be reloaded, such as when the DeviceReset event occurs.

Are you loading all of your content in Game.LoadContent? If you do, you should not be getting these errors.

0

精彩评论

暂无评论...
验证码 换一张
取 消