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

Accessing GraphicsDeviceManager outside of the main game class...

Last post 2/5/2010 4:27 PM by hawthorneluke. 17 replies.
  • 9/3/2008 8:36 PM

    Accessing GraphicsDeviceManager outside of the main game class...

         I have my options menu set up as a DrawableGameComponent, and I want to have the "Fullscreen" option there.

    My problem is that I cannot figure out how to access my GraphicsDeviceManager from a class that isn't the main XNA.framework.game class where I originally setup the DeviceManager and backbuffers and stuff...

    Please help a newbie out!

  • 9/3/2008 8:56 PM In reply to

    Re: Accessing GraphicsDeviceManager outside of the main game class...

    I know that the GraphicsDeviceManager registers itself with the Game.Services collection, so you should be able to get it via:

    IGraphicsDeviceService graphicsService = (IGraphicsDeviceService) Services.GetService(typeof(IGraphicsDeviceService));

    I'm not sure it it registers itself under the IGraphicsDeviceManager interface as well since it implements both, but if so you should be able to get it in the same way. In general, this is the design pattern you want to follow when exposing components from a Game object to any child components.

     

  • 9/3/2008 9:21 PM In reply to

    Re: Accessing GraphicsDeviceManager outside of the main game class...

    I think that my understanding may be too limited to implement what you just said...

    When I try to retrieve the Manager via that line of code, I get an error that says "Services" does not exist in the current context...

    Am I doing this all wrong by making my options menu an inherited class of  : DrawableGameComponent?  Could I instead derive my options menu from : Game1 where my GraphicsDeviceManager is defined and then access it with no restrictions???  I am confused, and as you can tell I am new to C#.

     

    EDIT:  Adding game.Services instead of just Services seemed to resolve the context error, so I will move forward with this and see where it takes me...  This is quite a learning experience.

  • 9/4/2008 1:06 AM In reply to

    Re: Accessing GraphicsDeviceManager outside of the main game class...

    Ok...

    I used the code and got to:

    graphicsDeviceService.GraphicsDevice.PresentationParameters.IsFullScreen = true;

    in my code, but that doesn't switch the screen to fullscreen, it does nothing !

    am I making some sort of simple mistake here?

  • 9/4/2008 2:23 AM In reply to

    Re: Accessing GraphicsDeviceManager outside of the main game class...

    No, you're on the right track. Unfortunately, by the time you have access to the GraphicsDevice in the DrawableGameComponent it has already been created, thus setting the presentation parameters values won't do any good. You could attempt to reapply the parameters by doing a device reset, but my first try at this didn't work.

    You could hack it, and add the following in your game component constructor (instead of LoadContent):

                IGraphicsDeviceService graphicsService = (IGraphicsDeviceService) Game.Services.GetService(typeof(IGraphicsDeviceService));
                if(graphicsService != null && graphicsService is GraphicsDeviceManager)
                {
                    (graphicsService as GraphicsDeviceManager).IsFullScreen = true;
                }

    This allows you to set the mode to fullscreen before the graphics device is created, but sort of defeats the disconnected service architecture. If anyone knows how to reset the graphics device safely feel free to chime in.
  • 9/4/2008 2:54 AM In reply to

    Re: Accessing GraphicsDeviceManager outside of the main game class...

    Or you could just use graphics.ToggleFullScreen()
  • 9/4/2008 3:00 AM In reply to

    Re: Accessing GraphicsDeviceManager outside of the main game class...

    You could, but its the same hack cast involved. ToggleFullScreen() is a method of the GraphicsDeviceManager class, whereas the service lookup only gets you a IGraphicsDeviceService, which has no such method.The same cast would need to be performed, which in many cases will be fine, but it's not ideal to rely on implementation details of the parent game from within a component.
  • 9/4/2008 3:06 AM In reply to

    Re: Accessing GraphicsDeviceManager outside of the main game class...

    Yeah, after I found out about the parameters bit my first attempt after trying the line in my last post was something like:

    parameters.BackBufferWidth = 800;

    parameters.BackBufferHeight = 600;

    parameters.IsFullScreen = true;

    GraphicsDevice.Reset(parameters);

    after making a new Presentation Parameters object.

    This gives me an InvalidOperationException.

    At least I am learning a lot of stuff while I work my way thorugh this.  There has to be a way to do this...  I know that somewhere out there some guy has a funtional fullscreen toggle from an options menu.

  • 9/4/2008 3:08 AM In reply to

    Re: Accessing GraphicsDeviceManager outside of the main game class...

    Sean James:
    Or you could just use graphics.ToggleFullScreen()

     

    Yeah this doesn't work at all from a DrawableGameComponent outside of the main Game class.  Is there any way that I can talk to the main game class from a drawablegamecomponent???

  • 9/4/2008 3:55 AM In reply to

    Re: Accessing GraphicsDeviceManager outside of the main game class...

    You could make the GraphicsDeviceManager in the Game public, then have a property in the component of the Game1 (or whatever the game class is called).

    So then you could do:

    ComponentType component = new ComponentType();
    component.Game = this;
    this.Components.Add(component);

    Then from the component you could do: this.Game.graphics.ToggleFullScreen();

    Or, you could just have the GraphicsDeviceManager as the property if you don't want to pass the whole game

  • 9/4/2008 4:13 AM In reply to

    Re: Accessing GraphicsDeviceManager outside of the main game class...

    I agree with all of the options you've brought up, and they will all work

    BUT

    for curiosities sake, if we wanted to stick with the service-based approach here, which is the most reliable and portable across projects, does anyone know how to trigger a rest of the graphics device that doesn't cause an exception?
  • 9/7/2008 7:38 PM In reply to

    Re: Accessing GraphicsDeviceManager outside of the main game class...

    Sean James:

    You could make the GraphicsDeviceManager in the Game public, then have a property in the component of the Game1 (or whatever the game class is called).

    So then you could do:

    ComponentType component = new ComponentType();
    component.Game = this;
    this.Components.Add(component);

    Then from the component you could do: this.Game.graphics.ToggleFullScreen();

    Or, you could just have the GraphicsDeviceManager as the property if you don't want to pass the whole game

     

    ComponentType doesn't register as anything?  How do I make it work?

  • 9/7/2008 8:08 PM In reply to

    Re: Accessing GraphicsDeviceManager outside of the main game class...

    Actually nevermind, I found a clever (maybe) way to handle things.

    I set up my options screen to simply remove itself (the only running component) when I click my fullscreen option, and the base Game1 has a single if statement in update:

    if (Components.Count == 0)

    {

    graphics.ToggleFullScreen();

    Components.Add(options);

    }

    Since there is never a point where no components are loaded other than this, it all works out!

  • 9/8/2008 5:57 PM In reply to

    Re: Accessing GraphicsDeviceManager outside of the main game class...

    Gate2501:

    Since there is never a point where no components are loaded other than this, it all works out!

    ... until you decide to add another component (which will undoubtedly happen only after you've forgotten about this kludge), then can't figure out why your game won't go fullscreen anymore :)


    Good judgment comes from experience, and experience comes from bad judgment.  -- Barry LePatner

    Sixty years ago I knew everything; now I know nothing; education is a progressive discovery of our own ignorance. -- Will Durant, American philosopher/author [1885-1981]

  • 12/8/2009 12:25 PM In reply to

    Re: Accessing GraphicsDeviceManager outside of the main game class...

    game.Services is also not working in my case.
    Actually i put the Code like this.
     IGraphicsDeviceService graphicsDeviceService = (IGraphicsDeviceService)Game.Services.GetService(typeof(IGraphicsDeviceService));

    and Error is look like this
    An object reference is required for the non-static field, method, or property 'Microsoft.Xna.Framework.Game.Services.get' 


  • 12/8/2009 2:03 PM In reply to

    Re: Accessing GraphicsDeviceManager outside of the main game class...

    You're using the class name "Game" instead of the name of the instance of that class (usually Game1). It also depends on where this code is.
    Jim Perry - Microsoft XNA MVP
    If people spent a minute searching the forums and reading the FAQs before posting I'd be out of a job.
      Got some XNA Game Studio/XNA Framework development info to share with the community? Put it on the XNA Wiki.
        Please mark posts as Answers or Good Feedback when appropriate.
  • 12/8/2009 10:33 PM In reply to

    Re: Accessing GraphicsDeviceManager outside of the main game class...

    Here's a solution that isn't quite so hacky:

    IGraphicsDeviceService svc = (IGraphicsDeviceService)game.Services.GetService(typeof(IGraphicsDeviceService));
    GraphicsDeviceManager man = (GraphicsDeviceManager)svc;
    man.IsFullScreen = true;
    man.ApplyChanges();

  • 2/5/2010 4:27 PM In reply to

    Re: Accessing GraphicsDeviceManager outside of the main game class...

    cdmorse26:
    Here's a solution that isn't quite so hacky:

    IGraphicsDeviceService svc = (IGraphicsDeviceService)game.Services.GetService(typeof(IGraphicsDeviceService));
    GraphicsDeviceManager man = (GraphicsDeviceManager)svc;
    man.IsFullScreen = true;
    man.ApplyChanges();


    I've searched the internet for a solution to the problem stated in this thread and this thread has the closest related info to what I'm looking for that I've managed to find, but when I try the above code, I'm told that I can't cast IGraphicsDeviceService svc as GraphicsDeviceManager at runtime.
    Any ideas?
Page 1 of 1 (18 items) Previous Next