Empirical
init.h
Go to the documentation of this file.
1 
10 #ifndef EMP_INIT_H
11 #define EMP_INIT_H
12 
14 #ifdef EMSCRIPTEN
15 
16 #include <emscripten.h>
17 #include "../tools/string_utils.h"
18 
19 extern "C" {
20  extern void EMP_Initialize();
21 }
22 
23 namespace emp {
24 
26  static void Initialize() {
27  static bool init = false; // Make sure we only initialize once!
28  if (!init) {
29  EMP_Initialize(); // Call JS initializations
30  init = true;
31  }
32  }
33 
35  static void InitializeAnim() {
36  static bool init = false; // Make sure we only initialize once!
37  if (!init) {
38  // Setup the animation callback in Javascript
39  EM_ASM({
40  window.requestAnimFrame = (function(callback) {
41  return window.requestAnimationFrame
42  || window.webkitRequestAnimationFrame
43  || window.mozRequestAnimationFrame
44  || window.oRequestAnimationFrame
45  || window.msRequestAnimationFrame
46  || function(callback) { window.setTimeout(callback, 1000 / 60); };
47  })();
48  });
49  }
50 
51  init = true;
52  }
53 
54  namespace web {
55  // Some helper functions.
56  // Live keyword means that whatever is passed in needs to be re-evaluated every update.
57  namespace {
59  template <typename VAR_TYPE>
60  std::function<std::string()> Live_impl(VAR_TYPE & var, bool) {
61  return [&var](){ return emp::to_string(var); };
62  }
63 
65  template <typename IN_TYPE>
66  std::function<std::string()> Live_impl(IN_TYPE && fun, int) {
67  return [fun](){ return emp::to_string(fun()); };
68  }
69  }
70 
72  template <typename T>
73  std::function<std::string()> Live(T && val) {
74  return Live_impl(std::forward<T>(val), true);
75  }
76  }
77 
78 }
79 
80 
81 // === Initialization for NON-emscripten to ignore macros ===
82 
83 #else
84 
85 #define EM_ASM(...)
86 #define EM_ASM_ARGS(...)
87 #define EM_ASM_INT(...) 0
88 #define EM_ASM_DOUBLE(...) 0.0
89 #define EM_ASM_INT_V(...) 0
90 #define EM_ASM_DOUBLE_V(...) 0.0
91 
92 #include <fstream>
93 
94 namespace emp {
95  std::ofstream debug_file("debug_file");
96 
98  static bool Initialize() {
99  // Nothing to do here yet...
100  return true;
101  }
102 
104  static bool InitializeAnim() {
105  // Nothing to do here yet...
106  return true;
107  }
108 
109 
110 }
111 
112 #endif
113 
114 
115 #endif
std::string to_string(ALL_TYPES &&...all_values)
Definition: string_utils.h:511
static bool Initialize()
Stub for when Emscripten is not in use.
Definition: init.h:98
std::ofstream debug_file("debug_file")
static bool InitializeAnim()
Stub for when Emscripten is not in use.
Definition: init.h:104
If we are in emscripten, make sure to include the header.
Definition: array.h:37