rngine 0.4.2 → 0.6.0

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.
Files changed (60) hide show
  1. package/README.md +1 -1
  2. package/android/kls_database.db +0 -0
  3. package/android/src/main/cpp/GameLoopJNI.cpp +84 -27
  4. package/android/src/main/java/com/margelo/nitro/rngine/GameAssets.kt +34 -20
  5. package/android/src/main/java/com/margelo/nitro/rngine/GameView.kt +80 -48
  6. package/android/src/main/java/com/margelo/nitro/rngine/Rect.kt +2 -2
  7. package/android/src/main/java/com/margelo/nitro/rngine/Screen.kt +9 -0
  8. package/android/src/main/java/com/margelo/nitro/rngine/Snapshot.kt +3 -0
  9. package/android/src/main/java/com/margelo/nitro/rngine/SnapshotSerializer.kt +33 -0
  10. package/lib/module/GameAssets.js +41 -0
  11. package/lib/module/GameAssets.js.map +1 -0
  12. package/lib/module/GameEngine.js +1 -1
  13. package/lib/module/GameEngine.js.map +1 -1
  14. package/lib/module/GameMethods.js +43 -0
  15. package/lib/module/GameMethods.js.map +1 -1
  16. package/lib/module/index.js +1 -1
  17. package/lib/module/index.js.map +1 -1
  18. package/lib/typescript/src/GameAssets.d.ts +2 -0
  19. package/lib/typescript/src/GameAssets.d.ts.map +1 -0
  20. package/lib/typescript/src/GameAssets.nitro.d.ts +2 -2
  21. package/lib/typescript/src/GameAssets.nitro.d.ts.map +1 -1
  22. package/lib/typescript/src/GameMethods.d.ts +8 -0
  23. package/lib/typescript/src/GameMethods.d.ts.map +1 -1
  24. package/lib/typescript/src/index.d.ts +2 -1
  25. package/lib/typescript/src/index.d.ts.map +1 -1
  26. package/lib/typescript/src/nativeTypes.d.ts +25 -44
  27. package/lib/typescript/src/nativeTypes.d.ts.map +1 -1
  28. package/lib/typescript/src/types.d.ts +2 -15
  29. package/lib/typescript/src/types.d.ts.map +1 -1
  30. package/nitrogen/generated/android/c++/JHybridGameAssetsSpec.cpp +31 -6
  31. package/nitrogen/generated/android/c++/JHybridGameAssetsSpec.hpp +2 -2
  32. package/nitrogen/generated/android/kotlin/com/margelo/nitro/rngine/HybridGameAssetsSpec.kt +3 -2
  33. package/nitrogen/generated/ios/Rngine-Swift-Cxx-Bridge.cpp +16 -0
  34. package/nitrogen/generated/ios/Rngine-Swift-Cxx-Bridge.hpp +65 -6
  35. package/nitrogen/generated/ios/Rngine-Swift-Cxx-Umbrella.hpp +1 -0
  36. package/nitrogen/generated/ios/c++/HybridGameAssetsSpecSwift.hpp +7 -2
  37. package/nitrogen/generated/ios/swift/Func_void.swift +46 -0
  38. package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +46 -0
  39. package/nitrogen/generated/ios/swift/HybridGameAssetsSpec.swift +2 -2
  40. package/nitrogen/generated/ios/swift/HybridGameAssetsSpec_cxx.swift +24 -8
  41. package/nitrogen/generated/shared/c++/HybridGameAssetsSpec.hpp +3 -2
  42. package/package.json +1 -1
  43. package/shared/GameLoop.cpp +11 -38
  44. package/shared/GameLoop.hpp +0 -2
  45. package/shared/GameMethods.cpp +2 -0
  46. package/src/GameAssets.nitro.ts +2 -2
  47. package/src/GameAssets.ts +47 -0
  48. package/src/GameEngine.tsx +1 -1
  49. package/src/GameMethods.ts +48 -0
  50. package/src/index.tsx +8 -1
  51. package/src/nativeTypes.ts +31 -46
  52. package/src/types.ts +3 -19
  53. package/android/src/main/java/com/margelo/nitro/rngine/RectSerializer.kt +0 -25
  54. package/lib/module/Shared.js +0 -85
  55. package/lib/module/Shared.js.map +0 -1
  56. package/lib/typescript/src/Shared.d.ts +0 -8
  57. package/lib/typescript/src/Shared.d.ts.map +0 -1
  58. package/shared/Rect.hpp +0 -16
  59. package/src/Shared.ts +0 -99
  60. /package/{shared → android/src/main/cpp}/ColorUtils.hpp +0 -0
@@ -1,79 +1,60 @@
1
- export type Screen = {
2
- /** Width of the screen in game units. */
3
- width: number;
4
- /** Height of the screen in game units. */
5
- height: number;
6
- /** Background color of the screen as a hex string e.g. `'#1a1a1a'`. */
7
- color?: string;
8
- /** Asset to render, use `require` with the file path to get the id. */
9
- asset?: number;
10
- /** Animation progress between 0 and 1, automatically advanced each tick for Lottie assets. */
11
- progress?: number;
12
- };
13
- export type Entity = {
14
- /** Unique identifier. Use `_` as a separator for group queries e.g. `'enemy_1'`. */
15
- id: string;
1
+ interface Positioned {
16
2
  /** X position in game units. */
17
3
  px: number;
18
4
  /** Y position in game units. */
19
5
  py: number;
6
+ }
7
+ interface Shaped {
20
8
  /** Width in game units. */
21
9
  width: number;
22
10
  /** Height in game units. */
23
11
  height: number;
12
+ }
13
+ interface Renderable {
24
14
  /** Fill color as a hex string e.g. `'#00ff00'`. */
25
15
  color?: string;
26
16
  /** Asset to render, use `require` with the file path to get the id. */
27
17
  asset?: number;
28
18
  /** Animation progress between 0 and 1, automatically advanced each tick for Lottie assets. */
29
19
  progress?: number;
20
+ }
21
+ interface Kinematic {
30
22
  /** Velocity on the X axis in game units per second. */
31
23
  vx?: number;
32
24
  /** Velocity on the Y axis in game units per second. */
33
25
  vy?: number;
34
- };
35
- export type CollisionPair = {
26
+ }
27
+ export interface Screen extends Shaped, Renderable {
28
+ }
29
+ export interface Entity extends Positioned, Shaped, Renderable, Kinematic {
30
+ /** Unique identifier. Use `_` as a separator for group queries e.g. `'enemy_1'`. */
31
+ id: string;
32
+ }
33
+ export interface EntityUpdate extends Partial<Positioned & Shaped & Renderable & Kinematic> {
34
+ /** Unique identifier or prefix of the entities to update. */
35
+ id: string;
36
+ }
37
+ export interface CollisionPair {
36
38
  /** Entity id or prefix of the first entity to watch. */
37
39
  a: string;
38
40
  /** Entity id or prefix of the second entity to watch. */
39
41
  b: string;
40
- };
41
- export type Collision = {
42
+ }
43
+ export interface Collision {
42
44
  /** Id of the first entity involved in the collision. */
43
45
  a: string;
44
46
  /** Id of the second entity involved in the collision. */
45
47
  b: string;
46
48
  /** Penetration depth of the collision. */
47
49
  depth: number;
48
- };
49
- export type System = {
50
+ }
51
+ export interface System {
50
52
  /** Entity ids or prefixes this system subscribes to. */
51
53
  entities?: string[];
52
54
  /** Collision pairs to watch for every tick. */
53
55
  collisions?: CollisionPair[];
54
56
  /** Called every tick with the resolved entities and collisions. */
55
57
  onTick: (entities: Entity[], collisions: Collision[]) => number;
56
- };
57
- export type EntityUpdate = {
58
- /** Unique identifier or prefix of the entities to update. */
59
- id: string;
60
- /** X position in game units. */
61
- px?: number;
62
- /** Y position in game units. */
63
- py?: number;
64
- /** Width in game units. */
65
- width?: number;
66
- /** Height in game units. */
67
- height?: number;
68
- /** Fill color as a hex string e.g. `'#00ff00'`. */
69
- color?: string;
70
- /** Asset to render, use `require` with the file path to get the id. */
71
- asset?: number;
72
- /** Animation progress between 0 and 1, automatically advanced each tick for Lottie assets. */
73
- progress?: number;
74
- /** Velocity on the X axis in game units per second. */
75
- vx?: number;
76
- /** Velocity on the Y axis in game units per second. */
77
- vy?: number;
78
- };
58
+ }
59
+ export {};
79
60
  //# sourceMappingURL=nativeTypes.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"nativeTypes.d.ts","sourceRoot":"","sources":["../../../src/nativeTypes.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,MAAM,GAAG;IACnB,yCAAyC;IACzC,KAAK,EAAE,MAAM,CAAC;IACd,0CAA0C;IAC1C,MAAM,EAAE,MAAM,CAAC;IACf,uEAAuE;IACvE,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,uEAAuE;IACvE,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,8FAA8F;IAC9F,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,MAAM,GAAG;IACnB,oFAAoF;IACpF,EAAE,EAAE,MAAM,CAAC;IACX,gCAAgC;IAChC,EAAE,EAAE,MAAM,CAAC;IACX,gCAAgC;IAChC,EAAE,EAAE,MAAM,CAAC;IACX,2BAA2B;IAC3B,KAAK,EAAE,MAAM,CAAC;IACd,4BAA4B;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,mDAA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MAAM,CAAC;IACV,yDAAyD;IACzD,CAAC,EAAE,MAAM,CAAC;IACV,0CAA0C;IAC1C,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,MAAM;IACrB,wDAAwD;IACxD,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB,+CAA+C;IAC/C,UAAU,CAAC,EAAE,aAAa,EAAE,CAAC;IAC7B,mEAAmE;IACnE,MAAM,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,EAAE,UAAU,EAAE,SAAS,EAAE,KAAK,MAAM,CAAC;CACjE"}
@@ -1,20 +1,7 @@
1
- import type { Screen as NativeScreen, Entity as NativeEntity, EntityUpdate as NativeEntityUpdate, System as NativeSystem, Collision } from './nativeTypes';
2
- export type Asset = number | object;
3
- export type Screen = Omit<NativeScreen, 'asset'> & {
4
- /** Asset to render, use `require` with the file path to get the id. */
5
- asset?: Asset;
6
- };
7
- export type Entity = Omit<NativeEntity, 'asset'> & {
8
- /** Asset to render, use `require` with the file path to get the id. */
9
- asset?: Asset;
10
- };
11
- export type EntityUpdate = Omit<NativeEntityUpdate, 'asset'> & {
12
- /** Asset to render, use `require` with the file path to get the id. */
13
- asset?: Asset;
14
- };
1
+ import type { System as NativeSystem, Collision, Screen, Entity } from './nativeTypes';
15
2
  export type System = Omit<NativeSystem, 'onTick'> & {
16
3
  /** Called every tick with the resolved entities and collisions. */
17
- onTick: (entities: NativeEntity[], collisions: Collision[]) => void;
4
+ onTick: (entities: Entity[], collisions: Collision[]) => void;
18
5
  };
19
6
  export type Config = {
20
7
  /** Number of game logic updates per second. */
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,MAAM,IAAI,YAAY,EACtB,MAAM,IAAI,YAAY,EACtB,YAAY,IAAI,kBAAkB,EAClC,MAAM,IAAI,YAAY,EACtB,SAAS,EACV,MAAM,eAAe,CAAC;AAEvB,MA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AAE,MAAM,EAAE,EAAE,UAAU,EAAE,SAAS,EAAE,KAAK,IAAI,CAAC;CAC/D,CAAC;AAEF,MAAM,MAAM,MAAM,GAAG;IACnB,+CAA+C;IAC/C,QAAQ,EAAE,MAAM,CAAC;IACjB,8CAA8C;IAC9C,MAAM,EAAE,MAAM,CAAC;IACf,iCAAiC;IACjC,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,iDAAiD;IACjD,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB,CAAC"}
@@ -9,6 +9,9 @@
9
9
 
10
10
 
11
11
 
12
+ #include <NitroModules/Promise.hpp>
13
+ #include <NitroModules/JPromise.hpp>
14
+ #include <NitroModules/JUnit.hpp>
12
15
  #include <string>
13
16
 
14
17
  namespace margelo::nitro::rngine {
@@ -49,13 +52,35 @@ namespace margelo::nitro::rngine {
49
52
  auto __result = method(_javaPart, id);
50
53
  return static_cast<bool>(__result);
51
54
  }
52
- void JHybridGameAssetsSpec::registerSvg(double id, const std::string& uri) {
53
- static const auto method = _javaPart->javaClassStatic()->getMethod<void(double /* id */, jni::alias_ref<jni::JString> /* uri */)>("registerSvg");
54
- method(_javaPart, id, jni::make_jstring(uri));
55
+ std::shared_ptr<Promise<void>> JHybridGameAssetsSpec::registerSvg(double id, const std::string& uri) {
56
+ static const auto method = _javaPart->javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>(double /* id */, jni::alias_ref<jni::JString> /* uri */)>("registerSvg");
57
+ auto __result = method(_javaPart, id, jni::make_jstring(uri));
58
+ return [&]() {
59
+ auto __promise = Promise<void>::create();
60
+ __result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& /* unit */) {
61
+ __promise->resolve();
62
+ });
63
+ __result->cthis()->addOnRejectedListener([=](const jni::alias_ref<jni::JThrowable>& __throwable) {
64
+ jni::JniException __jniError(__throwable);
65
+ __promise->reject(std::make_exception_ptr(__jniError));
66
+ });
67
+ return __promise;
68
+ }();
55
69
  }
56
- void JHybridGameAssetsSpec::registerLottie(double id, const std::string& json) {
57
- static const auto method = _javaPart->javaClassStatic()->getMethod<void(double /* id */, jni::alias_ref<jni::JString> /* json */)>("registerLottie");
58
- method(_javaPart, id, jni::make_jstring(json));
70
+ std::shared_ptr<Promise<void>> JHybridGameAssetsSpec::registerLottie(double id, const std::string& json) {
71
+ static const auto method = _javaPart->javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>(double /* id */, jni::alias_ref<jni::JString> /* json */)>("registerLottie");
72
+ auto __result = method(_javaPart, id, jni::make_jstring(json));
73
+ return [&]() {
74
+ auto __promise = Promise<void>::create();
75
+ __result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& /* unit */) {
76
+ __promise->resolve();
77
+ });
78
+ __result->cthis()->addOnRejectedListener([=](const jni::alias_ref<jni::JThrowable>& __throwable) {
79
+ jni::JniException __jniError(__throwable);
80
+ __promise->reject(std::make_exception_ptr(__jniError));
81
+ });
82
+ return __promise;
83
+ }();
59
84
  }
60
85
 
61
86
  } // namespace margelo::nitro::rngine
@@ -55,8 +55,8 @@ namespace margelo::nitro::rngine {
55
55
  public:
56
56
  // Methods
57
57
  bool isAssetRegistered(double id) override;
58
- void registerSvg(double id, const std::string& uri) override;
59
- void registerLottie(double id, const std::string& json) override;
58
+ std::shared_ptr<Promise<void>> registerSvg(double id, const std::string& uri) override;
59
+ std::shared_ptr<Promise<void>> registerLottie(double id, const std::string& json) override;
60
60
 
61
61
  private:
62
62
  jni::global_ref<JHybridGameAssetsSpec::JavaPart> _javaPart;
@@ -10,6 +10,7 @@ package com.margelo.nitro.rngine
10
10
  import androidx.annotation.Keep
11
11
  import com.facebook.jni.HybridData
12
12
  import com.facebook.proguard.annotations.DoNotStrip
13
+ import com.margelo.nitro.core.Promise
13
14
  import com.margelo.nitro.core.HybridObject
14
15
 
15
16
  /**
@@ -34,11 +35,11 @@ abstract class HybridGameAssetsSpec: HybridObject() {
34
35
 
35
36
  @DoNotStrip
36
37
  @Keep
37
- abstract fun registerSvg(id: Double, uri: String): Unit
38
+ abstract fun registerSvg(id: Double, uri: String): Promise<Unit>
38
39
 
39
40
  @DoNotStrip
40
41
  @Keep
41
- abstract fun registerLottie(id: Double, json: String): Unit
42
+ abstract fun registerLottie(id: Double, json: String): Promise<Unit>
42
43
 
43
44
  // Default implementation of `HybridObject.toString()`
44
45
  override fun toString(): String {
@@ -15,6 +15,22 @@
15
15
 
16
16
  namespace margelo::nitro::rngine::bridge::swift {
17
17
 
18
+ // pragma MARK: std::function<void()>
19
+ Func_void create_Func_void(void* NON_NULL swiftClosureWrapper) noexcept {
20
+ auto swiftClosure = Rngine::Func_void::fromUnsafe(swiftClosureWrapper);
21
+ return [swiftClosure = std::move(swiftClosure)]() mutable -> void {
22
+ swiftClosure.call();
23
+ };
24
+ }
25
+
26
+ // pragma MARK: std::function<void(const std::exception_ptr& /* error */)>
27
+ Func_void_std__exception_ptr create_Func_void_std__exception_ptr(void* NON_NULL swiftClosureWrapper) noexcept {
28
+ auto swiftClosure = Rngine::Func_void_std__exception_ptr::fromUnsafe(swiftClosureWrapper);
29
+ return [swiftClosure = std::move(swiftClosure)](const std::exception_ptr& error) mutable -> void {
30
+ swiftClosure.call(error);
31
+ };
32
+ }
33
+
18
34
  // pragma MARK: std::shared_ptr<HybridGameAssetsSpec>
19
35
  std::shared_ptr<HybridGameAssetsSpec> create_std__shared_ptr_HybridGameAssetsSpec_(void* NON_NULL swiftUnsafePointer) noexcept {
20
36
  Rngine::HybridGameAssetsSpec_cxx swiftPart = Rngine::HybridGameAssetsSpec_cxx::fromUnsafe(swiftUnsafePointer);
@@ -22,8 +22,11 @@ namespace Rngine { class HybridGameEngineSpec_cxx; }
22
22
  // Include C++ defined types
23
23
  #include "HybridGameAssetsSpec.hpp"
24
24
  #include "HybridGameEngineSpec.hpp"
25
+ #include <NitroModules/Promise.hpp>
26
+ #include <NitroModules/PromiseHolder.hpp>
25
27
  #include <NitroModules/Result.hpp>
26
28
  #include <exception>
29
+ #include <functional>
27
30
  #include <memory>
28
31
 
29
32
  /**
@@ -32,6 +35,62 @@ namespace Rngine { class HybridGameEngineSpec_cxx; }
32
35
  */
33
36
  namespace margelo::nitro::rngine::bridge::swift {
34
37
 
38
+ // pragma MARK: std::shared_ptr<Promise<void>>
39
+ /**
40
+ * Specialized version of `std::shared_ptr<Promise<void>>`.
41
+ */
42
+ using std__shared_ptr_Promise_void__ = std::shared_ptr<Promise<void>>;
43
+ inline std::shared_ptr<Promise<void>> create_std__shared_ptr_Promise_void__() noexcept {
44
+ return Promise<void>::create();
45
+ }
46
+ inline PromiseHolder<void> wrap_std__shared_ptr_Promise_void__(std::shared_ptr<Promise<void>> promise) noexcept {
47
+ return PromiseHolder<void>(std::move(promise));
48
+ }
49
+
50
+ // pragma MARK: std::function<void()>
51
+ /**
52
+ * Specialized version of `std::function<void()>`.
53
+ */
54
+ using Func_void = std::function<void()>;
55
+ /**
56
+ * Wrapper class for a `std::function<void()>`, this can be used from Swift.
57
+ */
58
+ class Func_void_Wrapper final {
59
+ public:
60
+ explicit Func_void_Wrapper(std::function<void()>&& func): _function(std::make_unique<std::function<void()>>(std::move(func))) {}
61
+ inline void call() const noexcept {
62
+ _function->operator()();
63
+ }
64
+ private:
65
+ std::unique_ptr<std::function<void()>> _function;
66
+ } SWIFT_NONCOPYABLE;
67
+ Func_void create_Func_void(void* NON_NULL swiftClosureWrapper) noexcept;
68
+ inline Func_void_Wrapper wrap_Func_void(Func_void value) noexcept {
69
+ return Func_void_Wrapper(std::move(value));
70
+ }
71
+
72
+ // pragma MARK: std::function<void(const std::exception_ptr& /* error */)>
73
+ /**
74
+ * Specialized version of `std::function<void(const std::exception_ptr&)>`.
75
+ */
76
+ using Func_void_std__exception_ptr = std::function<void(const std::exception_ptr& /* error */)>;
77
+ /**
78
+ * Wrapper class for a `std::function<void(const std::exception_ptr& / * error * /)>`, this can be used from Swift.
79
+ */
80
+ class Func_void_std__exception_ptr_Wrapper final {
81
+ public:
82
+ explicit Func_void_std__exception_ptr_Wrapper(std::function<void(const std::exception_ptr& /* error */)>&& func): _function(std::make_unique<std::function<void(const std::exception_ptr& /* error */)>>(std::move(func))) {}
83
+ inline void call(std::exception_ptr error) const noexcept {
84
+ _function->operator()(error);
85
+ }
86
+ private:
87
+ std::unique_ptr<std::function<void(const std::exception_ptr& /* error */)>> _function;
88
+ } SWIFT_NONCOPYABLE;
89
+ Func_void_std__exception_ptr create_Func_void_std__exception_ptr(void* NON_NULL swiftClosureWrapper) noexcept;
90
+ inline Func_void_std__exception_ptr_Wrapper wrap_Func_void_std__exception_ptr(Func_void_std__exception_ptr value) noexcept {
91
+ return Func_void_std__exception_ptr_Wrapper(std::move(value));
92
+ }
93
+
35
94
  // pragma MARK: std::shared_ptr<HybridGameAssetsSpec>
36
95
  /**
37
96
  * Specialized version of `std::shared_ptr<HybridGameAssetsSpec>`.
@@ -53,13 +112,13 @@ namespace margelo::nitro::rngine::bridge::swift {
53
112
  return Result<bool>::withError(error);
54
113
  }
55
114
 
56
- // pragma MARK: Result<void>
57
- using Result_void_ = Result<void>;
58
- inline Result_void_ create_Result_void_() noexcept {
59
- return Result<void>::withValue();
115
+ // pragma MARK: Result<std::shared_ptr<Promise<void>>>
116
+ using Result_std__shared_ptr_Promise_void___ = Result<std::shared_ptr<Promise<void>>>;
117
+ inline Result_std__shared_ptr_Promise_void___ create_Result_std__shared_ptr_Promise_void___(const std::shared_ptr<Promise<void>>& value) noexcept {
118
+ return Result<std::shared_ptr<Promise<void>>>::withValue(value);
60
119
  }
61
- inline Result_void_ create_Result_void_(const std::exception_ptr& error) noexcept {
62
- return Result<void>::withError(error);
120
+ inline Result_std__shared_ptr_Promise_void___ create_Result_std__shared_ptr_Promise_void___(const std::exception_ptr& error) noexcept {
121
+ return Result<std::shared_ptr<Promise<void>>>::withError(error);
63
122
  }
64
123
 
65
124
  // pragma MARK: std::shared_ptr<HybridGameEngineSpec>
@@ -16,6 +16,7 @@ namespace margelo::nitro::rngine { class HybridGameEngineSpec; }
16
16
  // Include C++ defined types
17
17
  #include "HybridGameAssetsSpec.hpp"
18
18
  #include "HybridGameEngineSpec.hpp"
19
+ #include <NitroModules/Promise.hpp>
19
20
  #include <NitroModules/Result.hpp>
20
21
  #include <exception>
21
22
  #include <memory>
@@ -14,6 +14,7 @@ namespace Rngine { class HybridGameAssetsSpec_cxx; }
14
14
 
15
15
 
16
16
 
17
+ #include <NitroModules/Promise.hpp>
17
18
  #include <string>
18
19
 
19
20
  #include "Rngine-Swift-Cxx-Umbrella.hpp"
@@ -74,17 +75,21 @@ namespace margelo::nitro::rngine {
74
75
  auto __value = std::move(__result.value());
75
76
  return __value;
76
77
  }
77
- inline void registerSvg(double id, const std::string& uri) override {
78
+ inline std::shared_ptr<Promise<void>> registerSvg(double id, const std::string& uri) override {
78
79
  auto __result = _swiftPart.registerSvg(std::forward<decltype(id)>(id), uri);
79
80
  if (__result.hasError()) [[unlikely]] {
80
81
  std::rethrow_exception(__result.error());
81
82
  }
83
+ auto __value = std::move(__result.value());
84
+ return __value;
82
85
  }
83
- inline void registerLottie(double id, const std::string& json) override {
86
+ inline std::shared_ptr<Promise<void>> registerLottie(double id, const std::string& json) override {
84
87
  auto __result = _swiftPart.registerLottie(std::forward<decltype(id)>(id), json);
85
88
  if (__result.hasError()) [[unlikely]] {
86
89
  std::rethrow_exception(__result.error());
87
90
  }
91
+ auto __value = std::move(__result.value());
92
+ return __value;
88
93
  }
89
94
 
90
95
  private:
@@ -0,0 +1,46 @@
1
+ ///
2
+ /// Func_void.swift
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ import NitroModules
9
+
10
+ /**
11
+ * Wraps a Swift `() -> Void` as a class.
12
+ * This class can be used from C++, e.g. to wrap the Swift closure as a `std::function`.
13
+ */
14
+ public final class Func_void {
15
+ public typealias bridge = margelo.nitro.rngine.bridge.swift
16
+
17
+ private let closure: () -> Void
18
+
19
+ public init(_ closure: @escaping () -> Void) {
20
+ self.closure = closure
21
+ }
22
+
23
+ @inline(__always)
24
+ public func call() -> Void {
25
+ self.closure()
26
+ }
27
+
28
+ /**
29
+ * Casts this instance to a retained unsafe raw pointer.
30
+ * This acquires one additional strong reference on the object!
31
+ */
32
+ @inline(__always)
33
+ public func toUnsafe() -> UnsafeMutableRawPointer {
34
+ return Unmanaged.passRetained(self).toOpaque()
35
+ }
36
+
37
+ /**
38
+ * Casts an unsafe pointer to a `Func_void`.
39
+ * The pointer has to be a retained opaque `Unmanaged<Func_void>`.
40
+ * This removes one strong reference from the object!
41
+ */
42
+ @inline(__always)
43
+ public static func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> Func_void {
44
+ return Unmanaged<Func_void>.fromOpaque(pointer).takeRetainedValue()
45
+ }
46
+ }
@@ -0,0 +1,46 @@
1
+ ///
2
+ /// Func_void_std__exception_ptr.swift
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ import NitroModules
9
+
10
+ /**
11
+ * Wraps a Swift `(_ error: Error) -> Void` as a class.
12
+ * This class can be used from C++, e.g. to wrap the Swift closure as a `std::function`.
13
+ */
14
+ public final class Func_void_std__exception_ptr {
15
+ public typealias bridge = margelo.nitro.rngine.bridge.swift
16
+
17
+ private let closure: (_ error: Error) -> Void
18
+
19
+ public init(_ closure: @escaping (_ error: Error) -> Void) {
20
+ self.closure = closure
21
+ }
22
+
23
+ @inline(__always)
24
+ public func call(error: std.exception_ptr) -> Void {
25
+ self.closure(RuntimeError.from(cppError: error))
26
+ }
27
+
28
+ /**
29
+ * Casts this instance to a retained unsafe raw pointer.
30
+ * This acquires one additional strong reference on the object!
31
+ */
32
+ @inline(__always)
33
+ public func toUnsafe() -> UnsafeMutableRawPointer {
34
+ return Unmanaged.passRetained(self).toOpaque()
35
+ }
36
+
37
+ /**
38
+ * Casts an unsafe pointer to a `Func_void_std__exception_ptr`.
39
+ * The pointer has to be a retained opaque `Unmanaged<Func_void_std__exception_ptr>`.
40
+ * This removes one strong reference from the object!
41
+ */
42
+ @inline(__always)
43
+ public static func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> Func_void_std__exception_ptr {
44
+ return Unmanaged<Func_void_std__exception_ptr>.fromOpaque(pointer).takeRetainedValue()
45
+ }
46
+ }
@@ -14,8 +14,8 @@ public protocol HybridGameAssetsSpec_protocol: HybridObject {
14
14
 
15
15
  // Methods
16
16
  func isAssetRegistered(id: Double) throws -> Bool
17
- func registerSvg(id: Double, uri: String) throws -> Void
18
- func registerLottie(id: Double, json: String) throws -> Void
17
+ func registerSvg(id: Double, uri: String) throws -> Promise<Void>
18
+ func registerLottie(id: Double, json: String) throws -> Promise<Void>
19
19
  }
20
20
 
21
21
  public extension HybridGameAssetsSpec_protocol {
@@ -137,24 +137,40 @@ open class HybridGameAssetsSpec_cxx {
137
137
  }
138
138
 
139
139
  @inline(__always)
140
- public final func registerSvg(id: Double, uri: std.string) -> bridge.Result_void_ {
140
+ public final func registerSvg(id: Double, uri: std.string) -> bridge.Result_std__shared_ptr_Promise_void___ {
141
141
  do {
142
- try self.__implementation.registerSvg(id: id, uri: String(uri))
143
- return bridge.create_Result_void_()
142
+ let __result = try self.__implementation.registerSvg(id: id, uri: String(uri))
143
+ let __resultCpp = { () -> bridge.std__shared_ptr_Promise_void__ in
144
+ let __promise = bridge.create_std__shared_ptr_Promise_void__()
145
+ let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_void__(__promise)
146
+ __result
147
+ .then({ __result in __promiseHolder.resolve() })
148
+ .catch({ __error in __promiseHolder.reject(__error.toCpp()) })
149
+ return __promise
150
+ }()
151
+ return bridge.create_Result_std__shared_ptr_Promise_void___(__resultCpp)
144
152
  } catch (let __error) {
145
153
  let __exceptionPtr = __error.toCpp()
146
- return bridge.create_Result_void_(__exceptionPtr)
154
+ return bridge.create_Result_std__shared_ptr_Promise_void___(__exceptionPtr)
147
155
  }
148
156
  }
149
157
 
150
158
  @inline(__always)
151
- public final func registerLottie(id: Double, json: std.string) -> bridge.Result_void_ {
159
+ public final func registerLottie(id: Double, json: std.string) -> bridge.Result_std__shared_ptr_Promise_void___ {
152
160
  do {
153
- try self.__implementation.registerLottie(id: id, json: String(json))
154
- return bridge.create_Result_void_()
161
+ let __result = try self.__implementation.registerLottie(id: id, json: String(json))
162
+ let __resultCpp = { () -> bridge.std__shared_ptr_Promise_void__ in
163
+ let __promise = bridge.create_std__shared_ptr_Promise_void__()
164
+ let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_void__(__promise)
165
+ __result
166
+ .then({ __result in __promiseHolder.resolve() })
167
+ .catch({ __error in __promiseHolder.reject(__error.toCpp()) })
168
+ return __promise
169
+ }()
170
+ return bridge.create_Result_std__shared_ptr_Promise_void___(__resultCpp)
155
171
  } catch (let __error) {
156
172
  let __exceptionPtr = __error.toCpp()
157
- return bridge.create_Result_void_(__exceptionPtr)
173
+ return bridge.create_Result_std__shared_ptr_Promise_void___(__exceptionPtr)
158
174
  }
159
175
  }
160
176
  }
@@ -15,6 +15,7 @@
15
15
 
16
16
 
17
17
 
18
+ #include <NitroModules/Promise.hpp>
18
19
  #include <string>
19
20
 
20
21
  namespace margelo::nitro::rngine {
@@ -49,8 +50,8 @@ namespace margelo::nitro::rngine {
49
50
  public:
50
51
  // Methods
51
52
  virtual bool isAssetRegistered(double id) = 0;
52
- virtual void registerSvg(double id, const std::string& uri) = 0;
53
- virtual void registerLottie(double id, const std::string& json) = 0;
53
+ virtual std::shared_ptr<Promise<void>> registerSvg(double id, const std::string& uri) = 0;
54
+ virtual std::shared_ptr<Promise<void>> registerLottie(double id, const std::string& json) = 0;
54
55
 
55
56
  protected:
56
57
  // Hybrid Setup
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rngine",
3
- "version": "0.4.2",
3
+ "version": "0.6.0",
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",
@@ -1,6 +1,5 @@
1
1
  #include "GameLoop.hpp"
2
2
  #include "Collision.hpp"
3
- #include "ColorUtils.hpp"
4
3
  #include "Entity.hpp"
5
4
  #include <android/log.h>
6
5
  #include <chrono>
@@ -60,37 +59,6 @@ void GameLoop::registerLottieDuration(double id, double duration) {
60
59
  id, duration);
61
60
  }
62
61
 
63
- std::vector<Rect> GameLoop::getRectsSnapshot() {
64
- std::lock_guard<std::mutex> snapshotLock(_snapshotMutex);
65
- std::vector<Rect> rects;
66
- rects.reserve(_entitiesSnapshot.size() + 1);
67
-
68
- rects.push_back({0, static_cast<float>(_screenSnapshot.width), 0,
69
- static_cast<float>(_screenSnapshot.height),
70
- static_cast<float>(_screenSnapshot.progress.value_or(0)),
71
- parseHexColor(_screenSnapshot.color),
72
- static_cast<int32_t>(_screenSnapshot.asset.value_or(0))});
73
-
74
- for (const auto &[id, entitySnapshot] : _entitiesSnapshot) {
75
- if (entitySnapshot.px + entitySnapshot.width < 0 ||
76
- entitySnapshot.px > _screenSnapshot.width ||
77
- entitySnapshot.py + entitySnapshot.height < 0 ||
78
- entitySnapshot.py > _screenSnapshot.height) {
79
- continue;
80
- }
81
-
82
- rects.push_back(
83
- {static_cast<float>(entitySnapshot.px),
84
- static_cast<float>(entitySnapshot.px + entitySnapshot.width),
85
- static_cast<float>(entitySnapshot.py),
86
- static_cast<float>(entitySnapshot.py + entitySnapshot.height),
87
- static_cast<float>(entitySnapshot.progress.value_or(0)),
88
- parseHexColor(entitySnapshot.color),
89
- static_cast<int32_t>(entitySnapshot.asset.value_or(0))});
90
- }
91
- return rects;
92
- }
93
-
94
62
  void GameLoop::runGameLoop() {
95
63
  using namespace std::chrono;
96
64
 
@@ -157,13 +125,18 @@ void GameLoop::runSystems() {
157
125
 
158
126
  uniqueEntityIdPairs.insert(uniqueEntityIdPairKey);
159
127
 
160
- float aLeft = entityA->px, aRight = entityA->px + entityA->width;
161
- float aTop = entityA->py, aBottom = entityA->py + entityA->height;
162
- float bLeft = entityB->px, bRight = entityB->px + entityB->width;
163
- float bTop = entityB->py, bBottom = entityB->py + entityB->height;
128
+ double aLeft = entityA->px - entityA->width / 2.0;
129
+ double aRight = entityA->px + entityA->width / 2.0;
130
+ double aTop = entityA->py - entityA->height / 2.0;
131
+ double aBottom = entityA->py + entityA->height / 2.0;
132
+
133
+ double bLeft = entityB->px - entityB->width / 2.0;
134
+ double bRight = entityB->px + entityB->width / 2.0;
135
+ double bTop = entityB->py - entityB->height / 2.0;
136
+ double bBottom = entityB->py + entityB->height / 2.0;
164
137
 
165
- float overlapX = std::min(aRight, bRight) - std::max(aLeft, bLeft);
166
- float overlapY = std::min(aBottom, bBottom) - std::max(aTop, bTop);
138
+ double overlapX = std::min(aRight, bRight) - std::max(aLeft, bLeft);
139
+ double overlapY = std::min(aBottom, bBottom) - std::max(aTop, bTop);
167
140
 
168
141
  if (overlapX > 0 && overlapY > 0) {
169
142
  collisions.push_back(Collision(entityA->id, entityB->id,