Monday, August 8, 2011

XNA Notes–Day 2

Random notes

Program.cs is a stub that launches the game, it's not even used in WP7

GraphicsDeviceManager implements IGraphicsDeviceService and IGraphicsDeviceManager, and adds itself to the "Services" property of the game object.

You can create your own services and get them directly from the game object. For example a spritebatch for public access with: Services.AddService(typeof(SpriteBatch), spriteBatch); and you can access it from anywhere you can access the game with SpriteBatch spriteBatch = Game.Services.GetService( typeof(SpriteBatch)) as SpriteBatch;

GraphicsDeviceManager (GDM)

Instantiating a GraphicsDeviceManager makes it create a GraphicsDevice as a property of game and of GDM. It also creates the GraphicsProfile property and the IsFullScreen property (that behaves differently depending on the platform).

The GDM also has preferences. The runtime attempts to use them and if it cant it falls back on the closest it can.

  • PreferedMultisampling (to blend pixels together and remove jagged edges, defaults to fault because of the overhead)
  • PreferedBackbufferHeight /Width (size of the window on non-fullscreen, resolution on fullscreen and ignored in XBox and WP7)
  • PreferBackBufferFormat / ScencilFormat (how many bits used for the deph and stencil buffers)
  • SupportedOrientations (for WP7)
  • SynchronizeWithVerticalRetrace (prevents tearing by pausing until ready to draw).

There are also events you can hook to, most interesting the PreparingDeviceSettings, lets you override any of the settings before the device is created.

Two interesting methods

  • ApplyChanges (flushes the changes)
  • ToggleFullscrenn (changes to and from full screen)

Game class

There are 2 methods to overload BeginDraw and EndDraw which are not visible on default. Overloading EndDraw requires you to call base.EndDraw.

Two other interesting methods to override, BeginRun and EndRun, called just before begins/ after finishes to run.

Events you can hook to:

  • OnActivated (Game is activated at the beginign and any time it regains focus)
  • OnDeactivated (Ditto)
  • OnExiting (Just once when the game is exiting)

Methods in Game

  • Exit (Causes the game to start shutting down)
  • ResetElapsedTime (Does exactly that)
  • Run (As expected in W and XBox, but throws an exception in WP7 as it's a blocking method)
  • SupressDraw (Stops calling draw until the next time Update is called)
    Tick (Advances one frame, calls Update and Draw)

Propierties in Game

  • InactiveSleepTime (Tells the system how long to sleep when not the active process, default 20 milliseconds)
  • IsActive (Tells you if you're the active process)
  • LaunchParameters (Gests the comand line parameters of in WP7 information oabout the parameters required for launch)

Game time

Game has two parameters to handle time IsFixedTimeStep and TargetElapsedTime, and the update method gets passed a GameTime object with the current time. Interesting detail is the IsRunningSlowly property in the GameTime object, which can be used in FixedTimeStep mode.
For measuring the performance of the game, variable time step should be used.

Components

Encapsulated game objects of 3 types, GameComponent, DrawableGameComponent and GamerServicesComponent.
Focusing on the first 2, they have the same structure more or less than Game.
You can add components in the Game class initialize method, and they get handled by the Game, with no supervision.

No comments:

Post a Comment