Use HoloJs in Win32 C++ projects

HoloJs overview
HoloJs apps
HoloJs in Win32 apps
HoloJs in UWP apps
VR and AR experiences with Spin Sample script apps

Hosting HoloJs in a Win32 C++ application

After following the steps outlined bellow, you will have a Win32 application that is executing a 3D application written in JavaScript and WebGL.

Requirements:

  1. Visual Studio 2017 with C++ development components installed
  2. Visual Studio Code for debugging your JavaScript

Steps:

  1. In Visual Studio, create a new C++ console application:

  2. Add the HoloJs Nuget package. Since its a pre-release package, make sure to check the "Include prerelease" checkbox when searching.
  3. In the source file that contains your main function, add these headers:
    #include "holojs/holojs.h"
    #include <memory>
    #include <Windows.h>

    using namespace std;
    using namespace HoloJs;

  4. Replace the main function body with these lines:

    // Instantiate the HoloJs object
    shared_ptr<IHoloJsScriptHost> scriptHost(CreateHoloJsScriptHost(), &DeleteHoloJsScriptHost);

    // Create a default configuration
    auto configuration = HoloJs::ViewConfiguration();
    scriptHost->initialize(configuration);

    // Run the app; this method returns when the user closes the window on desktop or
    // switches away from the app on VR and HoloLens.
    scriptHost->startUri(L"https://microsoft.github.io/HoloJS/samples/vr-cubes.xrs");

  5. Press F5 to run.
    Without a VR headset, you should get a window that renders cubes floating in space:

    If you have a VR headset connected, you should see cubes floating around you in a virtual room:

Debugging scripts running inside HoloJs

When HoloJs is hosted inside a Win32 app, you will use Visual Studio Code to debug your scripts.
  1. Enable debugging in HoloJs by adding this line of code before you start the script app:
    scriptHost->enableDebugger();
    Note: With the debugger enabled, the script app will not start until a debugger attaches. The window will appear hung until then.
  2. Start Visual Studio Code and open the folder where the scripts are located
  3. Create a debug configuration
    1. Go to the debugger panel (Ctrl + Shift + D)
    2. Add a new debug configuration
    3. Pick "node.js" from the options list
    4. Add the following to the launch.json file:
      {
      "type": "node",
      "request": "attach",
      "name": "Attach",
      "port": 9229,
      "protocol": "inspector"
      }
    5. This debug configuration is now saved in .vscode/launch.json and it will work every time you open this folder in Visual Studio Code.
  4. Start your Win32 app. You can launch it from Visual Studio (F5) or any other way.
  5. Press F5 in Visual Studio Code. The debugger will break in immediatelly. You can now step through your code, set breakpoints, continue execution, etc.
    Note: The list of scripts running can be found in the debug panel, under "Loaded scripts"