rngine 0.9.0 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rngine",
3
- "version": "0.9.0",
3
+ "version": "0.9.1",
4
4
  "description": "React Native 2D game engine powered by Nitro Modules",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",
@@ -71,6 +71,8 @@ void GameLoop::runGameLoop() {
71
71
  auto &gameRenderer = GameRenderer::getInstance();
72
72
 
73
73
  while (_isRunning) {
74
+ gameRenderer.processPendingTasks();
75
+
74
76
  double targetDeltaTime;
75
77
  {
76
78
  std::lock_guard<std::mutex> lock(_worldMutex);
@@ -43,63 +43,85 @@ GameRenderer::~GameRenderer() {
43
43
  }
44
44
 
45
45
  void GameRenderer::onSurfaceCreated(ANativeWindow *window) {
46
- __android_log_print(ANDROID_LOG_INFO, "GameRenderer", "onSurfaceCreated");
47
46
 
48
- _nativeWindow = window;
47
+ std::lock_guard<std::mutex> lock(_taskMutex);
48
+ _taskQueue.push([this, window]() {
49
+ __android_log_print(ANDROID_LOG_INFO, "GameRenderer", "onSurfaceCreated");
49
50
 
50
- if (!_initialized) {
51
- if (!initializeEGL()) {
52
- __android_log_print(ANDROID_LOG_ERROR, "GameRenderer",
53
- "onSurfaceCreated: EGL initialization failed");
54
- return;
51
+ _nativeWindow = window;
52
+
53
+ if (!_initialized) {
54
+ if (!initializeEGL()) {
55
+ __android_log_print(ANDROID_LOG_ERROR, "GameRenderer",
56
+ "onSurfaceCreated: EGL initialization failed");
57
+ return;
58
+ }
59
+ _initialized = true;
55
60
  }
56
- _initialized = true;
57
- }
58
61
 
59
- if (!createEGLSurface()) {
60
- __android_log_print(ANDROID_LOG_ERROR, "GameRenderer",
61
- "onSurfaceCreated: EGL surface creation failed");
62
- }
62
+ if (!createEGLSurface()) {
63
+ __android_log_print(ANDROID_LOG_ERROR, "GameRenderer",
64
+ "onSurfaceCreated: EGL surface creation failed");
65
+ }
66
+ });
63
67
  }
64
68
 
65
69
  void GameRenderer::onSurfaceChanged(int width, int height) {
66
- _width = width;
67
- _height = height;
68
- _surface = nullptr;
69
-
70
- __android_log_print(ANDROID_LOG_INFO, "GameRenderer",
71
- "onSurfaceChanged: Width: %d, Height: %d", _width,
72
- _height);
70
+ std::lock_guard<std::mutex> lock(_taskMutex);
71
+ _taskQueue.push([this, width, height]() {
72
+ _width = width;
73
+ _height = height;
74
+ _surface = nullptr;
75
+
76
+ __android_log_print(ANDROID_LOG_INFO, "GameRenderer",
77
+ "onSurfaceChanged: Width: %d, Height: %d", _width,
78
+ _height);
79
+ });
73
80
  }
74
81
 
75
82
  void GameRenderer::onSurfaceDestroyed() {
76
- if (_eglDisplay != EGL_NO_DISPLAY) {
77
- eglMakeCurrent(_eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
83
+ std::lock_guard<std::mutex> lock(_taskMutex);
84
+ _taskQueue.push([this]() {
85
+ if (_eglDisplay != EGL_NO_DISPLAY) {
86
+ eglMakeCurrent(_eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
87
+ EGL_NO_CONTEXT);
88
+
89
+ if (_eglSurface != EGL_NO_SURFACE) {
90
+ eglDestroySurface(_eglDisplay, _eglSurface);
91
+ _eglSurface = EGL_NO_SURFACE;
92
+ }
93
+ }
78
94
 
79
- if (_eglSurface != EGL_NO_SURFACE) {
80
- eglDestroySurface(_eglDisplay, _eglSurface);
81
- _eglSurface = EGL_NO_SURFACE;
95
+ if (_nativeWindow) {
96
+ ANativeWindow_release(_nativeWindow);
97
+ _nativeWindow = nullptr;
82
98
  }
83
- }
84
99
 
85
- if (_nativeWindow) {
86
- ANativeWindow_release(_nativeWindow);
87
- _nativeWindow = nullptr;
88
- }
100
+ _surface = nullptr;
101
+ _width = 0;
102
+ _height = 0;
103
+
104
+ __android_log_print(ANDROID_LOG_INFO, "GameRenderer", "onSurfaceDestroyed");
105
+ });
106
+ }
89
107
 
90
- _surface = nullptr;
91
- _width = 0;
92
- _height = 0;
108
+ void GameRenderer::processPendingTasks() {
109
+ std::queue<std::function<void()>> tasksToProcess;
93
110
 
94
- __android_log_print(ANDROID_LOG_INFO, "GameRenderer", "onSurfaceDestroyed");
111
+ {
112
+ std::lock_guard<std::mutex> lock(_taskMutex);
113
+ std::swap(tasksToProcess, _taskQueue);
114
+ }
115
+
116
+ while (!tasksToProcess.empty()) {
117
+ tasksToProcess.front()(); // Executes on the game loop thread
118
+ tasksToProcess.pop();
119
+ }
95
120
  }
96
121
 
97
122
  void GameRenderer::render(const Screen &screen,
98
123
  const std::map<std::string, Entity> &entities) {
99
124
 
100
- __android_log_print(ANDROID_LOG_DEBUG, "GameRenderer",
101
- "render: called, entities=%zu", entities.size());
102
-
103
125
  if (!_grContext || _eglSurface == EGL_NO_SURFACE || _width == 0 ||
104
126
  _height == 0) {
105
127
  __android_log_print(ANDROID_LOG_WARN, "GameRenderer",
@@ -107,12 +129,6 @@ void GameRenderer::render(const Screen &screen,
107
129
  return;
108
130
  }
109
131
 
110
- if (!eglMakeCurrent(_eglDisplay, _eglSurface, _eglSurface, _eglContext)) {
111
- __android_log_print(ANDROID_LOG_ERROR, "GameRenderer",
112
- "render: eglMakeCurrent failed: 0x%x", eglGetError());
113
- return;
114
- }
115
-
116
132
  if (!_surface) {
117
133
  if (!createSkSurface()) {
118
134
  return;
@@ -228,8 +244,6 @@ void GameRenderer::render(const Screen &screen,
228
244
 
229
245
  _grContext->flushAndSubmit(_surface.get());
230
246
  eglSwapBuffers(_eglDisplay, _eglSurface);
231
-
232
- __android_log_print(ANDROID_LOG_DEBUG, "GameRenderer", "render: complete");
233
247
  }
234
248
 
235
249
  bool GameRenderer::isEntityVisible(const Entity &entity,
@@ -342,8 +356,6 @@ bool GameRenderer::createEGLSurface() {
342
356
  }
343
357
  }
344
358
 
345
- eglMakeCurrent(_eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
346
-
347
359
  __android_log_print(ANDROID_LOG_INFO, "GameRenderer",
348
360
  "createEGLSurface: EGL surface created");
349
361
  return true;
@@ -36,6 +36,7 @@ public:
36
36
  void onSurfaceChanged(int width, int height);
37
37
  void onSurfaceDestroyed();
38
38
 
39
+ void processPendingTasks();
39
40
  void render(const Screen &screen,
40
41
  const std::map<std::string, Entity> &entities);
41
42
 
@@ -59,6 +60,9 @@ private:
59
60
  std::map<double, sk_sp<skottie::Animation>> _lottieCache;
60
61
  std::map<double, std::variant<sk_sp<SkSVGDOM>, sk_sp<SkImage>>> _imageCache;
61
62
 
63
+ std::mutex _taskMutex;
64
+ std::queue<std::function<void()>> _taskQueue;
65
+
62
66
  bool isEntityVisible(const Entity &entity, const SkRect &viewBounds);
63
67
 
64
68
  bool initializeEGL();