33 #include <emscripten.h> 
   34 #include <emscripten/em_asm.h> 
   35 #include <emscripten/html5.h> 
   36 #include <emscripten/val.h> 
   64 static EMSCRIPTEN_RESULT 
onMousePressed(
int eventType, 
const EmscriptenMouseEvent* mouseEvent, 
void* userData);
 
   65 static EM_BOOL           
onMouseReleased(
int eventType, 
const EmscriptenMouseEvent* mouseEvent, 
void* userData);
 
   66 static EM_BOOL           
onMouseDoubleClicked(
int eventType, 
const EmscriptenMouseEvent* mouseEvent, 
void* userData);
 
   67 static EM_BOOL           
onMouseMove(
int eventType, 
const EmscriptenMouseEvent* mouseEvent, 
void* userData);
 
   68 static EM_BOOL           
onMouseWheel(
int eventType, 
const EmscriptenWheelEvent* wheelEvent, 
void* userData);
 
   69 static EM_BOOL           
onKeyPressed(
int eventType, 
const EmscriptenKeyboardEvent* keyEvent, 
void* userData);
 
   70 static EM_BOOL           
onKeyReleased(
int eventType, 
const EmscriptenKeyboardEvent* keyEvent, 
void* userData);
 
   71 static EM_BOOL           
onTouchStart(
int eventType, 
const EmscriptenTouchEvent* touchEvent, 
void* userData);
 
   72 static EM_BOOL           
onTouchEnd(
int eventType, 
const EmscriptenTouchEvent* touchEvent, 
void* userData);
 
   73 static EM_BOOL           
onTouchMove(
int eventType, 
const EmscriptenTouchEvent* touchEvent, 
void* userData);
 
   74 static const char*       
onUnload(
int eventType, 
const void* reserved, 
void* userData);
 
   91     EmscriptenWebGLContextAttributes attributes;
 
   92     emscripten_webgl_init_context_attributes(&attributes);
 
   93     attributes.enableExtensionsByDefault = 
true;
 
   94     attributes.antialias                 = 
false;
 
   95     attributes.depth                     = 
true;
 
   96     attributes.stencil                   = 
true;
 
   97     attributes.alpha                     = 
true;
 
   98     attributes.majorVersion              = 2;
 
   99     attributes.minorVersion              = 0;
 
  100     attributes.preserveDrawingBuffer     = 
true;
 
  102     auto context = emscripten_webgl_create_context(
"#canvas", &attributes);
 
  104         SL_LOG(
"WebGL context created.");
 
  108     EMSCRIPTEN_RESULT result = emscripten_webgl_make_context_current(context);
 
  109     if (result == EMSCRIPTEN_RESULT_SUCCESS)
 
  110         SL_LOG(
"WebGL context made current.");
 
  112         SL_EXIT_MSG(
"Failed to make WebGL context current.");
 
  114     emscripten_set_mousedown_callback(
"#canvas", 
nullptr, 
false, 
onMousePressed);
 
  115     emscripten_set_mouseup_callback(
"#canvas", 
nullptr, 
false, 
onMouseReleased);
 
  117     emscripten_set_mousemove_callback(
"#canvas", 
nullptr, 
false, 
onMouseMove);
 
  118     emscripten_set_wheel_callback(
"#canvas", 
nullptr, 
false, 
onMouseWheel);
 
  119     emscripten_set_keydown_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, 
nullptr, 
false, 
onKeyPressed);
 
  120     emscripten_set_keyup_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, 
nullptr, 
false, 
onKeyReleased);
 
  121     emscripten_set_touchstart_callback(
"#canvas", 
nullptr, 
false, 
onTouchStart);
 
  122     emscripten_set_touchend_callback(
"#canvas", 
nullptr, 
false, 
onTouchEnd);
 
  123     emscripten_set_touchmove_callback(
"#canvas", 
nullptr, 
false, 
onTouchMove);
 
  124     emscripten_set_beforeunload_callback(
nullptr, 
onUnload);
 
  126     SL_LOG(
"------------------------------------------------------------------");
 
  127     SL_LOG(
"Platform         : Emscripten");
 
  136                 "data/images/textures/",
 
  137                 "data/images/fonts/",
 
  140                 "AppDemoEmscripten");
 
  159     int newCanvasWidth  = EM_ASM_INT(
return window.devicePixelRatio * 
window.innerWidth);
 
  160     int newCanvasHeight = EM_ASM_INT(
return window.devicePixelRatio * 
window.innerHeight);
 
  190     return appNeedsUpdate || viewNeedsUpdate || jobIsRunning || isLoading;
 
  197         let canvas = Module[
'canvas'];
 
  218                       (
int)(142.0 * EM_ASM_DOUBLE(
return window.devicePixelRatio)),
 
  220                       reinterpret_cast<void*
>(
onPaint),
 
  227     EM_ASM(document.querySelector(
"#loading-overlay").style.opacity = 0);
 
  244                                         const EmscriptenMouseEvent* mouseEvent,
 
  254     switch (mouseEvent->button)
 
  287                                const EmscriptenMouseEvent* mouseEvent,
 
  297     switch (mouseEvent->button)
 
  327                                     const EmscriptenMouseEvent* mouseEvent,
 
  334     switch (mouseEvent->button)
 
  364                            const EmscriptenMouseEvent* mouseEvent,
 
  370     if (mouseEvent->altKey && mouseEvent->ctrlKey)
 
  385                             const EmscriptenWheelEvent* wheelEvent,
 
  389     double deltaY = -wheelEvent->deltaY;
 
  401                             const EmscriptenKeyboardEvent* keyEvent,
 
  404     if (keyEvent->repeat)
 
  415                              const EmscriptenKeyboardEvent* keyEvent,
 
  426                             const EmscriptenTouchEvent* touchEvent,
 
  429     if (touchEvent->numTouches == 1)
 
  440     else if (touchEvent->numTouches == 2)
 
  456                           const EmscriptenTouchEvent* touchEvent,
 
  459     if (touchEvent->numTouches == 1)
 
  473         if (dt > 800 && dx < 15 && dy < 15)
 
  487     else if (touchEvent->numTouches == 2)
 
  500                            const EmscriptenTouchEvent* touchEvent,
 
  503     if (touchEvent->numTouches == 1)
 
  509     else if (touchEvent->numTouches == 2)
 
  522                             const void* reserved,
 
  536     return (
SLint)((double)coordinate * EM_ASM_DOUBLE(
return window.devicePixelRatio));
 
  544         case 9: 
return K_tab;
 
  548         case 18: 
return K_alt;
 
  549         case 27: 
return K_esc;
 
  553         case 35: 
return K_end;
 
  556         case 38: 
return K_up;
 
  561         case 96: 
return K_NP0;
 
  562         case 97: 
return K_NP1;
 
  563         case 98: 
return K_NP2;
 
  564         case 99: 
return K_NP3;
 
  565         case 100: 
return K_NP4;
 
  566         case 101: 
return K_NP5;
 
  567         case 102: 
return K_NP6;
 
  568         case 103: 
return K_NP7;
 
  569         case 104: 
return K_NP8;
 
  570         case 105: 
return K_NP9;
 
  576         case 112: 
return K_F1;
 
  577         case 113: 
return K_F2;
 
  578         case 114: 
return K_F3;
 
  579         case 115: 
return K_F4;
 
  580         case 116: 
return K_F5;
 
  581         case 117: 
return K_F6;
 
  582         case 118: 
return K_F7;
 
  583         case 119: 
return K_F8;
 
  584         case 120: 
return K_F9;
 
  585         case 121: 
return K_F10;
 
  586         case 122: 
return K_F11;
 
  587         case 123: 
return K_F12;
 
  588         default: 
return (
SLKey)key;
 
The App namespace declares the App::Config struct and the App::run function.
 
The AppCommon class holds the top-level instances of the app-demo.
 
static EM_BOOL onTouchEnd(int eventType, const EmscriptenTouchEvent *touchEvent, void *userData)
 
static SLint svIndex
Scene view index.
 
static SLbool onPaint()
Paint event handler that passes the event to the slPaintAllViews function.
 
static EM_BOOL onMouseWheel(int eventType, const EmscriptenWheelEvent *wheelEvent, void *userData)
 
static int canvasWidth
Width of the HTML canvas.
 
static const char * onUnload(int eventType, const void *reserved, void *userData)
 
static SLbool coreAssetsLoaded
Indicates whether core assets can be used.
 
static SLint startY
start position y in pixels
 
static EM_BOOL onKeyPressed(int eventType, const EmscriptenKeyboardEvent *keyEvent, void *userData)
 
static SLKey mapModifiersToSLModifiers(bool shiftDown, bool ctrlDown, bool altDown)
 
static EM_BOOL onMouseReleased(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
 
static EM_BOOL onMouseDoubleClicked(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
 
static double lastTouchDownTimeMS
Time of last touch down in milliseconds.
 
static EMSCRIPTEN_RESULT onMousePressed(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
 
static SLKey mapKeyToSLKey(unsigned long key)
 
static SLint mouseX
Last mouse position x in pixels.
 
static EM_BOOL onTouchMove(int eventType, const EmscriptenTouchEvent *touchEvent, void *userData)
 
static void updateCanvas()
 
static SLint lastHeight
Last window height in pixels.
 
static SLint startX
start position x in pixels
 
static SLVec2i touchDelta
Delta between two fingers in x.
 
static int lastTouchDownY
Y coordinate of last touch down.
 
static EM_BOOL onKeyReleased(int eventType, const EmscriptenKeyboardEvent *keyEvent, void *userData)
 
static SLint convertToCanvasCoordinate(SLint coordinate)
 
static SLint lastWidth
Last window width in pixels.
 
static long animationFrameID
ID of the current JavaScript animation frame.
 
static EM_BOOL onMouseMove(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
 
static SLint mouseY
Last mouse position y in pixels.
 
static SLVec2i touch2
Last finger touch 2 position in pixels.
 
static int lastTouchDownX
X coordinate of last touch down.
 
static int canvasHeight
Height of the HTML canvas.
 
static void onLoadingCoreAssets()
 
static EM_BOOL onTouchStart(int eventType, const EmscriptenTouchEvent *touchEvent, void *userData)
 
static EM_BOOL onAnimationFrame(double time, void *userData)
 
static SLKey modifiers
last modifier keys
 
static GLFWwindow * window
The global glfw window handle.
 
vector< SLstring > SLVstring
 
#define SL_EXIT_MSG(message)
 
SLKey
Keyboard key codes enumeration.
 
Singleton class for global render state.
 
void slTouch2Move(int sceneViewIndex, int xpos1, int ypos1, int xpos2, int ypos2)
 
void slMouseDown(int sceneViewIndex, SLMouseButton button, int xpos, int ypos, SLKey modifier)
 
void slTouch2Down(int sceneViewIndex, int xpos1, int ypos1, int xpos2, int ypos2)
 
void slLoadCoreAssetsAsync()
 
void slMouseMove(int sceneViewIndex, int x, int y)
 
void slSwitchScene(SLSceneView *sv, SLSceneID sceneID)
 
void slMouseWheel(int sceneViewIndex, int pos, SLKey modifier)
 
void slKeyRelease(int sceneViewIndex, SLKey key, SLKey modifier)
 
void slMouseUp(int sceneViewIndex, SLMouseButton button, int xpos, int ypos, SLKey modifier)
 
void slTouch2Up(int sceneViewIndex, int xpos1, int ypos1, int xpos2, int ypos2)
 
void slDoubleClick(int sceneViewIndex, SLMouseButton button, int xpos, int ypos, SLKey modifier)
 
void slResize(int sceneViewIndex, int width, int height)
 
void slKeyPress(int sceneViewIndex, SLKey key, SLKey modifier)
 
bool slUpdateParallelJob()
 
void slCreateApp(SLVstring &cmdLineArgs, const SLstring &dataPath, const SLstring &shaderPath, const SLstring &modelPath, const SLstring &texturePath, const SLstring &fontPath, const SLstring &videoPath, const SLstring &configPath, const SLstring &applicationName)
 
SLint slCreateSceneView(SLAssetManager *am, SLScene *scene, int screenWidth, int screenHeight, int dotsPerInch, SLSceneID initScene, void *onWndUpdateCallback, void *onSelectNodeMeshCallback, void *onNewSceneViewCallback, void *onImGuiBuild, void *onImGuiLoadConfig, void *onImGuiSaveConfig)
 
Declaration of the main Scene Library C-Interface.
 
static SLstring calibIniPath
That's where data/calibrations folder is located.
 
static optional< SLSceneID > sceneToLoad
Scene id to load at start up.
 
static SLAssetManager * assetManager
asset manager is the owner of all assets
 
static SLVSceneView sceneViews
Vector of sceneview pointers.
 
static SLAssetLoader * assetLoader
Asset-loader for async asset loading.
 
static SLScene * scene
Pointer to the one and only SLScene instance.
 
void checkIfAsyncLoadingIsDone()
 
SceneView class represents a dynamic real time 3D view onto the scene.
 
int run(Config config)
App::run implementation from App.h for the Emscripten platform.
 
Config config
The configuration set in App::run.
 
App configuration struct to be passed to the App::run function.
 
OnGuiLoadConfigCallback onGuiLoadConfig
 
OnNewSceneViewCallback onNewSceneView
 
OnUpdateCallback onUpdate
 
OnGuiSaveConfigCallback onGuiSaveConfig
 
OnGuiBuildCallback onGuiBuild