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.

Thursday, August 11, 2011

Baddies 3–Editor

Showing the game in a PictureBox

The first hurdle when making the game editor with windows forms was getting it to render XNA in a form. For this I used a picture box and followed the tutorial at http://royalexander.wordpress.com/2008/10/09/xna-30-and-winforms-the-easy-way/ The only change I had to do was to make sure the back buffer size of the xna game was the same as the size of the picture box, otherwise weird stretching ensued.


 

Coupling game and form

There is a high coupling between both game and form in the editor, as this is the fastest and cleanest method of working the interaction between both of them. None of the two classes are intended for any reuse or generic use, so it’s the best decision. For this, we just store a reference to each other during the initialization of the program so they are linked, and provide methods to access whatever is needed.


 

Creating a map

Creating a new map is an essential part of the editor. I created a new form that took care of receiving all the necessary information and then launched an event with this data.
This event was caught by the main editor form and then passed on to the game class, where we just create a MapGrid node and add it to the root.


 

Loading a TileSet

To load a new tile set we create a dialog that uses the windows interface to load the required image file with the tileset. If the image is found, we create one TilePictureBox out of each bit of the image that is of tileSize x tileSize size and set it in the appropriate form container, so when the user clicks on one he can use it to draw.
Making a specific class instead of just using a normal picturebox was necessary to make the tiles react to the OnClick event.

Wednesday, August 10, 2011

Baddies 2.8–Map

The map is just a list of lists, holding all the tiles in a matrix. The only thing it has special is the fact that it holds the tileset image, with all the images of the tiles, as well as other extra graphics used for other drawing modes (for example the grid). When the map wants to draw the tiles (be it as the actual tiles, or the grid, or the collision) it passes the corresponding graphic to the tiles in their draw function.


The map tiles are not nodes because of the need to access them easily with a 2D coordinate system, and the special operations that we need to do with them. It’s one of the cases where efficiency is more important than following the tree structure.

Baddies 2.7–GUI Input Management

The game engine needs to accept input from several devices, mainly Xbox, Windows and WP7. For each of them a different input controller must be used.
The input device is an independent class, it has no power to actually do anything or change anything. Instead what happens is it interprets the input of the user using various means (selection boxes, touches, radial menus, etc.) depending on the device. Once it knows what the action is (select units in rectangle, interact with clicked position, etc.) it sends a message with all the relevant data, to be caught and acted upon by whoever is responsible.


Having a system like this prevents having lots of cases for the different controllers distributed all through the code, and also avoids duplication of work.


The basic idea is that the primary input of the user gets transformed into some kind of command with data that then the engine then interprets homogenously, without knowing who sends the message. Adding new input devices is therefore trivial, as the engine just receives messages of commands to execute.


As for the GUI elements themselves, they hang from the input device they are designed for (no sense having a selection box in the Xbox for example), and the input device object is always on the top of any other nodes so it gets the input / drawn first. The input device has a GUINode that is active at the moment. That’s the one which attempts to answer to the user event (say a keypress), if it consumes the event, that’s it.  If not, we send a message to the system, to see if any other node wants anything to do with that event.


Having the GUINodes independent of the InputDevice class makes it so if we want a standard GUI element (say a minimap), we just add the MiniMapGUINode to the InputDeviceNode, and we know it’s going to be on top, whereas if we want something more local (for example a speech bubble) we add it to the player node, and it’s going to be in the proper Z height, at the same as the player.

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.