Skip to content

Commit fa78740

Browse files
committed
✨ Add an example using the cute framework
This shows off the very basic requirements of what's needed to embed a Dart as a game scripting language
1 parent 9c23bd4 commit fa78740

File tree

12 files changed

+404
-32
lines changed

12 files changed

+404
-32
lines changed

README.md

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,50 +15,46 @@ Additionally we may have a static target that uses the same interface as the dyn
1515

1616
I also hope to support all platforms that Dart currently supports, plus a few extra.
1717

18-
## Current Progress
19-
20-
A very, very simple Dart script works.
21-
2218
## Using
2319

24-
`TODO:`
20+
Lastest builds of the libraries are now available for certain targets as artifacts from [Github Actions](https://github.com/fuzzybinary/dart_shared_libray/actions) as well as all the headers necessary.
21+
22+
Github Actions currently builds a Windows x64 `.dll`, A Linux x64 `.so`, and a macOS x64 `.dylib`. You can build a M-series dylib for macOS, but Github Actions does not currently support it.
2523

2624
## Building
2725

2826
### Prerequisets
29-
You need
27+
You need:
3028
* git
31-
* All the things to get Dart source - https://github.com/dart-lang/sdk/wiki/Building#source
29+
* Dart 3+
3230
* C++ build tools for your platform (Visual Studio, XCode, gcc, etc)
3331
* CMake
3432

35-
## Contributing
33+
Optionally, I recommend installing [`depot_tools`](https://www.chromium.org/developers/how-tos/depottools/) and making sure it is on your path before running setup scripts. Without depot_tools, the scripts will download them anyway, but having them already set up will save you some time with subsequent builds.
3634

37-
`TODO:`
35+
### Patching and Building Dart
3836

39-
# What I Did
37+
> NOTE: If you are building on Windows, I recommend running `.\setup_env.ps1` before executing any other scripts.
38+
> This will set up some environment variables that will be needed to build Dart properly.
4039
41-
This section is as much for my benefit as for yours. Eventually, I hope to make a script that will do most of this.
40+
The first step is to build a statically linkable verison of Dart. This requires that we download Dart, patch some of the Dart build files, and then run the actual build. Thankfully there is a Dart script to do this.
4241

43-
* Modify the BUILD.gn files to add a `libdart` target. This is done by applying the patch in `./dart_sdk.patch`
44-
* Make sure all correct environment variables are set
45-
* On Windows, these are set with the `setup_env.ps1` script. Specifically, you need to set `GYP_MSVS_OVERRIDE_PATH`, `GYP_MSVS_VERSION`, and `DEPOT_TOOLS_WIN_TOOLCHAIN=0`
46-
* Builds libdart with:
47-
```bash
48-
# On windows, you will need to add `python` in front
49-
./tools/build.py --no-goma -m release libdart
50-
```
51-
* Revert the changes made to build files to leave a clean copy of the dart-sdk (this makes updating easier)
52-
* Build with CMake:
53-
```
54-
cmake -B ./.build .
55-
cmake --build ./.build
42+
```bash
43+
dart ./scripts/build_helpers/bin/build_dart.dart
5644
```
5745

58-
Updating the dart-sdk
59-
* Make sure environment variables are set (`setup_env.ps1`)
60-
* From `dart-sdk/sdk`:
61-
* `git checkout tags/[version]`
62-
* `gclient sync -D`
63-
* reapply dart_sdk.patch (`git apply ../../dart_sdk.patch`)
64-
* `python ./tools/build.py --no-goma -m release libdart`
46+
This script does the following:
47+
* Pulls down `depot_tools` if needed.
48+
* Clones a fresh copy of the Dart sdk git repo using `fetch` if needed.
49+
* Uses `gsync` to syncs the repo the the version of dart specificed in `.dart_version`.
50+
* Applies `dart_sdk.patch` to the repo to create the statically linkable `libdart` library
51+
* Builds `libdart`
52+
53+
### CMake
54+
55+
Once Dart is built, you can use CMake to generate build files and / or build the libraries and examples
56+
57+
```bash
58+
cmake -B ./.build .
59+
cmake --build .\.build\ --config release
60+
```

examples/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
cmake_minimum_required(VERSION 3.21)
22

33
add_subdirectory(simple_example)
4-
add_subdirectory(simple_example_ffi)
4+
add_subdirectory(simple_example_ffi)
5+
add_subdirectory(realtime_example)

examples/realtime_example/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.dart_tool/
2+
pubspec.lock
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
cmake_minimum_required(VERSION 3.21)
2+
3+
project(realtime_example)
4+
set(CMAKE_CXX_STANDARD 17)
5+
6+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
7+
8+
set(CUTE_FRAMEWORK_STATIC ON)
9+
10+
include(FetchContent)
11+
FetchContent_Declare(
12+
cute
13+
GIT_REPOSITORY https://github.com/RandyGaul/cute_framework
14+
)
15+
FetchContent_MakeAvailable(cute)
16+
17+
add_executable(realtime_example
18+
main.cpp
19+
drawable.cpp
20+
)
21+
22+
target_include_directories(realtime_example PRIVATE
23+
"."
24+
"${DART_DLL_DIR}"
25+
"${DART_DIR}/runtime/include"
26+
)
27+
28+
add_custom_target(ALWAYS_DO_POST_BUILD
29+
COMMAND ${CMAKE_COMMAND} -E copy -t $<TARGET_FILE_DIR:realtime_example> $<TARGET_RUNTIME_DLLS:simple_example_ffi>
30+
COMMAND ${CMAKE_COMMAND} -E copy_directory ${PROJECT_SOURCE_DIR}/dart $<TARGET_FILE_DIR:realtime_example>/dart
31+
COMMAND_EXPAND_LISTS
32+
)
33+
add_dependencies(realtime_example ALWAYS_DO_POST_BUILD)
34+
35+
target_link_libraries(realtime_example PUBLIC dart_dll cute)
36+
37+
if (MSVC)
38+
set_property(TARGET realtime_example PROPERTY VS_DEBUGGER_WORKING_DIRECTORY $<TARGET_FILE_DIR:realtime_example>)
39+
endif()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Dart: Attach to Process",
9+
"type": "dart",
10+
"request": "attach",
11+
"vmServiceUri": "http://127.0.0.1:5858/"
12+
}
13+
]
14+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import 'dart:ffi';
2+
3+
class CF_Color extends Struct {
4+
@Float()
5+
external double r;
6+
7+
@Float()
8+
external double g;
9+
10+
@Float()
11+
external double b;
12+
13+
@Float()
14+
external double a;
15+
}
16+
17+
class Drawable extends Struct {
18+
@Int32()
19+
external int x;
20+
21+
@Int32()
22+
external int y;
23+
24+
@Int32()
25+
external int width;
26+
27+
@Int32()
28+
external int height;
29+
30+
external CF_Color color;
31+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import 'dart:ffi';
2+
3+
import 'drawable.dart';
4+
5+
class WorkFfiCalls {
6+
final DynamicLibrary processLib = DynamicLibrary.process();
7+
8+
late final createEntity = processLib
9+
.lookup<NativeFunction<Uint32 Function(Int32, Int32, Int32, Int32)>>(
10+
'create_entity')
11+
.asFunction<int Function(int, int, int, int)>(isLeaf: true);
12+
late final destroyEntity = processLib
13+
.lookup<NativeFunction<Void Function(Uint32)>>('destroy_entity')
14+
.asFunction<void Function(int)>(isLeaf: true);
15+
late final getDrawable = processLib
16+
.lookup<NativeFunction<Pointer<Drawable> Function(Uint32)>>(
17+
'get_drawable')
18+
.asFunction<Pointer<Drawable> Function(int)>(isLeaf: true);
19+
20+
late final getKeyJustPressed = processLib
21+
.lookup<NativeFunction<Bool Function(Uint32)>>('get_key_just_pressed')
22+
.asFunction<bool Function(int)>(isLeaf: true);
23+
}
24+
25+
const int CF_KEY_RIGHT = 145;
26+
const int CF_KEY_LEFT = 146;
27+
const int CF_KEY_DOWN = 147;
28+
const int CF_KEY_UP = 148;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import 'dart:ffi';
2+
3+
import 'ffi_calls.dart';
4+
5+
WorkFfiCalls ffi = new WorkFfiCalls();
6+
7+
class Wall {
8+
late int entity;
9+
10+
Wall(int x, int y, int width, int height) {
11+
entity = ffi.createEntity(x, y, width, height);
12+
final drawable = ffi.getDrawable(entity);
13+
drawable.ref.color.r = 1.0;
14+
drawable.ref.color.g = 0.0;
15+
drawable.ref.color.b = 0.0;
16+
drawable.ref.color.a = 1.0;
17+
}
18+
}
19+
20+
List<Wall> walls = [];
21+
22+
// Dot!
23+
int dotEntity = 0;
24+
int dotX = 0;
25+
int dotY = 0;
26+
bool movingLeft = true;
27+
28+
void main() {
29+
print('main');
30+
walls.add(
31+
Wall(-320, -240, 5, 480),
32+
);
33+
walls.add(
34+
Wall(315, -240, 5, 480),
35+
);
36+
walls.add(
37+
Wall(-320, -240, 640, 5),
38+
);
39+
walls.add(
40+
Wall(-320, 235, 640, 5),
41+
);
42+
43+
dotEntity = ffi.createEntity(0, 0, 10, 10);
44+
final drawable = ffi.getDrawable(dotEntity);
45+
drawable.ref.color.g = 1.0;
46+
}
47+
48+
void frame(double dt) {
49+
if (movingLeft) {
50+
dotX -= 3;
51+
if (dotX < -200) {
52+
dotX = -200;
53+
movingLeft = false;
54+
}
55+
} else {
56+
dotX += 3;
57+
if (dotX > 200) {
58+
dotX = 200;
59+
movingLeft = true;
60+
}
61+
}
62+
63+
final dotDrawable = ffi.getDrawable(dotEntity);
64+
dotDrawable.ref.x = dotX;
65+
dotDrawable.ref.y = dotY;
66+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: worm_example
2+
environment:
3+
sdk: ">=2.17.0 <3.0.0"
4+
5+
dependencies:
6+
ffi: ^2.0.1
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include "drawable.h"
2+
3+
#include "cute.h"
4+
using namespace Cute;

0 commit comments

Comments
 (0)