Jump to content
  • Advertisement
Sign in to follow this  
Zbychs

[XNA][SOLVED] Where does ModelMesh.Draw() get GraphicsDevice from?

This topic is 3625 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm puzzled. I don't know where does ModelMesh.Draw() get GraphicsDevice from. It probably gets it somewhere during loading (Content.Load<Model()), but where from? A bit related questions: Is there a way to get Game or Services while not having a reference to Game stored? (Is there any singleton-like pattern built into XNA?) Is there a way to Load a GameComponent? (it requires a Game instance to construct, and I can't get it during loading) The method ContentTypeReader.Read(input, existingInstance) has an existingInstance parameter. Is it used anywhere? Could it be used here? [Edited by - Zbychs on July 15, 2008 7:04:41 PM]

Share this post


Link to post
Share on other sites
Advertisement
All graphics assets get set when they are created or loaded. You'll notice if you take any instance of SpriteBatch, Model, or Texture2D, they all have a GraphicsDevice property on them. So your model mesh's get it from the model when loaded by the content manager.

For your second question, no, there is no singleton pattern in the XNA game. You will have to pass the Services or game instance to the other object that wants access to it.

Third question, not that I know of. Since the content pipeline is not tied to the Game type, I don't believe there is any way to get the game in a ContentTypeReader.

Last question, again I don't know what exactly that existingInstance parameter is for. In my experience it has always been a null value and I've not seen any examples in the docs as to what it could be used for.

Share this post


Link to post
Share on other sites
Thanks for your reply Nick.

When it comes to the first question however, you say that SpriteBatch, Texture2D and Model have GraphicsDevice property on them. Batch and Texture yes, but Model doesn't have it, neither ModelMesh, neither ModelMeshPart.
But Effect (it is kept inside the model) has this property. Moreover it needs GraphicsDevice to be passed to it's constructor.
So my question is: where the Load<Model> function gets the GraphicsDevice from? (to pass it to the Effect constructor)

Share this post


Link to post
Share on other sites
Ah, well somewhere in that tree has one. The VertexDeclaration, VertexBuffer, and IndexBuffer inside of the Model/ModelMesh/ModelMeshPart tree also have that property.

To answer your question, they get it in the ContentTypeReader. You can drill up a bit to find it. The Read method of the ContentTypeReader has a ContentReader parameter. This, in turn, has a ContentManager property. The ContentManager then has a Services property. This is the Services from your game, so it then likely uses the GetService method on it to pull out the GraphicsDeviceManager. They can then get the GraphicsDevice from that.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!