SpriteBatch, Menus, 1080p support etc

 
SpriteBatch

I spent the last couple of days implementing a system to replace my primary usage of SpriteBatch throughout the project as it was causing some issues, and I also viewed it as inefficient due to the amount of sprites I was dealing with. I created a Layer2D object which houses a Vertex & Index buffer for a pre-defined set of sprites. I setup multiple layers for various elements, such as the Fleets, Systems, background blending etc. This gave me a few advantages:

* Zooming: I used to use some hinky zooming logic which worked using modifications to SpriteBatch.Draw’s scale argument to achieve a visual “zoom” effect. It worked in the end but the calculations were not nice at all. With Layer2D the sprites reside on a plane in 3D space, so to Zoom now, I just move the camera closer to that plane.

* Collision: Instead of relying on dubious zoom-related position finding (see point above) I now use a method similar to 3D picking to find position of the Mouse Cursor on a desired Layer2D, which is much more robust.

* Efficiency: Once I add a sprite to the Layer2D, I don’t have to re-create or re-setup any data to have it render each frame if it’s not moving. If it moves or rotates, the vertex data is updated in the buffer for that sprite only (4 vertices per sprite). Drawing involves one call to DrawUserIndexedPrimitives() per layer. From what I understand of SpriteBatch, it maintains a dynamic buffer internally so that each time you call Draw/DrawString, each frame, it adds Vertex information for that call to the buffer, and then writes it during SpriteBatch.End(); which for larger sets of sprites (6000+) seems like an awful lot of overhead.

* Shaders: My Layer2D accepts a shader on the constructor so that I can do anything I wish to customise how the sprites look. Though currently the effect will apply to every sprite in the Layer.

Menus

I also spent some time working on the interface before you get to the actual gameplay, which I think a lot of XNA developers overlook. Mainly, Press Start screen and Main Menu screen which can be briefly seen in the video below.

Since the game will be controlled quite differently on Xbox and PC, I use a ViewController system which, depending on the platform it is running on, is instantised with either XboxViewController or MouseViewController. These handle all the input-related movement and selection operations, exposing generic inputs to the GameplayScreen so that the camera can be moved or the Zoom level adjusted. It will mean less platform-specific code down the road when more UI interaction work is done.

1080p Support

I’ve decided to add support for 1080p for the Xbox version, which means supporting two resolution paths; 1080p and 720p. The Xbox doesn’t natively scale 1080p when viewing on smaller resolution displays, 720p does however. So my plan will be to default to 1080p unless the display/xbox is configured to 720p or lower, in which case 720p will be used. I’m writing all my menus and interface code to scale appropriately, as well as having a hotkey to switch between 1080p and 720p instantly whilst running to aid in testing.

Video

Finally, here’s a video of the new Layer2D system in operation. Only the mouse cursor and top-left text is being rendered by SpriteBatch now.

The multi-colored dots appearing is a prototype “Control Map” of the galaxy which provides a quick visual overview of the systems a certain player owns. You can see them change colors when fleets reach and take ownership of a system.

I made each fleet simply search for the closest system to it’s home system that it’s player doesn't own, and fly there. Once it arrived, and took ownership of the system and keep repeating the find-own procedure. If another fleet takes a closer planet, it will likely fly back closer to the home planet and re-take control. There’s no actual combat involved in the video, it's more to prototype the fleet targeting and movement system that the AI players will most likely use.

click to view the 720p version


Tags: , , ,
Categories: XNA

35 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed

Comments