Showing posts with label GUI. Show all posts
Showing posts with label GUI. Show all posts

Friday, September 21, 2007

Window System for XNA - New Release

Just to let everybody know that there should be a new release within the next week, basically when my internet connection is back up. The reason for the sudden release is because I've finally managed to fix the GUI rendering, with correct alpha values. The most important impact is that anti-aliased SpriteFont will be used from now on, and the old XNAExtras stuff is to be removed.

Project Site

If anyone is interested in what the problem was, and how I fixed it, then read on.

In the old system, every control was rendered to its own texture using render targets. Each child control would have its texture combined with those of the other children, to make up the texture of the parent control (after drawing itself). It was quite efficient because only the top level controls (generally windows) actually get their textures drawn to the screen, with the textures only redrawn when necessary, such as when the mouse hovers over a button, and a different image must be displayed. Render targets were also an easy way to perform clipping of child controls within their parent, and allowed the alpha value of a control to affect all children as well.

The problem with render targets was that control textures had to be drawn to a transparent texture, causing incorrect colour and alpha values to be produced during the alpha blending operation. In practical terms, it means that the edges of sprites with partial transparency and anti-aliased text would be mixed with white (or any other colour depending on the clear colour of the render target). This made either anti-aliased text unreadable, or made the edges of some controls the wrong colour. The reason I couldn't use SpriteFont, was because it created anti-aliased fonts. With XNAExtras I could make a simple font bitmap with colour keyed transparency, which made the text problem go away.

After playing with render states and pre multiplied alpha for a long time, I finally got fed up and tried to find another way to get it working. I was advised to draw directly to the screen, and use scissor rectangles to handle clipping. This worked great, except performance was abysmal. With just 4 complex windows on the screen, the framerate would start to drop. That was without any game running in the background! The reason for the poor performance was because scissor rectangles must be the same until SpriteBatch.End() is called. This means that I had a SpriteBatch.Begin() and End() call for every single control!

Next I decided to implement my own texture clipping, which all worked great, but there wasn't an obvious way to clip text. When using scissor rectangles for just text, the performance was still really bad. The final method was to use a hybrid approach. I would clip sprites on the CPU, and render text to textures which would be cached to allow fast draw calls. The text texture can then be clipped in the same way as the rest of the GUI sprites. Of course there was still the problem of partial alpha pixels being blended with the transparent texture colour. Basically I set the render states to draw with pre multiplied alpha, and set the background clear colour of the texture to the text colour, with the alpha channel set to 0. This means that the blending still takes place, but it produces the correct results. The performance is good once again!

So yeah, the project isn't dead, I've just had some big problems to deal with. Now that this is working, I will go back to adding more features like Xbox 360 support, and more widgets.

Thursday, April 26, 2007

Fixed Project Files

Now that I have an Internet connection again, I decided to fix the project files that you guys have had problems with. It seems Visual Studio uses relative paths for everything, except the XNA content assemblies.

Download Source Code

This release is exactly the same as the last one, so don't bother downloading if you've already fixed your project files.

Microsoft have recently released an update for XNA, which I will be checking out tonight. It has font support, although I gather it's fairly basic. I will try to integrate this into the GUI, although I suspect I will run into the same problems I had before with their font sample code. In case you haven't read my comments from a while ago, the problem is that anti-aliased fonts simply won't work with the GUI at the moment. The only way I can see it working is to implement multiple passes when rendering the controls. Personally I think that small font sizes should be used anyway, which look better without anti-aliasing.

Saturday, March 31, 2007

XNA WindowSystem Source Code

Tomorrow I'm moving back to England, and won't be able to work on the project for a little while, so I thought this was as good a time as any to release an early version of my GUI for XNA. For screenshots, see my previous post.

Download Source Code

Features
  • Input events
  • Individual control transparency
  • Controls can have multiple skins, representing different states (hover, pressed etc.)
  • Skinnable at the GUI level through static calls, or at the control level
  • GUI and input event system implemented as game components
  • Custom mouse cursors
  • Modal windows
High-Level Controls
  • Check boxes
  • Combo boxes
  • Dialogs
  • Display settings dialog
  • Icons
  • Images
  • Image buttons
  • Text buttons
  • Labels
  • Menus
  • Context menus
  • Message boxes
  • Radio buttons
  • Radio groups
  • Vertical scrollbars
  • Text boxes
  • Windows
Future Plans
  • Flexible system for control animations
  • Possibly a settings file for customizing skins
  • Possibly bring the GUI into 3D for cooler animations
  • Context menus for any control, currently has to be done manually for each case
  • More complete textbox, with text selection and copy/paste
  • Horizontal scrollbars
Most of those features wouldn't be that hard to implement, it's just a matter of time.

The code is in a pretty stable state at the moment, but I'm sure there's hundreds of bugs to be found in there. Please feel free to contact me regarding any bugs, questions, or suggestions.

One know issue is that hideous amounts of temporary textures are created while resizing windows. I know the reason, but am waiting to find a graceful solution. Basically the GUI draws each control to it's own texture, allowing simple clipping and transparencies. While resizing windows, the control texture is recreated many times a second, sometimes using up all available texture memory. The simple fix in place at the moment, is that the garbage collector is called whenever this happens. Not pretty, but it doesn't crash. Everything goes back to normal when resizing ends.

This project uses code based on the Event Based Input System of the XNA5D library, and to a lesser extent the GUI components. The XNA5D library is by John Sedlak, and is available at jsedlak.org/Xna5D.

The library also uses the BitmapFont portion of XNAExtras for text rendering. To add new fonts to your projects, use the BMFontGen tool which part of the same project. XNAExtras is by Gary Kacmarcik, and is available at blogs.msdn.com/garykac/articles/749188.aspx.

Finally, although I drew the complete graphical skin, the message box icons are by Ken Saunders, and are available at mouserunner.com/Spheres_ColoCons1_Free_Icons.html.

Sometime soon I will post an overview of the system, and possibly a few quick tutorials on how to use it. It's pretty well documented, so this shouldn't pose too much of a problem for most of you.

In addition, I'll be releasing the source code of my first project to actually use the GUI, a tile map editor. It's already in a pretty usable state, but is very much a work in progress.

Oh yeah, and I need a name for the project. At the moment I'm just using WindowSystem for XNA, which is really just a description. Any ideas would be appreciated.

Thursday, March 29, 2007

XNA WindowSystem Screenshots

It's been a while since my last post, but I've been working hard on my GUI for the XNA Framework. There's a couple already available for free on the web, but I was really going for more of a complete window system, that could be used for game tools as well as game interfaces.


These screenshots show most of the controls available, and examples of them in use. As part of the development process, I've been working on a tile map editor to test out how the GUI should be used, and to uncover bugs.