You can achieve single-frame page initialization (a.k.a. Instaload) if you can ensure that Cohtml receives all needed resources (html/css/img/fonts/etc.) for the page either before or during the first Advance call. This has several important benefits:
Advance
call.The overall workflow goes as follows:
OnResourceRequest
calls immediately (as in, in the same call stack). This is where preloading resources in memory comes in handy for performance reasons, but you can still read them from the hard drive here, as long as you pass the file contents to Cohtml before the OnResourceRequest
call is finished.If all resources that the HTML page needs were successfully loaded before or during their OnResourceRequest
callbacks, the HTML page is guaranteed to load fully on the first Advance. You can ensure that by checking if the ViewListener::OnFinishLoad callback was called.
You can find an example implementation of the steps listed below in the Gameface Instaload sample.
Configure the Cohtml system to use synchronous resource requests, via
systemSettings.AsynchronousResourceRequestCalls = false;
This makes sure that whenever Cohtml encounters a resource dependency, it will immediately ask you for it, instead of just scheduling a task and moving on with execution. All resource requests will now happen on the main thread and any work you do during them will block it. Therefore, it is advised to have the resources loaded in memory. Note that you are still allowed to not provide the resource during the call to OnResourceRequest
in synchronous mode - it just means that you will not achieve a single frame load.
Pick a convenient time for every type of resource that your page will use and preload them using the APIs outlined in our resource preloading guide. For this example, let's assume that we've picked a point in time after the Cohtml System is created, but before any HTML page is loaded in the View. All resource types are preloadable during this time.
void MyApp::Initialize() { PreloadImages("images"); PreloadFonts("fonts"); PreloadCSS("css"); PreloadHTML("html"); PreloadResources(); }
Page loading can trigger in several ways, for example by following hyperlinks, but for now let's consider just manually loading the page in C++
// Load through C++ View->LoadURL("my_page.html");
If all resources for the page my_page.html
are preloaded, your next call to cohtml::System::Advance after executing the code above, will mark all resources as "ready for use" by the Views. After that, when your code reaches cohtml::View::Advance, the Cohtml library will: