Showing posts with label notes. Show all posts
Showing posts with label notes. Show all posts

Tuesday, August 16, 2011

XNA Notes–Day 6

There is an onscreen keyboard that can be shown with Guide.BeginShowKeyboardInputwhich is an asyncronous object and you need to wait till the isCompleted parameter returns true. On Windows and XBox you need to add the GamerServicesComponent to the list of components the game has.

The Xbox can have a chat pad keyboard attached to the controller.

2 modes for audio: Fire and Forget (just call play on the audio and that's it, it plays until it finishes) and using SoundEffectInstance which lets you pause, resume, and things like that.

There is a Miscrosft Cross-Platform Audio Creations Tool to manage all audio, add effects and so on.

You can read microfone data from xna.

Monday, August 15, 2011

XNA Notes–Day 5


In short, the Importer reads from text into objects, and the Processor loads stuff (like the actual textures from the texture name) using other Processors (like the default processor for a texture) and applies effects.

Content Processor

To create a custom content processor you add a new Content Pipeline Extension Library and add a reference to it from your project.

Create a new class in this project and derive it from ContentProcessor, setting the template variable to whatever type of file we want to load and what type of file we're going to generate.

public MyContentProcessor : ContentProcessor<InputType, OutputType> {}

Then we override the Process function (that looks something like this)

public OutputType Process(InputType input, ContentProcessorContext context)

Finally, in the Content project, select the element to process and change the processor to the new one you made.

You can add properties to the content processor you create, and they appear on the visual studio properties when you select the asset in the content project.

Content Importer

To load new formats (game level).
You crterate a Content Pipeline Extension Project and create a class that derives from ContentImporter<ToImport.Extension> and override the Import function.

Content Writer

You inherit from the ContetTypeWriter<ClassToWrite> class,
and in that class you override the Write function and write out all the data you need to.

Saturday, August 13, 2011

XNA Notes–Day 4

Effects

The effects are what makes the 3D world of xna roll.

  • Basic effect is the default one and keeps track of stuff like lighting (ambient, directional, specular, emissive), texturing, colors and fog. It also has interfaces to be derived from by the user (one for matrix positioning, one for light and one for fog).
  • Alpha test effect is used for performance by rejecting pixels based off their alpha level.
  • Enviroment map effect is used for maping the enviroment into an object (think mirror).
  • Skinned Effect is ussed for animation of models, it relies heavily on a proper content processor.


States of the graphics device

A state can be passed to the Begin method of the spritebatch when selecting to draw something.

Blend state controls how colors blend together, can be used in various ways, among others to get the negative of the image, to control the alpha operations and play with the depth stencil.

Render targets

There is a RenderTarget2D class that allows you to render to it instead of the device, which you can use it by calling GraphicsDevice.SetRenderTarget(rt), and in HiDef mode you can have up to 4 different render targets going.

Wednesday, August 10, 2011

XNA Notes–Day 3

If you want to set a rectangle section of the screen to draw to, set the GrpahicsDevice.ScissorRectangle to a rectange and set the ScissorTestEnable property of the GraphicsDevice.RasterizerState to true.

GraphicsAdapter is used to deal with all the graphic output devices a pc might have (XBox and WP7 only have one), and their characteristics.

GraphicsDevice has a back buffer and uses double buffering. the Present method does the flip, and there are several flip modes

  • .PresentInterval.One (Flips when the display is in vertical retrace phase)
  • PresentInterval.Two (Flips every other vertical retrace)
  • PresentInterval.Immediate (Flips as soon as possible, can cause tearing but good for measuring performance)

GraphicsDevice.SupportedOrientations lets you determine the positions you let the game work in WP7.

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.

Saturday, August 6, 2011

XNA Notes–Day 1

I’ve been programming on and off in XNA for a few years now, but I never really tried to learn XNA, just picked it up on the go, googling whatever was needed at the time. But now I’m working on a more dedicated project, and not knowing my tools in depth can only lead to suboptimal solutions. Therefore, I have decided to pick up the XNA Game Studio 4.0 Programming book, by Tom Miller and Dean Johnson, and start studying it from the beginning.

This series of XNA notes are just that, notes from studying, with curious / useful facts from the book, as I have a horrible memory and writing things down is the only way to remember them. Besides, someone else might find something useful here, who knows?

So, without further ado, today’s notes:

Random notes

Game Studio 4.0 is broken up in two different profiles, Reach which encompasses features for all platforms and HiDef, which includes extra features that exist only in W and Xbox.

The origin of coordinates is the top left.

font.MeassureString(string) tells you the width and height of the string in that font.

SpriteBatch

Tinting in spritebatch.draw multiplies the original color of the image by the color of the tint.

Using a Vector2 instead of a rectangle means we don't do any stretching to the image when we render it on screen, we draw it with the original size.

Different overloads

In the appropriate draw overloads, the source rectangle lets you decide which part of the original image you're going to draw. Useful for frame animations.
In the appropriate draw overloads you can rotate the image using an angle in radians and an origin for the rotation.  You can also scale the image.
You have some crappy SpriteEffect options, which are to flip horizontaly, to flip vertically or none.
There is a Z ordering for the drawing between 1.0 (top) and 0.0 (bottom) that only matter if you play arround with the sorting modes.

Sprite Sorting

Using spritebatch.begin we can specify the draw mode, among others SpriteSortMode…

  • Texture (sort by texture, do all of the same texture at the same time as not to have to change, default),
  • FrontToBack / BackToFront (taking into account the depth specified for each draw call),
  • Deferred (Drawn in the order it is used in the draw calls),
  • Immediate (different from all others bacause hardware render calls are not batched, but instead we have one hardware call per draw call).

Sprite Blending

Using spritebatch.begin we can also specify the blend state, among others BlendState.

  • AlphaBlend (default blending mode, transparency works fine),
  • Opaque (No blending),
  • Aditive (adds the colors together).