This guide will show you how to
Coherent::UI::ViewContext
and create a single viewThe sample for this guide is HelloHTMLCocoa
Start by including the necessary headers:
First, initialize CoherentUI with the appropriate factory settings via InitializeCoherentUI
. If it succeeds, it returns a pointer to working ViewContextFactory, which you can later use to create view contexts.
Then create a view context with the appropriate settings and a listener for context events. The listener will be notified when the ViewContext
is ready to create views:
Coherent UI requires a license key as it's first initialization parameter. Trial versions come with a pre-bundled "License.h" file containing the key. Pro licensees should have received their key upon purchase. All samples presume that there is a "License.h" in the Coherent/UI folder and in it there is a COHERENT_KEY string with your license. However you can type the string wherever you want and pass it to the initialization function.
The "ViewEventListener" and "ContextEventListener" are classes inherited from Coherent::UI::ViewListener
and Coherent::UI::EventListener
respectively. They should implement the callbacks that a Coherent UI context will invoke upon certain events. If you are uncertain on how to initialize he factory, or the context or wire simple drawing please consult the HelloHTML samples bundled in the Coherent UI package.
Coherent::UI::ViewContext::Update
has to be called in the main loop. It processes the incoming events and executes the appropriate callbacks. Coherent::UI::ViewContext::FetchSurfaces
checks if new rendered surfaces are available for the active views. It calls Coherent::UI::ViewListener::OnDraw
and might request the creation or destruction of surfaces. The context is split in those two calls to allow the user to more easily accomodate a distinction between the logical update of the state of the views and their drawing. Check the Best Practices guide for tips on how to optimally organize calls to Coherent UI in your frame. In the Cocoa sample a 'ViewContext' bridge class has been created to aid the Objective-C <-> C++ interoperability.
Coherent::UI::ViewContext::Update
and Coherent::UI::ViewContext::FetchSurfaces
.When the context is up and running it will call the ContextReady
method of listener
and that is the appropriate time to create a view. To create a view you need a ViewInfo
struct with the properties of the view, a URL for the view and a ViewListener
for its events.
Coherent UI provides a custom protocol for URLs - coui. All files that are display through this protocol will be loaded from a custom FileHandler
. The default one loads the files using the current working directory as root, so for our project - coui://html/hello.html will be read from html/hello.html using the application working directory as base.
The ViewListener is notified for all view related events, such as when the view has been created, finished loading, etc. It is also responsible for creating and destroying the rendering surfaces for that view. There are at least two rendering surfaces for each view - while the listener is handling a surface, the next frame is being drawn on the other. On MacOSX Coherent UI can use both 'shared textures' (through IOSurfaces) and shared memory to pass the drawn surfaces to the client. The shared textures is the preferred way because the surfaces never leave the GPU memory allowing for optimal performance.
The class GLUtility
in the sample shows how to create the needed IOSurface and bind it to the application's OpenGL context.
Coherent UI draws lazily all the views - if no change is triggered (either by an event or animation) in the view, no call to Coherent::UI::ViewListener::OnDraw
will be made. In that case you must present the last received rendered frame of that view. Therefore when a Coherent::UI::ViewListener::OnDraw
event occurs, we copy the frame to a texture that is going to be used during the rendering of the scene.
Again refer to the GLUtility
class for the details regarding the update of the OpenGL texture itself. The sample creates Framebuffers for the textures it uses and performs a glBlitFramebuffer
to copy from the Coherent UI
surface to the one used to display the interface on screen. IOSurfaces automatically take care to update the textures bound to them. If you have created, as in the sample, a texture bound to the IOSurface requested in CreateSurface
, it'll always be up-to-date in the OnDraw
callback.
OnDraw
. Instead do as in the sample - create a texture and bind it to the IOSurface in the CreateSurface
method. Then in OnDraw
directly use the texture - it will be up-to-date.When a Coherent::UI::View
is no longer used it should be destroyed. Same goes for ViewContext
- it should be uninitialized when no longer needed.
To use the Debugger, please make sure that you have run bootstrap.sh on your installation.
To enable debugging of the views:
Coherent::UI::ContextSettings::DebuggerPort
to PORT - the port to be used by the remote debuggerDebugger
available in the Debugger folder in your Cohernet UI packageTo deploy an application using Coherent UI you need to deploy the following files:
HostDirectory
that is given when ViewContext
is initialized.