raylib 对 dart 的 ffi 绑定. API 基本与 C 的一致。将 C 代码复制粘贴到 dart 中,稍微做一些改动即可运行。
import 'package:raylib_dart/raylib_dart.dart';
int main()
{
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window");
SetTargetFPS(60);
while (!WindowShouldClose())
{
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
EndDrawing();
}
CloseWindow();
return 0;
}第一次执行以下命令会编译 raylib 的并生成动态库,会需要消耗比较久的时间
dart run example/example.dartint main(void)int main()dart 用 ~/ 代替 C 的 / 进行整除
用 dart 的 dot-shorthands
IsKeyDown(KEY_RIGHT);IsKeyDown(KEY_RIGHT); // Deprecated
IsKeyDown(.right); // RecommendedVector2/Vector3/Matrix/Ray 等用 vector_math
采用 image
合并为 RandomSequence
int* seq = LoadRandomSequence(count, min, max);
UnloadRandomSequence(seq);final seq = RandomSequence(count, min, max);TextFormat("FPS: %i (target: %i)", GetFPS(), currentFps);将变量用数组传入
TextFormat("FPS: %i (target: %i)", [GetFPS(), currentFps]);使用 String 类型而不是 char*
- SetLoadFileDataCallback
- SetSaveFileDataCallback
- SetLoadFileTextCallback
- SetSaveFileTextCallback
以 SetLoadFileDataCallback 为例子
// 使用 raylib 的默认回调
SetLoadFileDataCallback(); // SetLoadFileDataCallback(null);
// dart 进行 hook
SetLoadFileDataCallback((filename) => File(filename).readSync());- TraceLog
- MemAlloc
- MemRealloc
- MemFree
- LoadFileData
- UnloadFileData
- SaveFileData
- ExportDataAsCode
- LoadFileText
- UnloadFileText
- SaveFileText
- SetGamepadMappings
- SetTraceLogCallback: 也许可以合并到 logging 中
- 保持函数名不变
- 保留原来的枚举和常量
例如枚举,推荐使用 dot-shorthands 来简化代码。保留原来的常量,标注上 Deprecated, 同时给出推荐使用的写法。
Color color = RED; // Deprecated
Color color = .red; // Recommended
KeyboardKey key = KEY_A; // Deprecated
KeyboardKey key = .a; // Recommended