当前位置: 首页 > 工具软件 > Dub > 使用案例 >

d绑定sfml的dub配置

丁文轩
2023-12-01

原文
如何使用bindbc-sfml?
我在我的Linux系统上安装了它,没有使用加载器和静态SFML.我只是使用apt-getdub.我按顺序做了以下操作:

sudo apt-get install libsfml-dev libcsfml-dev
dub init dsfml bindbc-sfml
cd dsfml

编辑dub.json并添加:

"libs": [
        "csfml-audio",
        "csfml-graphics"
    ],
  "subConfigurations": {
        "bindbc-sfml": "staticBC"
    },
  "versions": [
        "SFML_Audio",
        "SFML_Graphics"
    ],

示例:

import bindbc.sfml;
void main()
{
  sfContextSettings* settings;
  sfEvent event;

  auto window = sfRenderWindow_create(
    sfVideoMode(750, 500),
    "标志",
    sfWindowStyle.sfDefaultStyle,
    settings
  );

  auto flag = sfCircleShape_create();
       flag.sfCircleShape_setRadius(150);
       flag.sfCircleShape_setPosition(sfVector2f(225, 100));
       flag.sfCircleShape_setFillColor(sfRed);

  while(window.sfRenderWindow_isOpen())
  {
    while(window.sfRenderWindow_pollEvent(&event))
    {
      if(event.type == sfEventType.sfEvtClosed)
      {
        window.sfRenderWindow_close();
      }
    }

    window.sfRenderWindow_clear(sfWhite);
    window.sfRenderWindow_drawCircleShape(flag, null);
    window.sfRenderWindow_display();
  }
}

dub示例:

{
    "authors": [
        "rillki"
    ],
    "copyright": "Copyright ? 2023, rillki",
    "dependencies": {
        "bindbc-sfml": "~>1.0.2"
    },
    "description": "D/SFML project template",
    "license": "BSL",
    "name": "d-sfml-project-template",
    "targetPath": "bin",
    "versions": [
        "SFML_Audio",
        "SFML_Graphics",
        "SFML_Network",
        "SFML_System",
        "SFML_Window",
        "SFML_250"
    ]
}
//示例:source/app.d:

module app;

import std.stdio: writeln;
import bindbc.sfml;

void main() {
    version(Windows) {
        import bindbc.loader;
        setCustomLoaderSearchPath("libs");
    }

    // 试加载sfml
    if(!loadSFML()) {
        writeln("无法加载SFML库!");
        return;
    }

    // 窗口尺寸
    enum width = 720;
    enum height = 480;
    enum title = "D/SFML项目";

    // 创建窗口
    auto window = sfWindow_create(sfVideoMode(width, height), title.toStringz, sfWindowStyle.sfDefaultStyle, null);
    scope(exit) { sfWindow_destroy(window); }

    while(sfWindow_isOpen(window)) {
        // 处理事件.
        sfEvent event;
        while(sfWindow_pollEvent(window, &event)) {
            if(event.type == sfEventType.sfEvtClosed) {
                sfWindow_close(window);
            }
        }

        // ...
    }
}

使用,你的dub.json会得到:

user@debian:~/Downloads$ cd dsfml
user@debian:~/Downloads/dsfml$ dub
Fetching bindbc-sfml 1.0.2 (getting selected version)
Fetching bindbc-loader 1.0.3 (getting selected version)
Starting Performing "debug" build using /usr/bin/dmd for x86_64.
Building bindbc-loader 1.0.3: building configuration [noBC]
//无BC
Building bindbc-sfml 1.0.2: building configuration [dynamic]
//动态
Building d-sfml-project-template ~master: building configuration [application]
Linking d-sfml-project-template
Running bin/d-sfml-project-template
Error Program exited with code -11

如果添加"BindSFML_Static"DUBversion指令中,我会切换到static,我猜你不希望这样.但是我也会得到一堆未定义错误.
如果想要静态的,我会简单像这样添加并运行库:

    "libs": [
        "csfml-audio",
        "csfml-graphics"
    ],
    "versions": [
        "SFML_Audio",
        "SFML_Graphics",
        "BindSFML_Static",
    ]
}
 类似资料: