Alright, I'm fairly new to the C# language so it may just be an issue with that.
In any case I'm trying to figure out how to access the graphicsDevice from a different gameComponent.
For example I want to create a SpriteBatch in a gameComponent in which the GraphicsComponent was not created in. So how would I go about accessing it
I don't know if this is just me being an idiot and there is a simple solution or if something more complex needs to be implemented so any help would be greatly appreciated.
Thanks!

How to access the graphicsDevice from a new gameComponent
Nick_0001
Ah...I hope I can be of any help!
WV John
Those are the best answers I've ever seen.
Thanks a bunch it helped a lot!
d2army
Here's an example class showing you how you can inherit from GameComponent class, along with overriding Draw(), Updae(), Start(), and the four usual GraphicsDevice's Create/Dispose/Resetting/Reset event handles.
namespace XNAStarField
{
public class StarField3D : GameComponent
{
private IGraphicsDeviceService graphicsService;
// …
// This star field component will need a camera class
// for use with World matrix, when multiplied with
// camera’s view and projection matrix for translation
// into screen space.
private Camera _cam;
// This method overrides GameComponent’s Start() function.
public override void Start ( )
{
// This will get the graphics device for this particular
// class.
graphicsService =
Game.GameServices.GetService<IGraphicsDeviceService> ( );
// This will determine if there’s a camera class in the
// list of collections of GameComponents.
bool IsCamera = false;
// Cross-check the list of GameComponents to see if there’s
// a camera class.
foreach ( GameComponent gs in Game.GameComponents )
{
if ( gs.GetType ( ) == typeof ( Camera ) )
{
_cam = (Camera)gs;
IsCamera = true;
}
}
// No camera class in the list of GameComponents
// Exit the application and throw the exception.
if ( !IsCamera )
throw new Exception (
"The star field needs a camera component." );
// …
// Create 4 event handles for determining of the device
// are resetting, if it’s going to reset, if the device
// have already been created, or if it’s in disposing
// state.
graphicsService.DeviceCreated += new
EventHandler ( graphicsService_DeviceCreated );
graphicsService.DeviceDisposing +=
new EventHandler ( graphicsService_DeviceDisposing );
graphicsService.DeviceReset += new
EventHandler ( graphicsService_DeviceReset );
graphicsService.DeviceResetting +=
new EventHandler ( graphicsService_DeviceResetting );
// Load the star field content.
LoadContent ( );
}
void graphicsService_DeviceResetting (
object sender, EventArgs e )
{
ReleaseContent ( );
}
void graphicsService_DeviceReset (
object sender, EventArgs e )
{
LoadContent ( );
}
void graphicsService_DeviceDisposing (
object sender, EventArgs e )
{
ReleaseContent ( );
}
void graphicsService_DeviceCreated (
object sender, EventArgs e )
{
LoadContent ( );
}
private void LoadContent ( )
{
// …
}
private void ReleaseContent ( )
{
// …
}
// …
public override void Draw()
{
// …
}
}
}
If you practice writing this code and you are writing a code to do certain things and give your code to your teammates in your team for creating a game project, then all the developer have to do is add their code to the project in the Solution Explorer, compile the code, go into your component designer (in this case the developer double-clicks in Game1.cs), add the compiled component, and the developer can compile the code and run the game project! Thus, no changes to the Game1.cs code necessary unless the developer specifies options in the property grid in Visual Studio 2005/Visual C# Express Edition.
I encourage you to watch the video about GameComponent if I'm unclear of what I'm saying. What I'm trying to explain to you is that each of the component such as Grid, Terrain, etc. inherits from GameComponent class. So, as taken from my example code above, if you add my StarField3D component to your list of components in Game1, then if you click in + sign for Game1.cs, you will notice that there's a Game1.Designer.cs. Double-click in the file and you will be looking at the generated code -- don't mess with it. Instead, if you look inside the InitializeComponent() function, there's a GameComponents.Add() function; equavelant to when you add a StarField3D component to Game1.cs [Design] area.
Here's my example of what I'm showing:
using System;
namespace _DStarField
{
partial class Game1
{
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent ( )
{
this.graphics = new Microsoft.Xna.Framework.Components.GraphicsComponent ( );
this.trueTypeFont1 = new XNAFont.TrueTypeFont ( );
this.camera1 = new XNACamera.Camera ( );
this.starField3D1 = new _DStarField.StarField3D ( );
//
// graphics
//
this.graphics.BackBufferFormat = Microsoft.Xna.Framework.Graphics.SurfaceFormat.Bgr32;
this.graphics.BackBufferHeight = 720;
this.graphics.BackBufferWidth = 1280;
this.graphics.IsFullScreen = true;
this.graphics.MinimumPixelShaderProfile = Microsoft.Xna.Framework.Graphics.ShaderProfile.PS_2_0;
this.graphics.MinimumVertexShaderProfile = Microsoft.Xna.Framework.Graphics.ShaderProfile.VS_2_0;
this.graphics.SynchronizeWithVerticalRetrace = false;
//
// trueTypeFont1
//
this.trueTypeFont1.FontName = "print";
this.trueTypeFont1.ImageExtension = XNAFont.ImageExtension.TGA;
this.trueTypeFont1.Path = "Font\\";
this.trueTypeFont1.Text = "";
this.trueTypeFont1.X = 0;
this.trueTypeFont1.Y = 0;
//
// camera1
//
this.camera1.AbsX = 0F;
this.camera1.AbsY = 0F;
this.camera1.AbsZ = 0F;
this.camera1.Pitch = 0F;
this.camera1.Roll = 0F;
this.camera1.X = 0F;
this.camera1.Y = 0F;
this.camera1.Yaw = 0F;
this.camera1.Z = 0F;
//
// starField3D1
//
this.starField3D1.ColorStarsMax = 5000;
this.starField3D1.ColorStarsMin = 750;
this.starField3D1.MonochromeStarsMax = 500;
this.starField3D1.MonochromeStarsMin = 125;
this.starField3D1.X = 0F;
this.starField3D1.Y = 0F;
this.starField3D1.Z = 0F;
//
// Game1
//
this.IsMouseVisible = true;
this.GameComponents.Add ( this.graphics );
this.GameComponents.Add ( this.trueTypeFont1 );
this.GameComponents.Add ( this.camera1 );
this.GameComponents.Add ( this.starField3D1 );
}
private Microsoft.Xna.Framework.Components.GraphicsComponent graphics;
private XNAFont.TrueTypeFont trueTypeFont1;
private XNACamera.Camera camera1;
private StarField3D starField3D1;
}
}
As you can see, I have added 3 components to the designer. It's as simple as dragging a component from a Toolbox. This is great for a lot of developers who want to change some or all properties in the property grid without having to go through the whole lines of code just to find what they're looking for and change the value in the property grid.
Does it make clear to you
XNA.Zach
Obtaining the device is very easy:
IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)GameServices.GetService(typeof(IGraphicsDeviceService));
Now you can use the graphics device :)graphicsService.GraphicsDevice
Thanks,
Let me know if this helps :)