-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ 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
- Loading branch information
1 parent
9c23bd4
commit fa78740
Showing
12 changed files
with
404 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
cmake_minimum_required(VERSION 3.21) | ||
|
||
add_subdirectory(simple_example) | ||
add_subdirectory(simple_example_ffi) | ||
add_subdirectory(simple_example_ffi) | ||
add_subdirectory(realtime_example) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.dart_tool/ | ||
pubspec.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
cmake_minimum_required(VERSION 3.21) | ||
|
||
project(realtime_example) | ||
set(CMAKE_CXX_STANDARD 17) | ||
|
||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) | ||
|
||
set(CUTE_FRAMEWORK_STATIC ON) | ||
|
||
include(FetchContent) | ||
FetchContent_Declare( | ||
cute | ||
GIT_REPOSITORY https://github.com/RandyGaul/cute_framework | ||
) | ||
FetchContent_MakeAvailable(cute) | ||
|
||
add_executable(realtime_example | ||
main.cpp | ||
drawable.cpp | ||
) | ||
|
||
target_include_directories(realtime_example PRIVATE | ||
"." | ||
"${DART_DLL_DIR}" | ||
"${DART_DIR}/runtime/include" | ||
) | ||
|
||
add_custom_target(ALWAYS_DO_POST_BUILD | ||
COMMAND ${CMAKE_COMMAND} -E copy -t $<TARGET_FILE_DIR:realtime_example> $<TARGET_RUNTIME_DLLS:simple_example_ffi> | ||
COMMAND ${CMAKE_COMMAND} -E copy_directory ${PROJECT_SOURCE_DIR}/dart $<TARGET_FILE_DIR:realtime_example>/dart | ||
COMMAND_EXPAND_LISTS | ||
) | ||
add_dependencies(realtime_example ALWAYS_DO_POST_BUILD) | ||
|
||
target_link_libraries(realtime_example PUBLIC dart_dll cute) | ||
|
||
if (MSVC) | ||
set_property(TARGET realtime_example PROPERTY VS_DEBUGGER_WORKING_DIRECTORY $<TARGET_FILE_DIR:realtime_example>) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Dart: Attach to Process", | ||
"type": "dart", | ||
"request": "attach", | ||
"vmServiceUri": "http://127.0.0.1:5858/" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import 'dart:ffi'; | ||
|
||
class CF_Color extends Struct { | ||
@Float() | ||
external double r; | ||
|
||
@Float() | ||
external double g; | ||
|
||
@Float() | ||
external double b; | ||
|
||
@Float() | ||
external double a; | ||
} | ||
|
||
class Drawable extends Struct { | ||
@Int32() | ||
external int x; | ||
|
||
@Int32() | ||
external int y; | ||
|
||
@Int32() | ||
external int width; | ||
|
||
@Int32() | ||
external int height; | ||
|
||
external CF_Color color; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import 'dart:ffi'; | ||
|
||
import 'drawable.dart'; | ||
|
||
class WorkFfiCalls { | ||
final DynamicLibrary processLib = DynamicLibrary.process(); | ||
|
||
late final createEntity = processLib | ||
.lookup<NativeFunction<Uint32 Function(Int32, Int32, Int32, Int32)>>( | ||
'create_entity') | ||
.asFunction<int Function(int, int, int, int)>(isLeaf: true); | ||
late final destroyEntity = processLib | ||
.lookup<NativeFunction<Void Function(Uint32)>>('destroy_entity') | ||
.asFunction<void Function(int)>(isLeaf: true); | ||
late final getDrawable = processLib | ||
.lookup<NativeFunction<Pointer<Drawable> Function(Uint32)>>( | ||
'get_drawable') | ||
.asFunction<Pointer<Drawable> Function(int)>(isLeaf: true); | ||
|
||
late final getKeyJustPressed = processLib | ||
.lookup<NativeFunction<Bool Function(Uint32)>>('get_key_just_pressed') | ||
.asFunction<bool Function(int)>(isLeaf: true); | ||
} | ||
|
||
const int CF_KEY_RIGHT = 145; | ||
const int CF_KEY_LEFT = 146; | ||
const int CF_KEY_DOWN = 147; | ||
const int CF_KEY_UP = 148; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import 'dart:ffi'; | ||
|
||
import 'ffi_calls.dart'; | ||
|
||
WorkFfiCalls ffi = new WorkFfiCalls(); | ||
|
||
class Wall { | ||
late int entity; | ||
|
||
Wall(int x, int y, int width, int height) { | ||
entity = ffi.createEntity(x, y, width, height); | ||
final drawable = ffi.getDrawable(entity); | ||
drawable.ref.color.r = 1.0; | ||
drawable.ref.color.g = 0.0; | ||
drawable.ref.color.b = 0.0; | ||
drawable.ref.color.a = 1.0; | ||
} | ||
} | ||
|
||
List<Wall> walls = []; | ||
|
||
// Dot! | ||
int dotEntity = 0; | ||
int dotX = 0; | ||
int dotY = 0; | ||
bool movingLeft = true; | ||
|
||
void main() { | ||
print('main'); | ||
walls.add( | ||
Wall(-320, -240, 5, 480), | ||
); | ||
walls.add( | ||
Wall(315, -240, 5, 480), | ||
); | ||
walls.add( | ||
Wall(-320, -240, 640, 5), | ||
); | ||
walls.add( | ||
Wall(-320, 235, 640, 5), | ||
); | ||
|
||
dotEntity = ffi.createEntity(0, 0, 10, 10); | ||
final drawable = ffi.getDrawable(dotEntity); | ||
drawable.ref.color.g = 1.0; | ||
} | ||
|
||
void frame(double dt) { | ||
if (movingLeft) { | ||
dotX -= 3; | ||
if (dotX < -200) { | ||
dotX = -200; | ||
movingLeft = false; | ||
} | ||
} else { | ||
dotX += 3; | ||
if (dotX > 200) { | ||
dotX = 200; | ||
movingLeft = true; | ||
} | ||
} | ||
|
||
final dotDrawable = ffi.getDrawable(dotEntity); | ||
dotDrawable.ref.x = dotX; | ||
dotDrawable.ref.y = dotY; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
name: worm_example | ||
environment: | ||
sdk: ">=2.17.0 <3.0.0" | ||
|
||
dependencies: | ||
ffi: ^2.0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#include "drawable.h" | ||
|
||
#include "cute.h" | ||
using namespace Cute; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#pragma once | ||
|
||
#include <dart_api.h> | ||
#include <cute.h> | ||
using namespace Cute; | ||
|
||
class Drawable { | ||
public: | ||
int x; | ||
int y; | ||
int width; | ||
int height; | ||
|
||
Color color; | ||
}; |
Oops, something went wrong.