Coherent UI for .NET  2.5.3
A modern user interface library for games
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
SlimDX Sample

For instructions on deploying the sample, please read .NET Sample guides.

The SlimDX sample demonstrates usage of Coherent UI with the SlimDX framework. SlimDX is the only .NET framework that provides usage of shared textures for Coherent UI views, which yield a perfomance improvement over using shared memory.

The sample is composed of the following files:

Game.cs - contains the main loop of the application, initialization and uninitialization routines.
Rendering.cs - DirectX initialization, texture creation and various utility properties.
InputHandling.cs - utilities for converting SlimDX supplied input events to Coherent events.
Listeners.cs - classes providing handlers for Coherent UI callbacks

A detailed description of each file follows.

Game.cs

The Game class creates and instance of SlimDXApp and initializes and runs it. The initialization routine creates the DirectX device and initializes Coherent UI.

Warning
You'll notice that the ContextListener class passed as parameter to the view context is saved to a member variable, because the if it wasn't it would be garbage collected and the system would throw an exception. This is valid for the ViewListener as well.

The game subscribes to the Idle event of Application and processes frames in the DoFrame method. Frame processing consists of updating the UI System, fetching the composed surfaces and drawing the texture received from Coherent UI as a fullscreen triangle. This is also where the system listener is polled for UI readiness so we know when can we create a view.

Rendering.cs

This file provides basic SlimDX initialization and utility routines, unrelated to Coherent UI integration.

The InitializeDirectX method creates a device and swap chain, compiles a simple shader for rendering a textured fullscreen triangle and sets the appropriate device states.
The RecreateTextureFromHost method provides means for recreating the texture that is drawn on the fullscreen triangle. This is used when resizing the game window.

InputHandling.cs

Here we have the conversion of the SlimDX events to Coherent UI events. Since the SlimDX form inherits System.Windows.Forms.Form, it receives the standard KeyEventArgs / MouseEventArgs, making the conversion very easy. We've also made use of the Windows function GetKeyState (since SlimDX is available on Windows only) to get the key state for the events. You'll notice that IsNumPad and IsAutoRepeat properties of the key events are always false, because we don't have that information in .NET. We could have used more native Windows APIs to determine the properties, but they are non-essential and draw the focus away from the sample's purpose, integrating Coherent UI.

Listeners.cs

You'll find the ContextListener and MyViewListener classes here.

The ContextListener class is the view context listener, which serves the purpose to notify the user when the UI System is ready to operate.

The MyViewListener class is what handles the important events for drawing. Since SlimDX supports shared textures, we can use this image transport mechanism for our view. To do that we must override the ViewListener.CreateSurface and ViewListener.DestroySurface methods, and also subscribe for the DrawTexture event.

The CreateSurface method asks SlimDX to create a shared surface with the specified width and height using a 32-bit BGRA format and returns the shared handle for the texture.
The DestroySurface method just disposes the texture associated with the handle.
The OnDraw handler receives a shared handle, obtains a Texture2D from it and invokes the DirectX11 context's CopySubresourceRegion method. This way the surface received from Coherent UI is copied to the texture that is drawn in the DoFrame method of the application.

There is another overload of OnDraw, receiving an array of pixels as input. It creates a DataStream from the pixels and calls the context's UpdateSubresource to upload the stream to video memory. This overload is used when the view is created with the shared memory flag (The ViewInfo.UsesSharedMemory property). The shared texture transport is preferred, but shared memory is supported for SlimDX as well.