comparison Platforms/Wasm/wasm-application.ts @ 238:126c9c0c9333 am

SimpleViewer demo running both with SDL and Wasm
author am@osimis.io
date Wed, 20 Jun 2018 09:25:39 +0200
parents f73d722d98c8
children ddbb339ed4cf
comparison
equal deleted inserted replaced
237:b4642964c355 238:126c9c0c9333
1 ///<reference path='stone-framework-loader.ts'/> 1 ///<reference path='stone-framework-loader.ts'/>
2 ///<reference path='wasm-viewport.ts'/> 2 ///<reference path='wasm-viewport.ts'/>
3 3
4 if (!('WebAssembly' in window)) { 4 if (!('WebAssembly' in window)) {
5 alert('Sorry, your browser does not support WebAssembly :('); 5 alert('Sorry, your browser does not support WebAssembly :(');
6 } 6 }
7 7
8 declare var StoneFrameworkModule : Stone.Framework; 8 declare var StoneFrameworkModule : Stone.Framework;
9 9
10 // global functions 10 // global functions
17 var ReleaseCppViewport: Function = null; 17 var ReleaseCppViewport: Function = null;
18 var StartWasmApplication: Function = null; 18 var StartWasmApplication: Function = null;
19 19
20 20
21 function UpdateContentThread() { 21 function UpdateContentThread() {
22 if (NotifyUpdateContent != null) { 22 if (NotifyUpdateContent != null) {
23 NotifyUpdateContent(); 23 NotifyUpdateContent();
24 } 24 }
25 25
26 setTimeout(UpdateContentThread, 100); // Update the viewport content every 100ms if need be 26 setTimeout(UpdateContentThread, 100); // Update the viewport content every 100ms if need be
27 } 27 }
28 28
29 29
30 function GetUriParameters() { 30 function GetUriParameters() {
31 var parameters = window.location.search.substr(1); 31 var parameters = window.location.search.substr(1);
32 32
33 if (parameters != null && 33 if (parameters != null &&
34 parameters != '') { 34 parameters != '') {
35 var result = {}; 35 var result = {};
36 var tokens = parameters.split('&'); 36 var tokens = parameters.split('&');
37 37
38 for (var i = 0; i < tokens.length; i++) { 38 for (var i = 0; i < tokens.length; i++) {
39 var tmp = tokens[i].split('='); 39 var tmp = tokens[i].split('=');
40 if (tmp.length == 2) { 40 if (tmp.length == 2) {
41 result[tmp[0]] = decodeURIComponent(tmp[1]); 41 result[tmp[0]] = decodeURIComponent(tmp[1]);
42 } 42 }
43 } 43 }
44 44
45 return result; 45 return result;
46 } 46 }
47 else { 47 else {
48 return {}; 48 return {};
49 } 49 }
50 } 50 }
51 51
52 module Stone { 52 module Stone {
53 53
54 // export declare type InitializationCallback = () => void; 54 export class WasmApplication {
55 55
56 // export declare var StoneFrameworkModule : any; 56 private viewport_: WasmViewport;
57 private canvasId_: string;
57 58
58 //const ASSETS_FOLDER : string = "assets/lib"; 59 private pimpl_: any; // Private pointer to the underlying WebAssembly C++ object
59 //const WASM_FILENAME : string = "orthanc-framework";
60 60
61 export class WasmApplication { 61 public constructor(canvasId: string) {
62 62 this.canvasId_ = canvasId;
63 private viewport_: WasmViewport; 63 //this.module_ = module;
64 private canvasId_: string;
65
66 private pimpl_: any; // Private pointer to the underlying WebAssembly C++ object
67
68 public constructor(canvasId: string) {
69 this.canvasId_ = canvasId;
70 //this.module_ = module;
71 }
72 } 64 }
65 }
73 } 66 }
74 67
75 68
76 function InitializeWasmApplication(canvasId: string): void { 69 function _InitializeWasmApplication(canvasId: string): void {
77 70
78 /************************************** */ 71 /************************************** */
79 CreateWasmApplication(); 72 CreateWasmApplication();
80 73
81 // parse uri and transmit the parameters to the app before initializing it 74 // parse uri and transmit the parameters to the app before initializing it
82 var parameters = GetUriParameters(); 75 var parameters = GetUriParameters();
83 76
84 for (var key in parameters) { 77 for (var key in parameters) {
85 if (parameters.hasOwnProperty(key)) { 78 if (parameters.hasOwnProperty(key)) {
86 SetStartupParameter(key, parameters[key]); 79 SetStartupParameter(key, parameters[key]);
87 }
88 } 80 }
81 }
89 82
90 StartWasmApplication(); 83 StartWasmApplication();
91 /************************************** */ 84 /************************************** */
92 85
93 UpdateContentThread(); 86 UpdateContentThread();
94 } 87 }
95 88
96 // Wait for the Orthanc Framework to be initialized (this initializes 89 function InitializeWasmApplication(wasmModuleName: string) {
97 // the WebAssembly environment) and then, create and initialize the Wasm application 90
98 Stone.Framework.Initialize(true, function () { 91 Stone.Framework.Configure(wasmModuleName);
92
93 // Wait for the Orthanc Framework to be initialized (this initializes
94 // the WebAssembly environment) and then, create and initialize the Wasm application
95 Stone.Framework.Initialize(true, function () {
99 96
100 console.log("Connecting C++ methods to JS methods"); 97 console.log("Connecting C++ methods to JS methods");
101 98
102 SetStartupParameter = StoneFrameworkModule.cwrap('SetStartupParameter', null, ['string', 'string']); 99 SetStartupParameter = StoneFrameworkModule.cwrap('SetStartupParameter', null, ['string', 'string']);
103 CreateWasmApplication = StoneFrameworkModule.cwrap('CreateWasmApplication', null, ['number']); 100 CreateWasmApplication = StoneFrameworkModule.cwrap('CreateWasmApplication', null, ['number']);
109 WasmWebService_NotifyError = StoneFrameworkModule.cwrap('WasmWebService_NotifyError', null, ['number', 'string', 'number']); 106 WasmWebService_NotifyError = StoneFrameworkModule.cwrap('WasmWebService_NotifyError', null, ['number', 'string', 'number']);
110 NotifyUpdateContent = StoneFrameworkModule.cwrap('NotifyUpdateContent', null, []); 107 NotifyUpdateContent = StoneFrameworkModule.cwrap('NotifyUpdateContent', null, []);
111 108
112 // Prevent scrolling 109 // Prevent scrolling
113 document.body.addEventListener('touchmove', function (event) { 110 document.body.addEventListener('touchmove', function (event) {
114 event.preventDefault(); 111 event.preventDefault();
115 }, false); 112 }, false);
116 113
117 114 _InitializeWasmApplication("canvas");
118 InitializeWasmApplication("canvas"); 115 });
119 }); 116 }