rngine 0.1.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.
- package/LICENSE +20 -0
- package/README.md +150 -0
- package/Rngine.podspec +29 -0
- package/android/CMakeLists.txt +31 -0
- package/android/build.gradle +118 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/cpp/GameLoopJNI.cpp +31 -0
- package/android/src/main/cpp/cpp-adapter.cpp +11 -0
- package/android/src/main/java/com/margelo/nitro/rngine/GameEngine.kt +34 -0
- package/android/src/main/java/com/margelo/nitro/rngine/GameView.kt +63 -0
- package/android/src/main/java/com/margelo/nitro/rngine/Rect.kt +9 -0
- package/android/src/main/java/com/margelo/nitro/rngine/RectSerializer.kt +23 -0
- package/android/src/main/java/com/margelo/nitro/rngine/RnginePackage.kt +29 -0
- package/ios/Rngine.swift +28 -0
- package/lib/module/GameEngine.js +15 -0
- package/lib/module/GameEngine.js.map +1 -0
- package/lib/module/GameEngine.nitro.js +4 -0
- package/lib/module/GameEngine.nitro.js.map +1 -0
- package/lib/module/GameMethods.js +44 -0
- package/lib/module/GameMethods.js.map +1 -0
- package/lib/module/GameMethods.nitro.js +4 -0
- package/lib/module/GameMethods.nitro.js.map +1 -0
- package/lib/module/index.js +6 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/types.js +2 -0
- package/lib/module/types.js.map +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/GameEngine.d.ts +13 -0
- package/lib/typescript/src/GameEngine.d.ts.map +1 -0
- package/lib/typescript/src/GameEngine.nitro.d.ts +7 -0
- package/lib/typescript/src/GameEngine.nitro.d.ts.map +1 -0
- package/lib/typescript/src/GameMethods.d.ts +16 -0
- package/lib/typescript/src/GameMethods.d.ts.map +1 -0
- package/lib/typescript/src/GameMethods.nitro.d.ts +18 -0
- package/lib/typescript/src/GameMethods.nitro.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +4 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/types.d.ts +48 -0
- package/lib/typescript/src/types.d.ts.map +1 -0
- package/nitro.json +33 -0
- package/nitrogen/generated/android/c++/JHybridGameEngineSpec.cpp +49 -0
- package/nitrogen/generated/android/c++/JHybridGameEngineSpec.hpp +63 -0
- package/nitrogen/generated/android/c++/views/JHybridGameEngineStateUpdater.cpp +53 -0
- package/nitrogen/generated/android/c++/views/JHybridGameEngineStateUpdater.hpp +49 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/rngine/HybridGameEngineSpec.kt +53 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/rngine/rngineOnLoad.kt +35 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/rngine/views/HybridGameEngineManager.kt +80 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/rngine/views/HybridGameEngineStateUpdater.kt +23 -0
- package/nitrogen/generated/android/rngine+autolinking.cmake +84 -0
- package/nitrogen/generated/android/rngine+autolinking.gradle +27 -0
- package/nitrogen/generated/android/rngineOnLoad.cpp +66 -0
- package/nitrogen/generated/android/rngineOnLoad.hpp +34 -0
- package/nitrogen/generated/ios/Rngine+autolinking.rb +62 -0
- package/nitrogen/generated/ios/Rngine-Swift-Cxx-Bridge.cpp +33 -0
- package/nitrogen/generated/ios/Rngine-Swift-Cxx-Bridge.hpp +40 -0
- package/nitrogen/generated/ios/Rngine-Swift-Cxx-Umbrella.hpp +41 -0
- package/nitrogen/generated/ios/RngineAutolinking.mm +43 -0
- package/nitrogen/generated/ios/RngineAutolinking.swift +26 -0
- package/nitrogen/generated/ios/c++/HybridGameEngineSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridGameEngineSpecSwift.hpp +75 -0
- package/nitrogen/generated/ios/c++/views/HybridGameEngineComponent.mm +118 -0
- package/nitrogen/generated/ios/swift/HybridGameEngineSpec.swift +55 -0
- package/nitrogen/generated/ios/swift/HybridGameEngineSpec_cxx.swift +147 -0
- package/nitrogen/generated/shared/c++/Entity.hpp +111 -0
- package/nitrogen/generated/shared/c++/HybridGameEngineSpec.cpp +21 -0
- package/nitrogen/generated/shared/c++/HybridGameEngineSpec.hpp +62 -0
- package/nitrogen/generated/shared/c++/HybridGameMethodsSpec.cpp +30 -0
- package/nitrogen/generated/shared/c++/HybridGameMethodsSpec.hpp +80 -0
- package/nitrogen/generated/shared/c++/System.hpp +91 -0
- package/nitrogen/generated/shared/c++/World.hpp +91 -0
- package/nitrogen/generated/shared/c++/views/HybridGameEngineComponent.cpp +72 -0
- package/nitrogen/generated/shared/c++/views/HybridGameEngineComponent.hpp +109 -0
- package/nitrogen/generated/shared/json/GameEngineConfig.json +9 -0
- package/package.json +180 -0
- package/src/GameEngine.nitro.ts +10 -0
- package/src/GameEngine.tsx +17 -0
- package/src/GameMethods.nitro.ts +18 -0
- package/src/GameMethods.ts +46 -0
- package/src/index.tsx +21 -0
- package/src/types.ts +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 amine-by
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
in the Software without restriction, including without limitation the rights
|
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# rngine
|
|
2
|
+
|
|
3
|
+
A React Native game engine for building 2D games, powered by [Nitro Modules](https://nitro.margelo.com/) for high-performance C++ game logic.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install rngine react-native-nitro-modules
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
> `react-native-nitro-modules` is required as this library relies on [Nitro Modules](https://nitro.margelo.com/).
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
import { GameEngine, configure, setVelocity } from 'rngine';
|
|
17
|
+
|
|
18
|
+
configure({
|
|
19
|
+
tickRate: 60,
|
|
20
|
+
world: { width: 800, height: 800, color: '#1a1a1a' },
|
|
21
|
+
entities: [
|
|
22
|
+
{
|
|
23
|
+
id: 'player',
|
|
24
|
+
px: 400,
|
|
25
|
+
py: 400,
|
|
26
|
+
width: 40,
|
|
27
|
+
height: 40,
|
|
28
|
+
color: '#00ff00',
|
|
29
|
+
vx: 0,
|
|
30
|
+
vy: 0,
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
id: 'enemy_1',
|
|
34
|
+
px: 100,
|
|
35
|
+
py: 100,
|
|
36
|
+
width: 40,
|
|
37
|
+
height: 40,
|
|
38
|
+
color: '#ff0000',
|
|
39
|
+
vx: 1000,
|
|
40
|
+
vy: 0,
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
id: 'enemy_2',
|
|
44
|
+
px: 200,
|
|
45
|
+
py: 200,
|
|
46
|
+
width: 40,
|
|
47
|
+
height: 40,
|
|
48
|
+
color: '#ff0000',
|
|
49
|
+
vx: -1000,
|
|
50
|
+
vy: 0,
|
|
51
|
+
},
|
|
52
|
+
],
|
|
53
|
+
systems: [
|
|
54
|
+
{
|
|
55
|
+
// runs every tick for all enemies
|
|
56
|
+
ids: ['enemy'],
|
|
57
|
+
onTick: (enemies) => {
|
|
58
|
+
enemies.forEach((enemy) => {
|
|
59
|
+
// reverse direction when reaching world edges
|
|
60
|
+
if (enemy.px <= 0 || enemy.px + enemy.width >= 800) {
|
|
61
|
+
setVelocity(enemy.id, -enemy.vx, enemy.vy);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
],
|
|
67
|
+
paused: false,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
export default function App() {
|
|
71
|
+
return <GameEngine style={{ flex: 1 }} />;
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Concepts
|
|
76
|
+
|
|
77
|
+
**Entities** are the objects in your game world. Each entity has a position, size, color, and velocity. See [`Entity`](./src/types.ts).
|
|
78
|
+
|
|
79
|
+
**Systems** define your game logic. Each system declares which entities it cares about via `ids` and runs every tick receiving those entities. See [`System`](./src/types.ts).
|
|
80
|
+
|
|
81
|
+
**World** defines the game world dimensions and background color. Entities outside the world bounds are automatically clipped. See [`World`](./src/types.ts).
|
|
82
|
+
|
|
83
|
+
## Entity Querying
|
|
84
|
+
|
|
85
|
+
Entity ids use `_` as a hierarchy separator, which enables prefix matching in systems and functions like `setVelocity`. Querying `'enemy'` matches all entities whose id starts with `enemy_`. So `enemy_1`, `enemy_magician_1`, and `enemy_warrior_2` are all returned. Querying `'enemy_magician'` narrows it down to only magician entities. Querying `'enemy_magician_1'` is an exact match.
|
|
86
|
+
|
|
87
|
+
This makes it easy to build entity groups naturally through naming. No extra configuration needed.
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
// matches enemy_1, enemy_magician_1, enemy_warrior_2
|
|
91
|
+
{ ids: ['enemy'], onTick: (entities) => {} }
|
|
92
|
+
|
|
93
|
+
// matches only enemy_magician_1, enemy_magician_2
|
|
94
|
+
{ ids: ['enemy_magician'], onTick: (entities) => {} }
|
|
95
|
+
|
|
96
|
+
// exact match
|
|
97
|
+
{ ids: ['enemy_magician_1'], onTick: (entities) => {} }
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## API
|
|
101
|
+
|
|
102
|
+
### `configure(config)`
|
|
103
|
+
|
|
104
|
+
Sets up the game engine. Call this before anything else.
|
|
105
|
+
|
|
106
|
+
| param | required | type | default | description |
|
|
107
|
+
| ---------- | -------- | ---------- | ------- | ------------------------------------- |
|
|
108
|
+
| `tickRate` | ✓ | `number` | — | Game logic updates per second |
|
|
109
|
+
| `world` | ✓ | `World` | — | World dimensions and background color |
|
|
110
|
+
| `entities` | | `Entity[]` | `[]` | Initial entities to spawn |
|
|
111
|
+
| `systems` | | `System[]` | `[]` | Systems to run each tick |
|
|
112
|
+
| `paused` | | `boolean` | `true` | Whether to start paused |
|
|
113
|
+
|
|
114
|
+
### `spawn(entity | entity[])`
|
|
115
|
+
|
|
116
|
+
Spawns one or more entities into the world. Skips duplicates by id.
|
|
117
|
+
|
|
118
|
+
### `despawn(id)`
|
|
119
|
+
|
|
120
|
+
Removes the entity with the given id, or all entities matching the given prefix.
|
|
121
|
+
|
|
122
|
+
### `setPosition(id, px, py)`
|
|
123
|
+
|
|
124
|
+
Sets the position of a single entity. Requires an exact id.
|
|
125
|
+
|
|
126
|
+
### `setVelocity(id, vx, vy)`
|
|
127
|
+
|
|
128
|
+
Sets the velocity of all entities matching the given id or prefix.
|
|
129
|
+
|
|
130
|
+
### `pause()`
|
|
131
|
+
|
|
132
|
+
Pauses the game loop. Systems stop running.
|
|
133
|
+
|
|
134
|
+
### `resume()`
|
|
135
|
+
|
|
136
|
+
Resumes the game loop.
|
|
137
|
+
|
|
138
|
+
## Contributing
|
|
139
|
+
|
|
140
|
+
- [Development workflow](CONTRIBUTING.md#development-workflow)
|
|
141
|
+
- [Sending a pull request](CONTRIBUTING.md#sending-a-pull-request)
|
|
142
|
+
- [Code of conduct](CODE_OF_CONDUCT.md)
|
|
143
|
+
|
|
144
|
+
## License
|
|
145
|
+
|
|
146
|
+
MIT
|
|
147
|
+
|
|
148
|
+
---
|
|
149
|
+
|
|
150
|
+
Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
|
package/Rngine.podspec
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = "Rngine"
|
|
7
|
+
s.version = package["version"]
|
|
8
|
+
s.summary = package["description"]
|
|
9
|
+
s.homepage = package["homepage"]
|
|
10
|
+
s.license = package["license"]
|
|
11
|
+
s.authors = package["author"]
|
|
12
|
+
|
|
13
|
+
s.platforms = { :ios => min_ios_version_supported }
|
|
14
|
+
s.source = { :git => "https://github.com/amine-by/rngine.git", :tag => "#{s.version}" }
|
|
15
|
+
|
|
16
|
+
s.source_files = [
|
|
17
|
+
"ios/**/*.{swift}",
|
|
18
|
+
"ios/**/*.{m,mm}",
|
|
19
|
+
"cpp/**/*.{hpp,cpp}",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
s.dependency 'React-jsi'
|
|
23
|
+
s.dependency 'React-callinvoker'
|
|
24
|
+
|
|
25
|
+
load 'nitrogen/generated/ios/Rngine+autolinking.rb'
|
|
26
|
+
add_nitrogen_files(s)
|
|
27
|
+
|
|
28
|
+
install_modules_dependencies(s)
|
|
29
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
project(rngine)
|
|
2
|
+
cmake_minimum_required(VERSION 3.9.0)
|
|
3
|
+
|
|
4
|
+
set(PACKAGE_NAME rngine)
|
|
5
|
+
set(CMAKE_VERBOSE_MAKEFILE ON)
|
|
6
|
+
set(CMAKE_CXX_STANDARD 20)
|
|
7
|
+
|
|
8
|
+
# Define C++ library and add all sources
|
|
9
|
+
add_library(
|
|
10
|
+
${PACKAGE_NAME}
|
|
11
|
+
SHARED
|
|
12
|
+
src/main/cpp/cpp-adapter.cpp
|
|
13
|
+
src/main/cpp/GameLoopJNI.cpp
|
|
14
|
+
../shared/GameLoop.cpp
|
|
15
|
+
../shared/GameMethods.cpp
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
# Add Nitrogen specs :)
|
|
19
|
+
include(${CMAKE_SOURCE_DIR}/../nitrogen/generated/android/rngine+autolinking.cmake)
|
|
20
|
+
|
|
21
|
+
# Set up local includes
|
|
22
|
+
include_directories("src/main/cpp" "../shared")
|
|
23
|
+
|
|
24
|
+
find_library(LOG_LIB log)
|
|
25
|
+
|
|
26
|
+
# Link all libraries together
|
|
27
|
+
target_link_libraries(
|
|
28
|
+
${PACKAGE_NAME}
|
|
29
|
+
${LOG_LIB}
|
|
30
|
+
android # <-- Android core
|
|
31
|
+
)
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
buildscript {
|
|
2
|
+
ext.Rngine = [
|
|
3
|
+
kotlinVersion: "2.0.21",
|
|
4
|
+
minSdkVersion: 24,
|
|
5
|
+
compileSdkVersion: 36,
|
|
6
|
+
targetSdkVersion: 36
|
|
7
|
+
]
|
|
8
|
+
|
|
9
|
+
ext.getExtOrDefault = { prop ->
|
|
10
|
+
if (rootProject.ext.has(prop)) {
|
|
11
|
+
return rootProject.ext.get(prop)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return Rngine[prop]
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
repositories {
|
|
18
|
+
google()
|
|
19
|
+
mavenCentral()
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
dependencies {
|
|
23
|
+
classpath "com.android.tools.build:gradle:8.7.2"
|
|
24
|
+
// noinspection DifferentKotlinGradleVersion
|
|
25
|
+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${getExtOrDefault('kotlinVersion')}"
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
def reactNativeArchitectures() {
|
|
30
|
+
def value = rootProject.getProperties().get("reactNativeArchitectures")
|
|
31
|
+
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
apply plugin: "com.android.library"
|
|
35
|
+
apply plugin: "kotlin-android"
|
|
36
|
+
apply from: '../nitrogen/generated/android/rngine+autolinking.gradle'
|
|
37
|
+
|
|
38
|
+
apply plugin: "com.facebook.react"
|
|
39
|
+
|
|
40
|
+
android {
|
|
41
|
+
namespace "com.margelo.nitro.rngine"
|
|
42
|
+
|
|
43
|
+
compileSdkVersion getExtOrDefault("compileSdkVersion")
|
|
44
|
+
|
|
45
|
+
defaultConfig {
|
|
46
|
+
minSdkVersion getExtOrDefault("minSdkVersion")
|
|
47
|
+
targetSdkVersion getExtOrDefault("targetSdkVersion")
|
|
48
|
+
|
|
49
|
+
externalNativeBuild {
|
|
50
|
+
cmake {
|
|
51
|
+
cppFlags "-frtti -fexceptions -Wall -fstack-protector-all"
|
|
52
|
+
arguments "-DANDROID_STL=c++_shared", "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
|
|
53
|
+
abiFilters (*reactNativeArchitectures())
|
|
54
|
+
|
|
55
|
+
buildTypes {
|
|
56
|
+
debug {
|
|
57
|
+
cppFlags "-O1 -g"
|
|
58
|
+
}
|
|
59
|
+
release {
|
|
60
|
+
cppFlags "-O2"
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
externalNativeBuild {
|
|
68
|
+
cmake {
|
|
69
|
+
path "CMakeLists.txt"
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
packagingOptions {
|
|
74
|
+
excludes = [
|
|
75
|
+
"META-INF",
|
|
76
|
+
"META-INF/**",
|
|
77
|
+
"**/libc++_shared.so",
|
|
78
|
+
"**/libfbjni.so",
|
|
79
|
+
"**/libjsi.so",
|
|
80
|
+
"**/libfolly_json.so",
|
|
81
|
+
"**/libfolly_runtime.so",
|
|
82
|
+
"**/libglog.so",
|
|
83
|
+
"**/libhermes.so",
|
|
84
|
+
"**/libhermes-executor-debug.so",
|
|
85
|
+
"**/libhermes_executor.so",
|
|
86
|
+
"**/libreactnative.so",
|
|
87
|
+
"**/libreactnativejni.so",
|
|
88
|
+
"**/libturbomodulejsijni.so",
|
|
89
|
+
"**/libreact_nativemodule_core.so",
|
|
90
|
+
"**/libjscexecutor.so"
|
|
91
|
+
]
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
buildFeatures {
|
|
95
|
+
buildConfig true
|
|
96
|
+
prefab true
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
buildTypes {
|
|
100
|
+
release {
|
|
101
|
+
minifyEnabled false
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
lint {
|
|
106
|
+
disable "GradleCompatible"
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
compileOptions {
|
|
110
|
+
sourceCompatibility JavaVersion.VERSION_1_8
|
|
111
|
+
targetCompatibility JavaVersion.VERSION_1_8
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
dependencies {
|
|
116
|
+
implementation "com.facebook.react:react-android"
|
|
117
|
+
implementation project(":react-native-nitro-modules")
|
|
118
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
#include "GameLoop.hpp"
|
|
2
|
+
#include <jni.h>
|
|
3
|
+
|
|
4
|
+
extern "C" {
|
|
5
|
+
JNIEXPORT jobject JNICALL
|
|
6
|
+
Java_com_margelo_nitro_rngine_GameView_getRectsSnapshot(JNIEnv *env, jobject) {
|
|
7
|
+
auto rects =
|
|
8
|
+
margelo::nitro::rngine::GameLoop::getInstance().getRectsSnapshot();
|
|
9
|
+
const size_t size = rects.size() * margelo::nitro::rngine::RECT_SIZE;
|
|
10
|
+
|
|
11
|
+
static std::vector<uint8_t> buffer;
|
|
12
|
+
buffer.resize(size);
|
|
13
|
+
|
|
14
|
+
uint8_t *data = buffer.data();
|
|
15
|
+
|
|
16
|
+
for (const auto &rect : rects) {
|
|
17
|
+
memcpy(data, &rect.left, sizeof(double));
|
|
18
|
+
data += sizeof(double);
|
|
19
|
+
memcpy(data, &rect.right, sizeof(double));
|
|
20
|
+
data += sizeof(double);
|
|
21
|
+
memcpy(data, &rect.top, sizeof(double));
|
|
22
|
+
data += sizeof(double);
|
|
23
|
+
memcpy(data, &rect.bottom, sizeof(double));
|
|
24
|
+
data += sizeof(double);
|
|
25
|
+
memcpy(data, &rect.color, sizeof(uint32_t));
|
|
26
|
+
data += sizeof(uint32_t);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return env->NewDirectByteBuffer(buffer.data(), (jlong)size);
|
|
30
|
+
}
|
|
31
|
+
} // extern "C"
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
package com.margelo.nitro.rngine
|
|
2
|
+
|
|
3
|
+
import android.os.Handler
|
|
4
|
+
import android.os.HandlerThread
|
|
5
|
+
import android.view.Choreographer
|
|
6
|
+
import com.facebook.react.uimanager.ThemedReactContext
|
|
7
|
+
|
|
8
|
+
class GameEngine(val context: ThemedReactContext) : HybridGameEngineSpec() {
|
|
9
|
+
private var renderThread: HandlerThread? = null
|
|
10
|
+
private var renderHandler: Handler? = null
|
|
11
|
+
|
|
12
|
+
private val frameCallback = object : Choreographer.FrameCallback {
|
|
13
|
+
override fun doFrame(frameTimeNanos: Long) {
|
|
14
|
+
renderHandler?.post { view.drawFrame() }
|
|
15
|
+
Choreographer.getInstance().postFrameCallback(this)
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
override val view: GameView = GameView(
|
|
20
|
+
context
|
|
21
|
+
).apply {
|
|
22
|
+
onAttached = {
|
|
23
|
+
renderThread = HandlerThread("RenderThread").also { it.start() }
|
|
24
|
+
renderHandler = Handler(renderThread!!.looper)
|
|
25
|
+
Choreographer.getInstance().postFrameCallback(frameCallback)
|
|
26
|
+
}
|
|
27
|
+
onDetached = {
|
|
28
|
+
Choreographer.getInstance().removeFrameCallback(frameCallback)
|
|
29
|
+
renderThread?.quitSafely()
|
|
30
|
+
renderThread = null
|
|
31
|
+
renderHandler = null
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
package com.margelo.nitro.rngine
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.graphics.Color
|
|
5
|
+
import android.graphics.Paint
|
|
6
|
+
import android.util.AttributeSet
|
|
7
|
+
import android.view.SurfaceView
|
|
8
|
+
import android.view.View
|
|
9
|
+
import java.nio.ByteBuffer
|
|
10
|
+
|
|
11
|
+
class GameView(
|
|
12
|
+
context: Context,
|
|
13
|
+
attrs: AttributeSet? = null,
|
|
14
|
+
defStyleAttr: Int = 0,
|
|
15
|
+
) : SurfaceView(
|
|
16
|
+
context,
|
|
17
|
+
attrs,
|
|
18
|
+
defStyleAttr,
|
|
19
|
+
) {
|
|
20
|
+
private external fun getRectsSnapshot(): ByteBuffer
|
|
21
|
+
var onAttached: () -> Unit = {}
|
|
22
|
+
var onDetached: () -> Unit = {}
|
|
23
|
+
|
|
24
|
+
private val paint = Paint().apply {
|
|
25
|
+
color = Color.TRANSPARENT
|
|
26
|
+
style = Paint.Style.FILL
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
init {
|
|
30
|
+
addOnAttachStateChangeListener(
|
|
31
|
+
object : OnAttachStateChangeListener {
|
|
32
|
+
override fun onViewAttachedToWindow(view: View) = onAttached()
|
|
33
|
+
override fun onViewDetachedFromWindow(view: View) = onDetached()
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
fun drawFrame() {
|
|
38
|
+
if (!holder.surface.isValid) return
|
|
39
|
+
val canvas = holder.lockCanvas() ?: return
|
|
40
|
+
|
|
41
|
+
val rects = RectSerializer.decode(getRectsSnapshot())
|
|
42
|
+
|
|
43
|
+
val worldRect = rects.first()
|
|
44
|
+
val scaleX = canvas.width / worldRect.right.toFloat()
|
|
45
|
+
val scaleY = canvas.height / worldRect.bottom.toFloat()
|
|
46
|
+
val scale = minOf(scaleX, scaleY)
|
|
47
|
+
|
|
48
|
+
val offsetX = (canvas.width - worldRect.right.toFloat() * scale) / 2f
|
|
49
|
+
val offsetY = (canvas.height - worldRect.bottom.toFloat() * scale) / 2f
|
|
50
|
+
|
|
51
|
+
rects.forEach { rect ->
|
|
52
|
+
paint.color = rect.color
|
|
53
|
+
canvas.drawRect(
|
|
54
|
+
rect.left.toFloat() * scale + offsetX,
|
|
55
|
+
rect.top.toFloat() * scale + offsetY,
|
|
56
|
+
rect.right.toFloat() * scale + offsetX,
|
|
57
|
+
rect.bottom.toFloat() * scale + offsetY,
|
|
58
|
+
paint
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
holder.unlockCanvasAndPost(canvas)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
package com.margelo.nitro.rngine
|
|
2
|
+
|
|
3
|
+
import java.nio.ByteBuffer
|
|
4
|
+
import java.nio.ByteOrder
|
|
5
|
+
|
|
6
|
+
object RectSerializer {
|
|
7
|
+
private const val RECT_SIZE = 4 * 8 + 4
|
|
8
|
+
fun decode(buffer: ByteBuffer): ArrayList<Rect> {
|
|
9
|
+
buffer.order(ByteOrder.nativeOrder())
|
|
10
|
+
val rects = ArrayList<Rect>()
|
|
11
|
+
|
|
12
|
+
while (buffer.remaining() >= RECT_SIZE) {
|
|
13
|
+
val left = buffer.double
|
|
14
|
+
val right = buffer.double
|
|
15
|
+
val top = buffer.double
|
|
16
|
+
val bottom = buffer.double
|
|
17
|
+
val color = buffer.int
|
|
18
|
+
|
|
19
|
+
rects.add(Rect(left, right, top, bottom, color))
|
|
20
|
+
}
|
|
21
|
+
return rects
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
package com.margelo.nitro.rngine
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.BaseReactPackage
|
|
4
|
+
import com.facebook.react.bridge.NativeModule
|
|
5
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
6
|
+
import com.facebook.react.module.model.ReactModuleInfoProvider
|
|
7
|
+
import com.facebook.react.uimanager.ViewManager
|
|
8
|
+
|
|
9
|
+
import com.margelo.nitro.rngine.views.HybridGameEngineManager
|
|
10
|
+
|
|
11
|
+
class RnginePackage : BaseReactPackage() {
|
|
12
|
+
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
|
|
13
|
+
return null
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
|
|
17
|
+
return ReactModuleInfoProvider { HashMap() }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
|
|
21
|
+
return listOf(HybridGameEngineManager())
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
companion object {
|
|
25
|
+
init {
|
|
26
|
+
System.loadLibrary("rngine")
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
package/ios/Rngine.swift
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
class HybridRngine : HybridRngineSpec {
|
|
2
|
+
|
|
3
|
+
// UIView
|
|
4
|
+
var view: UIView = UIView()
|
|
5
|
+
|
|
6
|
+
// props
|
|
7
|
+
var color: String = "#000" {
|
|
8
|
+
didSet {
|
|
9
|
+
view.backgroundColor = hexStringToUIColor(hexColor: color)
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
func hexStringToUIColor(hexColor: String) -> UIColor {
|
|
14
|
+
let stringScanner = Scanner(string: hexColor)
|
|
15
|
+
|
|
16
|
+
if(hexColor.hasPrefix("#")) {
|
|
17
|
+
stringScanner.scanLocation = 1
|
|
18
|
+
}
|
|
19
|
+
var color: UInt32 = 0
|
|
20
|
+
stringScanner.scanHexInt32(&color)
|
|
21
|
+
|
|
22
|
+
let r = CGFloat(Int(color >> 16) & 0x000000FF)
|
|
23
|
+
let g = CGFloat(Int(color >> 8) & 0x000000FF)
|
|
24
|
+
let b = CGFloat(Int(color) & 0x000000FF)
|
|
25
|
+
|
|
26
|
+
return UIColor(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: 1)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { getHostComponent } from 'react-native-nitro-modules';
|
|
4
|
+
const GameEngineConfig = require('../nitrogen/generated/shared/json/GameEngineConfig.json');
|
|
5
|
+
/**
|
|
6
|
+
* A game engine view component that renders the game world.
|
|
7
|
+
* Size it using the style prop like any other React Native view.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```tsx
|
|
11
|
+
* <GameEngine style={{ flex: 1 }} />
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export default getHostComponent('GameEngine', () => GameEngineConfig);
|
|
15
|
+
//# sourceMappingURL=GameEngine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["getHostComponent","GameEngineConfig","require"],"sourceRoot":"../../src","sources":["GameEngine.tsx"],"mappings":";;AAAA,SAASA,gBAAgB,QAAQ,4BAA4B;AAC7D,MAAMC,gBAAgB,GAAGC,OAAO,CAAC,yDAAyD,CAAC;AAG3F;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAeF,gBAAgB,CAC7B,YAAY,EACZ,MAAMC,gBACR,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":[],"sourceRoot":"../../src","sources":["GameEngine.nitro.ts"],"mappings":"","ignoreList":[]}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { NitroModules } from 'react-native-nitro-modules';
|
|
4
|
+
const gameMethods = NitroModules.createHybridObject('GameMethods');
|
|
5
|
+
|
|
6
|
+
/** Sets up the game engine with the given configuration. Call this before anything else. */
|
|
7
|
+
export function configure({
|
|
8
|
+
tickRate,
|
|
9
|
+
world,
|
|
10
|
+
entities = [],
|
|
11
|
+
systems = [],
|
|
12
|
+
paused = true
|
|
13
|
+
}) {
|
|
14
|
+
gameMethods.setTickRate(tickRate);
|
|
15
|
+
gameMethods.setWorld(world);
|
|
16
|
+
gameMethods.setEntities(entities);
|
|
17
|
+
gameMethods.setSystems(systems);
|
|
18
|
+
if (paused) {
|
|
19
|
+
gameMethods.pause();
|
|
20
|
+
} else {
|
|
21
|
+
gameMethods.resume();
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Pauses the game loop. Systems will stop running. */
|
|
26
|
+
export const pause = () => gameMethods.pause();
|
|
27
|
+
|
|
28
|
+
/** Resumes the game loop. */
|
|
29
|
+
export const resume = () => gameMethods.resume();
|
|
30
|
+
|
|
31
|
+
/** Spawns one or more entities into the world. Skips duplicates by id. */
|
|
32
|
+
export const spawn = entities => {
|
|
33
|
+
gameMethods.spawn(Array.isArray(entities) ? entities : [entities]);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/** Removes an entity or all entities matching the given id prefix from the world. */
|
|
37
|
+
export const despawn = id => gameMethods.despawn(id);
|
|
38
|
+
|
|
39
|
+
/** Sets the position of the entity with the exact given id. */
|
|
40
|
+
export const setPosition = (id, px, py) => gameMethods.setP(id, px, py);
|
|
41
|
+
|
|
42
|
+
/** Sets the velocity of all entities matching the given id or prefix. */
|
|
43
|
+
export const setVelocity = (id, vx, vy) => gameMethods.setV(id, vx, vy);
|
|
44
|
+
//# sourceMappingURL=GameMethods.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["NitroModules","gameMethods","createHybridObject","configure","tickRate","world","entities","systems","paused","setTickRate","setWorld","setEntities","setSystems","pause","resume","spawn","Array","isArray","despawn","id","setPosition","px","py","setP","setVelocity","vx","vy","setV"],"sourceRoot":"../../src","sources":["GameMethods.ts"],"mappings":";;AAAA,SAASA,YAAY,QAAQ,4BAA4B;AAIzD,MAAMC,WAAW,GAAGD,YAAY,CAACE,kBAAkB,CAAc,aAAa,CAAC;;AAE/E;AACA,OAAO,SAASC,SAASA,CAAC;EACxBC,QAAQ;EACRC,KAAK;EACLC,QAAQ,GAAG,EAAE;EACbC,OAAO,GAAG,EAAE;EACZC,MAAM,GAAG;AACH,CAAC,EAAE;EACTP,WAAW,CAACQ,WAAW,CAACL,QAAQ,CAAC;EACjCH,WAAW,CAACS,QAAQ,CAACL,KAAK,CAAC;EAC3BJ,WAAW,CAACU,WAAW,CAACL,QAAQ,CAAC;EACjCL,WAAW,CAACW,UAAU,CAACL,OAAO,CAAC;EAC/B,IAAIC,MAAM,EAAE;IACVP,WAAW,CAACY,KAAK,CAAC,CAAC;EACrB,CAAC,MAAM;IACLZ,WAAW,CAACa,MAAM,CAAC,CAAC;EACtB;AACF;;AAEA;AACA,OAAO,MAAMD,KAAK,GAAGA,CAAA,KAAMZ,WAAW,CAACY,KAAK,CAAC,CAAC;;AAE9C;AACA,OAAO,MAAMC,MAAM,GAAGA,CAAA,KAAMb,WAAW,CAACa,MAAM,CAAC,CAAC;;AAEhD;AACA,OAAO,MAAMC,KAAK,GAAIT,QAA2B,IAAK;EACpDL,WAAW,CAACc,KAAK,CAACC,KAAK,CAACC,OAAO,CAACX,QAAQ,CAAC,GAAGA,QAAQ,GAAG,CAACA,QAAQ,CAAC,CAAC;AACpE,CAAC;;AAED;AACA,OAAO,MAAMY,OAAO,GAAIC,EAAU,IAAKlB,WAAW,CAACiB,OAAO,CAACC,EAAE,CAAC;;AAE9D;AACA,OAAO,MAAMC,WAAW,GAAGA,CAACD,EAAU,EAAEE,EAAU,EAAEC,EAAU,KAC5DrB,WAAW,CAACsB,IAAI,CAACJ,EAAE,EAAEE,EAAE,EAAEC,EAAE,CAAC;;AAE9B;AACA,OAAO,MAAME,WAAW,GAAGA,CAACL,EAAU,EAAEM,EAAU,EAAEC,EAAU,KAC5DzB,WAAW,CAAC0B,IAAI,CAACR,EAAE,EAAEM,EAAE,EAAEC,EAAE,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":[],"sourceRoot":"../../src","sources":["GameMethods.nitro.ts"],"mappings":"","ignoreList":[]}
|