Top Level API

The VIVEPORT Top Level API provide global functions to setup and teardown the platform:

/** Language: C#, for Unity **/
namespace Viveport
{
   public delegate void StatusCallback(int nResult);

   class Api
   {
      public static int Init(StatusCallback callback, string appId);
      public static int Shutdown(StatusCallback callback);
      public static string Version();
      public static void QueryRuntimeMode(QueryRuntimeModeCallback);
   }
}

How to Use Top Level API

The API Init() should be called before you access any other SDK functions. It is used to setup the library runtime. Because Init() may take time, VIVEPORT SDK provides StatusCallback parameter to let you know if all init stuff is done without blocking the main thread.

On the other hand, another API Shutdown() is responsible for teardown of the library runtime after you finish using it. VIVEPORT SDK provides StatusCallback parameter to let you know if all teardown stuff is done without blocking the main thread.

The VIVEPORT SDK also provides Version() to output the current version of client-side SDK package.

Note

The callback QueryRunTimeHandler(int nResult, int nMode) of the API QueryRuntimeMode() has two return parameters:

nResult: 0 - successful, 1 - fail

nMode: 1 - VIVEPORT desktop mode, 2 - VIVEPORT arcade mode

You can use VIVEPORT Arcade API once you have switched VIVEPORT arcade mode.

Important

Please see the Error Codes and Trouble Shooting pages to know what to do when you got problems using the VIVEPORT SDK.

Detailed Example in Unity

using UnityEngine;
using System;
using Viveport;

public class ViveportDemo : MonoBehaviour
{
    static string VIVEPORT_ID = "bd67b286-aafc-449d-8896-bb7e9b351876";

    // Use this for initialization
    void Start ()
    {
        Api.Init(InitStatusHandler, VIVEPORT_ID);
    }

    private static void InitStatusHandler(int nResult)
    {
        Viveport.Core.Logger.Log("Init(): " + nResult);
        if (nResult != 0)
        {
           Viveport.Core.Logger.Log("Platform setup error ...");

           // Handle error
        }

        Viveport.Core.Logger.Log("Version: " + Api.Version());
        Api.QueryRuntimeMode(QueryRunTimeHandler);  // QueryRunMode function
    }

    private static void ShutdownHandler(int nResult)
    {
        Viveport.Core.Logger.Log("Shutdown(): " + nResult);
        if (nResult != 0)
        {
            // Handle error
        }

        // Do something
        // Do more things
    }

    private static void QueryRunTimeHandler(int nResult,int nMode)
    {
        if (nResult == 0)
        {
            Viveport.Core.Logger.Log("QueryRunTimeHandler is successful" + nResult + "Running mode is " + nMode);
            // nMode = 1 (Viveport Desktop mode), nMode = 2 (Viveport Arcade mode)
            if (nMode == 1)
            {
                // Use Viveport API
            }
            else
            {
                // Use Viveport Arcade API
            }
        }
        else
        {
           Viveport.Core.Logger.Log("QueryRunTimeHandler error: " + nResult);
        }
    }
 }

Detailed Example in Unreal

ViveportApiDemo.h

#include "Components/ActorComponent.h"
#include "ViveportApiDemo.generated.h"
#include "ViveportApi.h"


UCLASS( ClassGroup=(Viveport), meta=(BlueprintSpawnableComponent) )
class VIVEPORTSDK_API UViveportApiDemo : public UActorComponent
{
    GENERATED_BODY()

 public:
    // Sets default values for this component's properties
    UViveportApiDemo();

    // Called when the game starts
    void BeginPlay() override;

    // Called every frame
    void TickComponent(
                        float DeltaTime,
                        ELevelTick TickType,
                        FActorComponentTickFunction* ThisTickFunction
                      ) override;

    /** The VIVEPORT_ID for auth verify */
    FString VIVEPORT_ID = "bd67b286-aafc-449d-8896-bb7e9b351876";
    FString VIVEPORT_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDFypCg0OHf"
                         + "BC+VZLSWPbNSgDo9qg/yQORDwGy1rKIboMj3IXn4Zy6h6bgn"
                         + "8kiMY7VI0lPwIj9lijT3ZxkzuTsI5GsK//Y1bqeTol4OUFR+"
                         + "47gj+TUuekAS2WMtglKox+/7mO6CA1gV+jZrAKo6YSVmPd+o"
                         + "FsgisRcqEgNh5MIURQIDAQAB";

 private:
    // callback interface
    class MyInitCallback : public ViveportApiStatus
    {
    public:
        void OnSuccess(
            ) override;
        void OnFailure(int error_code
            ) override;
    };

    MyInitCallback myInitCallback;

    class MyShutdownCallback : public ViveportApiStatus
    {
    public:
        void OnSuccess(
            ) override;
        void OnFailure(int error_code
            ) override;
    };

    MyShutdownCallback myShutdownCallback;
};

ViveportApiDemo.cpp

#include "ViveportSDKPrivatePCH.h"
#include "ViveportApiDemo.h"

UViveportApiDemo::UViveportApiDemo()
{
    // Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
    // off to improve performance if you don't need them.
    bWantsBeginPlay = true;
    PrimaryComponentTick.bCanEverTick = true;
}

// Called when the game starts
void UViveportApiDemo::BeginPlay()
{
    Super::BeginPlay();
    FString fstring = UViveportApi::Version();
    GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);

    UViveportApi::GetLicense(&myLicenseChecker, VIVEPORT_ID, VIVEPORT_KEY);
    UViveportApi::Init(&myInitCallback, VIVEPORT_ID);
}

void UViveportApiDemo::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
    Super::EndPlay(EndPlayReason);
    UViveportApi::Shutdown(&myShutdownCallback);
}

// Called every frame
void UViveportApiDemo::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
    Super::TickComponent( DeltaTime, TickType, ThisTickFunction );
}

void UViveportApiDemo::MyInitCallback::OnSuccess()
{
    FString fstring("Init success.");
    GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);
}

void UViveportApiDemo::MyInitCallback::OnFailure(int error_code)
{
    FString fstring = FString::Printf(TEXT("Init failure. error=%d"), error_code);
    GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);
}

void UViveportApiDemo::MyShutdownCallback::OnSuccess()
{
    FString fstring("Shutdown success.");
    GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);
}

void UViveportApiDemo::MyShutdownCallback::OnFailure(int error_code)
{
    FString fstring = FString::Printf(TEXT("Shutdown failure. error=%d"), error_code);
    GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);
}